From cb8d84783e68f3b297a6d7ad7945df395ace1b4f Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 15 Aug 2018 18:27:30 -0400 Subject: [PATCH] Fix broken state unit tests --- state/database.go | 4 +++- state/database_test.go | 32 +++++--------------------------- state/test_utils.go | 14 ++++++++++++++ state/trie_test.go | 7 ++----- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/state/database.go b/state/database.go index 92e2c1df..bd350ad9 100644 --- a/state/database.go +++ b/state/database.go @@ -59,7 +59,7 @@ type Database struct { // EXTCODESIZE calls. codeSizeCache *lru.Cache - storeCache *lru.Cache + storeCache *lru.Cache Tracing bool } @@ -96,9 +96,11 @@ func NewDatabase(stateDB, codeDB dbm.DB, storeCacheSize int) (*Database, error) db.ethTrieDB = ethtrie.NewDatabase(&core.EthereumDB{CodeDB: codeDB}) var err error + if db.codeSizeCache, err = lru.New(codeSizeCacheSize); err != nil { return nil, err } + if db.storeCache, err = lru.New(storeCacheSize); err != nil { return nil, err } diff --git a/state/database_test.go b/state/database_test.go index 906f457e..b300d33d 100644 --- a/state/database_test.go +++ b/state/database_test.go @@ -7,25 +7,17 @@ import ( ethcmn "github.com/ethereum/go-ethereum/common" ethstate "github.com/ethereum/go-ethereum/core/state" "github.com/stretchr/testify/require" - dbm "github.com/tendermint/tendermint/libs/db" ) -func newDatabase() *Database { - memDB := dbm.NewMemDB() - db, _ := NewDatabase(memDB, memDB) - - return db -} - func TestDatabaseInterface(t *testing.T) { require.Implements(t, (*ethstate.Database)(nil), new(Database)) } func TestDatabaseLatestVersion(t *testing.T) { - testDB := newDatabase() - var version int64 + testDB := newTestDatabase() + version = testDB.LatestVersion() require.Equal(t, int64(0), version) @@ -34,27 +26,13 @@ func TestDatabaseLatestVersion(t *testing.T) { require.Equal(t, int64(1), version) } -// func TestDatabaseOpenTrie(t *testing.T) { -// testDB := newDatabase() - -// testTrie, err := testDB.OpenTrie(rootHashFromVersion(0)) -// require.Nil(t, err) -// require.IsType(t, &Trie{}, testTrie) -// require.NotNil(t, testTrie.(*Trie).store) -// require.NotNil(t, testTrie.(*Trie).accountsCache) -// require.NotNil(t, testTrie.(*Trie).storageCache) -// require.NotNil(t, testTrie.(*Trie).ethTrieDB) -// require.False(t, testTrie.(*Trie).empty) - -// } - func TestDatabaseCopyTrie(t *testing.T) { // TODO: Implement once CopyTrie is implemented t.SkipNow() } func TestDatabaseContractCode(t *testing.T) { - testDB := newDatabase() + testDB := newTestDatabase() testCases := []struct { db *Database @@ -87,7 +65,7 @@ func TestDatabaseContractCode(t *testing.T) { } func TestDatabaseContractCodeSize(t *testing.T) { - testDB := newDatabase() + testDB := newTestDatabase() testCases := []struct { db *Database @@ -125,7 +103,7 @@ func TestDatabaseContractCodeSize(t *testing.T) { } func TestDatabaseTrieDB(t *testing.T) { - testDB := newDatabase() + testDB := newTestDatabase() db := testDB.TrieDB() require.Equal(t, testDB.ethTrieDB, db) diff --git a/state/test_utils.go b/state/test_utils.go index 937d7caa..66b52fdc 100644 --- a/state/test_utils.go +++ b/state/test_utils.go @@ -1,10 +1,13 @@ package state import ( + "fmt" "math/rand" "time" ethcmn "github.com/ethereum/go-ethereum/common" + + dbm "github.com/tendermint/tendermint/libs/db" ) type ( @@ -21,3 +24,14 @@ type ( func init() { rand.Seed(time.Now().UnixNano()) } + +func newTestDatabase() *Database { + memDB := dbm.NewMemDB() + + testDB, err := NewDatabase(memDB, memDB, 100) + if err != nil { + panic(fmt.Sprintf("failed to create database: %v", err)) + } + + return testDB +} diff --git a/state/trie_test.go b/state/trie_test.go index 8de2628e..84a5c048 100644 --- a/state/trie_test.go +++ b/state/trie_test.go @@ -8,20 +8,17 @@ import ( ethcmn "github.com/ethereum/go-ethereum/common" ethstate "github.com/ethereum/go-ethereum/core/state" "github.com/stretchr/testify/require" - dbm "github.com/tendermint/tendermint/libs/db" ) func newTestTrie() *Trie { - memDB := dbm.NewMemDB() - testDB, _ := NewDatabase(memDB, memDB) + testDB := newTestDatabase() testTrie, _ := testDB.OpenTrie(rootHashFromVersion(0)) return testTrie.(*Trie) } func newTestPrefixTrie() *Trie { - memDB := dbm.NewMemDB() - testDB, _ := NewDatabase(memDB, memDB) + testDB := newTestDatabase() prefix := make([]byte, ethcmn.HashLength) rand.Read(prefix)