Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vms/platformvm: Remove double block building logic #2380

Merged
merged 41 commits into from
Dec 8, 2023

Conversation

dhrubabasu
Copy link
Contributor

@dhrubabasu dhrubabasu commented Nov 28, 2023

Why this should be merged

We currently build blocks twice by calling buildBlock twice. This PR removes this double-building logic and attempts to make the consensus engine messaging more obviously correct. This PR also removes the usage of timer.Timer.

How this works

TLDR we should only pass the PendingTxs message to the engine once we either:

  1. Have a tx in the mempool
  2. The NextStakerChangeTime is in the past

The block timer is now only responsible for checking if the NextStakerChangeTime is in the past. If it is, we call mempool.RequestBuildBlock() to pass PendingTxs message to the engine with the emptyBlocksPermitted override set to True.

ResetBlockTimer is called whenever the preferred block is updated: https://github.com/ava-labs/avalanchego/blob/dev/vms/platformvm/vm.go#L400-L405. This is the only time that NextStakerChangeTime can change so we force a recalculation of the block timer in ResetBlockTimer.

How this was tested

CI

@dhrubabasu dhrubabasu self-assigned this Nov 28, 2023
@dhrubabasu dhrubabasu added the cleanup Code quality improvement label Nov 28, 2023
@dhrubabasu dhrubabasu added this to the v1.10.17 milestone Nov 28, 2023
@dhrubabasu dhrubabasu marked this pull request as ready for review November 28, 2023 02:21
Base automatically changed from move-engine-to-mempool to dev November 28, 2023 16:18
// We can build a block now
now := b.txExecutorBackend.Clk.Time()
if !b.nextStakerChangeTime.After(now) {
// Block needs to be issued to advance time.
b.Mempool.RequestBuildBlock(true /*=emptyBlockPermitted*/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description says: setNextBuildBlockTime now no longer builds a block. It only is responsible for updating the nextStakerChangeTime field. We're not calling buildBlock in here anymore, but we're sort of building a block by calling b.Mempool.RequestBuildBlock.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return here? The rest of the work we do in this method seems unnecessary because BuildBlock is about to be invoked by the engine, and therefore the nextStakerChangeTime doesn't matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-worked this function a bit and now return early

@@ -92,28 +91,14 @@ func New(
func (b *builder) BuildBlock(context.Context) (snowman.Block, error) {
b.Mempool.DisableAdding()
defer func() {
b.Mempool.RequestBuildBlock(false /*=emptyBlockPermitted*/)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to build another block immediately after building a block. For example, the mempool is so full that there are more txs in it that can fit in a single block

danlaine
danlaine previously approved these changes Nov 28, 2023
@dhrubabasu dhrubabasu force-pushed the stop-building-block-twice branch from e10a052 to 5b70127 Compare December 4, 2023 19:58
@abi87 abi87 linked an issue Dec 7, 2023 that may be closed by this pull request
@@ -93,27 +94,12 @@ func (b *builder) BuildBlock(context.Context) (snowman.Block, error) {
b.Mempool.DisableAdding()
defer func() {
b.Mempool.EnableAdding()
b.ResetBlockTimer()
b.Mempool.RequestBuildBlock(false /*=emptyBlockPermitted*/)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The timer previously fired to advance the chain time
  2. The next staker change time is to move a staker from pending to current
  3. There is an invalid tx in the mempool

I think we can end up building an invalid block and not scheduling to build another block to advance the timestamp.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #2447

Comment on lines +112 to +115
b.txExecutorBackend.Ctx.Log.Error("block builder encountered a fatal error",
zap.Error(err),
)
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than killing the block builder loop we could do something like specify duration = time.Second or something... But really this should never happen.

@StephenButtolph StephenButtolph self-assigned this Dec 7, 2023
Co-authored-by: aaronbuchwald <[email protected]>
Signed-off-by: Stephen Buttolph <[email protected]>
BuildBlock(context.Context) (snowman.Block, error)
// ShutdownBlockTimer stops block creation requests to advance the chain
// timestamp.
ShutdownBlockTimer()
Copy link
Contributor

@patrick-ogrady patrick-ogrady Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Would it make sense to just call this Shutdown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like ShutdownBlockTimer more tbh- we're not really shutting down the block builder, just the timer

@StephenButtolph StephenButtolph added this pull request to the merge queue Dec 8, 2023
Merged via the queue into dev with commit eb570dd Dec 8, 2023
16 checks passed
@StephenButtolph StephenButtolph deleted the stop-building-block-twice branch December 8, 2023 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code quality improvement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants