Skip to content

Commit b747754

Browse files
committed
Merge pull request bnb-chain#1881 from Gustav-Simonsson/state_new_error
core/state, core, miner: handle missing root error from state.New
2 parents f466243 + 1b1f293 commit b747754

23 files changed

Lines changed: 182 additions & 95 deletions

‎cmd/evm/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func run(ctx *cli.Context) {
113113
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))
114114

115115
db, _ := ethdb.NewMemDatabase()
116-
statedb := state.New(common.Hash{}, db)
116+
statedb, _ := state.New(common.Hash{}, db)
117117
sender := statedb.CreateAccount(common.StringToAddress("sender"))
118118
receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
119119
receiver.SetCode(common.Hex2Bytes(ctx.GlobalString(CodeFlag.Name)))

‎cmd/geth/blocktestcmd.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ func runBlockTest(ctx *cli.Context) {
101101

102102
func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, error) {
103103
cfg := utils.MakeEthConfig(ClientIdentifier, Version, ctx)
104-
cfg.NewDB = func(path string) (ethdb.Database, error) { return ethdb.NewMemDatabase() }
104+
db, _ := ethdb.NewMemDatabase()
105+
cfg.NewDB = func(path string) (ethdb.Database, error) { return db, nil }
105106
cfg.MaxPeers = 0 // disable network
106107
cfg.Shh = false // disable whisper
107108
cfg.NAT = nil // disable port mapping
@@ -113,7 +114,7 @@ func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, er
113114
// import the genesis block
114115
ethereum.ResetWithGenesisBlock(test.Genesis)
115116
// import pre accounts
116-
_, err = test.InsertPreState(ethereum)
117+
_, err = test.InsertPreState(db, cfg.AccountManager)
117118
if err != nil {
118119
return ethereum, fmt.Errorf("InsertPreState: %v", err)
119120
}
@@ -123,7 +124,10 @@ func runOneBlockTest(ctx *cli.Context, test *tests.BlockTest) (*eth.Ethereum, er
123124
if err != nil {
124125
return ethereum, fmt.Errorf("Block Test load error: %v", err)
125126
}
126-
newDB := cm.State()
127+
newDB, err := cm.State()
128+
if err != nil {
129+
return ethereum, fmt.Errorf("Block Test get state error: %v", err)
130+
}
127131
if err := test.ValidatePostState(newDB); err != nil {
128132
return ethereum, fmt.Errorf("post state validation failed: %v", err)
129133
}

‎cmd/geth/chaincmd.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ func dump(ctx *cli.Context) {
179179
fmt.Println("{}")
180180
utils.Fatalf("block not found")
181181
} else {
182-
state := state.New(block.Root(), chainDb)
182+
state, err := state.New(block.Root(), chainDb)
183+
if err != nil {
184+
utils.Fatalf("could not create new state: %v", err)
185+
return
186+
}
183187
fmt.Printf("%s\n", state.Dump())
184188
}
185189
}

‎core/block_processor.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ func (sm *BlockProcessor) Process(block *types.Block) (logs vm.Logs, receipts ty
207207

208208
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs vm.Logs, receipts types.Receipts, err error) {
209209
// Create a new state based on the parent's root (e.g., create copy)
210-
state := state.New(parent.Root(), sm.chainDb)
210+
state, err := state.New(parent.Root(), sm.chainDb)
211+
if err != nil {
212+
return nil, nil, err
213+
}
211214
header := block.Header()
212215
uncles := block.Uncles()
213216
txs := block.Transactions()

‎core/block_processor_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestNumber(t *testing.T) {
4646
pow := ezp.New()
4747
_, chain := proc()
4848

49-
statedb := state.New(chain.Genesis().Root(), chain.chainDb)
49+
statedb, _ := state.New(chain.Genesis().Root(), chain.chainDb)
5050
header := makeHeader(chain.Genesis(), statedb)
5151
header.Number = big.NewInt(3)
5252
err := ValidateHeader(pow, header, chain.Genesis().Header(), false, false)

‎core/blockchain.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (self *BlockChain) SetProcessor(proc types.BlockProcessor) {
199199
self.processor = proc
200200
}
201201

202-
func (self *BlockChain) State() *state.StateDB {
202+
func (self *BlockChain) State() (*state.StateDB, error) {
203203
return state.New(self.CurrentBlock().Root(), self.chainDb)
204204
}
205205

‎core/chain_makers.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ func (b *BlockGen) OffsetTime(seconds int64) {
156156
// values. Inserting them into BlockChain requires use of FakePow or
157157
// a similar non-validating proof of work implementation.
158158
func GenerateChain(parent *types.Block, db ethdb.Database, n int, gen func(int, *BlockGen)) []*types.Block {
159-
statedb := state.New(parent.Root(), db)
159+
statedb, err := state.New(parent.Root(), db)
160+
if err != nil {
161+
panic(err)
162+
}
160163
blocks := make(types.Blocks, n)
161164
genblock := func(i int, h *types.Header) *types.Block {
162165
b := &BlockGen{parent: parent, i: i, chain: blocks, header: h, statedb: statedb}

‎core/chain_makers_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func ExampleGenerateChain() {
8484
return
8585
}
8686

87-
state := chainman.State()
87+
state, _ := chainman.State()
8888
fmt.Printf("last block: #%d\n", chainman.CurrentBlock().Number())
8989
fmt.Println("balance of addr1:", state.GetBalance(addr1))
9090
fmt.Println("balance of addr2:", state.GetBalance(addr2))

‎core/genesis.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
6060
return nil, err
6161
}
6262

63-
statedb := state.New(common.Hash{}, chainDb)
63+
// creating with empty hash always works
64+
statedb, _ := state.New(common.Hash{}, chainDb)
6465
for addr, account := range genesis.Alloc {
6566
address := common.HexToAddress(addr)
6667
statedb.AddBalance(address, common.String2Big(account.Balance))
@@ -115,9 +116,9 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
115116
}
116117

117118
// GenesisBlockForTesting creates a block in which addr has the given wei balance.
118-
// The state trie of the block is written to db.
119+
// The state trie of the block is written to db. the passed db needs to contain a state root
119120
func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance *big.Int) *types.Block {
120-
statedb := state.New(common.Hash{}, db)
121+
statedb, _ := state.New(common.Hash{}, db)
121122
obj := statedb.GetOrNewStateObject(addr)
122123
obj.SetBalance(balance)
123124
root, err := statedb.Commit()

‎core/state/managed_state_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var addr = common.BytesToAddress([]byte("test"))
2727

2828
func create() (*ManagedState, *account) {
2929
db, _ := ethdb.NewMemDatabase()
30-
statedb := New(common.Hash{}, db)
30+
statedb, _ := New(common.Hash{}, db)
3131
ms := ManageState(statedb)
3232
so := &StateObject{address: addr, nonce: 100}
3333
ms.StateDB.stateObjects[addr.Str()] = so

0 commit comments

Comments
 (0)