github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/consensus/bor/bor_test.go (about)

     1  package bor
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/ethereum/go-ethereum/common"
    10  	"github.com/ethereum/go-ethereum/common/hexutil" //nolint:typecheck
    11  	"github.com/ethereum/go-ethereum/core"
    12  	"github.com/ethereum/go-ethereum/core/rawdb"
    13  	"github.com/ethereum/go-ethereum/core/state"
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  	"github.com/ethereum/go-ethereum/core/vm"
    16  	"github.com/ethereum/go-ethereum/params"
    17  )
    18  
    19  func TestGenesisContractChange(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	addr0 := common.Address{0x1}
    23  
    24  	b := &Bor{
    25  		config: &params.BorConfig{
    26  			Sprint: map[string]uint64{
    27  				"0": 10,
    28  			}, // skip sprint transactions in sprint
    29  			BlockAlloc: map[string]interface{}{
    30  				// write as interface since that is how it is decoded in genesis
    31  				"2": map[string]interface{}{
    32  					addr0.Hex(): map[string]interface{}{
    33  						"code":    hexutil.Bytes{0x1, 0x2},
    34  						"balance": "0",
    35  					},
    36  				},
    37  				"4": map[string]interface{}{
    38  					addr0.Hex(): map[string]interface{}{
    39  						"code":    hexutil.Bytes{0x1, 0x3},
    40  						"balance": "0x1000",
    41  					},
    42  				},
    43  			},
    44  		},
    45  	}
    46  
    47  	genspec := &core.Genesis{
    48  		Alloc: map[common.Address]core.GenesisAccount{
    49  			addr0: {
    50  				Balance: big.NewInt(0),
    51  				Code:    []byte{0x1, 0x1},
    52  			},
    53  		},
    54  	}
    55  
    56  	db := rawdb.NewMemoryDatabase()
    57  	genesis := genspec.MustCommit(db)
    58  
    59  	statedb, err := state.New(genesis.Root(), state.NewDatabase(db), nil)
    60  	require.NoError(t, err)
    61  
    62  	config := params.ChainConfig{}
    63  	chain, err := core.NewBlockChain(db, nil, &config, b, vm.Config{}, nil, nil, nil)
    64  	require.NoError(t, err)
    65  
    66  	addBlock := func(root common.Hash, num int64) (common.Hash, *state.StateDB) {
    67  		h := &types.Header{
    68  			ParentHash: root,
    69  			Number:     big.NewInt(num),
    70  		}
    71  		b.Finalize(chain, h, statedb, nil, nil)
    72  
    73  		// write state to database
    74  		root, err := statedb.Commit(false)
    75  		require.NoError(t, err)
    76  		require.NoError(t, statedb.Database().TrieDB().Commit(root, true, nil))
    77  
    78  		statedb, err := state.New(h.Root, state.NewDatabase(db), nil)
    79  		require.NoError(t, err)
    80  
    81  		return root, statedb
    82  	}
    83  
    84  	require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x1})
    85  
    86  	root := genesis.Root()
    87  
    88  	// code does not change
    89  	root, statedb = addBlock(root, 1)
    90  	require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x1})
    91  
    92  	// code changes 1st time
    93  	root, statedb = addBlock(root, 2)
    94  	require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x2})
    95  
    96  	// code same as 1st change
    97  	root, statedb = addBlock(root, 3)
    98  	require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x2})
    99  
   100  	// code changes 2nd time
   101  	_, statedb = addBlock(root, 4)
   102  	require.Equal(t, statedb.GetCode(addr0), []byte{0x1, 0x3})
   103  
   104  	// make sure balance change DOES NOT take effect
   105  	require.Equal(t, statedb.GetBalance(addr0), big.NewInt(0))
   106  }
   107  
   108  func TestEncodeSigHeaderJaipur(t *testing.T) {
   109  	t.Parallel()
   110  
   111  	// As part of the EIP-1559 fork in mumbai, an incorrect seal hash
   112  	// was used for Bor that did not included the BaseFee. The Jaipur
   113  	// block is a hard fork to fix that.
   114  	h := &types.Header{
   115  		Difficulty: new(big.Int),
   116  		Number:     big.NewInt(1),
   117  		Extra:      make([]byte, 32+65),
   118  	}
   119  
   120  	var (
   121  		// hash for the block without the BaseFee
   122  		hashWithoutBaseFee = common.HexToHash("0x1be13e83939b3c4701ee57a34e10c9290ce07b0e53af0fe90b812c6881826e36")
   123  		// hash for the block with the baseFee
   124  		hashWithBaseFee = common.HexToHash("0xc55b0cac99161f71bde1423a091426b1b5b4d7598e5981ad802cce712771965b")
   125  	)
   126  
   127  	// Jaipur NOT enabled and BaseFee not set
   128  	hash := SealHash(h, &params.BorConfig{JaipurBlock: big.NewInt(10)})
   129  	require.Equal(t, hash, hashWithoutBaseFee)
   130  
   131  	// Jaipur enabled (Jaipur=0) and BaseFee not set
   132  	hash = SealHash(h, &params.BorConfig{JaipurBlock: common.Big0})
   133  	require.Equal(t, hash, hashWithoutBaseFee)
   134  
   135  	h.BaseFee = big.NewInt(2)
   136  
   137  	// Jaipur enabled (Jaipur=Header block) and BaseFee set
   138  	hash = SealHash(h, &params.BorConfig{JaipurBlock: common.Big1})
   139  	require.Equal(t, hash, hashWithBaseFee)
   140  
   141  	// Jaipur NOT enabled and BaseFee set
   142  	hash = SealHash(h, &params.BorConfig{JaipurBlock: big.NewInt(10)})
   143  	require.Equal(t, hash, hashWithoutBaseFee)
   144  }