Skip to content

Commit

Permalink
Fix unexpected state transition and unmet mock expectations (#12345)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaidashenko authored Mar 7, 2024
1 parent 375ccb1 commit f8c448a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions common/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func (n *node[CHAIN_ID, HEAD, RPC]) verifyChainID(callerCtx context.Context, lgg

st := n.State()
switch st {
case nodeStateClosed:
// The node is already closed, and any subsequent transition is invalid.
// To make spotting such transitions a bit easier, return the invalid node state.
return nodeStateLen
case nodeStateDialed, nodeStateOutOfSync, nodeStateInvalidChainID, nodeStateSyncing:
default:
panic(fmt.Sprintf("cannot verify node in state %v", st))
Expand Down
3 changes: 3 additions & 0 deletions common/client/node_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ func (n *node[CHAIN_ID, HEAD, RPC]) transitionToUnreachable(fn func()) {
}

func (n *node[CHAIN_ID, HEAD, RPC]) declareState(state nodeState) {
if n.State() == nodeStateClosed {
return
}
switch state {
case nodeStateInvalidChainID:
n.declareInvalidChainID()
Expand Down
4 changes: 2 additions & 2 deletions common/client/node_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,8 @@ func TestUnit_NodeLifecycle_unreachableLoop(t *testing.T) {
})
defer func() { assert.NoError(t, node.close()) }()

rpc.On("Dial", mock.Anything).Return(nil).Twice()
rpc.On("ChainID", mock.Anything).Return(nodeChainID, nil).Twice()
rpc.On("Dial", mock.Anything).Return(nil)
rpc.On("ChainID", mock.Anything).Return(nodeChainID, nil)
rpc.On("IsSyncing", mock.Anything).Return(true, nil)

setupRPCForAliveLoop(t, rpc)
Expand Down

0 comments on commit f8c448a

Please sign in to comment.