Skip to content

Commit b34bf7f

Browse files
bdchathamclaudedevin-ai-integration[bot]
authored
perf(evmonlyapp): hash a block's transactions before taking the sender lock (#4364)
Backports #4235 from `giga-1` to `main` (PLT-1303, part of PLT-1297). `takeSenders` keccaks every raw transaction while holding the `checkedSenders` lock on the execute loop, and `CheckTx` writes that same cache constantly, so admission stalls for the length of a block's worth of hashes (~790 µs for 1,848 txs on the original measurement). `takeSenders` now computes all hashes first with `hashRawTxs`, which splits the block across up to `GOMAXPROCS` workers (at least `minTxsPerHashWorker` = 64 txs each, so small blocks stay on one goroutine), and the lock covers only the `senderCache.take` lookups. Main has since replaced the plain map with the two-generation `senderCache`, so the port keeps that structure and only moves the hashing out of the lock. Unlike the `giga-1` version, the fan-out uses `scope.Parallel` instead of a bare `sync.WaitGroup` + `go func`, per the sei-tendermint conventions. No consensus or state impact: lookups still run in block index order, so a duplicate tx in a block still resolves only the first occurrence. `TestHashRawTxsMatchesKeccak256Hash` pins `hashRawTxs` to `crypto.Keccak256Hash` across the single-worker and multi-worker paths; the evmonlyapp package tests pass under `-race`, and `make lint` / `make fmtcheck` are clean. Link to Devin session: https://app.devin.ai/sessions/d808db90106341278344623de10b92c0 Open in Devin Desktop: https://app.devin.ai/desktop/session/d808db90106341278344623de10b92c0?variant=devin Requested by: @bdchatham Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent b57bd8b commit b34bf7f

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎sei-tendermint/internal/evmonlyapp/app.go‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
gigatypes "github.com/sei-protocol/sei-chain/sei-db/state_db/giga/types"
3030
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
3131
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
32+
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils/scope"
3233
tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types"
3334
"go.opentelemetry.io/otel"
3435
)
@@ -87,6 +88,9 @@ func (c *senderCache) take(hash common.Hash) utils.Option[common.Address] {
8788
return utils.None[common.Address]()
8889
}
8990

91+
// minTxsPerHashWorker is the minimum transaction count assigned to a hash worker.
92+
const minTxsPerHashWorker = 64
93+
9094
type evmOnlyApplication struct {
9195
abci.BaseApplication
9296

@@ -381,14 +385,48 @@ func (a *evmOnlyApplication) rememberSender(hash common.Hash, sender common.Addr
381385
// decoding is needed.
382386
func (a *evmOnlyApplication) takeSenders(txs [][]byte) []utils.Option[common.Address] {
383387
out := make([]utils.Option[common.Address], len(txs))
388+
// Hashed outside the lock; CheckTx writes this map constantly.
389+
hashes := hashRawTxs(txs)
384390
for senders := range a.checkedSenders.Lock() {
385-
for i, raw := range txs {
386-
out[i] = senders.take(crypto.Keccak256Hash(raw))
391+
for i, hash := range hashes {
392+
out[i] = senders.take(hash)
387393
}
388394
}
389395
return out
390396
}
391397

398+
// hashRawTxs returns the keccak of every raw transaction, aligned with txs.
399+
func hashRawTxs(txs [][]byte) []common.Hash {
400+
hashes := make([]common.Hash, len(txs))
401+
workers := min(runtime.GOMAXPROCS(0), len(txs))
402+
if workers <= 1 || len(txs) <= minTxsPerHashWorker {
403+
hashRawTxRange(txs, hashes, 0, len(txs))
404+
return hashes
405+
}
406+
chunk := max((len(txs)+workers-1)/workers, minTxsPerHashWorker)
407+
_ = scope.Parallel(func(s scope.ParallelScope) error {
408+
for start := 0; start < len(txs); start += chunk {
409+
end := min(start+chunk, len(txs))
410+
s.Spawn(func() error {
411+
hashRawTxRange(txs, hashes, start, end)
412+
return nil
413+
})
414+
}
415+
return nil
416+
})
417+
return hashes
418+
}
419+
420+
// hashRawTxRange hashes txs[start:end] into hashes.
421+
func hashRawTxRange(txs [][]byte, hashes []common.Hash, start, end int) {
422+
state := crypto.NewKeccakState()
423+
for i := start; i < end; i++ {
424+
state.Reset()
425+
_, _ = state.Write(txs[i])
426+
_, _ = state.Read(hashes[i][:])
427+
}
428+
}
429+
392430
func (a *evmOnlyApplication) parseTx(raw []byte) (*ethtypes.Transaction, common.Address, error) {
393431
tx := new(ethtypes.Transaction)
394432
if err := tx.UnmarshalBinary(raw); err != nil {

‎sei-tendermint/internal/evmonlyapp/app_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,23 @@ func TestEVMOnlyApplicationTimesEveryFinalizeBlockPhase(t *testing.T) {
841841
}
842842
}
843843

844+
// TestHashRawTxsMatchesKeccak256Hash pins hashRawTxs to crypto.Keccak256Hash, which keys the sender cache.
845+
func TestHashRawTxsMatchesKeccak256Hash(t *testing.T) {
846+
for _, count := range []int{0, 1, 2, 17, 64, 65, 200, 1848} {
847+
t.Run(fmt.Sprintf("count=%d", count), func(t *testing.T) {
848+
txs := make([][]byte, count)
849+
for i := range txs {
850+
txs[i] = []byte(fmt.Sprintf("raw transaction %d with a body of some length", i))
851+
}
852+
hashes := hashRawTxs(txs)
853+
require.Equal(t, count, len(hashes))
854+
for i, raw := range txs {
855+
require.Equal(t, crypto.Keccak256Hash(raw), hashes[i], "tx %d", i)
856+
}
857+
})
858+
}
859+
}
860+
844861
// A block's state lands in the store behind FinalizeBlock. Readers of committed
845862
// state see it before Commit, and the next block builds on it whether or not
846863
// the store has caught up.

0 commit comments

Comments
 (0)