Skip to content

Commit

Permalink
Mempool changes ethernow (#165)
Browse files Browse the repository at this point in the history
* Mempool blobs minimalistic approach
  • Loading branch information
lukanus authored Mar 11, 2024
1 parent eab795d commit b3e34da
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 55 deletions.
10 changes: 7 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import (
// ExecutionResult includes all output after executing given evm
// message no matter the execution itself is successful or not.
type ExecutionResult struct {
Fee *uint256.Int
UsedGas uint64 // Total used gas, not including the refunded gas
RefundedGas uint64 // Total gas refunded after execution
Err error // Any error encountered during the execution(listed in core/vm/errors.go)
ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode)
Hash common.Hash
}

// Unwrap returns the internal evm error which allows us for further
Expand Down Expand Up @@ -234,7 +236,7 @@ func (st *StateTransition) to() common.Address {

func (st *StateTransition) buyGas() error {
mgval := new(big.Int).SetUint64(st.msg.GasLimit)
mgval = mgval.Mul(mgval, st.msg.GasPrice)
mgval.Mul(mgval, st.msg.GasPrice)
balanceCheck := new(big.Int).Set(mgval)
if st.msg.GasFeeCap != nil {
balanceCheck.SetUint64(st.msg.GasLimit)
Expand Down Expand Up @@ -460,17 +462,19 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}
effectiveTipU256, _ := uint256.FromBig(effectiveTip)

fee := new(uint256.Int)
if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 {
// Skip fee payment when NoBaseFee is set and the fee fields
// are 0. This avoids a negative effectiveTip being applied to
// the coinbase when simulating calls.
} else {
fee := new(uint256.Int).SetUint64(st.gasUsed())
fee.SetUint64(st.gasUsed())
fee.Mul(fee, effectiveTipU256)
st.state.AddBalance(st.evm.Context.Coinbase, fee)
}

return &ExecutionResult{
Fee: fee,
UsedGas: st.gasUsed(),
RefundedGas: gasRefund,
Err: vmerr,
Expand All @@ -488,7 +492,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) uint64 {

// Return ETH for remaining gas, exchanged at the original rate.
remaining := uint256.NewInt(st.gasRemaining)
remaining = remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice))
remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice))
st.state.AddBalance(st.msg.From, remaining)

