2018-09-27 17:52:40 -07:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/hex"
|
2018-11-16 16:37:31 -08:00
|
|
|
"encoding/json"
|
2018-09-27 17:52:40 -07:00
|
|
|
"fmt"
|
2018-11-16 16:37:31 -08:00
|
|
|
"io/ioutil"
|
2018-09-27 17:52:40 -07:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-11-16 16:37:31 -08:00
|
|
|
|
|
|
|
protobuf "github.com/golang/protobuf/proto"
|
2018-09-27 17:52:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlockParser(t *testing.T) {
|
2019-11-05 14:34:33 -08:00
|
|
|
// These (valid on testnet) correspond to the transactions in testdata/blocks
|
|
|
|
var txhashes = []string{
|
|
|
|
"81096ff101a4f01d25ffd34a446bee4368bd46c233a59ac0faf101e1861c6b22",
|
|
|
|
"921dc41bef3a0d887c615abac60a29979efc8b4bbd3d887caeb6bb93501bde8e",
|
|
|
|
"d8e4c336ffa69dacaa4e0b4eaf8e3ae46897f1930a573c10b53837a03318c980",
|
|
|
|
"4d5ccbfc6984680c481ff5ce145b8a93d59dfea90c150dfa45c938ab076ee5b2",
|
|
|
|
"df2b03619d441ce3d347e9278d87618e975079d0e235dfb3b3d8271510f707aa",
|
|
|
|
"8d2593edfc328fa637b4ac91c7d569ee922bb9a6fda7cea230e92deb3ae4b634",
|
|
|
|
}
|
|
|
|
txindex := 0
|
2018-12-14 16:17:27 -08:00
|
|
|
testBlocks, err := os.Open("../testdata/blocks")
|
2018-09-27 17:52:40 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer testBlocks.Close()
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(testBlocks)
|
|
|
|
for i := 0; scan.Scan(); i++ {
|
|
|
|
blockDataHex := scan.Text()
|
|
|
|
blockData, err := hex.DecodeString(blockDataHex)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
block := NewBlock()
|
|
|
|
blockData, err = block.ParseFromSlice(blockData)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(errors.Wrap(err, fmt.Sprintf("parsing block %d", i)))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some basic sanity checks
|
|
|
|
if block.hdr.Version != 4 {
|
|
|
|
t.Error("Read wrong version in a test block.")
|
|
|
|
break
|
|
|
|
}
|
2019-09-20 14:33:53 -07:00
|
|
|
if block.GetVersion() != 4 {
|
|
|
|
t.Error("Read wrong version in a test block.")
|
|
|
|
break
|
|
|
|
}
|
2019-02-25 10:57:06 -08:00
|
|
|
if block.GetTxCount() < 1 {
|
|
|
|
t.Error("No transactions in block")
|
|
|
|
break
|
|
|
|
}
|
2019-11-05 14:34:33 -08:00
|
|
|
if len(block.Transactions()) != block.GetTxCount() {
|
|
|
|
t.Error("Number of transactions mismatch")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if block.HasSaplingTransactions() {
|
|
|
|
t.Error("Unexpected Saping tx")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, tx := range block.Transactions() {
|
|
|
|
if tx.HasSaplingTransactions() {
|
|
|
|
t.Error("Unexpected Saping tx")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if hex.EncodeToString(tx.GetDisplayHash()) != txhashes[txindex] {
|
|
|
|
t.Error("incorrect tx hash")
|
|
|
|
}
|
|
|
|
txindex++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockParserFail(t *testing.T) {
|
|
|
|
testBlocks, err := os.Open("../testdata/badblocks")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer testBlocks.Close()
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(testBlocks)
|
|
|
|
|
|
|
|
// the first "block" contains an illegal hex character
|
|
|
|
{
|
|
|
|
scan.Scan()
|
|
|
|
blockDataHex := scan.Text()
|
|
|
|
_, err := hex.DecodeString(blockDataHex)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("unexpected success parsing illegal hex bad block")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; scan.Scan(); i++ {
|
|
|
|
blockDataHex := scan.Text()
|
|
|
|
blockData, err := hex.DecodeString(blockDataHex)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
block := NewBlock()
|
|
|
|
blockData, err = block.ParseFromSlice(blockData)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("unexpected success parsing bad block")
|
|
|
|
}
|
2019-02-25 10:57:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 07:18:15 -07:00
|
|
|
// Checks on the first 20 blocks from mainnet genesis.
|
2019-02-25 10:57:06 -08:00
|
|
|
func TestGenesisBlockParser(t *testing.T) {
|
|
|
|
blockFile, err := os.Open("../testdata/mainnet_genesis")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer blockFile.Close()
|
|
|
|
|
|
|
|
scan := bufio.NewScanner(blockFile)
|
|
|
|
for i := 0; scan.Scan(); i++ {
|
|
|
|
blockDataHex := scan.Text()
|
|
|
|
blockData, err := hex.DecodeString(blockDataHex)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
block := NewBlock()
|
|
|
|
blockData, err = block.ParseFromSlice(blockData)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some basic sanity checks
|
|
|
|
if block.hdr.Version != 4 {
|
|
|
|
t.Error("Read wrong version in genesis block.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-05-14 07:18:15 -07:00
|
|
|
if block.GetHeight() != i {
|
|
|
|
t.Errorf("Got wrong height for block %d: %d", i, block.GetHeight())
|
2019-02-25 10:57:06 -08:00
|
|
|
}
|
2018-09-27 17:52:40 -07:00
|
|
|
}
|
|
|
|
}
|
2018-11-16 16:37:31 -08:00
|
|
|
|
|
|
|
func TestCompactBlocks(t *testing.T) {
|
|
|
|
type compactTest struct {
|
|
|
|
BlockHeight int `json:"block"`
|
|
|
|
BlockHash string `json:"hash"`
|
2019-09-20 14:33:53 -07:00
|
|
|
PrevHash string `json:"prev"`
|
2018-11-16 16:37:31 -08:00
|
|
|
Full string `json:"full"`
|
|
|
|
Compact string `json:"compact"`
|
|
|
|
}
|
|
|
|
var compactTests []compactTest
|
|
|
|
|
2018-12-14 16:17:27 -08:00
|
|
|
blockJSON, err := ioutil.ReadFile("../testdata/compact_blocks.json")
|
2018-11-16 16:37:31 -08:00
|
|
|
if err != nil {
|
2018-11-17 19:02:49 -08:00
|
|
|
t.Fatal(err)
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(blockJSON, &compactTests)
|
|
|
|
if err != nil {
|
2018-11-17 19:02:49 -08:00
|
|
|
t.Fatal(err)
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 block.GetHeight() != test.BlockHeight {
|
|
|
|
t.Errorf("incorrect block height in testnet block %d", test.BlockHeight)
|
|
|
|
continue
|
|
|
|
}
|
2018-12-04 15:21:53 -08:00
|
|
|
if hex.EncodeToString(block.GetDisplayHash()) != test.BlockHash {
|
2018-11-16 16:37:31 -08:00
|
|
|
t.Errorf("incorrect block hash in testnet block %x", test.BlockHash)
|
|
|
|
continue
|
|
|
|
}
|
2019-11-05 14:34:33 -08:00
|
|
|
if hex.EncodeToString(block.GetDisplayPrevHash()) != test.PrevHash {
|
|
|
|
t.Errorf("incorrect block prevhash in testnet block %x", test.BlockHash)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-16 16:37:31 -08:00
|
|
|
|
|
|
|
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)
|
2018-12-04 15:21:53 -08:00
|
|
|
break
|
2018-11-16 16:37:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|