Skip to content

Commit

Permalink
Refactor report builder interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
winder committed Jan 9, 2025
1 parent 0e95111 commit e7bb048
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 93 deletions.
6 changes: 3 additions & 3 deletions execute/outcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ func (p *Plugin) getFilterOutcome(
p.msgHasher,
p.reportCodec,
p.estimateProvider,
observation.Nonces,
p.destChain,
uint64(maxReportLength),
p.offchainCfg.BatchGasLimit,
report.WithMaxReportSizeBytes(maxReportLength),
report.WithMaxGas(p.offchainCfg.BatchGasLimit),
report.WithExtraMessageCheck(report.CheckNonces(observation.Nonces)),
)

outcomeReports, selectedReports, err := selectReport(
Expand Down
76 changes: 62 additions & 14 deletions execute/report/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,78 @@ type ExecReportBuilder interface {
Build() ([]cciptypes.ExecutePluginReportSingleChain, error)
}

func NewBuilder(
type Option func(erb *execReportBuilder)

func WithMaxGas(maxGas uint64) func(*execReportBuilder) {
return func(erb *execReportBuilder) {
erb.maxGas = maxGas
}
}

func WithMaxReportSizeBytes(maxReportSizeBytes uint64) Option {
return func(erb *execReportBuilder) {
erb.maxReportSizeBytes = maxReportSizeBytes
}
}

func MaxMessages(maxMessages uint64) Option {
return func(erb *execReportBuilder) {
erb.maxMessages = maxMessages
panic("not implemented")
}
}

// WithExtraMessageCheck adds additional message checks to the default ones.
func WithExtraMessageCheck(check Check) Option {
return func(erb *execReportBuilder) {
erb.checks = append(erb.checks, check)
}
}

func newBuilderInternal(
logger logger.Logger,
hasher cciptypes.MessageHasher,
encoder cciptypes.ExecutePluginCodec,
estimateProvider cciptypes.EstimateProvider,
nonces map[cciptypes.ChainSelector]map[string]uint64,
destChainSelector cciptypes.ChainSelector,
maxReportSizeBytes uint64,
maxGas uint64,
) ExecReportBuilder {
return &execReportBuilder{
options ...Option,
) *execReportBuilder {
defaultChecks := []Check{
CheckIfPseudoDeleted(),
CheckAlreadyExecuted(),
CheckTokenData(),
CheckTooCostly(),
}

builder := &execReportBuilder{
lggr: logger,

checks: defaultChecks,
encoder: encoder,
hasher: hasher,
estimateProvider: estimateProvider,
sendersNonce: nonces,
expectedNonce: make(map[cciptypes.ChainSelector]map[string]uint64),

destChainSelector: destChainSelector,
maxReportSizeBytes: maxReportSizeBytes,
maxGas: maxGas,
destChainSelector: destChainSelector,
}

for _, option := range options {
if option != nil {
option(builder)
}
}

return builder
}

func NewBuilder(
logger logger.Logger,
hasher cciptypes.MessageHasher,
encoder cciptypes.ExecutePluginCodec,
estimateProvider cciptypes.EstimateProvider,
destChainSelector cciptypes.ChainSelector,
options ...Option,
) ExecReportBuilder {
return newBuilderInternal(logger, hasher, encoder, estimateProvider, destChainSelector, options...)
}

// validationMetadata contains all metadata needed to accumulate results across multiple reports and messages.
Expand All @@ -63,17 +112,16 @@ type execReportBuilder struct {
encoder cciptypes.ExecutePluginCodec
hasher cciptypes.MessageHasher
estimateProvider cciptypes.EstimateProvider
sendersNonce map[cciptypes.ChainSelector]map[string]uint64

// Config
checks []Check
destChainSelector cciptypes.ChainSelector
maxReportSizeBytes uint64
maxGas uint64
maxMessages uint64

// State
accumulated validationMetadata
// expectedNonce is used to track nonces for multiple messages from the same sender.
expectedNonce map[cciptypes.ChainSelector]map[string]uint64

// Result
execReports []cciptypes.ExecutePluginReportSingleChain
Expand Down
Loading

0 comments on commit e7bb048

Please sign in to comment.