Skip to content

Commit

Permalink
Fix negative ETA caused by rollback in vm.SetState (#3036)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed May 21, 2024
1 parent 7106666 commit 551f8d3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
16 changes: 8 additions & 8 deletions snow/engine/snowman/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ func (b *Bootstrapper) Clear(context.Context) error {
}

func (b *Bootstrapper) Start(ctx context.Context, startReqID uint32) error {
b.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_SNOWMAN,
State: snow.Bootstrapping,
})
if err := b.VM.SetState(ctx, snow.Bootstrapping); err != nil {
return fmt.Errorf("failed to notify VM that bootstrapping has started: %w", err)
}

lastAccepted, err := b.getLastAccepted(ctx)
if err != nil {
return err
Expand All @@ -161,14 +169,6 @@ func (b *Bootstrapper) Start(ctx context.Context, startReqID uint32) error {
zap.Uint64("lastAcceptedHeight", lastAcceptedHeight),
)

b.Ctx.State.Set(snow.EngineState{
Type: p2p.EngineType_ENGINE_TYPE_SNOWMAN,
State: snow.Bootstrapping,
})
if err := b.VM.SetState(ctx, snow.Bootstrapping); err != nil {
return fmt.Errorf("failed to notify VM that bootstrapping has started: %w", err)
}

// Set the starting height
b.startingHeight = lastAcceptedHeight
b.requestID = startReqID
Expand Down
31 changes: 31 additions & 0 deletions snow/engine/snowman/bootstrap/bootstrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,37 @@ func TestBootstrapperReceiveStaleAncestorsMessage(t *testing.T) {
require.Equal(snow.Bootstrapping, config.Ctx.State.Get().State)
}

func TestBootstrapperRollbackOnSetState(t *testing.T) {
require := require.New(t)

config, _, _, vm := newConfig(t)

blks := snowmantest.BuildChain(2)
initializeVMWithBlockchain(vm, blks)

blks[1].StatusV = choices.Accepted

bs, err := New(
config,
func(context.Context, uint32) error {
config.Ctx.State.Set(snow.EngineState{
Type: p2ppb.EngineType_ENGINE_TYPE_SNOWMAN,
State: snow.NormalOp,
})
return nil
},
)
require.NoError(err)

vm.SetStateF = func(context.Context, snow.State) error {
blks[1].StatusV = choices.Processing
return nil
}

require.NoError(bs.Start(context.Background(), 0))
require.Equal(blks[0].HeightV, bs.startingHeight)
}

func initializeVMWithBlockchain(vm *block.TestVM, blocks []*snowmantest.Block) {
vm.CantSetState = false
vm.LastAcceptedF = func(context.Context) (ids.ID, error) {
Expand Down

0 comments on commit 551f8d3

Please sign in to comment.