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

preload sender and check for errors #1480

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zk/stages/stage_sequence_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func sequencingBatchStep(
log.Info(fmt.Sprintf("[%s] Info tree updates", logPrefix), "count", len(newLogs), "latestIndex", latestIndex)
default:
if batchState.isLimboRecovery() {
batchState.blockState.transactionsForInclusion, err = getLimboTransaction(ctx, cfg, batchState.limboRecoveryData.limboTxHash)
batchState.blockState.transactionsForInclusion, err = getLimboTransaction(ctx, cfg, batchState.limboRecoveryData.limboTxHash, executionAt)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions zk/stages/stage_sequence_execute_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ func addSenders(
cryptoContext := secp256k1.ContextForThread(1)
senders := make([]common.Address, 0, len(finalTransactions))
for _, transaction := range finalTransactions {
from, ok := transaction.GetSender()
if ok {
senders = append(senders, from)
continue
}

// shouldn't be hit as we preload this value before processing the transaction
// to look for errors in handling it.
from, err := signer.SenderWithContext(cryptoContext, transaction)
if err != nil {
return err
Expand Down
28 changes: 21 additions & 7 deletions zk/stages/stage_sequence_execute_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/zk/utils"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/secp256k1"
)

func getNextPoolTransactions(ctx context.Context, cfg SequenceBlockCfg, executionAt, forkId uint64, alreadyYielded mapset.Set[[32]byte]) ([]types.Transaction, []common.Hash, bool, error) {
Expand All @@ -38,7 +39,7 @@ func getNextPoolTransactions(ctx context.Context, cfg SequenceBlockCfg, executio
if allConditionsOk, _, err = cfg.txPool.YieldBest(cfg.yieldSize, &slots, poolTx, executionAt, gasLimit, 0, alreadyYielded); err != nil {
return err
}
yieldedTxs, yieldedIds, toRemove, err := extractTransactionsFromSlot(&slots)
yieldedTxs, yieldedIds, toRemove, err := extractTransactionsFromSlot(&slots, executionAt, cfg)
if err != nil {
return err
}
Expand All @@ -55,7 +56,7 @@ func getNextPoolTransactions(ctx context.Context, cfg SequenceBlockCfg, executio
return transactions, ids, allConditionsOk, err
}

func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *common.Hash) ([]types.Transaction, error) {
func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *common.Hash, executionAt uint64) ([]types.Transaction, error) {
cfg.txPool.LockFlusher()
defer cfg.txPool.UnlockFlusher()

Expand All @@ -70,7 +71,7 @@ func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *comm
if slots != nil {
// ignore the toRemove value here, we know the RLP will be sound as we had to read it from the pool
// in the first place to get it into limbo
transactions, _, _, err = extractTransactionsFromSlot(slots)
transactions, _, _, err = extractTransactionsFromSlot(slots, executionAt, cfg)
if err != nil {
return err
}
Expand All @@ -84,10 +85,12 @@ func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *comm
return transactions, nil
}

func extractTransactionsFromSlot(slot *types2.TxsRlp) ([]types.Transaction, []common.Hash, []common.Hash, error) {
func extractTransactionsFromSlot(slot *types2.TxsRlp, currentHeight uint64, cfg SequenceBlockCfg) ([]types.Transaction, []common.Hash, []common.Hash, error) {
ids := make([]common.Hash, 0, len(slot.TxIds))
transactions := make([]types.Transaction, 0, len(slot.Txs))
toRemove := make([]common.Hash, 0)
signer := types.MakeSigner(cfg.chainConfig, currentHeight, 0)
cryptoContext := secp256k1.ContextForThread(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use only 1 context for all transactions. This is sequential computation. Why not parallel?

for idx, txBytes := range slot.Txs {
transaction, err := types.DecodeTransaction(txBytes)
if err == io.EOF {
Expand All @@ -96,12 +99,23 @@ func extractTransactionsFromSlot(slot *types2.TxsRlp) ([]types.Transaction, []co
if err != nil {
// we have a transaction that cannot be decoded or a similar issue. We don't want to handle
// this tx so just WARN about it and remove it from the pool and continue
log.Warn("Failed to decode transaction from pool, skipping and removing from pool", "error", err)
log.Warn("[extractTransaction] Failed to decode transaction from pool, skipping and removing from pool",
"error", err,
"id", slot.TxIds[idx])
toRemove = append(toRemove, slot.TxIds[idx])
continue
}
var sender common.Address
copy(sender[:], slot.Senders.At(idx))

// now attempt to recover the sender
sender, err := signer.SenderWithContext(cryptoContext, transaction)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pprof shows that this line takes 27% of total time. I run sequencer and benchmark

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sender, err := signer.SenderWithContext(cryptoContext, transaction)

Copy link
Collaborator

@doutv doutv Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related issue:
okx#211

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why attempt to recover sender from public key? there should be sender address in the transaction

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sender always needs recovering at some point, it isn't part of the RLP for a transaction. Once decoded it is cached on the transaction but until then something needs to recover it.

if err != nil {
log.Warn("[extractTransaction] Failed to recover sender from transaction, skipping and removing from pool",
"error", err,
"hash", transaction.Hash())
toRemove = append(toRemove, slot.TxIds[idx])
continue
}

transaction.SetSender(sender)
transactions = append(transactions, transaction)
ids = append(ids, slot.TxIds[idx])
Expand Down
Loading