erigon22: HistoryReader22 #5203

This commit is contained in:
Alex Sharov 2022-08-27 15:36:42 +07:00 committed by GitHub
parent a0af80014c
commit 16fc9c7277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 162 additions and 3 deletions

View File

@ -0,0 +1,159 @@
package state
import (
"fmt"
"github.com/ledgerwatch/erigon-lib/kv"
libstate "github.com/ledgerwatch/erigon-lib/state"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types/accounts"
)
// HistoryReader22 Implements StateReader and StateWriter
type HistoryReader22 struct {
ac *libstate.Aggregator22Context
ri *libstate.ReadIndices
txNum uint64
trace bool
tx kv.Tx
}
func NewHistoryReader22(ac *libstate.Aggregator22Context, ri *libstate.ReadIndices) *HistoryReader22 {
return &HistoryReader22{ac: ac, ri: ri}
}
func (hr *HistoryReader22) SetTx(tx kv.Tx) { hr.tx = tx }
func (hr *HistoryReader22) SetRwTx(tx kv.RwTx) {
hr.ri.SetTx(tx)
}
func (hr *HistoryReader22) SetTxNum(txNum uint64) {
hr.txNum = txNum
if hr.ri != nil {
hr.ri.SetTxNum(txNum)
}
}
func (hr *HistoryReader22) FinishTx() error {
return hr.ri.FinishTx()
}
func (hr *HistoryReader22) SetTrace(trace bool) {
hr.trace = trace
}
func (hr *HistoryReader22) ReadAccountData(address common.Address) (*accounts.Account, error) {
if hr.ri != nil {
if err := hr.ri.ReadAccountData(address.Bytes()); err != nil {
return nil, err
}
}
enc, ok, err := hr.ac.ReadAccountDataNoState(address.Bytes(), hr.txNum)
if err != nil {
return nil, err
}
if !ok {
enc, err = hr.tx.GetOne(kv.PlainState, address.Bytes())
if err != nil {
return nil, err
}
}
if len(enc) == 0 {
if hr.trace {
fmt.Printf("ReadAccountData [%x] => []\n", address)
}
return nil, nil
}
var a accounts.Account
if err := accounts.Deserialise2(&a, enc); err != nil {
return nil, fmt.Errorf("ReadAccountData(%x): %w", address, err)
}
if hr.trace {
fmt.Printf("ReadAccountData [%x] => [nonce: %d, balance: %d, codeHash: %x]\n", address, a.Nonce, &a.Balance, a.CodeHash)
}
return &a, nil
}
func (hr *HistoryReader22) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
if hr.ri != nil {
if err := hr.ri.ReadAccountStorage(address.Bytes(), key.Bytes()); err != nil {
return nil, err
}
}
enc, ok, err := hr.ac.ReadAccountStorageNoState(address.Bytes(), key.Bytes(), hr.txNum)
if err != nil {
return nil, err
}
if !ok {
k := append(address.Bytes(), key.Bytes()...)
enc, err = hr.tx.GetOne(kv.PlainState, k)
if err != nil {
return nil, err
}
}
if hr.trace {
if enc == nil {
fmt.Printf("ReadAccountStorage [%x] [%x] => []\n", address, key.Bytes())
} else {
fmt.Printf("ReadAccountStorage [%x] [%x] => [%x]\n", address, key.Bytes(), enc)
}
}
if enc == nil {
return nil, nil
}
return enc, nil
}
func (hr *HistoryReader22) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
if hr.ri != nil {
if err := hr.ri.ReadAccountCode(address.Bytes()); err != nil {
return nil, err
}
}
enc, ok, err := hr.ac.ReadAccountCodeNoState(address.Bytes(), hr.txNum)
if err != nil {
return nil, err
}
if !ok {
enc, err = hr.tx.GetOne(kv.Code, address.Bytes())
if err != nil {
return nil, err
}
}
if hr.trace {
fmt.Printf("ReadAccountCode [%x] => [%x]\n", address, enc)
}
return enc, nil
}
func (hr *HistoryReader22) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
if hr.ri != nil {
if err := hr.ri.ReadAccountCodeSize(address.Bytes()); err != nil {
return 0, err
}
}
size, ok, err := hr.ac.ReadAccountCodeSizeNoState(address.Bytes(), hr.txNum)
if err != nil {
return 0, err
}
if !ok {
enc, err := hr.tx.GetOne(kv.Code, address.Bytes())
if err != nil {
return 0, err
}
size = len(enc)
}
if err != nil {
return 0, err
}
if hr.trace {
fmt.Printf("ReadAccountCodeSize [%x] => [%d]\n", address, size)
}
return size, nil
}
func (hr *HistoryReader22) ReadAccountIncarnation(address common.Address) (uint64, error) {
return 0, nil
}

View File

@ -19,7 +19,7 @@ func bytesToUint64(buf []byte) (x uint64) {
return return
} }
// Implements StateReader and StateWriter // HistoryReader23 Implements StateReader and StateWriter
type HistoryReader23 struct { type HistoryReader23 struct {
ac *libstate.AggregatorContext ac *libstate.AggregatorContext
ri *libstate.ReadIndices ri *libstate.ReadIndices

View File

@ -648,9 +648,9 @@ func (ms *MockSentry) HeaderDownload() *headerdownload.HeaderDownload {
func (ms *MockSentry) NewHistoricalStateReader(blockNum uint64, tx kv.Tx) *state.IntraBlockState { func (ms *MockSentry) NewHistoricalStateReader(blockNum uint64, tx kv.Tx) *state.IntraBlockState {
if ms.HistoryV2 { if ms.HistoryV2 {
agg, _ := libstate.NewAggregator(path.Join(ms.Dirs.DataDir, "agg22"), stagedsync.AggregationStep) agg, _ := libstate.NewAggregator22(path.Join(ms.Dirs.DataDir, "agg22"), stagedsync.AggregationStep)
defer agg.Close() defer agg.Close()
r := state.NewHistoryReader23(agg.MakeContext(), nil) r := state.NewHistoryReader22(agg.MakeContext(), nil)
r.SetTx(tx) r.SetTx(tx)
r.SetTxNum(ms.txNums.MinOf(blockNum)) r.SetTxNum(ms.txNums.MinOf(blockNum))
return state.New(r) return state.New(r)