Skip to content

Commit

Permalink
preload sender and check for errors (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexoscott authored Nov 20, 2024
1 parent 09a92b5 commit b8d3c79
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
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)
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)
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

0 comments on commit b8d3c79

Please sign in to comment.