Skip to content

Commit

Permalink
Merge branch 'zkevm' into fix-stage-batches-resequence
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Staykov authored Oct 11, 2024
2 parents c2f7f11 + 7d2ba1a commit 1a0812e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
16 changes: 11 additions & 5 deletions erigon-lib/types/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,6 @@ func (ctx *TxParseContext) parseTransactionBody(payload []byte, pos, p0 int, slo
}
//ctx.keccak1.Sum(slot.IdHash[:0])
_, _ = ctx.Keccak1.(io.Reader).Read(slot.IDHash[:32])
if validateHash != nil {
if err := validateHash(slot.IDHash[:32]); err != nil {
return p, err
}
}

if !ctx.withSender {
return p, nil
Expand Down Expand Up @@ -620,6 +615,12 @@ func (ctx *TxParseContext) parseTransactionBody(payload []byte, pos, p0 int, slo
//take last 20 bytes as address
copy(sender, ctx.buf[12:32])

if validateHash != nil {
if err := validateHash(slot.IDHash[:32]); err != nil {
return p, err
}
}

return p, nil
}

Expand Down Expand Up @@ -853,6 +854,7 @@ func (s *TxSlots) Append(slot *TxSlot, sender []byte, isLocal bool) {
}

type TxsRlp struct {
TxIds []common.Hash
Txs [][]byte
Senders Addresses
IsLocal []bool
Expand All @@ -869,10 +871,14 @@ func (s *TxsRlp) Resize(targetSize uint) {
for uint(len(s.IsLocal)) < targetSize {
s.IsLocal = append(s.IsLocal, false)
}
for uint(len(s.TxIds)) < targetSize {
s.TxIds = append(s.TxIds, common.Hash{})
}
//todo: set nil to overflow txs
s.Txs = s.Txs[:targetSize]
s.Senders = s.Senders[:length.Addr*targetSize]
s.IsLocal = s.IsLocal[:targetSize]
s.TxIds = s.TxIds[:targetSize]
}

var addressesGrowth = make([]byte, length.Addr)
Expand Down
2 changes: 0 additions & 2 deletions zk/stages/stage_sequence_execute_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/ledgerwatch/erigon/zk/erigon_db"
"github.com/ledgerwatch/erigon/zk/hermez_db"
zktypes "github.com/ledgerwatch/erigon/zk/types"
"github.com/ledgerwatch/erigon/zk/utils"
"github.com/ledgerwatch/secp256k1"
)

Expand Down Expand Up @@ -191,7 +190,6 @@ func finaliseBlock(
finalHeader := finalBlock.HeaderNoCopy()
finalHeader.Root = newRoot
finalHeader.Coinbase = batchState.getCoinbase(batchContext.cfg)
finalHeader.GasLimit = utils.GetBlockGasLimitForFork(batchState.forkId)
finalHeader.ReceiptHash = types.DeriveSha(builtBlockElements.receipts)
finalHeader.Bloom = types.CreateBloom(builtBlockElements.receipts)
newNum := finalBlock.Number()
Expand Down
20 changes: 15 additions & 5 deletions zk/stages/stage_sequence_execute_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ 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, err := extractTransactionsFromSlot(&slots)
yieldedTxs, toRemove, err := extractTransactionsFromSlot(&slots)
if err != nil {
return err
}
for _, txId := range toRemove {
cfg.txPool.MarkForDiscardFromPendingBest(txId)
}
transactions = append(transactions, yieldedTxs...)
return nil
}); err != nil {
Expand All @@ -63,7 +66,9 @@ func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *comm
}

if slots != nil {
transactions, err = extractTransactionsFromSlot(slots)
// 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)
if err != nil {
return err
}
Expand All @@ -77,22 +82,27 @@ func getLimboTransaction(ctx context.Context, cfg SequenceBlockCfg, txHash *comm
return transactions, nil
}

func extractTransactionsFromSlot(slot *types2.TxsRlp) ([]types.Transaction, error) {
func extractTransactionsFromSlot(slot *types2.TxsRlp) ([]types.Transaction, []common.Hash, error) {
transactions := make([]types.Transaction, 0, len(slot.Txs))
toRemove := make([]common.Hash, 0)
for idx, txBytes := range slot.Txs {
transaction, err := types.DecodeTransaction(txBytes)
if err == io.EOF {
continue
}
if err != nil {
return nil, err
// 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)
toRemove = append(toRemove, slot.TxIds[idx])
continue
}
var sender common.Address
copy(sender[:], slot.Senders.At(idx))
transaction.SetSender(sender)
transactions = append(transactions, transaction)
}
return transactions, nil
return transactions, toRemove, nil
}

type overflowType uint8
Expand Down
1 change: 1 addition & 0 deletions zk/txpool/pool_zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ func (p *TxPool) best(n uint16, txs *types.TxsRlp, tx kv.Tx, onTopOf, availableG
}

txs.Txs[count] = rlpTx
txs.TxIds[count] = mt.Tx.IDHash
copy(txs.Senders.At(count), sender.Bytes())
txs.IsLocal[count] = isLocal
toSkip.Add(mt.Tx.IDHash)
Expand Down
1 change: 1 addition & 0 deletions zk/txpool/pool_zk_limbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (p *TxPool) GetLimboTxRplsByHash(tx kv.Tx, txHash *common.Hash) (*types.Txs
for i := uint32(0); i < txSize; i++ {
limboTx := limboBlock.Transactions[i]
txsRlps.Txs[i] = limboTx.Rlp
txsRlps.TxIds[i] = limboTx.Hash
copy(txsRlps.Senders.At(int(i)), limboTx.Sender[:])
txsRlps.IsLocal[i] = true // all limbo tx are considered local //TODO: explain better about local
}
Expand Down

0 comments on commit 1a0812e

Please sign in to comment.