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-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 {
|
2021-03-30 09:53:54 +00:00
|
|
|
db ethdb.Getter
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 09:53:54 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *PlainStateReader) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
2020-12-08 09:44:29 +00:00
|
|
|
enc, err := r.db.Get(dbutils.PlainStateBucket, address.Bytes())
|
|
|
|
if err != nil && !errors.Is(err, ethdb.ErrKeyNotFound) {
|
|
|
|
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())
|
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-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
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
code, err := r.db.Get(dbutils.CodeBucket, codeHash.Bytes())
|
|
|
|
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) {
|
2020-12-08 09:44:29 +00:00
|
|
|
if b, err := r.db.Get(dbutils.IncarnationMapBucket, address.Bytes()); err == nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|