From 8d51f966db83d96a3016ccd781ced092a1e8e3af Mon Sep 17 00:00:00 2001 From: Alberto Benegiamo Date: Thu, 30 Nov 2023 13:24:05 +0100 Subject: [PATCH] minor refactoring --- vms/proposervm/block.go | 96 +++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/vms/proposervm/block.go b/vms/proposervm/block.go index 2b72f78c6316..fa6134eccad2 100644 --- a/vms/proposervm/block.go +++ b/vms/proposervm/block.go @@ -203,37 +203,15 @@ func (p *postForkCommonComponents) buildChild( return nil, err } - delay := newTimestamp.Sub(parentTimestamp) - if delay < proposer.MaxBuildDelay { - parentHeight := p.innerBlk.Height() - proposerID := p.vm.ctx.NodeID - minDelay, err := p.vm.Windower.Delay(ctx, parentHeight+1, parentPChainHeight, proposerID, proposer.MaxBuildWindows) - if err != nil { - p.vm.ctx.Log.Error("unexpected build block failure", - zap.String("reason", "failed to calculate required timestamp delay"), - zap.Stringer("parentID", parentID), - zap.Error(err), - ) - return nil, err - } - - if delay < minDelay { - // It's not our turn to propose a block yet. This is likely caused - // by having previously notified the consensus engine to attempt to - // build a block on top of a block that is no longer the preferred - // block. - p.vm.ctx.Log.Debug("build block dropped", - zap.Time("parentTimestamp", parentTimestamp), - zap.Duration("minDelay", minDelay), - zap.Time("blockTimestamp", newTimestamp), - ) - - // In case the inner VM only issued one pendingTxs message, we - // should attempt to re-handle that once it is our turn to build the - // block. - p.vm.notifyInnerBlockReady() - return nil, errProposerWindowNotStarted - } + buildSignedBlock, err := p.shouldBuildBlock( + ctx, + parentID, + parentTimestamp, + parentPChainHeight, + newTimestamp, + ) + if err != nil { + return nil, err } var innerBlock snowman.Block @@ -250,22 +228,22 @@ func (p *postForkCommonComponents) buildChild( // Build the child var statelessChild block.SignedBlock - if delay >= proposer.MaxVerifyDelay { - statelessChild, err = block.BuildUnsigned( + if buildSignedBlock { + statelessChild, err = block.Build( parentID, newTimestamp, pChainHeight, + p.vm.StakingCertLeaf, innerBlock.Bytes(), + p.vm.ctx.ChainID, + p.vm.StakingLeafSigner, ) } else { - statelessChild, err = block.Build( + statelessChild, err = block.BuildUnsigned( parentID, newTimestamp, pChainHeight, - p.vm.StakingCertLeaf, innerBlock.Bytes(), - p.vm.ctx.ChainID, - p.vm.StakingLeafSigner, ) } if err != nil { @@ -382,3 +360,47 @@ func (p *postForkCommonComponents) verifyPostDurangoBlockDelay( return nil } + +func (p *postForkCommonComponents) shouldBuildBlock( + ctx context.Context, + parentID ids.ID, + parentTimestamp time.Time, + parentPChainHeight uint64, + newTimestamp time.Time, +) (bool, error) { + delay := newTimestamp.Sub(parentTimestamp) + if delay >= proposer.MaxBuildDelay { + return false, nil + } + + parentHeight := p.innerBlk.Height() + proposerID := p.vm.ctx.NodeID + minDelay, err := p.vm.Windower.Delay(ctx, parentHeight+1, parentPChainHeight, proposerID, proposer.MaxBuildWindows) + if err != nil { + p.vm.ctx.Log.Error("unexpected build block failure", + zap.String("reason", "failed to calculate required timestamp delay"), + zap.Stringer("parentID", parentID), + zap.Error(err), + ) + return false, err + } + + if delay < minDelay { + // It's not our turn to propose a block yet. This is likely caused + // by having previously notified the consensus engine to attempt to + // build a block on top of a block that is no longer the preferred + // block. + p.vm.ctx.Log.Debug("build block dropped", + zap.Time("parentTimestamp", parentTimestamp), + zap.Duration("minDelay", minDelay), + zap.Time("blockTimestamp", newTimestamp), + ) + + // In case the inner VM only issued one pendingTxs message, we + // should attempt to re-handle that once it is our turn to build the + // block. + p.vm.notifyInnerBlockReady() + return false, errProposerWindowNotStarted + } + return true, nil +}