erigon-pulse/core/state/plain_state_reader.go
Alex Sharov 0be3044b7e
rename (#1978)
* rename

* rename "make grpc"

* rename "abi bindings templates"

* rename "abi bindings templates"
2021-05-20 19:25:53 +01:00

81 lines
2.2 KiB
Go

package state
import (
"bytes"
"encoding/binary"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/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.KVGetter
}
func NewPlainStateReader(db ethdb.KVGetter) *PlainStateReader {
return &PlainStateReader{
db: db,
}
}
func (r *PlainStateReader) ReadAccountData(address common.Address) (*accounts.Account, error) {
enc, err := r.db.GetOne(dbutils.PlainStateBucket, address.Bytes())
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 *PlainStateReader) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
enc, err := r.db.GetOne(dbutils.PlainStateBucket, compositeKey)
if err != nil {
return nil, err
}
if len(enc) == 0 {
return nil, nil
}
return enc, nil
}
func (r *PlainStateReader) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
if bytes.Equal(codeHash.Bytes(), emptyCodeHash) {
return nil, nil
}
code, err := r.db.GetOne(dbutils.CodeBucket, codeHash.Bytes())
if len(code) == 0 {
return nil, nil
}
return code, err
}
func (r *PlainStateReader) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
code, err := r.ReadAccountCode(address, incarnation, codeHash)
return len(code), err
}
func (r *PlainStateReader) ReadAccountIncarnation(address common.Address) (uint64, error) {
b, err := r.db.GetOne(dbutils.IncarnationMapBucket, address.Bytes())
if err != nil {
return 0, err
}
if len(b) == 0 {
return 0, nil
}
return binary.BigEndian.Uint64(b), nil
}