// Also return remaining gas to the block gas counter so it is
Expand Down
2 changes: 0 additions & 2 deletions eth/downloader/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ func (api *DownloaderAPI) Syncing(ctx context.Context) (*rpc.Subscription, error
notifier.Notify(rpcSub.ID, status)
case <-rpcSub.Err():
return
case <-notifier.Closed():
return
}
}
}()
Expand Down
6 changes: 0 additions & 6 deletions eth/filters/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,6 @@ func (api *FilterAPI) NewPendingTransactions(ctx context.Context, fullTx *bool)
}
case <-rpcSub.Err():
return
case <-notifier.Closed():
return
}
}
}()
Expand Down Expand Up @@ -242,8 +240,6 @@ func (api *FilterAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
notifier.Notify(rpcSub.ID, h)
case <-rpcSub.Err():
return
case <-notifier.Closed():
return
}
}
}()
Expand Down Expand Up @@ -279,8 +275,6 @@ func (api *FilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subsc
}
case <-rpcSub.Err(): // client send an unsubscribe request
return
case <-notifier.Closed(): // connection dropped
return
}
}
}()
Expand Down
6 changes: 0 additions & 6 deletions eth/filters/dropped_tx_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ func (api *FilterAPI) DroppedTransactions(ctx context.Context) (*rpc.Subscriptio
case <-rpcSub.Err():
droppedSub.Unsubscribe()
return
case <-notifier.Closed():
droppedSub.Unsubscribe()
return
}
}
}()
Expand Down Expand Up @@ -236,9 +233,6 @@ func (api *FilterAPI) RejectedTransactions(ctx context.Context) (*rpc.Subscripti
case <-rpcSub.Err():
rejectedSub.Unsubscribe()
return
case <-notifier.Closed():
rejectedSub.Unsubscribe()
return
}
}
}()
Expand Down
14 changes: 0 additions & 14 deletions eth/filters/peers_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,6 @@ func (api *FilterAPI) NewHeadsWithPeers(ctx context.Context) (*rpc.Subscription,
case <-rpcSub.Err():
headersSub.Unsubscribe()
return
case <-notifier.Closed():
headersSub.Unsubscribe()
return
}
}
}()
Expand Down Expand Up @@ -401,8 +398,6 @@ func (api *FilterAPI) NewFullBlocksWithPeers(ctx context.Context) (*rpc.Subscrip
return
case <-reorgSub.Err():
return
case <-notifier.Closed():
return
}
for _, hash := range hashes {
peerid, _ := blockPeerMap.Get(hash)
Expand Down Expand Up @@ -505,9 +500,6 @@ func (api *FilterAPI) NewPendingTransactionsWithPeers(ctx context.Context) (*rpc
case <-rpcSub.Err():
pendingTxSub.Unsubscribe()
return
case <-notifier.Closed():
pendingTxSub.Unsubscribe()
return
}
}
}()
Expand Down Expand Up @@ -545,9 +537,6 @@ func (api *FilterAPI) NewTransactionReceipts(ctx context.Context) (*rpc.Subscrip
case <-rpcSub.Err():
headersSub.Unsubscribe()
return
case <-notifier.Closed():
headersSub.Unsubscribe()
return
}
}
}()
Expand Down Expand Up @@ -575,9 +564,6 @@ func (api *FilterAPI) ReorgFeed(ctx context.Context) (*rpc.Subscription, error)
case <-rpcSub.Err():
sub.Unsubscribe()
return
case <-notifier.Closed():
sub.Unsubscribe()
return
}
}
}()
Expand Down
65 changes: 48 additions & 17 deletions eth/filters/trace_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ func (api *FilterAPI) NewPendingTransactionsWithTrace(ctx context.Context, trace
notifier.Notify(rpcSub.ID, tracedTxs)
case <-rpcSub.Err():
return
case <-notifier.Closed():
return
}
}
}()
Expand Down Expand Up @@ -179,9 +177,6 @@ func (api *FilterAPI) NewFullBlocksWithTrace(ctx context.Context, tracerOptsJSON
case err := <-reorgSub.Err():
log.Error("ReorgSub error", "error", err)
return
case <-notifier.Closed():
log.Error("Nofitier closed Error")
return
}

for _, hash := range hashes {
Expand All @@ -196,12 +191,12 @@ func (api *FilterAPI) NewFullBlocksWithTrace(ctx context.Context, tracerOptsJSON
log.Error("failed to marshal block", "err", err, "block", block.Number())
continue
}

trace, err := traceBlock(block, chainConfig, api.sys.chain, tracerOpts)
if err != nil {
log.Info("failed to trace block", "err", err, "block", block.Number())
continue
}
trace, _ := traceBlock(block, chainConfig, api.sys.chain, tracerOpts)
// if err != nil {
// log.Info("failed to trace block", "err", err, "hash", hash, "block", block.Number())
// continue
// }
marshalBlock["trace"] = trace
marshalReceipts := make(map[common.Hash]map[string]interface{})
receipts, err := api.sys.backend.GetReceipts(ctx, hash)
Expand Down Expand Up @@ -246,9 +241,8 @@ func traceTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockContex
tracer, err := blocknative.NewTracerWithOpts(tracerOpts)
if err != nil {
return nil, err
}
txContext := core.NewEVMTxContext(message)
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{Tracer: tracer})
}
vmenv := vm.NewEVM(vmctx, core.NewEVMTxContext(message), statedb, chainConfig, vm.Config{Tracer: tracer, NoBaseFee: true})
statedb.SetTxContext(txCtx.TxHash, txCtx.TxIndex)

if _, err = core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.GasLimit)); err != nil {
Expand All @@ -262,6 +256,30 @@ func traceTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockContex
return trace, err
}

