98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package parser
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
protobuf "github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
func TestBlockParser(t *testing.T) {
|
|
testBlocks, err := os.Open("testdata/blocks")
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompactBlocks(t *testing.T) {
|
|
type compactTest struct {
|
|
BlockHeight int `json:"block"`
|
|
BlockHash string `json:"hash"`
|
|
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 block.GetHeight() != test.BlockHeight {
|
|
t.Errorf("incorrect block height in testnet block %d", test.BlockHeight)
|
|
continue
|
|
}
|
|
if hex.EncodeToString(block.GetHash()) != test.BlockHash {
|
|
t.Errorf("incorrect block hash in testnet block %x", test.BlockHash)
|
|
continue
|
|
}
|
|
|
|
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)
|
|
continue
|
|
}
|
|
}
|
|
|
|
}
|