github.com/haliliceylan/bsc@v1.1.10-0.20220501224556-eb78d644ebcb/core/types/block_test.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"bytes"
    21  	"hash"
    22  	"math/big"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/common/math"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/params"
    30  	"github.com/ethereum/go-ethereum/rlp"
    31  	"golang.org/x/crypto/sha3"
    32  )
    33  
    34  // from bcValidBlockTest.json, "SimpleTx"
    35  func TestBlockEncoding(t *testing.T) {
    36  	blockEnc := common.FromHex("f90260f901f9a083cafc574e1f51ba9dc0568fc617a08ea2429fb384059c972f13b19fa1c8dd55a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a05fe50b260da6308036625b850b5d6ced6d0a9f814c0688bc91ffb7b7a3a54b67a0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845506eb0780a0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4f861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1c0")
    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("Difficulty", block.Difficulty(), big.NewInt(131072))
    48  	check("GasLimit", block.GasLimit(), uint64(3141592))
    49  	check("GasUsed", block.GasUsed(), uint64(21000))
    50  	check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
    51  	check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
    52  	check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
    53  	check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
    54  	check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
    55  	check("Time", block.Time(), uint64(1426516743))
    56  	check("Size", block.Size(), common.StorageSize(len(blockEnc)))
    57  
    58  	tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), 50000, big.NewInt(10), nil)
    59  	tx1, _ = tx1.WithSignature(HomesteadSigner{}, common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100"))
    60  	check("len(Transactions)", len(block.Transactions()), 1)
    61  	check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash())
    62  	ourBlockEnc, err := rlp.EncodeToBytes(&block)
    63  	if err != nil {
    64  		t.Fatal("encode error: ", err)
    65  	}
    66  	if !bytes.Equal(ourBlockEnc, blockEnc) {
    67  		t.Errorf("encoded block mismatch:\ngot:  %x\nwant: %x", ourBlockEnc, blockEnc)
    68  	}
    69  }
    70  
    71  func TestEIP2718BlockEncoding(t *testing.T) {
    72  	blockEnc := common.FromHex("f90319f90211a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017a0e6e49996c7ec59f7a23d22b83239a60151512c65613bf84a0d7da336399ebc4aa0cafe75574d59780665a97fbfd11365c7545aa8f1abf4e5e12e8243334ef7286bb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000820200832fefd882a410845506eb0796636f6f6c65737420626c6f636b206f6e20636861696ea0bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff49888a13a5a8c8f2bb1c4f90101f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba09bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094fa08a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b1b89e01f89b01800a8301e24194095e7baea6a6c7c4c2dfeb977efac326af552d878080f838f7940000000000000000000000000000000000000001e1a0000000000000000000000000000000000000000000000000000000000000000001a03dbacc8d0259f2508625e97fdfc57cd85fdd16e5821bc2c10bdd1a52649e8335a0476e10695b183a87b0aa292a7f4b78ef0c3fbe62aa2c42c84e1d9c3da159ef14c0")
    73  	var block Block
    74  	if err := rlp.DecodeBytes(blockEnc, &block); err != nil {
    75  		t.Fatal("decode error: ", err)
    76  	}
    77  
    78  	check := func(f string, got, want interface{}) {
    79  		if !reflect.DeepEqual(got, want) {
    80  			t.Errorf("%s mismatch: got %v, want %v", f, got, want)
    81  		}
    82  	}
    83  	check("Difficulty", block.Difficulty(), big.NewInt(131072))
    84  	check("GasLimit", block.GasLimit(), uint64(3141592))
    85  	check("GasUsed", block.GasUsed(), uint64(42000))
    86  	check("Coinbase", block.Coinbase(), common.HexToAddress("8888f1f195afa192cfee860698584c030f4c9db1"))
    87  	check("MixDigest", block.MixDigest(), common.HexToHash("bd4472abb6659ebe3ee06ee4d7b72a00a9f4d001caca51342001075469aff498"))
    88  	check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
    89  	check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
    90  	check("Time", block.Time(), uint64(1426516743))
    91  	check("Size", block.Size(), common.StorageSize(len(blockEnc)))
    92  
    93  	// Create legacy tx.
    94  	to := common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87")
    95  	tx1 := NewTx(&LegacyTx{
    96  		Nonce:    0,
    97  		To:       &to,
    98  		Value:    big.NewInt(10),
    99  		Gas:      50000,
   100  		GasPrice: big.NewInt(10),
   101  	})
   102  	sig := common.Hex2Bytes("9bea4c4daac7c7c52e093e6a4c35dbbcf8856f1af7b059ba20253e70848d094f8a8fae537ce25ed8cb5af9adac3f141af69bd515bd2ba031522df09b97dd72b100")
   103  	tx1, _ = tx1.WithSignature(HomesteadSigner{}, sig)
   104  
   105  	// Create ACL tx.
   106  	addr := common.HexToAddress("0x0000000000000000000000000000000000000001")
   107  	tx2 := NewTx(&AccessListTx{
   108  		ChainID:    big.NewInt(1),
   109  		Nonce:      0,
   110  		To:         &to,
   111  		Gas:        123457,
   112  		GasPrice:   big.NewInt(10),
   113  		AccessList: AccessList{{Address: addr, StorageKeys: []common.Hash{{0}}}},
   114  	})
   115  	sig2 := common.Hex2Bytes("3dbacc8d0259f2508625e97fdfc57cd85fdd16e5821bc2c10bdd1a52649e8335476e10695b183a87b0aa292a7f4b78ef0c3fbe62aa2c42c84e1d9c3da159ef1401")
   116  	tx2, _ = tx2.WithSignature(NewEIP2930Signer(big.NewInt(1)), sig2)
   117  
   118  	check("len(Transactions)", len(block.Transactions()), 2)
   119  	check("Transactions[0].Hash", block.Transactions()[0].Hash(), tx1.Hash())
   120  	check("Transactions[1].Hash", block.Transactions()[1].Hash(), tx2.Hash())
   121  	check("Transactions[1].Type()", block.Transactions()[1].Type(), uint8(AccessListTxType))
   122  
   123  	ourBlockEnc, err := rlp.EncodeToBytes(&block)
   124  	if err != nil {
   125  		t.Fatal("encode error: ", err)
   126  	}
   127  	if !bytes.Equal(ourBlockEnc, blockEnc) {
   128  		t.Errorf("encoded block mismatch:\ngot:  %x\nwant: %x", ourBlockEnc, blockEnc)
   129  	}
   130  }
   131  
   132  func TestUncleHash(t *testing.T) {
   133  	uncles := make([]*Header, 0)
   134  	h := CalcUncleHash(uncles)
   135  	exp := common.HexToHash("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
   136  	if h != exp {
   137  		t.Fatalf("empty uncle hash is wrong, got %x != %x", h, exp)
   138  	}
   139  }
   140  
   141  var benchBuffer = bytes.NewBuffer(make([]byte, 0, 32000))
   142  
   143  func BenchmarkEncodeBlock(b *testing.B) {
   144  	block := makeBenchBlock()
   145  	b.ResetTimer()
   146  
   147  	for i := 0; i < b.N; i++ {
   148  		benchBuffer.Reset()
   149  		if err := rlp.Encode(benchBuffer, block); err != nil {
   150  			b.Fatal(err)
   151  		}
   152  	}
   153  }
   154  
   155  // testHasher is the helper tool for transaction/receipt list hashing.
   156  // The original hasher is trie, in order to get rid of import cycle,
   157  // use the testing hasher instead.
   158  type testHasher struct {
   159  	hasher hash.Hash
   160  }
   161  
   162  func newHasher() *testHasher {
   163  	return &testHasher{hasher: sha3.NewLegacyKeccak256()}
   164  }
   165  
   166  func (h *testHasher) Reset() {
   167  	h.hasher.Reset()
   168  }
   169  
   170  func (h *testHasher) Update(key, val []byte) {
   171  	h.hasher.Write(key)
   172  	h.hasher.Write(val)
   173  }
   174  
   175  func (h *testHasher) Hash() common.Hash {
   176  	return common.BytesToHash(h.hasher.Sum(nil))
   177  }
   178  
   179  func makeBenchBlock() *Block {
   180  	var (
   181  		key, _   = crypto.GenerateKey()
   182  		txs      = make([]*Transaction, 70)
   183  		receipts = make([]*Receipt, len(txs))
   184  		signer   = LatestSigner(params.TestChainConfig)
   185  		uncles   = make([]*Header, 3)
   186  	)
   187  	header := &Header{
   188  		Difficulty: math.BigPow(11, 11),
   189  		Number:     math.BigPow(2, 9),
   190  		GasLimit:   12345678,
   191  		GasUsed:    1476322,
   192  		Time:       9876543,
   193  		Extra:      []byte("coolest block on chain"),
   194  	}
   195  	for i := range txs {
   196  		amount := math.BigPow(2, int64(i))
   197  		price := big.NewInt(300000)
   198  		data := make([]byte, 100)
   199  		tx := NewTransaction(uint64(i), common.Address{}, amount, 123457, price, data)
   200  		signedTx, err := SignTx(tx, signer, key)
   201  		if err != nil {
   202  			panic(err)
   203  		}
   204  		txs[i] = signedTx
   205  		receipts[i] = NewReceipt(make([]byte, 32), false, tx.Gas())
   206  	}
   207  	for i := range uncles {
   208  		uncles[i] = &Header{
   209  			Difficulty: math.BigPow(11, 11),
   210  			Number:     math.BigPow(2, 9),
   211  			GasLimit:   12345678,
   212  			GasUsed:    1476322,
   213  			Time:       9876543,
   214  			Extra:      []byte("benchmark uncle"),
   215  		}
   216  	}
   217  	return NewBlock(header, txs, uncles, receipts, newHasher())
   218  }