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