Skip to content

Commit b7ead65

Browse files
authored
Merge pull request #241 from thirdweb-dev/07-22-validate_blocks_before_committing
validate blocks before committing
2 parents f54705e + df00900 commit b7ead65

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

internal/orchestrator/committer.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Committer struct {
2929
publisher *publisher.Publisher
3030
workMode WorkMode
3131
workModeChan chan WorkMode
32+
validator *Validator
3233
}
3334

3435
type CommitterOption func(*Committer)
@@ -39,6 +40,12 @@ func WithCommitterWorkModeChan(ch chan WorkMode) CommitterOption {
3940
}
4041
}
4142

43+
func WithValidator(validator *Validator) CommitterOption {
44+
return func(c *Committer) {
45+
c.validator = validator
46+
}
47+
}
48+
4249
func NewCommitter(rpc rpc.IRPCClient, storage storage.IStorage, opts ...CommitterOption) *Committer {
4350
triggerInterval := config.Cfg.Committer.Interval
4451
if triggerInterval == 0 {
@@ -210,6 +217,18 @@ func (c *Committer) getSequentialBlockDataToCommit(ctx context.Context) ([]commo
210217
return nil, nil
211218
}
212219

220+
if c.validator != nil {
221+
validBlocks, invalidBlocks, err := c.validator.ValidateBlocks(blocksData)
222+
if err != nil {
223+
return nil, err
224+
}
225+
if len(invalidBlocks) > 0 {
226+
log.Warn().Msgf("Found %d invalid blocks in commit batch, continuing with %d valid blocks", len(invalidBlocks), len(validBlocks))
227+
// continue with valid blocks only
228+
blocksData = validBlocks
229+
}
230+
}
231+
213232
// Sort blocks by block number
214233
sort.Slice(blocksData, func(i, j int) bool {
215234
return blocksData[i].Block.Number.Cmp(blocksData[j].Block.Number) < 0

internal/orchestrator/orchestrator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ func (o *Orchestrator) Start() {
8585
committerWorkModeChan := make(chan WorkMode, 1)
8686
workModeMonitor.RegisterChannel(committerWorkModeChan)
8787
defer workModeMonitor.UnregisterChannel(committerWorkModeChan)
88-
committer := NewCommitter(o.rpc, o.storage, WithCommitterWorkModeChan(committerWorkModeChan))
88+
validator := NewValidator(o.rpc, o.storage)
89+
committer := NewCommitter(o.rpc, o.storage, WithCommitterWorkModeChan(committerWorkModeChan), WithValidator(validator))
8990
committer.Start(ctx)
9091
}()
9192
}

0 commit comments

Comments
 (0)