|
| 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 | +} |
0 commit comments