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-05-21 12:27:52 +00:00
|
|
|
|
2021-07-29 11:53:13 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
2020-05-15 07:52:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2021-07-28 02:47:38 +00:00
|
|
|
db kv.Getter
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func NewPlainStateReader(db kv.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
2021-07-28 03:43:51 +00:00
|
|
|
enc, err := r.db.GetOne(kv.PlainState, address.Bytes())
|
2021-04-05 13:04:58 +00:00
|
|
|
if err != nil {
|
2020-12-08 09:44:29 +00:00
|
|
|
return nil, err
|
2020-05-22 15:11:00 +00:00
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
if len(enc) == 0 {
|
2020-05-22 15:11:00 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
var a accounts.Account
|
|
|
|
if err = a.DecodeForStorage(enc); err != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
return nil, err
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
return &a, nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
|
2020-12-08 09:44:29 +00:00
|
|
|
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
|
2021-07-28 03:43:51 +00:00
|
|
|
enc, err := r.db.GetOne(kv.PlainState, compositeKey)
|
2021-04-05 13:04:58 +00:00
|
|
|
if err != nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil, nil
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 09:44:29 +00:00
|
|
|
func (r *PlainStateReader) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
|
|
|
|
if bytes.Equal(codeHash.Bytes(), emptyCodeHash) {
|
|
|
|
return nil, nil
|
2020-05-22 15:11:00 +00:00
|
|
|
}
|
2021-07-28 03:43:51 +00:00
|
|
|
code, err := r.db.GetOne(kv.Code, codeHash.Bytes())
|
2020-12-08 09:44:29 +00:00
|
|
|
if len(code) == 0 {
|
|
|
|
return nil, nil
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
return code, err
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 09:44:29 +00:00
|
|
|
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
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountIncarnation(address common.Address) (uint64, error) {
|
2021-07-28 02:47:38 +00:00
|
|
|
b, err := r.db.GetOne(kv.IncarnationMap, address.Bytes())
|
2021-04-05 13:04:58 +00:00
|
|
|
if err != nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2021-04-05 13:04:58 +00:00
|
|
|
if len(b) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint64(b), nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|