MCPcopy Create free account
hub / github.com/CPChain/chain / TestBlockEncoding

Function TestBlockEncoding

types/block_test.go:34–71  ·  view source on GitHub ↗

from bcValidBlockTest.json, "SimpleTx"

(t *testing.T)

Source from the content-addressed store, hash-verified

32
33// from bcValidBlockTest.json, "SimpleTx"
34func TestBlockEncoding(t *testing.T) {
35 blockEnc := common.FromHex("f90259f901f2a083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001832fefd8825208845506eb0780f846b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0c0c0f862f86080800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1")
36
37 var block Block
38 if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
39 t.Fatal("decode error: ", err)
40 }
41
42 check := func(f string, got, want interface{}) {
43 if !reflect.DeepEqual(got, want) {
44 t.Errorf("%s mismatch: got %v, want %v", f, got, want)
45 }
46 }
47 check("GasLimit", block.GasLimit(), uint64(3141592))
48 check("GasUsed", block.GasUsed(), uint64(21000))
49 check("Coinbase", strings.ToLower(block.Coinbase().Hex()), "0x8888f1f195afa192cfee860698584c030f4c9db1")
50 check("StateRoot", strings.ToLower(block.StateRoot().Hex()), "0xef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017")
51 check("Hash", block.Hash().Hex(), "0x62334eae00769d9efe592007afeae06044e3625de335dae1a0d276b40d697a14")
52 check("Time", block.Time(), big.NewInt(1426516743))
53 check("Size", block.Size(), common.StorageSize(len(blockEnc)))
54
55 tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil)
56
57 tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100"))
58 fmt.Println(block.Transactions()[0].Hash())
59 fmt.Println(tx1.data)
60 fmt.Println(tx1.Hash())
61 check("len(Transactions)", len(block.Transactions()), 1)
62 check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash())
63
64 ourBlockEnc, err := rlp.EncodeToBytes(&block)
65 if err != nil {
66 t.Fatal("encode error: ", err)
67 }
68 if !bytes.Equal(ourBlockEnc, blockEnc) {
69 t.Errorf("encoded block mismatch:\ngot: %x\nwant: %x", ourBlockEnc, blockEnc)
70 }
71}
72
73func TestBlockEncodingBuildHex(t *testing.T) {
74

Callers

nothing calls this directly

Calls 14

GasLimitMethod · 0.95
GasUsedMethod · 0.95
CoinbaseMethod · 0.95
StateRootMethod · 0.95
HashMethod · 0.95
TimeMethod · 0.95
SizeMethod · 0.95
WithSignatureMethod · 0.95
TransactionsMethod · 0.95
HashMethod · 0.95
NewTransactionFunction · 0.85
FatalMethod · 0.65

Tested by

no test coverage detected