refactor state_database.go to better match original
This commit is contained in:
parent
f487162c07
commit
ee9bad734b
@ -30,27 +30,27 @@ var (
|
|||||||
errNotFound = errors.New("not found")
|
errNotFound = errors.New("not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
// StateDatabase interface is a union of the subset of the geth state.Database interface required
|
// Database interface is a union of the subset of the geth state.Database interface required
|
||||||
// to support the vm.StateDB implementation as well as methods specific to this Postgres based implementation
|
// to support the vm.StateDB implementation as well as methods specific to this Postgres based implementation
|
||||||
type StateDatabase interface {
|
type Database interface {
|
||||||
ContractCode(codeHash common.Hash) ([]byte, error)
|
ContractCode(codeHash common.Hash) ([]byte, error)
|
||||||
ContractCodeSize(codeHash common.Hash) (int, error)
|
ContractCodeSize(codeHash common.Hash) (int, error)
|
||||||
StateAccount(addressHash, blockHash common.Hash) (*types.StateAccount, error)
|
StateAccount(addressHash, blockHash common.Hash) (*types.StateAccount, error)
|
||||||
StorageValue(addressHash, slotHash, blockHash common.Hash) ([]byte, error)
|
StorageValue(addressHash, slotHash, blockHash common.Hash) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ StateDatabase = &stateDatabase{}
|
var _ Database = &cachingDB{}
|
||||||
|
|
||||||
type stateDatabase struct {
|
type cachingDB struct {
|
||||||
db sql.Database
|
db sql.Database
|
||||||
codeSizeCache *lru.Cache
|
codeSizeCache *lru.Cache
|
||||||
codeCache *fastcache.Cache
|
codeCache *fastcache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStateDatabase returns a new Database implementation using the passed parameters
|
// NewDatabase returns a new Database implementation using the passed parameters
|
||||||
func NewStateDatabase(db sql.Database) *stateDatabase {
|
func NewDatabase(db sql.Database) *cachingDB {
|
||||||
csc, _ := lru.New(codeSizeCacheSize)
|
csc, _ := lru.New(codeSizeCacheSize)
|
||||||
return &stateDatabase{
|
return &cachingDB{
|
||||||
db: db,
|
db: db,
|
||||||
codeSizeCache: csc,
|
codeSizeCache: csc,
|
||||||
codeCache: fastcache.New(codeCacheSize),
|
codeCache: fastcache.New(codeCacheSize),
|
||||||
@ -58,7 +58,7 @@ func NewStateDatabase(db sql.Database) *stateDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ContractCode satisfies Database, it returns the contract code for a given codehash
|
// ContractCode satisfies Database, it returns the contract code for a given codehash
|
||||||
func (sd *stateDatabase) ContractCode(codeHash common.Hash) ([]byte, error) {
|
func (sd *cachingDB) ContractCode(codeHash common.Hash) ([]byte, error) {
|
||||||
if code := sd.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
|
if code := sd.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
|
||||||
return code, nil
|
return code, nil
|
||||||
}
|
}
|
||||||
@ -79,7 +79,7 @@ func (sd *stateDatabase) ContractCode(codeHash common.Hash) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ContractCodeSize satisfies Database, it returns the length of the code for a provided codehash
|
// ContractCodeSize satisfies Database, it returns the length of the code for a provided codehash
|
||||||
func (sd *stateDatabase) ContractCodeSize(codeHash common.Hash) (int, error) {
|
func (sd *cachingDB) ContractCodeSize(codeHash common.Hash) (int, error) {
|
||||||
if cached, ok := sd.codeSizeCache.Get(codeHash); ok {
|
if cached, ok := sd.codeSizeCache.Get(codeHash); ok {
|
||||||
return cached.(int), nil
|
return cached.(int), nil
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ func (sd *stateDatabase) ContractCodeSize(codeHash common.Hash) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StateAccount satisfies Database, it returns the types.StateAccount for a provided address and block hash
|
// StateAccount satisfies Database, it returns the types.StateAccount for a provided address and block hash
|
||||||
func (sd *stateDatabase) StateAccount(addressHash, blockHash common.Hash) (*types.StateAccount, error) {
|
func (sd *cachingDB) StateAccount(addressHash, blockHash common.Hash) (*types.StateAccount, error) {
|
||||||
res := StateAccountResult{}
|
res := StateAccountResult{}
|
||||||
err := sd.db.QueryRow(context.Background(), GetStateAccount, addressHash.Hex(), blockHash.Hex()).
|
err := sd.db.QueryRow(context.Background(), GetStateAccount, addressHash.Hex(), blockHash.Hex()).
|
||||||
Scan(&res.Balance, &res.Nonce, &res.CodeHash, &res.StorageRoot, &res.Removed)
|
Scan(&res.Balance, &res.Nonce, &res.CodeHash, &res.StorageRoot, &res.Removed)
|
||||||
@ -111,7 +111,7 @@ func (sd *stateDatabase) StateAccount(addressHash, blockHash common.Hash) (*type
|
|||||||
|
|
||||||
// StorageValue satisfies Database, it returns the RLP-encoded storage value for the provided address, slot,
|
// StorageValue satisfies Database, it returns the RLP-encoded storage value for the provided address, slot,
|
||||||
// and block hash
|
// and block hash
|
||||||
func (sd *stateDatabase) StorageValue(addressHash, slotHash, blockHash common.Hash) ([]byte, error) {
|
func (sd *cachingDB) StorageValue(addressHash, slotHash, blockHash common.Hash) ([]byte, error) {
|
||||||
res := StorageSlotResult{}
|
res := StorageSlotResult{}
|
||||||
err := sd.db.QueryRow(context.Background(), GetStorageSlot,
|
err := sd.db.QueryRow(context.Background(), GetStorageSlot,
|
||||||
addressHash.Hex(), slotHash.Hex(), blockHash.Hex()).
|
addressHash.Hex(), slotHash.Hex(), blockHash.Hex()).
|
Loading…
Reference in New Issue
Block a user