Skip to content

Commit

Permalink
Fix handling of stateReady in MakeChainIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbuchwald committed Dec 30, 2024
1 parent 92b73a8 commit fe04049
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
4 changes: 4 additions & 0 deletions statesync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ func (c *Client[T]) finish(ctx context.Context, err error) {
close(c.done)
}

func (c *Client[T]) MustStateSync() bool {
return c.mustStateSync
}

func (c *Client[T]) Done() <-chan error {
return c.done
}
Expand Down
31 changes: 18 additions & 13 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,9 @@ func (vm *VM) Initialize(
},
})

// Initialize the chain index before starting the syncer/builder/gossiper
// which will each read from the chain index
if err := vm.initChainStore(); err != nil {
return nil, err
}
lastAccepted, err := vm.initLastAccepted(ctx)
if err != nil {
return nil, err
}
// Switch away from true
chainIndex, err := makeChainIndex(ctx, vm.chainStore, lastAccepted, lastAccepted, true)
if err != nil {
return nil, err
}
vm.chainIndex = chainIndex

if err := vm.initStateSync(ctx); err != nil {
return nil, err
Expand Down Expand Up @@ -358,7 +346,24 @@ func (vm *VM) Initialize(
snowApp.WithHandler(api.Path, api.Handler)
}

// TODO: return false if we are mid state sync
// XXX: the VM requires access to the chainIndex returned by makeChainIndex. Passing a function that must be called this
// way is a bit awkward, but there's a bit of a chicken or egg problem to initialize it because it requires
// the last accepted block, ChainIndex, and flag indicating if the state is ready.
// One alternative is to separate Initialize into two functions where the extra function sets the chainIndex, but that's
// also rather awkward.
stateReady := !vm.SyncClient.MustStateSync()
var lastAccepted *chain.OutputBlock
if stateReady {
lastAccepted, err = vm.initLastAccepted(ctx)
if err != nil {
return nil, err
}
}
chainIndex, err := makeChainIndex(ctx, vm.chainStore, lastAccepted, lastAccepted, stateReady)
if err != nil {
return nil, err
}
vm.chainIndex = chainIndex
return vm.chainStore, nil
}

Expand Down

0 comments on commit fe04049

Please sign in to comment.