Skip to content

Commit b936db3

Browse files
cody-littleyCody Littley
andauthored
Wire up receipt hashing (#4684)
The receipt hashes reported to the hash builder now obey the official hash spec. Co-authored-by: Cody Littley <cody.littley@seinetwork.io>
1 parent d27422c commit b936db3

18 files changed

Lines changed: 511 additions & 46 deletions
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package receipthash
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"sync"
8+
9+
ethtypes "github.com/ethereum/go-ethereum/core/types"
10+
"github.com/sei-protocol/seilog"
11+
12+
"github.com/sei-protocol/sei-chain/giga/apphash"
13+
)
14+
15+
var logger = seilog.NewLogger("giga", "apphash", "receipthash")
16+
17+
// ReceiptHasher computes the receipt hash of each block submitted to it on a background goroutine, and reports the
18+
// hashes in submission order.
19+
type ReceiptHasher struct {
20+
21+
// Called with each block's receipt hash. An error stops the hasher.
22+
report func(ctx context.Context, blockHeight uint64, receiptHash apphash.ReceiptHash) error
23+
24+
// The blocks waiting to be hashed, in submission order, followed by the request Close() sends.
25+
requests chan receiptHashRequest
26+
27+
// Cancelled by Close(), or by the first block that fails. Its cause is the error every later call returns.
28+
ctx context.Context
29+
30+
// Cancels ctx with a cause.
31+
cancel context.CancelCauseFunc
32+
33+
// Tracks the hashing goroutine, which Close() waits for.
34+
wg sync.WaitGroup
35+
36+
// The error from the block that stopped the hashing goroutine, or nil if none did. Set by the hashing goroutine
37+
// as it exits, and read by Close() once it has exited.
38+
failure error
39+
}
40+
41+
// A request for the hashing goroutine: a block to hash, or the request Close() sends.
42+
type receiptHashRequest struct {
43+
44+
// The height of the block whose receipts these are.
45+
blockHeight uint64
46+
47+
// The block's receipts, as passed to ComputeReceiptHash().
48+
receipts []*ethtypes.Receipt
49+
50+
// Set only on the request Close() sends, and closed once every block submitted before it has been reported.
51+
drained chan struct{}
52+
}
53+
54+
// NewReceiptHasher starts a hasher that passes each block's receipt hash to report. Errors if config is invalid.
55+
func NewReceiptHasher(
56+
// Cancelling it stops the hasher.
57+
ctx context.Context,
58+
// Configures the hasher.
59+
config ReceiptHasherConfig,
60+
// Called with each block's receipt hash, in submission order. An error stops the hasher. Its signature matches
61+
// AppHashBuilder.ReportReceiptHash().
62+
report func(ctx context.Context, blockHeight uint64, receiptHash apphash.ReceiptHash) error,
63+
) (*ReceiptHasher, error) {
64+
if err := config.Validate(); err != nil {
65+
return nil, fmt.Errorf("invalid receipt hasher config: %w", err)
66+
}
67+
ctx, cancel := context.WithCancelCause(ctx)
68+
h := &ReceiptHasher{
69+
report: report,
70+
requests: make(chan receiptHashRequest, config.BufferSize),
71+
ctx: ctx,
72+
cancel: cancel,
73+
}
74+
h.wg.Go(h.run)
75+
return h, nil
76+
}
77+
78+
// Submit queues a block's receipts to be hashed, blocking while the queue is full. Receipts that
79+
// ComputeReceiptHash() rejects stop the hasher once they are hashed rather than failing this call. Errors if the
80+
// hasher has stopped, or if ctx is done first.
81+
func (h *ReceiptHasher) Submit(
82+
ctx context.Context,
83+
// The height of the block whose receipts these are.
84+
blockHeight uint64,
85+
// The block's receipts, as passed to ComputeReceiptHash(). Must not be mutated after the call.
86+
receipts []*ethtypes.Receipt,
87+
) error {
88+
request := receiptHashRequest{blockHeight: blockHeight, receipts: receipts}
89+
if err := h.enqueue(ctx, request); err != nil {
90+
return fmt.Errorf("failed to submit the receipts of block %d: %w", blockHeight, err)
91+
}
92+
return nil
93+
}
94+
95+
// Close reports the receipt hash of every block submitted before the call, then stops the hasher and returns once
96+
// its goroutine has exited. A block submitted concurrently with the call may or may not be reported, as may every
97+
// block still queued if the hasher's context is cancelled. Errors if a block failed to be hashed or reported.
98+
func (h *ReceiptHasher) Close() error {
99+
drained := make(chan struct{})
100+
// A hasher already stopped refuses the request, and has nothing left to report.
101+
if h.enqueue(context.Background(), receiptHashRequest{drained: drained}) == nil {
102+
select {
103+
case <-drained:
104+
case <-h.ctx.Done():
105+
}
106+
}
107+
h.cancel(errors.New("receipt hasher is closed"))
108+
h.wg.Wait()
109+
if h.failure != nil {
110+
return fmt.Errorf("failed to close receipt hasher: %w", h.failure)
111+
}
112+
return nil
113+
}
114+
115+
// Hands request to the hashing goroutine.
116+
func (h *ReceiptHasher) enqueue(ctx context.Context, request receiptHashRequest) error {
117+
// A stopped hasher must refuse the request rather than race it into the buffer, where nothing would run it.
118+
if h.ctx.Err() != nil {
119+
return h.stoppedError()
120+
}
121+
select {
122+
case h.requests <- request:
123+
return nil
124+
case <-h.ctx.Done():
125+
return h.stoppedError()
126+
case <-ctx.Done():
127+
return fmt.Errorf("receipt hasher call abandoned: %w", ctx.Err())
128+
}
129+
}
130+
131+
// Returns the error every call returns once the hasher is stopped.
132+
func (h *ReceiptHasher) stoppedError() error {
133+
return fmt.Errorf("receipt hasher stopped: %w", context.Cause(h.ctx))
134+
}
135+
136+
// The hashing goroutine. Hashes and reports blocks until the request Close() sends, a cancelled context, or a block
137+
// that fails.
138+
func (h *ReceiptHasher) run() {
139+
for {
140+
select {
141+
case <-h.ctx.Done():
142+
return
143+
case request := <-h.requests:
144+
if request.drained != nil {
145+
close(request.drained)
146+
return
147+
}
148+
if err := h.hashAndReport(request); err != nil {
149+
logger.Error("receipt hasher stopped", "err", err)
150+
h.failure = err
151+
h.cancel(err)
152+
return
153+
}
154+
}
155+
}
156+
}
157+
158+
// Computes the receipt hash of one block and reports it.
159+
func (h *ReceiptHasher) hashAndReport(request receiptHashRequest) error {
160+
receiptHash, err := ComputeReceiptHash(request.receipts)
161+
if err != nil {
162+
return fmt.Errorf("failed to hash the receipts of block %d: %w", request.blockHeight, err)
163+
}
164+
if err := h.report(h.ctx, request.blockHeight, receiptHash); err != nil {
165+
return fmt.Errorf("failed to report the receipt hash of block %d: %w", request.blockHeight, err)
166+
}
167+
return nil
168+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package receipthash
2+
3+
import "fmt"
4+
5+
// ReceiptHasherConfig configures a ReceiptHasher.
6+
type ReceiptHasherConfig struct {
7+
8+
// The number of blocks whose receipts may wait to be hashed before Submit() blocks. Higher values absorb longer
9+
// stalls, at the cost of memory.
10+
BufferSize int
11+
}
12+
13+
// DefaultReceiptHasherConfig returns a ReceiptHasherConfig holding every default.
14+
func DefaultReceiptHasherConfig() ReceiptHasherConfig {
15+
return ReceiptHasherConfig{
16+
BufferSize: 16,
17+
}
18+
}
19+
20+
// Validate returns an error if the configuration is invalid.
21+
func (c *ReceiptHasherConfig) Validate() error {
22+
if c.BufferSize <= 0 {
23+
return fmt.Errorf("buffer size must be positive, got %d", c.BufferSize)
24+
}
25+
return nil
26+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package receipthash
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
ethtypes "github.com/ethereum/go-ethereum/core/types"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/sei-protocol/sei-chain/giga/apphash"
12+
)
13+
14+
// A receipt hash reported by a ReceiptHasher.
15+
type reportedReceiptHash struct {
16+
blockHeight uint64
17+
receiptHash apphash.ReceiptHash
18+
}
19+
20+
// recordReports returns a report function that sends each receipt hash to the returned channel, which holds up to
21+
// capacity of them.
22+
func recordReports(
23+
capacity int,
24+
) (func(context.Context, uint64, apphash.ReceiptHash) error, <-chan reportedReceiptHash) {
25+
reported := make(chan reportedReceiptHash, capacity)
26+
report := func(_ context.Context, blockHeight uint64, receiptHash apphash.ReceiptHash) error {
27+
reported <- reportedReceiptHash{blockHeight: blockHeight, receiptHash: receiptHash}
28+
return nil
29+
}
30+
return report, reported
31+
}
32+
33+
// newReceiptHasher starts a ReceiptHasher with the default config and the given buffer size.
34+
func newReceiptHasher(
35+
t *testing.T,
36+
bufferSize int,
37+
report func(context.Context, uint64, apphash.ReceiptHash) error,
38+
) *ReceiptHasher {
39+
t.Helper()
40+
config := DefaultReceiptHasherConfig()
41+
config.BufferSize = bufferSize
42+
hasher, err := NewReceiptHasher(t.Context(), config, report)
43+
require.NoError(t, err)
44+
return hasher
45+
}
46+
47+
func TestReceiptHasherReportsEveryBlockInOrderBeforeClosing(t *testing.T) {
48+
report, reported := recordReports(3)
49+
hasher := newReceiptHasher(t, 3, report)
50+
51+
blocks := [][]*ethtypes.Receipt{specReceipts()[:1], nil, specReceipts()}
52+
for i, receipts := range blocks {
53+
blockHeight := uint64(10 + i) //nolint:gosec // small test heights
54+
require.NoError(t, hasher.Submit(t.Context(), blockHeight, receipts))
55+
}
56+
require.NoError(t, hasher.Close())
57+
58+
require.Len(t, reported, len(blocks))
59+
for i, receipts := range blocks {
60+
blockHeight := uint64(10 + i) //nolint:gosec // small test heights
61+
want := reportedReceiptHash{blockHeight: blockHeight, receiptHash: receiptHashOf(t, receipts)}
62+
require.Equal(t, want, <-reported)
63+
}
64+
}
65+
66+
func TestReceiptHasherStopsOnReceiptsItCannotHash(t *testing.T) {
67+
report, reported := recordReports(1)
68+
hasher := newReceiptHasher(t, 1, report)
69+
70+
require.NoError(t, hasher.Submit(t.Context(), 1, []*ethtypes.Receipt{nil}))
71+
72+
require.ErrorContains(t, hasher.Close(), "failed to hash the receipts of block 1")
73+
require.ErrorContains(t, hasher.Submit(t.Context(), 2, nil), "receipt hasher stopped")
74+
require.Empty(t, reported)
75+
}
76+
77+
func TestReceiptHasherStopsWhenAReportFails(t *testing.T) {
78+
reportErr := errors.New("builder stopped")
79+
hasher := newReceiptHasher(t, 1, func(context.Context, uint64, apphash.ReceiptHash) error {
80+
return reportErr
81+
})
82+
83+
require.NoError(t, hasher.Submit(t.Context(), 1, nil))
84+
85+
err := hasher.Close()
86+
require.ErrorIs(t, err, reportErr)
87+
require.ErrorContains(t, err, "failed to report the receipt hash of block 1")
88+
require.ErrorIs(t, hasher.Submit(t.Context(), 2, nil), reportErr)
89+
}
90+
91+
func TestReceiptHasherSubmitGivesUpWhenItsContextIsDone(t *testing.T) {
92+
release := make(chan struct{})
93+
reporting := make(chan struct{}, 1)
94+
hasher := newReceiptHasher(t, 1, func(context.Context, uint64, apphash.ReceiptHash) error {
95+
reporting <- struct{}{}
96+
<-release
97+
return nil
98+
})
99+
100+
// Block 1 holds the hashing goroutine, and block 2 fills the queue behind it.
101+
require.NoError(t, hasher.Submit(t.Context(), 1, nil))
102+
<-reporting
103+
require.NoError(t, hasher.Submit(t.Context(), 2, nil))
104+
ctx, cancel := context.WithCancel(t.Context())
105+
cancel()
106+
require.ErrorIs(t, hasher.Submit(ctx, 3, nil), context.Canceled)
107+
108+
close(release)
109+
<-reporting
110+
require.NoError(t, hasher.Close())
111+
}
112+
113+
func TestReceiptHasherRefusesAnInvalidConfig(t *testing.T) {
114+
report, _ := recordReports(0)
115+
_, err := NewReceiptHasher(t.Context(), ReceiptHasherConfig{BufferSize: 0}, report)
116+
require.ErrorContains(t, err, "buffer size must be positive")
117+
}

‎giga/evmonly/cmd/evmonly-loadtest/pipeline.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ func runPrebuilt(ctx context.Context, cfg config, state *generatedState, workloa
163163
if err := storage.StateDB().CommitStateChanges(1, genesisChanges); err != nil {
164164
return fmt.Errorf("commit generated genesis state: %w", err)
165165
}
166+
// Genesis is committed without executing a block, so it has no transactions and its receipts are an empty
167+
// list.
168+
if err := storage.ReceiptHasher().Submit(ctx, 1, nil); err != nil {
169+
return fmt.Errorf("submit generated genesis receipts: %w", err)
170+
}
166171

167172
profiles, err := startProfiles(cfg)
168173
if err != nil {

‎giga/evmonly/executor.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ethereum/go-ethereum/core/vm"
1515
"github.com/ethereum/go-ethereum/crypto"
1616
"github.com/ethereum/go-ethereum/params"
17+
"github.com/sei-protocol/sei-chain/giga/apphash/receipthash"
1718
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
1819
seidbmetrics "github.com/sei-protocol/sei-chain/sei-db/common/metrics"
1920
"github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt"
@@ -32,6 +33,7 @@ type Executor struct {
3233
storeMu sync.Mutex
3334
stateStore gigatypes.StateDB
3435
receiptStore receipt.ReceiptStore
36+
receiptHasher *receipthash.ReceiptHasher
3537
changeSetEncoder NamedChangeSetEncoder
3638
// Optional: nil commits only the state encoder's changesets.
3739
blockChangeSetEncoder BlockChangeSetEncoder

‎giga/evmonly/giga_store.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ func (e *Executor) executePreparedBlockWithStore(ctx context.Context, req Prepar
152152
return nil, fmt.Errorf("store receipts for block %d: %w", req.Context.Number, err)
153153
}
154154
}
155+
if receiptHasher := e.receiptHasher; receiptHasher != nil {
156+
e.blockPhases.SetPhase(phaseSubmitReceiptHash)
157+
if err := receiptHasher.Submit(ctx, req.Context.Number, nonceConsumingReceipts(result)); err != nil {
158+
return nil, fmt.Errorf("submit receipts to be hashed for block %d: %w", req.Context.Number, err)
159+
}
160+
}
155161
// One commit is in flight at a time, so the previous one lands before this block starts its
156162
// own. It has had this block's whole execution to run, so it rarely still holds.
157163
if !settleBeforeEncoding {

‎giga/evmonly/phases.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const (
3131
phaseEncodeReceipts = "encode_receipts"
3232
// phaseWriteReceipts covers writing the block's receipts to the receipt store.
3333
phaseWriteReceipts = "write_receipts"
34+
// phaseSubmitReceiptHash covers handing the block's receipts to the receipt hasher.
35+
phaseSubmitReceiptHash = "submit_receipt_hash"
3436
// phaseCommitState covers starting the block's state commit.
3537
phaseCommitState = "commit_state"
3638
)

‎giga/evmonly/receipt.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ethereum/go-ethereum/common"
1010
"github.com/ethereum/go-ethereum/core"
11+
ethtypes "github.com/ethereum/go-ethereum/core/types"
1112

1213
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
1314
"github.com/sei-protocol/sei-chain/sei-db/ledger_db/receipt"
@@ -135,6 +136,18 @@ func (e *Executor) receiptRecordsParallel(ctx context.Context, blockNumber uint6
135136
return compacted, nil
136137
}
137138

139+
// nonceConsumingReceipts returns the receipts of the block's transactions that consumed a nonce, which is every
140+
// transaction but the rejected ones, in block order.
141+
func nonceConsumingReceipts(result *BlockResult) []*ethtypes.Receipt {
142+
receipts := make([]*ethtypes.Receipt, 0, len(result.Receipts))
143+
for i, ethReceipt := range result.Receipts {
144+
if !result.Txs[i].Rejected {
145+
receipts = append(receipts, ethReceipt)
146+
}
147+
}
148+
return receipts
149+
}
150+
138151
// occParallelReceiptThreshold is the block size below which fanning receipt encoding out costs more
139152
// than it saves.
140153
const occParallelReceiptThreshold = 64

0 commit comments

Comments
 (0)