Initial stateDB and related interface implementations

This commit is contained in:
Aleksandr Bezobchuk
2018-10-02 20:22:15 -04:00
parent 17fa941fa1
commit be81045e26
10 changed files with 678 additions and 38 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ func (cc *ChainContext) CalcDifficulty(_ ethcons.ChainReader, _ uint64, _ *ethty
//
// TODO: Figure out if this needs to be hooked up to any part of the ABCI?
func (cc *ChainContext) Finalize(
_ ethcons.ChainReader, _ *ethtypes.Header, _ *ethstate.StateDB,
_ ethcons.ChainReader, _ *ethtypes.Header, _ ethstate.StateDB,
_ []*ethtypes.Transaction, _ []*ethtypes.Header, _ []*ethtypes.Receipt,
) (*ethtypes.Block, error) {
return nil, nil
-66
View File
@@ -1,66 +0,0 @@
package core
import (
ethdb "github.com/ethereum/go-ethereum/ethdb"
dbm "github.com/tendermint/tendermint/libs/db"
)
// EthereumDB implements Ethereum's ethdb.Database and ethdb.Batch interfaces.
// It will be used to facilitate persistence of codeHash => code mappings.
type EthereumDB struct {
CodeDB dbm.DB
}
// Put implements Ethereum's ethdb.Putter interface. It wraps the database
// write operation supported by both batches and regular databases.
func (edb *EthereumDB) Put(key []byte, value []byte) error {
edb.CodeDB.Set(key, value)
return nil
}
// Get implements Ethereum's ethdb.Database interface. It returns a value for a
// given key.
func (edb *EthereumDB) Get(key []byte) ([]byte, error) {
return edb.CodeDB.Get(key), nil
}
// Has implements Ethereum's ethdb.Database interface. It returns a boolean
// determining if the underlying database has the given key or not.
func (edb *EthereumDB) Has(key []byte) (bool, error) {
return edb.CodeDB.Has(key), nil
}
// Delete implements Ethereum's ethdb.Database interface. It removes a given
// key from the underlying database.
func (edb *EthereumDB) Delete(key []byte) error {
edb.CodeDB.Delete(key)
return nil
}
// Close implements Ethereum's ethdb.Database interface. It closes the
// underlying database.
func (edb *EthereumDB) Close() {
edb.CodeDB.Close()
}
// NewBatch implements Ethereum's ethdb.Database interface. It returns a new
// Batch object used for batch database operations.
func (edb *EthereumDB) NewBatch() ethdb.Batch {
return edb
}
// ValueSize implements Ethereum's ethdb.Database interface. It performs a
// no-op.
func (edb *EthereumDB) ValueSize() int {
return 0
}
// Write implements Ethereum's ethdb.Database interface. It performs a no-op.
func (edb *EthereumDB) Write() error {
return nil
}
// Reset implements Ethereum's ethdb.Database interface. It performs a no-op.
func (edb *EthereumDB) Reset() {
}
-147
View File
@@ -1,147 +0,0 @@
package core
// NOTE: A bulk of these unit tests will change and evolve as the context and
// implementation of ChainConext evolves.
import (
"fmt"
"testing"
ethdb "github.com/ethereum/go-ethereum/ethdb"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tendermint/libs/db"
)
type (
kvPair struct {
key, value []byte
}
)
func newEthereumDB() *EthereumDB {
memDB := dbm.NewMemDB()
return &EthereumDB{CodeDB: memDB}
}
func TestEthereumDBInterface(t *testing.T) {
require.Implements(t, (*ethdb.Database)(nil), new(EthereumDB))
require.Implements(t, (*ethdb.Batch)(nil), new(EthereumDB))
}
func TestEthereumDBGet(t *testing.T) {
testEDB := newEthereumDB()
testCases := []struct {
edb *EthereumDB
data *kvPair
key []byte
expectedValue []byte
}{
{
edb: testEDB,
key: []byte("foo"),
expectedValue: nil,
},
{
edb: testEDB,
data: &kvPair{[]byte("foo"), []byte("bar")},
key: []byte("foo"),
expectedValue: []byte("bar"),
},
}
for i, tc := range testCases {
if tc.data != nil {
tc.edb.Put(tc.data.key, tc.data.value)
}
value, err := tc.edb.Get(tc.key)
require.Nil(t, err, fmt.Sprintf("unexpected error: test case #%d", i))
require.Equal(t, tc.expectedValue, value, fmt.Sprintf("unexpected result: test case #%d", i))
}
}
func TestEthereumDBHas(t *testing.T) {
testEDB := newEthereumDB()
testCases := []struct {
edb *EthereumDB
data *kvPair
key []byte
expectedValue bool
}{
{
edb: testEDB,
key: []byte("foo"),
expectedValue: false,
},
{
edb: testEDB,
data: &kvPair{[]byte("foo"), []byte("bar")},
key: []byte("foo"),
expectedValue: true,
},
}
for i, tc := range testCases {
if tc.data != nil {
tc.edb.Put(tc.data.key, tc.data.value)
}
ok, err := tc.edb.Has(tc.key)
require.Nil(t, err, fmt.Sprintf("unexpected error: test case #%d", i))
require.Equal(t, tc.expectedValue, ok, fmt.Sprintf("unexpected result: test case #%d", i))
}
}
func TestEthereumDBDelete(t *testing.T) {
testEDB := newEthereumDB()
testCases := []struct {
edb *EthereumDB
data *kvPair
key []byte
}{
{
edb: testEDB,
key: []byte("foo"),
},
{
edb: testEDB,
data: &kvPair{[]byte("foo"), []byte("bar")},
key: []byte("foo"),
},
}
for i, tc := range testCases {
if tc.data != nil {
tc.edb.Put(tc.data.key, tc.data.value)
}
err := tc.edb.Delete(tc.key)
ok, _ := tc.edb.Has(tc.key)
require.Nil(t, err, fmt.Sprintf("unexpected error: test case #%d", i))
require.False(t, ok, fmt.Sprintf("unexpected existence of key: test case #%d", i))
}
}
func TestEthereumDBNewBatch(t *testing.T) {
edb := newEthereumDB()
batch := edb.NewBatch()
require.Equal(t, edb, batch)
}
func TestEthereumDBValueSize(t *testing.T) {
edb := newEthereumDB()
size := edb.ValueSize()
require.Equal(t, 0, size)
}
func TestEthereumDBWrite(t *testing.T) {
edb := newEthereumDB()
err := edb.Write()
require.Nil(t, err)
}