2020-05-15 07:52:45 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-05-21 12:27:52 +00:00
|
|
|
"bytes"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ StateReader = (*PlainStateReader)(nil)
|
|
|
|
|
|
|
|
// PlainStateReader reads data from so called "plain state".
|
|
|
|
// Data in the plain state is stored using un-hashed account/storage items
|
|
|
|
// as opposed to the "normal" state that uses hashes of merkle paths to store items.
|
|
|
|
type PlainStateReader struct {
|
|
|
|
db ethdb.Getter
|
|
|
|
uncommitedIncarnations map[common.Address]uint64
|
2020-05-21 12:27:52 +00:00
|
|
|
accountCache *lru.Cache
|
|
|
|
storageCache *lru.Cache
|
|
|
|
codeCache *lru.Cache
|
|
|
|
codeSizeCache *lru.Cache
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:52 +00:00
|
|
|
func NewPlainStateReaderWithFallback(db ethdb.Getter, incarnations map[common.Address]uint64) *PlainStateReader {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &PlainStateReader{
|
|
|
|
db: db,
|
|
|
|
uncommitedIncarnations: incarnations,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:52 +00:00
|
|
|
func (r *PlainStateReader) SetAccountCache(accountCache *lru.Cache) {
|
|
|
|
r.accountCache = accountCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) SetStorageCache(storageCache *lru.Cache) {
|
|
|
|
r.storageCache = storageCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) SetCodeCache(codeCache *lru.Cache) {
|
|
|
|
r.codeCache = codeCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) SetCodeSizeCache(codeSizeCache *lru.Cache) {
|
|
|
|
r.codeSizeCache = codeSizeCache
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (r *PlainStateReader) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.accountCache != nil {
|
|
|
|
if cached, ok := r.accountCache.Get(address); ok {
|
|
|
|
if cached == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return cached.(*accounts.Account), nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
enc, err := r.db.Get(dbutils.PlainStateBucket, address[:])
|
|
|
|
if err == nil {
|
|
|
|
acc := &accounts.Account{}
|
|
|
|
if err = acc.DecodeForStorage(enc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.accountCache != nil {
|
|
|
|
r.accountCache.Add(address, acc)
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
return acc, nil
|
|
|
|
} else if !entryNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.accountCache != nil {
|
|
|
|
r.accountCache.Add(address, nil)
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
|
2020-05-21 12:27:52 +00:00
|
|
|
var storageKeyP *[20 + 32]byte
|
|
|
|
if r.storageCache != nil {
|
|
|
|
var storageKey [20 + 32]byte
|
|
|
|
copy(storageKey[:], address[:])
|
|
|
|
copy(storageKey[20:], key[:])
|
|
|
|
if cached, ok := r.storageCache.Get(storageKey); ok {
|
|
|
|
if cached == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return cached.([]byte), nil
|
|
|
|
}
|
|
|
|
storageKeyP = &storageKey
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
enc, err := r.db.Get(dbutils.PlainStateBucket, dbutils.PlainGenerateCompositeStorageKey(address, incarnation, *key))
|
|
|
|
if err == nil {
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.storageCache != nil {
|
|
|
|
r.storageCache.Add(*storageKeyP, enc)
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
return enc, nil
|
|
|
|
} else if !entryNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.storageCache != nil {
|
|
|
|
r.storageCache.Add(*storageKeyP, nil)
|
|
|
|
}
|
|
|
|
return nil, nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountCode(address common.Address, codeHash common.Hash) ([]byte, error) {
|
2020-05-21 12:27:52 +00:00
|
|
|
if bytes.Equal(codeHash[:], emptyCodeHash) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if r.codeCache != nil {
|
|
|
|
if cached, ok := r.codeCache.Get(address); ok {
|
|
|
|
return cached.([]byte), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code, err := r.db.Get(dbutils.CodeBucket, codeHash[:])
|
|
|
|
if r.codeCache != nil {
|
|
|
|
r.codeCache.Add(address, code)
|
|
|
|
}
|
|
|
|
return code, err
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountCodeSize(address common.Address, codeHash common.Hash) (int, error) {
|
2020-05-21 12:27:52 +00:00
|
|
|
if bytes.Equal(codeHash[:], emptyCodeHash) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if r.codeSizeCache != nil {
|
|
|
|
if cached, ok := r.codeSizeCache.Get(address); ok {
|
|
|
|
return cached.(int), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code, err := r.db.Get(dbutils.CodeBucket, codeHash[:])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if r.codeSizeCache != nil {
|
|
|
|
r.codeSizeCache.Add(address, len(code))
|
|
|
|
}
|
|
|
|
return len(code), nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountIncarnation(address common.Address) (uint64, error) {
|
|
|
|
if inc, ok := r.uncommitedIncarnations[address]; ok {
|
|
|
|
return inc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
incarnation, found, err := ethdb.PlainGetCurrentAccountIncarnation(r.db, address)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
return incarnation, nil
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:27:52 +00:00
|
|
|
return 0, nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func entryNotFound(err error) bool {
|
|
|
|
return err == ethdb.ErrKeyNotFound
|
|
|
|
}
|