2020-05-15 07:52:45 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-05-21 12:27:52 +00:00
|
|
|
"bytes"
|
2020-05-22 15:11:00 +00:00
|
|
|
"encoding/binary"
|
2020-07-10 06:03:18 +00:00
|
|
|
"errors"
|
2020-05-21 12:27:52 +00:00
|
|
|
|
2020-07-10 06:03:18 +00:00
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
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 {
|
2020-07-10 06:03:18 +00:00
|
|
|
db ethdb.Getter
|
|
|
|
accountCache *fastcache.Cache
|
|
|
|
storageCache *fastcache.Cache
|
|
|
|
codeCache *fastcache.Cache
|
|
|
|
codeSizeCache *fastcache.Cache
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 12:27:21 +00:00
|
|
|
func NewPlainStateReader(db ethdb.Getter) *PlainStateReader {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &PlainStateReader{
|
2020-07-10 06:03:18 +00:00
|
|
|
db: db,
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (r *PlainStateReader) SetAccountCache(accountCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
r.accountCache = accountCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (r *PlainStateReader) SetStorageCache(storageCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
r.storageCache = storageCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (r *PlainStateReader) SetCodeCache(codeCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
r.codeCache = codeCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (r *PlainStateReader) SetCodeSizeCache(codeSizeCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
r.codeSizeCache = codeSizeCache
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (r *PlainStateReader) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
2020-05-22 15:11:00 +00:00
|
|
|
var enc []byte
|
|
|
|
var ok bool
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.accountCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
enc, ok = r.accountCache.HasGet(nil, address[:])
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
enc, err = r.db.Get(dbutils.PlainStateBucket, address[:])
|
2020-07-10 06:03:18 +00:00
|
|
|
if err != nil && !errors.Is(err, ethdb.ErrKeyNotFound) {
|
2020-05-15 07:52:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
if !ok && r.accountCache != nil {
|
|
|
|
r.accountCache.Set(address[:], enc)
|
|
|
|
}
|
|
|
|
if enc == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
acc := &accounts.Account{}
|
|
|
|
if err := acc.DecodeForStorage(enc); err != nil {
|
|
|
|
return nil, err
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
return acc, 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-22 15:11:00 +00:00
|
|
|
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address, incarnation, *key)
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.storageCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
if enc, ok := r.storageCache.HasGet(nil, compositeKey); ok {
|
|
|
|
return enc, nil
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
enc, err := r.db.Get(dbutils.PlainStateBucket, compositeKey)
|
2020-07-10 06:03:18 +00:00
|
|
|
if err != nil && !errors.Is(err, ethdb.ErrKeyNotFound) {
|
2020-05-15 07:52:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if r.storageCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
r.storageCache.Set(compositeKey, enc)
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
return enc, 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 r.codeCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
if code, ok := r.codeCache.HasGet(nil, address[:]); ok {
|
|
|
|
return code, nil
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
code, err := r.db.Get(dbutils.CodeBucket, codeHash[:])
|
2020-05-22 15:11:00 +00:00
|
|
|
if r.codeCache != nil && len(code) <= 1024 {
|
|
|
|
r.codeCache.Set(address[:], code)
|
|
|
|
}
|
|
|
|
if r.codeSizeCache != nil {
|
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(len(code)))
|
|
|
|
r.codeSizeCache.Set(address[:], b[:])
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
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 {
|
2020-05-22 15:11:00 +00:00
|
|
|
if b, ok := r.codeSizeCache.HasGet(nil, address[:]); ok {
|
|
|
|
return int(binary.BigEndian.Uint32(b)), nil
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
code, err := r.db.Get(dbutils.CodeBucket, codeHash[:])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if r.codeSizeCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(len(code)))
|
|
|
|
r.codeSizeCache.Set(address[:], b[:])
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
return len(code), nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountIncarnation(address common.Address) (uint64, error) {
|
2020-05-26 12:27:21 +00:00
|
|
|
if b, err := r.db.Get(dbutils.IncarnationMapBucket, address[:]); err == nil {
|
|
|
|
return binary.BigEndian.Uint64(b), nil
|
2020-07-10 06:03:18 +00:00
|
|
|
} else if errors.Is(err, ethdb.ErrKeyNotFound) {
|
2020-05-26 12:27:21 +00:00
|
|
|
return 0, nil
|
|
|
|
} else {
|
2020-05-15 07:52:45 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|