Skip to content

Commit a2327be

Browse files
Steven LIUSteven LIU
Steven LIU
authored and
Steven LIU
committed
Optimize hash calculation
1 parent 451cb91 commit a2327be

File tree

2 files changed

+76
-27
lines changed

2 files changed

+76
-27
lines changed

zk/stages/stage_sequence_execute.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"sync"
78
"time"
89

910
"github.com/ledgerwatch/erigon-lib/common"
@@ -390,9 +391,22 @@ func sequencingBatchStep(
390391
return err
391392
}
392393

393-
batchState.blockState.transactionsForInclusion = append(batchState.blockState.transactionsForInclusion, newTransactions...)
394+
hashResults := make([]common.Hash, len(newTransactions))
395+
var wg sync.WaitGroup
396+
394397
for idx, tx := range newTransactions {
395-
batchState.blockState.transactionHashesToSlots[tx.Hash()] = newIds[idx]
398+
wg.Add(1)
399+
go func(idx int, tx types.Transaction) {
400+
defer wg.Done()
401+
hashResults[idx] = tx.Hash()
402+
}(idx, tx)
403+
}
404+
405+
wg.Wait()
406+
407+
batchState.blockState.transactionsForInclusion = append(batchState.blockState.transactionsForInclusion, newTransactions...)
408+
for idx, hash := range hashResults {
409+
batchState.blockState.transactionHashesToSlots[hash] = newIds[idx]
396410
}
397411

398412
if len(batchState.blockState.transactionsForInclusion) == 0 {

zk/stages/stage_sequence_execute_transactions.go

+60-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package stages
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/ledgerwatch/erigon-lib/common"
78
"github.com/ledgerwatch/erigon-lib/kv"
@@ -90,35 +91,69 @@ func extractTransactionsFromSlot(slot *types2.TxsRlp, currentHeight uint64, cfg
9091
transactions := make([]types.Transaction, 0, len(slot.Txs))
9192
toRemove := make([]common.Hash, 0)
9293
signer := types.MakeSigner(cfg.chainConfig, currentHeight, 0)
93-
cryptoContext := secp256k1.ContextForThread(1)
94+
type result struct {
95+
index int
96+
transaction types.Transaction
97+
id common.Hash
98+
toRemove bool
99+
err error
100+
}
101+
102+
results := make([]*result, len(slot.Txs))
103+
var wg sync.WaitGroup
104+
94105
for idx, txBytes := range slot.Txs {
95-
transaction, err := types.DecodeTransaction(txBytes)
96-
if err == io.EOF {
97-
continue
98-
}
99-
if err != nil {
100-
// we have a transaction that cannot be decoded or a similar issue. We don't want to handle
101-
// this tx so just WARN about it and remove it from the pool and continue
102-
log.Warn("[extractTransaction] Failed to decode transaction from pool, skipping and removing from pool",
103-
"error", err,
104-
"id", slot.TxIds[idx])
105-
toRemove = append(toRemove, slot.TxIds[idx])
106-
continue
107-
}
106+
wg.Add(1)
107+
go func(idx int, txBytes []byte) {
108+
defer wg.Done()
108109

109-
// now attempt to recover the sender
110-
sender, err := signer.SenderWithContext(cryptoContext, transaction)
111-
if err != nil {
112-
log.Warn("[extractTransaction] Failed to recover sender from transaction, skipping and removing from pool",
113-
"error", err,
114-
"hash", transaction.Hash())
115-
toRemove = append(toRemove, slot.TxIds[idx])
110+
cryptoContext := secp256k1.ContextForThread(1)
111+
112+
res := &result{index: idx}
113+
114+
transaction, err := types.DecodeTransaction(txBytes)
115+
if err == io.EOF {
116+
res.toRemove = true
117+
results[idx] = res
118+
return
119+
}
120+
if err != nil {
121+
res.toRemove = true
122+
res.id = slot.TxIds[idx]
123+
res.err = err
124+
results[idx] = res
125+
return
126+
}
127+
128+
sender, err := signer.SenderWithContext(cryptoContext, transaction)
129+
if err != nil {
130+
res.toRemove = true
131+
res.id = slot.TxIds[idx]
132+
res.err = err
133+
results[idx] = res
134+
return
135+
}
136+
137+
transaction.SetSender(sender)
138+
res.transaction = transaction
139+
res.id = slot.TxIds[idx]
140+
results[idx] = res
141+
}(idx, txBytes)
142+
}
143+
144+
wg.Wait()
145+
146+
for _, res := range results {
147+
if res.toRemove {
148+
toRemove = append(toRemove, res.id)
149+
if res.err != nil {
150+
log.Warn("[extractTransaction] Failed to process transaction",
151+
"error", res.err, "id", res.id)
152+
}
116153
continue
117154
}
118-
119-
transaction.SetSender(sender)
120-
transactions = append(transactions, transaction)
121-
ids = append(ids, slot.TxIds[idx])
155+
transactions = append(transactions, res.transaction)
156+
ids = append(ids, res.id)
122157
}
123158
return transactions, ids, toRemove, nil
124159
}

0 commit comments

Comments
 (0)