Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental: MPT block witness for Erigon 3 #12847

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion cmd/state/commands/opcode_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func runBlock(engine consensus.Engine, ibs *state.IntraBlockState, txnWriter sta
usedGas := new(uint64)
usedBlobGas := new(uint64)
var receipts types.Receipts
core.InitializeBlockExecution(engine, nil, header, chainConfig, ibs, logger, nil)
core.InitializeBlockExecution(engine, nil, header, chainConfig, ibs, nil, logger, nil)
rules := chainConfig.Rules(block.NumberU64(), block.Time())
for i, txn := range block.Transactions() {
ibs.SetTxContext(i)
Expand Down
22 changes: 14 additions & 8 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func ExecuteBlockEphemerally(
gp.AddGas(block.GasLimit()).AddBlobGas(chainConfig.GetMaxBlobGasPerBlock())

// TODO: send the new tracer once we switch to the tracing.Hook
if err := InitializeBlockExecution(engine, chainReader, block.Header(), chainConfig, ibs, logger, nil); err != nil {
if err := InitializeBlockExecution(engine, chainReader, block.Header(), chainConfig, ibs, stateWriter, logger, nil); err != nil {
return nil, err
}

var rejectedTxs []*RejectedTx
includedTxs := make(types.Transactions, 0, block.Transactions().Len())
receipts := make(types.Receipts, 0, block.Transactions().Len())
noop := state.NewNoopWriter()
// noop := state.NewNoopWriter()
for i, txn := range block.Transactions() {
ibs.SetTxContext(i)
writeTrace := false
Expand All @@ -122,7 +122,7 @@ func ExecuteBlockEphemerally(
vmConfig.Tracer = tracer
writeTrace = true
}
receipt, _, err := ApplyTransaction(chainConfig, blockHashFunc, engine, nil, gp, ibs, noop, header, txn, usedGas, usedBlobGas, *vmConfig)
receipt, _, err := ApplyTransaction(chainConfig, blockHashFunc, engine, nil, gp, ibs, stateWriter, header, txn, usedGas, usedBlobGas, *vmConfig)
if writeTrace {
if ftracer, ok := vmConfig.Tracer.(vm.FlushableTracer); ok {
ftracer.Flush(txn)
Expand Down Expand Up @@ -167,15 +167,19 @@ func ExecuteBlockEphemerally(
return nil, fmt.Errorf("bloom computed by execution: %x, in header: %x", bloom, header.Bloom)
}
}

var newBlock *types.Block
var err error
if !vmConfig.ReadOnly {
txs := block.Transactions()
if _, _, _, _, err := FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, block.Withdrawals(), chainReader, false, logger); err != nil {
newBlock, _, _, _, err = FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, block.Withdrawals(), chainReader, true, logger)
if err != nil {
return nil, err
}
}
blockLogs := ibs.Logs()
newRoot := newBlock.Root()
execRs := &EphemeralExecResult{
StateRoot: newRoot,
TxRoot: types.DeriveSha(includedTxs),
ReceiptRoot: receiptSha,
Bloom: bloom,
Expand Down Expand Up @@ -353,13 +357,15 @@ func FinalizeBlockExecution(
}

func InitializeBlockExecution(engine consensus.Engine, chain consensus.ChainHeaderReader, header *types.Header,
cc *chain.Config, ibs *state.IntraBlockState, logger log.Logger, tracer *tracing.Hooks,
cc *chain.Config, ibs *state.IntraBlockState, stateWriter state.StateWriter, logger log.Logger, tracer *tracing.Hooks,
) error {
engine.Initialize(cc, chain, header, ibs, func(contract libcommon.Address, data []byte, ibState *state.IntraBlockState, header *types.Header, constCall bool) ([]byte, error) {
return SysCallContract(contract, data, cc, ibState, header, engine, constCall)
}, logger, tracer)
noop := state.NewNoopWriter()
ibs.FinalizeTx(cc.Rules(header.Number.Uint64(), header.Time), noop)
if stateWriter == nil {
stateWriter = state.NewNoopWriter()
}
ibs.FinalizeTx(cc.Rules(header.Number.Uint64(), header.Time), stateWriter)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func GenerateChain(config *chain.Config, parent *types.Block, engine consensus.E
}
}
if b.engine != nil {
err := InitializeBlockExecution(b.engine, nil, b.header, config, ibs, logger, nil)
err := InitializeBlockExecution(b.engine, nil, b.header, config, ibs, nil, logger, nil)
if err != nil {
return nil, nil, fmt.Errorf("call to InitializeBlockExecution: %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ func (sdb *IntraBlockState) AddBalance(addr libcommon.Address, amount *uint256.I
bi.count++
return
}

stateObject := sdb.GetOrNewStateObject(addr)
stateObject.AddBalance(amount, reason)
}
Expand Down
Loading
Loading