mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-23 04:03:49 +00:00
80 lines
2.2 KiB
Go
80 lines
2.2 KiB
Go
package state
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
|
"github.com/ledgerwatch/erigon-lib/kv/kvcache"
|
|
"github.com/ledgerwatch/erigon/common"
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
|
)
|
|
|
|
// CachedReader2 is a wrapper for an instance of type StateReader
|
|
// This wrapper only makes calls to the underlying reader if the item is not in the cache
|
|
type CachedReader2 struct {
|
|
cache kvcache.CacheView
|
|
db kv.Tx
|
|
}
|
|
|
|
// NewCachedReader2 wraps a given state reader into the cached reader
|
|
func NewCachedReader2(cache kvcache.CacheView, tx kv.Tx) *CachedReader2 {
|
|
return &CachedReader2{cache: cache, db: tx}
|
|
}
|
|
|
|
// ReadAccountData is called when an account needs to be fetched from the state
|
|
func (r *CachedReader2) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
|
enc, err := r.cache.Get(address[:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(enc) == 0 {
|
|
return nil, nil
|
|
}
|
|
var a accounts.Account
|
|
if err = a.DecodeForStorage(enc); err != nil {
|
|
return nil, err
|
|
}
|
|
return &a, nil
|
|
}
|
|
|
|
func (r *CachedReader2) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
|
|
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
|
|
enc, err := r.cache.Get(compositeKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(enc) == 0 {
|
|
return nil, nil
|
|
}
|
|
return enc, nil
|
|
}
|
|
|
|
func (r *CachedReader2) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
|
|
if bytes.Equal(codeHash.Bytes(), emptyCodeHash) {
|
|
return nil, nil
|
|
}
|
|
code, err := r.cache.GetCode(codeHash.Bytes())
|
|
if len(code) == 0 {
|
|
return nil, nil
|
|
}
|
|
return code, err
|
|
}
|
|
|
|
func (r *CachedReader2) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
|
|
code, err := r.ReadAccountCode(address, incarnation, codeHash)
|
|
return len(code), err
|
|
}
|
|
|
|
func (r *CachedReader2) ReadAccountIncarnation(address common.Address) (uint64, error) {
|
|
b, err := r.db.GetOne(kv.IncarnationMap, address.Bytes())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if len(b) == 0 {
|
|
return 0, nil
|
|
}
|
|
return binary.BigEndian.Uint64(b), nil
|
|
}
|