From 9e5f03b6c487175cc5aa1224e5e12fd573f483a7 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 27 Jun 2017 15:57:06 +0200 Subject: core/state: access trie through Database interface, track errors (#14589) With this commit, core/state's access to the underlying key/value database is mediated through an interface. Database errors are tracked in StateDB and returned by CommitTo or the new Error method. Motivation for this change: We can remove the light client's duplicated copy of core/state. The light client now supports node iteration, so tracing and storage enumeration can work with the light client (not implemented in this commit). --- tests/block_test_util.go | 4 ++-- tests/state_test_util.go | 48 +++++++++++------------------------------------- tests/util.go | 7 +++++-- 3 files changed, 18 insertions(+), 41 deletions(-) (limited to 'tests') diff --git a/tests/block_test_util.go b/tests/block_test_util.go index b9678a77b..24d4672b6 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -204,7 +204,7 @@ func runBlockTest(homesteadBlock, daoForkBlock, gasPriceFork *big.Int, test *Blo // InsertPreState populates the given database with the genesis // accounts defined by the test. func (t *BlockTest) InsertPreState(db ethdb.Database) (*state.StateDB, error) { - statedb, err := state.New(common.Hash{}, db) + statedb, err := state.New(common.Hash{}, state.NewDatabase(db)) if err != nil { return nil, err } @@ -232,7 +232,7 @@ func (t *BlockTest) InsertPreState(db ethdb.Database) (*state.StateDB, error) { } } - root, err := statedb.Commit(false) + root, err := statedb.CommitTo(db, false) if err != nil { return nil, fmt.Errorf("error writing state: %v", err) } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index c1892cdcc..58acdd488 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -20,7 +20,6 @@ import ( "bytes" "fmt" "io" - "math/big" "strconv" "strings" "testing" @@ -99,7 +98,7 @@ func benchStateTest(chainConfig *params.ChainConfig, test VmTest, env map[string statedb := makePreState(db, test.Pre) b.StartTimer() - RunState(chainConfig, statedb, env, test.Exec) + RunState(chainConfig, statedb, db, env, test.Exec) } func runStateTests(chainConfig *params.ChainConfig, tests map[string]VmTest, skipTests []string) error { @@ -143,16 +142,9 @@ func runStateTest(chainConfig *params.ChainConfig, test VmTest) error { env["currentTimestamp"] = test.Env.CurrentTimestamp.(string) } - var ( - ret []byte - // gas *big.Int - // err error - logs []*types.Log - ) + ret, logs, root, _ := RunState(chainConfig, statedb, db, env, test.Transaction) - ret, logs, _, _ = RunState(chainConfig, statedb, env, test.Transaction) - - // Compare expected and actual return + // Return value: var rexp []byte if strings.HasPrefix(test.Out, "#") { n, _ := strconv.Atoi(test.Out[1:]) @@ -163,61 +155,43 @@ func runStateTest(chainConfig *params.ChainConfig, test VmTest) error { if !bytes.Equal(rexp, ret) { return fmt.Errorf("return failed. Expected %x, got %x\n", rexp, ret) } - - // check post state + // Post state content: for addr, account := range test.Post { address := common.HexToAddress(addr) if !statedb.Exist(address) { return fmt.Errorf("did not find expected post-state account: %s", addr) } - if balance := statedb.GetBalance(address); balance.Cmp(math.MustParseBig256(account.Balance)) != 0 { return fmt.Errorf("(%x) balance failed. Expected: %v have: %v\n", address[:4], math.MustParseBig256(account.Balance), balance) } - if nonce := statedb.GetNonce(address); nonce != math.MustParseUint64(account.Nonce) { return fmt.Errorf("(%x) nonce failed. Expected: %v have: %v\n", address[:4], account.Nonce, nonce) } - for addr, value := range account.Storage { v := statedb.GetState(address, common.HexToHash(addr)) vexp := common.HexToHash(value) - if v != vexp { return fmt.Errorf("storage failed:\n%x: %s:\nexpected: %x\nhave: %x\n(%v %v)\n", address[:4], addr, vexp, v, vexp.Big(), v.Big()) } } } - - root, _ := statedb.Commit(false) + // Root: if common.HexToHash(test.PostStateRoot) != root { return fmt.Errorf("Post state root error. Expected: %s have: %x", test.PostStateRoot, root) } - - // check logs - if len(test.Logs) > 0 { - if err := checkLogs(test.Logs, logs); err != nil { - return err - } - } - - return nil + // Logs: + return checkLogs(test.Logs, logs) } -func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, env, tx map[string]string) ([]byte, []*types.Log, *big.Int, error) { +func RunState(chainConfig *params.ChainConfig, statedb *state.StateDB, db ethdb.Database, env, tx map[string]string) ([]byte, []*types.Log, common.Hash, error) { environment, msg := NewEVMEnvironment(false, chainConfig, statedb, env, tx) gaspool := new(core.GasPool).AddGas(math.MustParseBig256(env["currentGasLimit"])) - root, _ := statedb.Commit(false) - statedb.Reset(root) - snapshot := statedb.Snapshot() - - ret, gasUsed, err := core.ApplyMessage(environment, msg, gaspool) + ret, _, err := core.ApplyMessage(environment, msg, gaspool) if err != nil { statedb.RevertToSnapshot(snapshot) } - statedb.Commit(chainConfig.IsEIP158(environment.Context.BlockNumber)) - - return ret, statedb.Logs(), gasUsed, err + root, _ := statedb.CommitTo(db, chainConfig.IsEIP158(environment.Context.BlockNumber)) + return ret, statedb.Logs(), root, err } diff --git a/tests/util.go b/tests/util.go index a3a9a1f64..ff02679ec 100644 --- a/tests/util.go +++ b/tests/util.go @@ -48,7 +48,6 @@ func init() { } func checkLogs(tlog []Log, logs []*types.Log) error { - if len(tlog) != len(logs) { return fmt.Errorf("log length mismatch. Expected %d, got %d", len(tlog), len(logs)) } else { @@ -106,10 +105,14 @@ func (self Log) Topics() [][]byte { } func makePreState(db ethdb.Database, accounts map[string]Account) *state.StateDB { - statedb, _ := state.New(common.Hash{}, db) + sdb := state.NewDatabase(db) + statedb, _ := state.New(common.Hash{}, sdb) for addr, account := range accounts { insertAccount(statedb, addr, account) } + // Commit and re-open to start with a clean state. + root, _ := statedb.CommitTo(db, false) + statedb, _ = state.New(root, sdb) return statedb } -- cgit v1.2.3