diff options
author | Péter Szilágyi <peterke@gmail.com> | 2018-09-24 20:57:49 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2019-03-06 19:35:03 +0800 |
commit | 054412e33528e53f6deae940c870217b614707b9 (patch) | |
tree | 7ffc999bb39384e1bfa8c71d80923879fc2e866b /core/state | |
parent | 15eee47ebf878b4eff3c2359b9eaa57bba397448 (diff) | |
download | go-tangerine-054412e33528e53f6deae940c870217b614707b9.tar go-tangerine-054412e33528e53f6deae940c870217b614707b9.tar.gz go-tangerine-054412e33528e53f6deae940c870217b614707b9.tar.bz2 go-tangerine-054412e33528e53f6deae940c870217b614707b9.tar.lz go-tangerine-054412e33528e53f6deae940c870217b614707b9.tar.xz go-tangerine-054412e33528e53f6deae940c870217b614707b9.tar.zst go-tangerine-054412e33528e53f6deae940c870217b614707b9.zip |
all: clean up and proerly abstract database access
Diffstat (limited to 'core/state')
-rw-r--r-- | core/state/database.go | 4 | ||||
-rw-r--r-- | core/state/iterator_test.go | 5 | ||||
-rw-r--r-- | core/state/managed_state_test.go | 4 | ||||
-rw-r--r-- | core/state/state_test.go | 7 | ||||
-rw-r--r-- | core/state/statedb_test.go | 37 | ||||
-rw-r--r-- | core/state/sync.go | 3 | ||||
-rw-r--r-- | core/state/sync_test.go | 15 |
7 files changed, 43 insertions, 32 deletions
diff --git a/core/state/database.go b/core/state/database.go index f6ea144b9..ce085747a 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -68,7 +68,7 @@ type Trie interface { Hash() common.Hash NodeIterator(startKey []byte) trie.NodeIterator GetKey([]byte) []byte // TODO(fjl): remove this when SecureTrie is removed - Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error + Prove(key []byte, fromLevel uint, proofDb ethdb.Writer) error } // NewDatabase creates a backing store for state. The returned database is safe for @@ -179,6 +179,6 @@ func (m cachedTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) { return root, err } -func (m cachedTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error { +func (m cachedTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.Writer) error { return m.SecureTrie.Prove(key, fromLevel, proofDb) } diff --git a/core/state/iterator_test.go b/core/state/iterator_test.go index 9e46c851c..69f51c4c7 100644 --- a/core/state/iterator_test.go +++ b/core/state/iterator_test.go @@ -51,7 +51,9 @@ func TestNodeIteratorCoverage(t *testing.T) { t.Errorf("state entry not reported %x", hash) } } - for _, key := range db.TrieDB().DiskDB().(*ethdb.MemDatabase).Keys() { + it := db.TrieDB().DiskDB().(ethdb.Database).NewIterator() + for it.Next() { + key := it.Key() if bytes.HasPrefix(key, []byte("secure-key-")) { continue } @@ -59,4 +61,5 @@ func TestNodeIteratorCoverage(t *testing.T) { t.Errorf("state entry not reported %x", key) } } + it.Release() } diff --git a/core/state/managed_state_test.go b/core/state/managed_state_test.go index 3d9c4e867..fdfde96ad 100644 --- a/core/state/managed_state_test.go +++ b/core/state/managed_state_test.go @@ -20,13 +20,13 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/core/rawdb" ) var addr = common.BytesToAddress([]byte("test")) func create() (*ManagedState, *account) { - statedb, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase())) + statedb, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase())) ms := ManageState(statedb) ms.StateDB.SetNonce(addr, 100) ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr)) diff --git a/core/state/state_test.go b/core/state/state_test.go index a09273f3b..606f2a6f6 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -22,13 +22,14 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" checker "gopkg.in/check.v1" ) type StateSuite struct { - db *ethdb.MemDatabase + db ethdb.Database state *StateDB } @@ -87,7 +88,7 @@ func (s *StateSuite) TestDump(c *checker.C) { } func (s *StateSuite) SetUpTest(c *checker.C) { - s.db = ethdb.NewMemDatabase() + s.db = rawdb.NewMemoryDatabase() s.state, _ = New(common.Hash{}, NewDatabase(s.db)) } @@ -141,7 +142,7 @@ func (s *StateSuite) TestSnapshotEmpty(c *checker.C) { // use testing instead of checker because checker does not support // printing/logging in tests (-check.vv does not work) func TestSnapshot2(t *testing.T) { - state, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase())) + state, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase())) stateobjaddr0 := toAddr([]byte("so0")) stateobjaddr1 := toAddr([]byte("so1")) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 69392d972..c2d2b2f69 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -31,15 +31,15 @@ import ( check "gopkg.in/check.v1" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethdb" ) // Tests that updating a state trie does not leak any database writes prior to // actually committing the state. func TestUpdateLeaks(t *testing.T) { // Create an empty state database - db := ethdb.NewMemDatabase() + db := rawdb.NewMemoryDatabase() state, _ := New(common.Hash{}, NewDatabase(db)) // Update it with some accounts @@ -56,18 +56,19 @@ func TestUpdateLeaks(t *testing.T) { state.IntermediateRoot(false) } // Ensure that no data was leaked into the database - for _, key := range db.Keys() { - value, _ := db.Get(key) - t.Errorf("State leaked into database: %x -> %x", key, value) + it := db.NewIterator() + for it.Next() { + t.Errorf("State leaked into database: %x -> %x", it.Key(), it.Value()) } + it.Release() } // Tests that no intermediate state of an object is stored into the database, // only the one right before the commit. func TestIntermediateLeaks(t *testing.T) { // Create two state databases, one transitioning to the final state, the other final from the beginning - transDb := ethdb.NewMemDatabase() - finalDb := ethdb.NewMemDatabase() + transDb := rawdb.NewMemoryDatabase() + finalDb := rawdb.NewMemoryDatabase() transState, _ := New(common.Hash{}, NewDatabase(transDb)) finalState, _ := New(common.Hash{}, NewDatabase(finalDb)) @@ -103,16 +104,20 @@ func TestIntermediateLeaks(t *testing.T) { if _, err := finalState.Commit(false); err != nil { t.Fatalf("failed to commit final state: %v", err) } - for _, key := range finalDb.Keys() { + it := finalDb.NewIterator() + for it.Next() { + key := it.Key() if _, err := transDb.Get(key); err != nil { - val, _ := finalDb.Get(key) - t.Errorf("entry missing from the transition database: %x -> %x", key, val) + t.Errorf("entry missing from the transition database: %x -> %x", key, it.Value()) } } - for _, key := range transDb.Keys() { + it.Release() + + it = transDb.NewIterator() + for it.Next() { + key := it.Key() if _, err := finalDb.Get(key); err != nil { - val, _ := transDb.Get(key) - t.Errorf("extra entry in the transition database: %x -> %x", key, val) + t.Errorf("extra entry in the transition database: %x -> %x", key, it.Value()) } } } @@ -122,7 +127,7 @@ func TestIntermediateLeaks(t *testing.T) { // https://github.com/ethereum/go-ethereum/pull/15549. func TestCopy(t *testing.T) { // Create a random state test to copy and modify "independently" - orig, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase())) + orig, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase())) for i := byte(0); i < 255; i++ { obj := orig.GetOrNewStateObject(common.BytesToAddress([]byte{i})) @@ -342,7 +347,7 @@ func (test *snapshotTest) String() string { func (test *snapshotTest) run() bool { // Run all actions and create snapshots. var ( - state, _ = New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase())) + state, _ = New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase())) snapshotRevs = make([]int, len(test.snapshots)) sindex = 0 ) @@ -433,7 +438,7 @@ func (s *StateSuite) TestTouchDelete(c *check.C) { // TestCopyOfCopy tests that modified objects are carried over to the copy, and the copy of the copy. // See https://github.com/ethereum/go-ethereum/pull/15225#issuecomment-380191512 func TestCopyOfCopy(t *testing.T) { - sdb, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase())) + sdb, _ := New(common.Hash{}, NewDatabase(rawdb.NewMemoryDatabase())) addr := common.HexToAddress("aaaa") sdb.SetBalance(addr, big.NewInt(42)) diff --git a/core/state/sync.go b/core/state/sync.go index c566e7907..5290411a3 100644 --- a/core/state/sync.go +++ b/core/state/sync.go @@ -20,12 +20,13 @@ import ( "bytes" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" ) // NewStateSync create a new state trie download scheduler. -func NewStateSync(root common.Hash, database trie.DatabaseReader) *trie.Sync { +func NewStateSync(root common.Hash, database ethdb.Reader) *trie.Sync { var syncer *trie.Sync callback := func(leaf []byte, parent common.Hash) error { var obj Account diff --git a/core/state/sync_test.go b/core/state/sync_test.go index 317740160..ab4718b04 100644 --- a/core/state/sync_test.go +++ b/core/state/sync_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" @@ -38,7 +39,7 @@ type testAccount struct { // makeTestState create a sample test state to test node-wise reconstruction. func makeTestState() (Database, common.Hash, []*testAccount) { // Create an empty state - db := NewDatabase(ethdb.NewMemDatabase()) + db := NewDatabase(rawdb.NewMemoryDatabase()) state, _ := New(common.Hash{}, db) // Fill it with some arbitrary data @@ -124,7 +125,7 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) error { // Tests that an empty state is not scheduled for syncing. func TestEmptyStateSync(t *testing.T) { empty := common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") - if req := NewStateSync(empty, ethdb.NewMemDatabase()).Missing(1); len(req) != 0 { + if req := NewStateSync(empty, rawdb.NewMemoryDatabase()).Missing(1); len(req) != 0 { t.Errorf("content requested for empty state: %v", req) } } @@ -139,7 +140,7 @@ func testIterativeStateSync(t *testing.T, batch int) { srcDb, srcRoot, srcAccounts := makeTestState() // Create a destination state and sync with the scheduler - dstDb := ethdb.NewMemDatabase() + dstDb := rawdb.NewMemoryDatabase() sched := NewStateSync(srcRoot, dstDb) queue := append([]common.Hash{}, sched.Missing(batch)...) @@ -171,7 +172,7 @@ func TestIterativeDelayedStateSync(t *testing.T) { srcDb, srcRoot, srcAccounts := makeTestState() // Create a destination state and sync with the scheduler - dstDb := ethdb.NewMemDatabase() + dstDb := rawdb.NewMemoryDatabase() sched := NewStateSync(srcRoot, dstDb) queue := append([]common.Hash{}, sched.Missing(0)...) @@ -208,7 +209,7 @@ func testIterativeRandomStateSync(t *testing.T, batch int) { srcDb, srcRoot, srcAccounts := makeTestState() // Create a destination state and sync with the scheduler - dstDb := ethdb.NewMemDatabase() + dstDb := rawdb.NewMemoryDatabase() sched := NewStateSync(srcRoot, dstDb) queue := make(map[common.Hash]struct{}) @@ -248,7 +249,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) { srcDb, srcRoot, srcAccounts := makeTestState() // Create a destination state and sync with the scheduler - dstDb := ethdb.NewMemDatabase() + dstDb := rawdb.NewMemoryDatabase() sched := NewStateSync(srcRoot, dstDb) queue := make(map[common.Hash]struct{}) @@ -295,7 +296,7 @@ func TestIncompleteStateSync(t *testing.T) { checkTrieConsistency(srcDb.TrieDB().DiskDB().(ethdb.Database), srcRoot) // Create a destination state and sync with the scheduler - dstDb := ethdb.NewMemDatabase() + dstDb := rawdb.NewMemoryDatabase() sched := NewStateSync(srcRoot, dstDb) added := []common.Hash{} |