Skip to content

Commit

Permalink
rpcdaemon: Support Astrid on debug_* endpoints (#12365)
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamc1 authored Oct 18, 2024
1 parent f9f7aa5 commit eb5b145
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 149 deletions.
27 changes: 27 additions & 0 deletions polygon/bridge/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/polygon/polygoncommon"
"github.com/erigontech/erigon/rlp"
)

type Reader struct {
Expand Down Expand Up @@ -183,3 +184,29 @@ func messageFromData(to libcommon.Address, data []byte) *types.Message {

return &msg
}

// NewStateSyncEventMessages creates a corresponding message that can be passed to EVM for multiple state sync events
func NewStateSyncEventMessages(stateSyncEvents []rlp.RawValue, stateReceiverContract *libcommon.Address, gasLimit uint64) []*types.Message {
msgs := make([]*types.Message, len(stateSyncEvents))
for i, event := range stateSyncEvents {
msg := types.NewMessage(
state.SystemAddress, // from
stateReceiverContract,
0, // nonce
u256.Num0, // amount
gasLimit,
u256.Num0, // gasPrice
nil, // feeCap
nil, // tip
event,
nil, // accessList
false, // checkNonce
true, // isFree
nil, // maxFeePerBlobGas
)

msgs[i] = &msg
}

return msgs
}
52 changes: 9 additions & 43 deletions polygon/tracer/trace_bor_state_sync_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import (

"github.com/erigontech/erigon-lib/chain"
libcommon "github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/kv"

"github.com/erigontech/erigon/common/u256"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
Expand All @@ -36,31 +33,22 @@ import (
tracersConfig "github.com/erigontech/erigon/eth/tracers/config"
"github.com/erigontech/erigon/polygon/bor/borcfg"
bortypes "github.com/erigontech/erigon/polygon/bor/types"
"github.com/erigontech/erigon/rlp"
"github.com/erigontech/erigon/turbo/services"
"github.com/erigontech/erigon/turbo/transactions"
)

func TraceBorStateSyncTxnDebugAPI(
ctx context.Context,
dbTx kv.Tx,
chainConfig *chain.Config,
traceConfig *tracersConfig.TraceConfig,
ibs *state.IntraBlockState,
blockReader services.FullBlockReader,
blockHash libcommon.Hash,
blockNum uint64,
blockTime uint64,
blockCtx evmtypes.BlockContext,
stream *jsoniter.Stream,
callTimeout time.Duration,
msgs []*types.Message,
) error {
stateSyncEvents, err := blockReader.EventsByBlock(ctx, dbTx, blockHash, blockNum)
if err != nil {
stream.WriteNil()
return err
}

txCtx := initStateSyncTxContext(blockNum, blockHash)
tracer, streaming, cancel, err := transactions.AssembleTracer(ctx, traceConfig, txCtx.TxHash, stream, callTimeout)
if err != nil {
Expand All @@ -70,79 +58,57 @@ func TraceBorStateSyncTxnDebugAPI(

defer cancel()
stateReceiverContract := chainConfig.Bor.(*borcfg.BorConfig).StateReceiverContractAddress()
tracer = NewBorStateSyncTxnTracer(tracer, len(stateSyncEvents), stateReceiverContract)
tracer = NewBorStateSyncTxnTracer(tracer, len(msgs), stateReceiverContract)
rules := chainConfig.Rules(blockNum, blockTime)
stateWriter := state.NewNoopWriter()
execCb := func(evm *vm.EVM, refunds bool) (*evmtypes.ExecutionResult, error) {
return traceBorStateSyncTxn(ctx, ibs, stateWriter, stateReceiverContract, stateSyncEvents, evm, rules, txCtx, refunds)
return traceBorStateSyncTxn(ctx, ibs, stateWriter, msgs, evm, rules, txCtx, refunds)
}

return transactions.ExecuteTraceTx(blockCtx, txCtx, ibs, traceConfig, chainConfig, stream, tracer, streaming, execCb)
}

func TraceBorStateSyncTxnTraceAPI(
ctx context.Context,
dbTx kv.Tx,
vmConfig *vm.Config,
chainConfig *chain.Config,
blockReader services.FullBlockReader,
ibs *state.IntraBlockState,
stateWriter state.StateWriter,
blockCtx evmtypes.BlockContext,
blockHash libcommon.Hash,
blockNum uint64,
blockTime uint64,
msgs []*types.Message,
) (*evmtypes.ExecutionResult, error) {
stateSyncEvents, err := blockReader.EventsByBlock(ctx, dbTx, blockHash, blockNum)
if err != nil {
return nil, err
}

stateReceiverContract := chainConfig.Bor.(*borcfg.BorConfig).StateReceiverContractAddress()
if vmConfig.Tracer != nil {
vmConfig.Tracer = NewBorStateSyncTxnTracer(vmConfig.Tracer, len(stateSyncEvents), stateReceiverContract)
vmConfig.Tracer = NewBorStateSyncTxnTracer(vmConfig.Tracer, len(msgs), stateReceiverContract)
}

txCtx := initStateSyncTxContext(blockNum, blockHash)
rules := chainConfig.Rules(blockNum, blockTime)
evm := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, *vmConfig)
return traceBorStateSyncTxn(ctx, ibs, stateWriter, stateReceiverContract, stateSyncEvents, evm, rules, txCtx, true)

return traceBorStateSyncTxn(ctx, ibs, stateWriter, msgs, evm, rules, txCtx, true)
}

func traceBorStateSyncTxn(
ctx context.Context,
ibs *state.IntraBlockState,
stateWriter state.StateWriter,
stateReceiverContract libcommon.Address,
stateSyncEvents []rlp.RawValue,
msgs []*types.Message,
evm *vm.EVM,
rules *chain.Rules,
txCtx evmtypes.TxContext,
refunds bool,
) (*evmtypes.ExecutionResult, error) {
for _, eventData := range stateSyncEvents {
for _, msg := range msgs {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}

msg := types.NewMessage(
state.SystemAddress, // from
&stateReceiverContract,
0, // nonce
u256.Num0, // amount
core.SysCallGasLimit,
u256.Num0, // gasPrice
nil, // feeCap
nil, // tip
eventData,
nil, // accessList
false, // checkNonce
true, // isFree
nil, // maxFeePerBlobGas
)

gp := new(core.GasPool).AddGas(msg.Gas()).AddBlobGas(msg.BlobGas())
_, err := core.ApplyMessage(evm, msg, gp, refunds, false /* gasBailout */)
if err != nil {
Expand Down
36 changes: 3 additions & 33 deletions turbo/jsonrpc/erigon_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ import (
"github.com/erigontech/erigon-lib/kv/rawdbv3"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/cmd/state/exec3"
"github.com/erigontech/erigon/common/u256"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/eth/ethutils"
"github.com/erigontech/erigon/eth/filters"
Expand Down Expand Up @@ -379,36 +376,9 @@ func (api *ErigonImpl) GetBlockReceiptsByBlockHash(ctx context.Context, cannonic
}

if chainConfig.Bor != nil {
var events []*types.Message
if api.bridgeReader != nil {
events, err = api.bridgeReader.Events(ctx, blockNum)
if err != nil {
return nil, err
}
} else {
rawEvents, err := api._blockReader.EventsByBlock(ctx, tx, block.Hash(), blockNum)
if err != nil {
return nil, err
}

msgs := make([]*types.Message, len(rawEvents))
to := chainConfig.Bor.StateReceiverContractAddress()
for i, event := range rawEvents {
msg := types.NewMessage(
state.SystemAddress,
&to,
0, u256.Num0,
core.SysCallGasLimit,
u256.Num0,
nil, nil,
event, nil, false,
true, // isFree
nil, // maxFeePerBlobGas
)
msgs[i] = &msg
}

events = msgs
events, err := api.stateSyncEvents(ctx, tx, block.Hash(), blockNum, chainConfig)
if err != nil {
return nil, err
}

if len(events) != 0 {
Expand Down
24 changes: 24 additions & 0 deletions turbo/jsonrpc/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ import (
"github.com/erigontech/erigon/common/math"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/misc"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/core/types/accounts"
ethFilters "github.com/erigontech/erigon/eth/filters"
"github.com/erigontech/erigon/ethdb/prune"
"github.com/erigontech/erigon/polygon/bor/borcfg"
"github.com/erigontech/erigon/polygon/bridge"
"github.com/erigontech/erigon/rpc"
ethapi2 "github.com/erigontech/erigon/turbo/adapter/ethapi"
"github.com/erigontech/erigon/turbo/jsonrpc/receipts"
Expand Down Expand Up @@ -296,6 +299,27 @@ func (api *BaseAPI) headerByRPCNumber(ctx context.Context, number rpc.BlockNumbe
return api._blockReader.Header(ctx, tx, h, n)
}

func (api *BaseAPI) stateSyncEvents(ctx context.Context, tx kv.Tx, blockHash common.Hash, blockNum uint64, chainConfig *chain.Config) ([]*types.Message, error) {
var stateSyncEvents []*types.Message
if api.bridgeReader != nil {
events, err := api.bridgeReader.Events(ctx, blockNum)
if err != nil {
return nil, err
}
stateSyncEvents = events
} else {
events, err := api._blockReader.EventsByBlock(ctx, tx, blockHash, blockNum)
if err != nil {
return nil, err
}

stateReceiverContract := chainConfig.Bor.(*borcfg.BorConfig).StateReceiverContractAddress()
stateSyncEvents = bridge.NewStateSyncEventMessages(events, &stateReceiverContract, core.SysCallGasLimit)
}

return stateSyncEvents, nil
}

// checks the pruning state to see if we would hold information about this
// block in state history or not. Some strange issues arise getting account
// history for blocks that have been pruned away giving nonce too low errors
Expand Down
71 changes: 8 additions & 63 deletions turbo/jsonrpc/eth_receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"context"
"errors"
"fmt"

"github.com/RoaringBitmap/roaring"

"github.com/erigontech/erigon-lib/chain"
"github.com/erigontech/erigon-lib/common"
"github.com/erigontech/erigon-lib/kv"
Expand All @@ -30,10 +32,7 @@ import (
"github.com/erigontech/erigon-lib/kv/stream"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon/cmd/state/exec3"
"github.com/erigontech/erigon/common/u256"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb/rawtemporaldb"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/eth/ethutils"
"github.com/erigontech/erigon/eth/filters"
Expand Down Expand Up @@ -465,36 +464,9 @@ func (api *APIImpl) GetTransactionReceipt(ctx context.Context, txnHash common.Ha
return nil, fmt.Errorf("getReceipts error: %w", err)
}

var events []*types.Message
if api.bridgeReader != nil {
events, err = api.bridgeReader.Events(ctx, blockNum)
if err != nil {
return nil, err
}
} else {
rawEvents, err := api._blockReader.EventsByBlock(ctx, tx, block.Hash(), blockNum)
if err != nil {
return nil, err
}

msgs := make([]*types.Message, len(rawEvents))
to := chainConfig.Bor.StateReceiverContractAddress()
for i, event := range rawEvents {
msg := types.NewMessage(
state.SystemAddress,
&to,
0, u256.Num0,
core.SysCallGasLimit,
u256.Num0,
nil, nil,
event, nil, false,
true, // isFree
nil, // maxFeePerBlobGas
)
msgs[i] = &msg
}

events = msgs
events, err := api.stateSyncEvents(ctx, tx, block.Hash(), blockNum, chainConfig)
if err != nil {
return nil, err
}

if len(events) == 0 {
Expand Down Expand Up @@ -552,36 +524,9 @@ func (api *APIImpl) GetBlockReceipts(ctx context.Context, numberOrHash rpc.Block
}

if chainConfig.Bor != nil {
var events []*types.Message
if api.bridgeReader != nil {
events, err = api.bridgeReader.Events(ctx, blockNum)
if err != nil {
return nil, err
}
} else {
rawEvents, err := api._blockReader.EventsByBlock(ctx, tx, block.Hash(), blockNum)
if err != nil {
return nil, err
}

msgs := make([]*types.Message, len(rawEvents))
to := chainConfig.Bor.StateReceiverContractAddress()
for i, event := range rawEvents {
msg := types.NewMessage(
state.SystemAddress,
&to,
0, u256.Num0,
core.SysCallGasLimit,
u256.Num0,
nil, nil,
event, nil, false,
true, // isFree
nil, // maxFeePerBlobGas
)
msgs[i] = &msg
}

events = msgs
events, err := api.stateSyncEvents(ctx, tx, block.Hash(), blockNum, chainConfig)
if err != nil {
return nil, err
}

if len(events) != 0 {
Expand Down
Loading

0 comments on commit eb5b145

Please sign in to comment.