github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/types/block_test.go (about)

     1  package types
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	gethCommon "github.com/onflow/go-ethereum/common"
     8  	gethTypes "github.com/onflow/go-ethereum/core/types"
     9  	gethRLP "github.com/onflow/go-ethereum/rlp"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func Test_BlockHash(t *testing.T) {
    15  	b := Block{
    16  		ParentBlockHash: gethCommon.HexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
    17  		Height:          1,
    18  		TotalSupply:     big.NewInt(1000),
    19  		ReceiptRoot:     gethCommon.Hash{0x2, 0x3, 0x4},
    20  		TotalGasUsed:    135,
    21  		TransactionHashes: []gethCommon.Hash{
    22  			gethCommon.HexToHash("0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
    23  		},
    24  	}
    25  
    26  	h1, err := b.Hash()
    27  	require.NoError(t, err)
    28  
    29  	b.Height = 2
    30  
    31  	h2, err := b.Hash()
    32  	require.NoError(t, err)
    33  
    34  	// hashes should not equal if any data is changed
    35  	assert.NotEqual(t, h1, h2)
    36  
    37  	b.PopulateReceiptRoot(nil)
    38  	require.Equal(t, gethTypes.EmptyReceiptsHash, b.ReceiptRoot)
    39  
    40  	res := Result{
    41  		GasConsumed: 10,
    42  	}
    43  	b.PopulateReceiptRoot([]*Result{&res})
    44  	require.NotEqual(t, gethTypes.EmptyReceiptsHash, b.ReceiptRoot)
    45  }
    46  
    47  func Test_DecodeBlocks(t *testing.T) {
    48  	bv0 := blockV0{
    49  		ParentBlockHash: GenesisBlockHash,
    50  		Height:          1,
    51  		UUIDIndex:       2,
    52  		TotalSupply:     3,
    53  		StateRoot:       gethCommon.Hash{0x01},
    54  		ReceiptRoot:     gethCommon.Hash{0x02},
    55  	}
    56  	b0, err := gethRLP.EncodeToBytes(bv0)
    57  	require.NoError(t, err)
    58  
    59  	b := decodeBlockBreakingChanges(b0)
    60  
    61  	require.Equal(t, b.TotalSupply.Uint64(), bv0.TotalSupply)
    62  	require.Equal(t, b.Height, bv0.Height)
    63  	require.Equal(t, b.ParentBlockHash, bv0.ParentBlockHash)
    64  	require.Empty(t, b.Timestamp)
    65  	require.Empty(t, b.TotalGasUsed)
    66  
    67  	bv1 := blockV1{
    68  		ParentBlockHash:   GenesisBlockHash,
    69  		Height:            1,
    70  		UUIDIndex:         2,
    71  		TotalSupply:       3,
    72  		StateRoot:         gethCommon.Hash{0x01},
    73  		ReceiptRoot:       gethCommon.Hash{0x02},
    74  		TransactionHashes: []gethCommon.Hash{{0x04}},
    75  	}
    76  
    77  	b1, err := gethRLP.EncodeToBytes(bv1)
    78  	require.NoError(t, err)
    79  
    80  	b = decodeBlockBreakingChanges(b1)
    81  
    82  	require.Equal(t, b.TotalSupply.Uint64(), bv1.TotalSupply)
    83  	require.Equal(t, b.Height, bv1.Height)
    84  	require.Equal(t, b.ParentBlockHash, bv1.ParentBlockHash)
    85  	require.Equal(t, b.TransactionHashes, bv1.TransactionHashes)
    86  	require.Empty(t, b.Timestamp)
    87  	require.Empty(t, b.TotalGasUsed)
    88  
    89  	bv2 := blockV2{
    90  		ParentBlockHash:   GenesisBlockHash,
    91  		Height:            1,
    92  		TotalSupply:       2,
    93  		StateRoot:         gethCommon.Hash{0x01},
    94  		ReceiptRoot:       gethCommon.Hash{0x02},
    95  		TransactionHashes: []gethCommon.Hash{{0x04}},
    96  	}
    97  
    98  	b2, err := gethRLP.EncodeToBytes(bv2)
    99  	require.NoError(t, err)
   100  
   101  	b = decodeBlockBreakingChanges(b2)
   102  
   103  	require.Equal(t, b.TotalSupply.Uint64(), bv2.TotalSupply)
   104  	require.Equal(t, b.Height, bv2.Height)
   105  	require.Equal(t, b.ParentBlockHash, bv2.ParentBlockHash)
   106  	require.Equal(t, b.TransactionHashes, bv2.TransactionHashes)
   107  	require.Empty(t, b.Timestamp)
   108  	require.Empty(t, b.TotalGasUsed)
   109  
   110  	bv3 := blockV3{
   111  		ParentBlockHash:   GenesisBlockHash,
   112  		Height:            1,
   113  		TotalSupply:       2,
   114  		ReceiptRoot:       gethCommon.Hash{0x02},
   115  		TransactionHashes: []gethCommon.Hash{{0x04}},
   116  	}
   117  
   118  	b3, err := gethRLP.EncodeToBytes(bv3)
   119  	require.NoError(t, err)
   120  
   121  	b = decodeBlockBreakingChanges(b3)
   122  
   123  	require.Equal(t, b.TotalSupply.Uint64(), bv3.TotalSupply)
   124  	require.Equal(t, b.Height, bv3.Height)
   125  	require.Equal(t, b.ParentBlockHash, bv3.ParentBlockHash)
   126  	require.Equal(t, b.TransactionHashes, bv3.TransactionHashes)
   127  	require.Empty(t, b.Timestamp)
   128  	require.Empty(t, b.TotalGasUsed)
   129  
   130  	bv4 := blockV4{
   131  		ParentBlockHash:   GenesisBlockHash,
   132  		Height:            1,
   133  		TotalSupply:       big.NewInt(4),
   134  		ReceiptRoot:       gethCommon.Hash{0x02},
   135  		TransactionHashes: []gethCommon.Hash{{0x04}},
   136  	}
   137  
   138  	b4, err := gethRLP.EncodeToBytes(bv4)
   139  	require.NoError(t, err)
   140  
   141  	b = decodeBlockBreakingChanges(b4)
   142  
   143  	require.Equal(t, b.TotalSupply, bv4.TotalSupply)
   144  	require.Equal(t, b.Height, bv4.Height)
   145  	require.Equal(t, b.ParentBlockHash, bv4.ParentBlockHash)
   146  	require.Equal(t, b.TransactionHashes, bv4.TransactionHashes)
   147  	require.Empty(t, b.Timestamp)
   148  	require.Empty(t, b.TotalGasUsed)
   149  
   150  	bv5 := blockV5{
   151  		ParentBlockHash:   GenesisBlockHash,
   152  		Height:            1,
   153  		TotalSupply:       big.NewInt(2),
   154  		ReceiptRoot:       gethCommon.Hash{0x02},
   155  		TransactionHashes: []gethCommon.Hash{{0x04}},
   156  		Timestamp:         100,
   157  	}
   158  
   159  	b5, err := gethRLP.EncodeToBytes(bv5)
   160  	require.NoError(t, err)
   161  
   162  	b = decodeBlockBreakingChanges(b5)
   163  
   164  	require.Equal(t, b.Timestamp, bv5.Timestamp)
   165  	require.Equal(t, b.TotalSupply, bv5.TotalSupply)
   166  	require.Equal(t, b.Height, bv5.Height)
   167  	require.Equal(t, b.ParentBlockHash, bv5.ParentBlockHash)
   168  	require.Equal(t, b.TransactionHashes, bv5.TransactionHashes)
   169  	require.Empty(t, b.TotalGasUsed)
   170  }