e3: experiment with Ibs.Reset (#6159)

This commit is contained in:
Alex Sharov 2022-12-04 11:45:06 +07:00 committed by GitHub
parent 82c7779a85
commit c401a2e9d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 21 deletions

View File

@ -543,7 +543,7 @@ func loopExec(db kv.RwDB, ctx context.Context, unwind uint64) error {
if err = sync.Run(db, tx, initialCycle, false /* quiet */); err != nil {
return err
}
fmt.Printf("loop time: %s\n", time.Since(t))
log.Info("[Integration] ", "loop time", time.Since(t))
tx.Rollback()
tx, err = db.BeginRw(ctx)
if err != nil {

View File

@ -47,6 +47,8 @@ type Worker struct {
starkNetEvm *vm.CVMAdapter
evm *vm.EVM
ibs *state.IntraBlockState
}
func NewWorker(lock sync.Locker, background bool, chainDb kv.RoDB, wg *sync.WaitGroup, rs *state.State22, blockReader services.FullBlockReader, chainConfig *params.ChainConfig, logger log.Logger, genesis *core.Genesis, resultCh chan *exec22.TxTask, engine consensus.Engine) *Worker {
@ -71,13 +73,9 @@ func NewWorker(lock sync.Locker, background bool, chainDb kv.RoDB, wg *sync.Wait
starkNetEvm: &vm.CVMAdapter{Cvm: vm.NewCVM(nil)},
evm: vm.NewEVM(evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, chainConfig, vm.Config{}),
}
w.getHeader = func(hash common.Hash, number uint64) *types.Header {
h, err := blockReader.Header(ctx, w.chainTx, hash, number)
if err != nil {
panic(err)
}
return h
}
w.ibs = state.New(w.stateReader)
w.posa, w.isPoSA = engine.(consensus.PoSA)
return w
@ -122,7 +120,9 @@ func (rw *Worker) RunTxTask(txTask *exec22.TxTask) {
rw.stateWriter.SetTxNum(txTask.TxNum)
rw.stateReader.ResetReadSet()
rw.stateWriter.ResetWriteSet()
ibs := state.New(rw.stateReader)
rw.ibs.Reset()
ibs := rw.ibs
rules := txTask.Rules
daoForkTx := rw.chainConfig.DAOForkSupport && rw.chainConfig.DAOForkBlock != nil && rw.chainConfig.DAOForkBlock.Uint64() == txTask.BlockNum && txTask.TxIndex == -1
var err error
@ -186,9 +186,6 @@ func (rw *Worker) RunTxTask(txTask *exec22.TxTask) {
ibs.Prepare(txHash, txTask.BlockHash, txTask.TxIndex)
msg := txTask.TxAsMessage
//blockContext := core.NewEVMBlockContext(header, getHashFn, rw.engine, nil /* author */)
//txContext := core.NewEVMTxContext(msg)
//vmenv := vm.NewEVM(blockContext, txContext, ibs, rw.chainConfig, vmConfig)
var vmenv vm.VMInterface
if txTask.Tx.IsStarkNet() {
rw.starkNetEvm.Reset(evmtypes.TxContext{}, ibs)

View File

@ -20,6 +20,7 @@ import (
"github.com/ledgerwatch/erigon/core/systemcontracts"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"
@ -238,6 +239,10 @@ type ReconWorker struct {
chain ChainReader
isPoSA bool
posa consensus.PoSA
starkNetEvm *vm.CVMAdapter
evm *vm.EVM
ibs *state2.IntraBlockState
}
func NewReconWorker(lock sync.Locker, wg *sync.WaitGroup, rs *state2.ReconState,
@ -258,9 +263,12 @@ func NewReconWorker(lock sync.Locker, wg *sync.WaitGroup, rs *state2.ReconState,
logger: logger,
genesis: genesis,
engine: engine,
starkNetEvm: &vm.CVMAdapter{Cvm: vm.NewCVM(nil)},
evm: vm.NewEVM(evmtypes.BlockContext{}, evmtypes.TxContext{}, nil, chainConfig, vm.Config{}),
}
rw.epoch = NewEpochReader(chainTx)
rw.chain = NewChainReader(chainConfig, chainTx, blockReader)
rw.ibs = state2.New(rw.stateReader)
rw.posa, rw.isPoSA = engine.(consensus.PoSA)
return rw
}
@ -285,8 +293,9 @@ func (rw *ReconWorker) runTxTask(txTask *exec22.TxTask) {
rw.stateReader.SetTxNum(txTask.TxNum)
rw.stateReader.ResetError()
rw.stateWriter.SetTxNum(txTask.TxNum)
rw.ibs.Reset()
ibs := rw.ibs
rules := txTask.Rules
ibs := state2.New(rw.stateReader)
daoForkTx := rw.chainConfig.DAOForkSupport && rw.chainConfig.DAOForkBlock != nil && rw.chainConfig.DAOForkBlock.Uint64() == txTask.BlockNum && txTask.TxIndex == -1
var err error
if txTask.BlockNum == 0 && txTask.TxIndex == -1 {
@ -338,9 +347,11 @@ func (rw *ReconWorker) runTxTask(txTask *exec22.TxTask) {
var vmenv vm.VMInterface
if txTask.Tx.IsStarkNet() {
vmenv = &vm.CVMAdapter{Cvm: vm.NewCVM(ibs)}
rw.starkNetEvm.Reset(evmtypes.TxContext{}, ibs)
vmenv = rw.starkNetEvm
} else {
vmenv = vm.NewEVM(txTask.EvmBlockContext, core.NewEVMTxContext(msg), ibs, rw.chainConfig, vmConfig)
rw.evm.ResetBetweenBlocks(txTask.EvmBlockContext, core.NewEVMTxContext(msg), ibs, vmConfig, txTask.Rules)
vmenv = rw.evm
}
//fmt.Printf("txNum=%d, blockNum=%d, txIndex=%d, evm=%p\n", txTask.TxNum, txTask.BlockNum, txTask.TxIndex, vmenv)
_, err = core.ApplyMessage(vmenv, msg, gp, true /* refunds */, false /* gasBailout */)

View File

@ -30,6 +30,7 @@ import (
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/trie"
"github.com/ledgerwatch/log/v3"
"golang.org/x/exp/maps"
)
type revision struct {
@ -127,16 +128,29 @@ func (sdb *IntraBlockState) Error() error {
// Reset clears out all ephemeral state objects from the state db, but keeps
// the underlying state trie to avoid reloading data for the next operations.
func (sdb *IntraBlockState) Reset() {
sdb.stateObjects = make(map[common.Address]*stateObject)
sdb.stateObjectsDirty = make(map[common.Address]struct{})
maps.Clear(sdb.nilAccounts)
maps.Clear(sdb.stateObjects)
maps.Clear(sdb.stateObjectsDirty)
sdb.thash = common.Hash{}
sdb.bhash = common.Hash{}
sdb.txIndex = 0
sdb.logs = make(map[common.Hash][]*types.Log)
maps.Clear(sdb.logs)
sdb.logSize = 0
sdb.clearJournalAndRefund()
sdb.accessList = newAccessList()
sdb.balanceInc = make(map[common.Address]*BalanceIncrease)
//sdb.clearJournalAndRefund()
//sdb.accessList = newAccessList() // this reset by .Prepare() method
maps.Clear(sdb.balanceInc)
//sdb.nilAccounts = make(map[common.Address]struct{})
//sdb.stateObjects = make(map[common.Address]*stateObject)
//sdb.stateObjectsDirty = make(map[common.Address]struct{})
//sdb.thash = common.Hash{}
//sdb.bhash = common.Hash{}
//sdb.txIndex = 0
//sdb.logs = make(map[common.Hash][]*types.Log)
//sdb.logSize = 0
//sdb.clearJournalAndRefund()
//sdb.accessList = newAccessList()
//sdb.balanceInc = make(map[common.Address]*BalanceIncrease)
}
func (sdb *IntraBlockState) AddLog(log2 *types.Log) {