// traceBlockTx traces a transaction with the given contexts.
func traceBlockTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockContext, chainConfig *params.ChainConfig, statedb *state.StateDB, tracerOpts blocknative.TracerOpts) (*core.ExecutionResult, *blocknative.Trace, error) {

tracerOpts.DisableBlockContext = false
tracer, err := blocknative.NewTracerWithOpts(tracerOpts)
if err != nil {
return nil, nil, err
}

vmenv := vm.NewEVM(vmctx, core.NewEVMTxContext(message), statedb, chainConfig, vm.Config{Tracer: tracer, NoBaseFee: false})
statedb.SetTxContext(txCtx.TxHash, txCtx.TxIndex)

result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.GasLimit))
if err != nil {
return result, nil, fmt.Errorf("tracing failed: %w", err)
}
trace, err := tracer.GetTrace()
if err != nil {
return nil, nil, err
}

return result, trace, err
}

// traceBlock traces all transactions in a block.
func traceBlock(block *types.Block, chainConfig *params.ChainConfig, chain *core.BlockChain, tracerOpts blocknative.TracerOpts) ([]*blocknative.Trace, error) {
parent := chain.GetBlockByHash(block.ParentHash())
Expand All @@ -281,6 +299,7 @@ func traceBlock(block *types.Block, chainConfig *params.ChainConfig, chain *core
blockCtx = core.NewEVMBlockContext(block.Header(), chain, nil)
signer = types.MakeSigner(chainConfig, block.Number(), block.Time())
results = make([]*blocknative.Trace, len(txs))
results2 = make([]*core.ExecutionResult, len(txs))
)

for i, tx := range txs {
Expand All @@ -295,10 +314,22 @@ func traceBlock(block *types.Block, chainConfig *params.ChainConfig, chain *core
TxIndex: i,
TxHash: tx.Hash(),
}
results[i], err = traceTx(msg, txCtx, blockCtx, chainConfig, statedb, tracerOpts)
results2[i], results[i], err = traceBlockTx(msg, txCtx, blockCtx, chainConfig, statedb, tracerOpts)
if results2[i] != nil {
results2[i].Hash = tx.Hash()
}
if err != nil {
log.Error("failed to trace block in transaction", "err", err, "tx", tx.Hash())
return nil, err
cconf, _ := json.Marshal(chainConfig)
topts, _ := json.Marshal(tracerOpts)
exec, _ := json.Marshal(results2)
log.Error("failed to trace block in tx config",
"err", err,
"blockHash", block.Hash(),
"tx", tx.Hash(),
"conf", string(cconf),
"tracerOpts", string(topts))
log.Error("failed to trace block in transaction 1a", "err", err, "blockHash", block.Hash(), "tx", tx.Hash(), "exec", string(exec))
return results, err
}
statedb.Finalise(is158)
}
Expand Down
4 changes: 2 additions & 2 deletions eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (api *API) TraceChain(ctx context.Context, start, end rpc.BlockNumber, conf
}
sub := notifier.CreateSubscription()

resCh := api.traceChain(from, to, config, notifier.Closed())
resCh := api.traceChain(from, to, config, sub.Err())
go func() {
for result := range resCh {
notifier.Notify(sub.ID, result)
Expand All @@ -240,7 +240,7 @@ func (api *API) TraceChain(ctx context.Context, start, end rpc.BlockNumber, conf
// the end block but excludes the start one. The return value will be one item per
// transaction, dependent on the requested tracer.
// The tracing procedure should be aborted in case the closed signal is received.
func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed <-chan interface{}) chan *blockTraceResult {
func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed <-chan error) chan *blockTraceResult {
reexec := defaultTraceReexec
if config != nil && config.Reexec != nil {
reexec = *config.Reexec
Expand Down
2 changes: 0 additions & 2 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ func (api *adminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription, error)
return
case <-rpcSub.Err():
return
case <-notifier.Closed():
return
}
}
}()
Expand Down
2 changes: 0 additions & 2 deletions p2p/simulations/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ func (t *TestAPI) Events(ctx context.Context) (*rpc.Subscription, error) {
return
case <-rpcSub.Err():
return
case <-notifier.Closed():
return
}
}
}()
Expand Down
1 change: 0 additions & 1 deletion rpc/testservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ func (s *notificationTestService) SomeSubscription(ctx context.Context, n, val i
}
}
select {
case <-notifier.Closed():
case <-subscription.Err():
}
if s.unsubscribed != nil {
Expand Down

0 comments on commit b3e34da

Please sign in to comment.