github.com/insight-chain/inb-go@v1.1.3-0.20191221022159-da049980ae38/core/types/block_test.go (about)

     1  // Copyright 2014 The inb-go Authors
     2  // This file is part of the inb-go library.
     3  //
     4  // The inb-go library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The inb-go library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the inb-go library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"math/big"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/insight-chain/inb-go/common"
    27  	"github.com/insight-chain/inb-go/rlp"
    28  )
    29  
    30  // from bcValidBlockTest.json, "SimpleTx"
    31  func TestBlockEncoding(t *testing.T) {
    32  	blockEnc := common.FromHex("f90260f901f9a083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0")
    33  	var block Block
    34  	if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
    35  		t.Fatal("decode error: ", err)
    36  	}
    37  
    38  	check := func(f string, got, want interface{}) {
    39  		if !reflect.DeepEqual(got, want) {
    40  			t.Errorf("%s mismatch: got %v, want %v", f, got, want)
    41  		}
    42  	}
    43  	check("Difficulty", block.Difficulty(), big.NewInt(131072))
    44  	check("ResLimit", block.GasLimit(), uint64(3141592))
    45  	check("ResUsed", block.GasUsed(), uint64(21000))
    46  	check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
    47  	check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
    48  	check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
    49  	check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
    50  	check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
    51  	check("Time", block.Time(), big.NewInt(1426516743))
    52  	check("Size", block.Size(), common.StorageSize(len(blockEnc)))
    53  
    54  	tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, nil, Ordinary)
    55  
    56  	tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100"))
    57  	fmt.Println(block.Transactions()[0].Hash())
    58  	fmt.Println(tx1.data)
    59  	fmt.Println(tx1.Hash())
    60  	check("len(Transactions)", len(block.Transactions()), 1)
    61  	check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash())
    62  
    63  	ourBlockEnc, err := rlp.EncodeToBytes(&block)
    64  	if err != nil {
    65  		t.Fatal("encode error: ", err)
    66  	}
    67  	if !bytes.Equal(ourBlockEnc, blockEnc) {
    68  		t.Errorf("encoded block mismatch:\ngot:  %x\nwant: %x", ourBlockEnc, blockEnc)
    69  	}
    70  }