From f5baf998d88417285ee322e92a50793c6c3c0a3e Mon Sep 17 00:00:00 2001 From: danwt <30197399+danwt@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:00:33 +0000 Subject: [PATCH] clean --- block/p2p.go | 2 +- block/produce.go | 18 ++++++++++-------- block/submit.go | 14 ++++++-------- dofraud/apply.go | 6 ++++-- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/block/p2p.go b/block/p2p.go index c756a43ce..a327d7e76 100644 --- a/block/p2p.go +++ b/block/p2p.go @@ -67,7 +67,7 @@ func (m *Manager) OnReceivedBlock(event pubsub.Message) { // gossipBlock sends created blocks by the sequencer to full-nodes using P2P gossipSub func (m *Manager) gossipBlock(ctx context.Context, block types.Block, commit types.Commit) error { m.logger.Info("Gossipping block", "height", block.Header.Height) - commit = *m.fraudBlockAndCommit(dofraud.Gossip, block.Header.Height, &block) + m.fraudBlockAndCommit(dofraud.Gossip, block.Header.Height, &block, &commit) gossipedBlock := p2p.BlockData{Block: block, Commit: commit} gossipedBlockBytes, err := gossipedBlock.MarshalBinary() if err != nil { diff --git a/block/produce.go b/block/produce.go index f98e48418..f7588204b 100644 --- a/block/produce.go +++ b/block/produce.go @@ -239,7 +239,6 @@ func (m *Manager) produceBlock(opts ProduceBlockOptions) (*types.Block, *types.C // dequeue consensus messages for the new sequencers while creating a new block block = m.Executor.CreateBlock(newHeight, lastCommit, lastHeaderHash, proposerHashForBlock, m.State, maxBlockDataSize) - m.fraudBlockAndCommit(dofraud.Produce, newHeight, block) // this cannot happen if there are any sequencer set updates // AllowEmpty should be always true in this case @@ -251,6 +250,7 @@ func (m *Manager) produceBlock(opts ProduceBlockOptions) (*types.Block, *types.C if err != nil { return nil, nil, fmt.Errorf("create commit: %w: %w", err, ErrNonRecoverable) } + m.fraudBlockAndCommit(dofraud.Produce, newHeight, block, commit) m.logger.Info("Block created.", "height", newHeight, "num_tx", len(block.Data.Txs), "size", block.SizeBytes()+commit.SizeBytes()) types.RollappBlockSizeBytesGauge.Set(float64(len(block.Data.Txs))) @@ -352,12 +352,14 @@ func getHeaderHashAndCommit(store store.Store, height uint64) ([32]byte, *types. return lastBlock.Header.Hash(), lastCommit, nil } -// modifies the block, returns new commit -func (m *Manager) fraudBlockAndCommit(variant dofraud.FraudVariant, h uint64, b *types.Block) *types.Commit { - m.fraudSim.Apply(m.logger, h, variant, b) - comm, err := m.createCommit(b) - if err != nil { - m.logger.Error("Fraud block, create commit.", "err", err) +// if a fraud is specified, apply it (modify block, commit) +func (m *Manager) fraudBlockAndCommit(variant dofraud.FraudVariant, h uint64, b *types.Block, c *types.Commit) { + if m.fraudSim.Apply(m.logger, h, variant, b) { + comm, err := m.createCommit(b) + if err != nil { + m.logger.Error("Fraud block, create commit.", "err", err) + } else { + *c = *comm + } } - return comm } diff --git a/block/submit.go b/block/submit.go index c68f44374..50266b70e 100644 --- a/block/submit.go +++ b/block/submit.go @@ -44,13 +44,13 @@ func (m *Manager) SubmitLoop(ctx context.Context, func SubmitLoopInner( ctx context.Context, logger types.Logger, - bytesProduced chan int, // a channel of block and commit bytes produced - maxSkewTime time.Duration, // max time between last submitted block and last produced block allowed. if this threshold is reached block production is stopped. + bytesProduced chan int, // a channel of block and commit bytes produced + maxSkewTime time.Duration, // max time between last submitted block and last produced block allowed. if this threshold is reached block production is stopped. unsubmittedBlocksNum func() uint64, // func that returns the amount of non-submitted blocks - unsubmittedBlocksBytes func() int, // func that returns bytes from non-submitted blocks + unsubmittedBlocksBytes func() int, // func that returns bytes from non-submitted blocks batchSkewTime func() time.Duration, // func that returns measured time between last submitted block and last produced block - maxBatchSubmitTime time.Duration, // max time to allow between batches - maxBatchSubmitBytes uint64, // max size of serialised batch in bytes + maxBatchSubmitTime time.Duration, // max time to allow between batches + maxBatchSubmitBytes uint64, // max size of serialised batch in bytes createAndSubmitBatch func(maxSizeBytes uint64) (bytes uint64, err error), ) error { eg, ctx := errgroup.WithContext(ctx) @@ -329,10 +329,8 @@ func UpdateBatchSubmissionGauges(skewBytes uint64, skewBlocks uint64, skewTime t types.RollappPendingSubmissionsSkewTimeMinutes.Set(float64(skewTime.Minutes())) } -// (if frauds are specified) func (m *Manager) applyFraudsToBatch(batch *types.Batch) { for i, block := range batch.Blocks { - comm := m.fraudBlockAndCommit(dofraud.DA, block.Header.Height, block) - batch.Commits[i] = comm + m.fraudBlockAndCommit(dofraud.DA, block.Header.Height, block, batch.Commits[i]) } } diff --git a/dofraud/apply.go b/dofraud/apply.go index 1df83fd83..7947ba9b6 100644 --- a/dofraud/apply.go +++ b/dofraud/apply.go @@ -57,10 +57,11 @@ func (k key) String() string { return fmt.Sprintf("%d:%d", k.height, k.variant) } -func (f *Frauds) Apply(log types.Logger, height uint64, fraudVariant FraudVariant, b *types.Block) { +// apply any loaded frauds, no-op if none +func (f *Frauds) Apply(log types.Logger, height uint64, fraudVariant FraudVariant, b *types.Block) bool { cmd, ok := f.frauds[key{height, fraudVariant}.String()] if !ok { - return + return false } for _, fraud := range cmd.ts { @@ -102,4 +103,5 @@ func (f *Frauds) Apply(log types.Logger, height uint64, fraudVariant FraudVarian } log.Info("Applied fraud.", "height", height, "variant", fraudVariant, "types", cmd.ts) + return true }