2020-08-19 11:46:20 +00:00
|
|
|
package adapter
|
2020-06-28 06:10:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-08-24 17:00:40 +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/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/erigon/crypto"
|
2020-06-28 06:10:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StateReader struct {
|
2021-08-24 07:28:58 +00:00
|
|
|
accHistoryC, storageHistoryC kv.Cursor
|
|
|
|
accChangesC, storageChangesC kv.CursorDupSort
|
|
|
|
blockNr uint64
|
|
|
|
tx kv.Tx
|
2020-06-28 06:10:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 02:47:38 +00:00
|
|
|
func NewStateReader(tx kv.Tx, blockNr uint64) *StateReader {
|
2021-08-24 07:28:58 +00:00
|
|
|
c1, _ := tx.Cursor(kv.AccountsHistory)
|
|
|
|
c2, _ := tx.Cursor(kv.StorageHistory)
|
|
|
|
c3, _ := tx.CursorDupSort(kv.AccountChangeSet)
|
|
|
|
c4, _ := tx.CursorDupSort(kv.StorageChangeSet)
|
2020-06-28 06:10:27 +00:00
|
|
|
return &StateReader{
|
2021-08-24 07:28:58 +00:00
|
|
|
tx: tx,
|
|
|
|
blockNr: blockNr,
|
|
|
|
accHistoryC: c1, storageHistoryC: c2, accChangesC: c3, storageChangesC: c4,
|
2020-06-28 06:10:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StateReader) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
2021-08-24 07:28:58 +00:00
|
|
|
enc, err := state.GetAsOf(r.tx, r.accHistoryC, r.accChangesC, false /* storage */, address[:], r.blockNr+1)
|
2020-06-28 06:10:27 +00:00
|
|
|
if err != nil || enc == nil || len(enc) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var acc accounts.Account
|
|
|
|
if err := acc.DecodeForStorage(enc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &acc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StateReader) 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-08-24 07:28:58 +00:00
|
|
|
return state.GetAsOf(r.tx, r.storageHistoryC, r.storageChangesC, true /* storage */, compositeKey, r.blockNr+1)
|
2020-06-28 06:10:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 09:44:29 +00:00
|
|
|
func (r *StateReader) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
|
2020-06-28 06:10:27 +00:00
|
|
|
if bytes.Equal(codeHash[:], crypto.Keccak256(nil)) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var val []byte
|
2021-07-28 03:43:51 +00:00
|
|
|
v, err := r.tx.GetOne(kv.Code, codeHash[:])
|
2020-06-28 06:10:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-12 08:39:04 +00:00
|
|
|
val = common.CopyBytes(v)
|
2020-06-28 06:10:27 +00:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:44:29 +00:00
|
|
|
func (r *StateReader) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
|
|
|
|
code, err := r.ReadAccountCode(address, incarnation, codeHash)
|
2020-06-28 06:10:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(code), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StateReader) ReadAccountIncarnation(address common.Address) (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|