Skip to content

Commit

Permalink
node: Use SetStreamLastMiniblock when registering a single mini-block (
Browse files Browse the repository at this point in the history
…#24)

`SetStreamLastMiniblock` is 2-2.5% more gas efficient than
`SetStreamLastMiniblockBatch` when registering a single mini block.
Therefore use `SetStreamLastMiniblock` when there is only 1 mini block
to register.
  • Loading branch information
bas-vk authored May 21, 2024
1 parent 3460b7c commit ec2e03c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions core/node/events/stream_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ func (s *streamCacheImpl) processMiniblockProposalBatch(
var (
log = dlog.FromCtx(ctx)
miniblocks = make([]contracts.SetMiniblock, 0, len(candidates))
success []StreamId
err error
)

for streamID, candidate := range candidates {
Expand All @@ -374,10 +376,21 @@ func (s *streamCacheImpl) processMiniblockProposalBatch(
})
}

success, _, err := s.params.Registry.SetStreamLastMiniblockBatch(ctx, miniblocks)
if err != nil {
log.Error("SetStreamLastMiniblockBatch failed", "err", err)
return err
// SetStreamLastMiniblock is more efficient when registering a single block
if len(miniblocks) == 1 {
mb := miniblocks[0]
if err = s.params.Registry.SetStreamLastMiniblock(
ctx, mb.StreamId, mb.PrevMiniBlockHash, mb.LastMiniblockHash, mb.LastMiniblockNum, false); err != nil {
log.Error("SetStreamLastMiniblock failed", "err", err)
return err
}
success = append(success, mb.StreamId)
} else {
success, _, err = s.params.Registry.SetStreamLastMiniblockBatch(ctx, miniblocks)
if err != nil {
log.Error("SetStreamLastMiniblockBatch failed", "err", err)
return err
}
}

for _, streamSetInRegistry := range success {
Expand Down

0 comments on commit ec2e03c

Please sign in to comment.