|
| 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 | +} |
0 commit comments