lightwalletd/parser/block_test.go

80 lines
2.1 KiB
Go

// Copyright (c) 2019-2020 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
package parser
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"github.com/pkg/errors"
protobuf "github.com/golang/protobuf/proto"
)
func TestCompactBlocks(t *testing.T) {
type compactTest struct {
BlockHeight int `json:"block"`
BlockHash string `json:"hash"`
PrevHash string `json:"prev"`
Full string `json:"full"`
Compact string `json:"compact"`
}
var compactTests []compactTest
blockJSON, err := ioutil.ReadFile("../testdata/compact_blocks.json")
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(blockJSON, &compactTests)
if err != nil {
t.Fatal(err)
}
for _, test := range compactTests {
blockData, _ := hex.DecodeString(test.Full)
block := NewBlock()
blockData, err = block.ParseFromSlice(blockData)
if err != nil {
t.Error(errors.Wrap(err, fmt.Sprintf("parsing testnet block %d", test.BlockHeight)))
continue
}
if len(blockData) > 0 {
t.Error("Extra data remaining")
}
if block.GetHeight() != test.BlockHeight {
t.Errorf("incorrect block height in testnet block %d", test.BlockHeight)
continue
}
if hex.EncodeToString(block.GetDisplayHash()) != test.BlockHash {
t.Errorf("incorrect block hash in testnet block %x", test.BlockHash)
continue
}
if hex.EncodeToString(block.GetDisplayPrevHash()) != test.PrevHash {
t.Errorf("incorrect block prevhash in testnet block %x", test.BlockHash)
continue
}
if !bytes.Equal(block.GetPrevHash(), block.hdr.HashPrevBlock) {
t.Error("block and block header prevhash don't match")
}
compact := block.ToCompact()
marshaled, err := protobuf.Marshal(compact)
if err != nil {
t.Errorf("could not marshal compact testnet block %d", test.BlockHeight)
continue
}
encodedCompact := hex.EncodeToString(marshaled)
if encodedCompact != test.Compact {
t.Errorf("wrong data for compact testnet block %d\nhave: %s\nwant: %s\n", test.BlockHeight, encodedCompact, test.Compact)
break
}
}
}