merkleblock_test.go raw

   1  package bloom_test
   2  
   3  import (
   4  	"bytes"
   5  	"encoding/hex"
   6  	"github.com/p9c/p9/pkg/block"
   7  	"testing"
   8  	
   9  	"github.com/p9c/p9/pkg/bloom"
  10  	"github.com/p9c/p9/pkg/chainhash"
  11  	"github.com/p9c/p9/pkg/wire"
  12  )
  13  
  14  func TestMerkleBlock3(t *testing.T) {
  15  	blockStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b" +
  16  		"4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdc" +
  17  		"c96b2c3ff60abe184f196367291b4d4c86041b8fa45d630101000000010" +
  18  		"00000000000000000000000000000000000000000000000000000000000" +
  19  		"0000ffffffff08044c86041b020a02ffffffff0100f2052a01000000434" +
  20  		"104ecd3229b0571c3be876feaac0442a9f13c5a572742927af1dc623353" +
  21  		"ecf8c202225f64868137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5" +
  22  		"672bb15ad5d4cac00000000"
  23  	blockBytes, e := hex.DecodeString(blockStr)
  24  	if e != nil {
  25  		t.Errorf("TestMerkleBlock3 DecodeString failed: %v", e)
  26  		return
  27  	}
  28  	blk, e := block.NewFromBytes(blockBytes)
  29  	if e != nil {
  30  		t.Errorf("TestMerkleBlock3 NewFromBytes failed: %v", e)
  31  		return
  32  	}
  33  	f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
  34  	inputStr := "63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5"
  35  	hash, e := chainhash.NewHashFromStr(inputStr)
  36  	if e != nil {
  37  		t.Errorf("TestMerkleBlock3 NewHashFromStr failed: %v", e)
  38  		return
  39  	}
  40  	f.AddHash(hash)
  41  	mBlock, _ := bloom.NewMerkleBlock(blk, f)
  42  	wantStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4" +
  43  		"b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc" +
  44  		"96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50c" +
  45  		"c069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196" +
  46  		"30101"
  47  	want, e := hex.DecodeString(wantStr)
  48  	if e != nil {
  49  		t.Errorf("TestMerkleBlock3 DecodeString failed: %v", e)
  50  		return
  51  	}
  52  	got := bytes.NewBuffer(nil)
  53  	e = mBlock.BtcEncode(got, wire.ProtocolVersion, wire.LatestEncoding)
  54  	if e != nil {
  55  		t.Errorf("TestMerkleBlock3 BtcEncode failed: %v", e)
  56  		return
  57  	}
  58  	if !bytes.Equal(want, got.Bytes()) {
  59  		t.Errorf("TestMerkleBlock3 failed merkle block comparison: "+
  60  			"got %v want %v", got.Bytes(), want,
  61  		)
  62  		return
  63  	}
  64  }
  65