Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Nov 30, 2023
1 parent 8631693 commit 8d51f96
Showing 1 changed file with 59 additions and 37 deletions.
96 changes: 59 additions & 37 deletions vms/proposervm/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 8d51f96

Please sign in to comment.