refactor: use struct instead of array for state change
This commit is contained in:
parent
b62fef7541
commit
95a9dc9db0
@ -387,20 +387,23 @@ func (we *watcherEvents) checkStateChanges(oldState, newState *types.TipSet) map
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used to store the state change
|
// Used to store the state for a stateChange
|
||||||
type stateChange interface{}
|
type stateData interface{}
|
||||||
|
|
||||||
// Will be checked to ensure it has length 2
|
// A change in state from -> to
|
||||||
type stateData []stateChange
|
type stateChange struct {
|
||||||
|
from stateData
|
||||||
|
to stateData
|
||||||
|
}
|
||||||
|
|
||||||
// StateChangeHandler arguments:
|
// StateChangeHandler arguments:
|
||||||
// `oldTs` is the state "from" tipset
|
// `oldTs` is the state "from" tipset
|
||||||
// `newTs` is the state "to" tipset
|
// `newTs` is the state "to" tipset
|
||||||
// `data` is the old / new state
|
// `states` is the old / new state
|
||||||
// `curH`-`ts.Height` = `confidence`
|
// `curH`-`ts.Height` = `confidence`
|
||||||
type StateChangeHandler func(oldTs, newTs *types.TipSet, data stateData, curH abi.ChainEpoch) (more bool, err error)
|
type StateChangeHandler func(oldTs, newTs *types.TipSet, states *stateChange, curH abi.ChainEpoch) (more bool, err error)
|
||||||
|
|
||||||
type StateMatchFunc func(oldTs, newTs *types.TipSet) (bool, stateData, error)
|
type StateMatchFunc func(oldTs, newTs *types.TipSet) (bool, *stateChange, error)
|
||||||
|
|
||||||
// StateChanged registers a callback which is triggered when a specified state
|
// StateChanged registers a callback which is triggered when a specified state
|
||||||
// change occurs or a timeout is reached.
|
// change occurs or a timeout is reached.
|
||||||
@ -432,9 +435,9 @@ type StateMatchFunc func(oldTs, newTs *types.TipSet) (bool, stateData, error)
|
|||||||
// `StateChangeHandler` is called)
|
// `StateChangeHandler` is called)
|
||||||
func (we *watcherEvents) StateChanged(check CheckFunc, scHnd StateChangeHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch, mf StateMatchFunc) error {
|
func (we *watcherEvents) StateChanged(check CheckFunc, scHnd StateChangeHandler, rev RevertHandler, confidence int, timeout abi.ChainEpoch, mf StateMatchFunc) error {
|
||||||
hnd := func(data eventData, prevTs, ts *types.TipSet, height abi.ChainEpoch) (bool, error) {
|
hnd := func(data eventData, prevTs, ts *types.TipSet, height abi.ChainEpoch) (bool, error) {
|
||||||
states, ok := data.(stateData)
|
states, ok := data.(*stateChange)
|
||||||
if data != nil && (!ok || len(states) != 2) {
|
if data != nil && !ok {
|
||||||
panic("StateChangeHandler: stateData passed to watcher must be a 2 element array: [old state, new state]")
|
panic("expected *stateChange")
|
||||||
}
|
}
|
||||||
|
|
||||||
return scHnd(prevTs, ts, states, height)
|
return scHnd(prevTs, ts, states, height)
|
||||||
|
@ -1081,18 +1081,18 @@ func TestStateChanged(t *testing.T) {
|
|||||||
|
|
||||||
more := true
|
more := true
|
||||||
var applied, reverted bool
|
var applied, reverted bool
|
||||||
var appliedData stateData
|
var appliedData *stateChange
|
||||||
var appliedOldTs *types.TipSet
|
var appliedOldTs *types.TipSet
|
||||||
var appliedNewTs *types.TipSet
|
var appliedNewTs *types.TipSet
|
||||||
var appliedH abi.ChainEpoch
|
var appliedH abi.ChainEpoch
|
||||||
var matchData stateData
|
var matchData *stateChange
|
||||||
|
|
||||||
confidence := 3
|
confidence := 3
|
||||||
timeout := abi.ChainEpoch(20)
|
timeout := abi.ChainEpoch(20)
|
||||||
|
|
||||||
err := events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
err := events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
||||||
return false, true, nil
|
return false, true, nil
|
||||||
}, func(oldTs, newTs *types.TipSet, data stateData, curH abi.ChainEpoch) (bool, error) {
|
}, func(oldTs, newTs *types.TipSet, data *stateChange, curH abi.ChainEpoch) (bool, error) {
|
||||||
require.Equal(t, false, applied)
|
require.Equal(t, false, applied)
|
||||||
applied = true
|
applied = true
|
||||||
appliedData = data
|
appliedData = data
|
||||||
@ -1103,7 +1103,7 @@ func TestStateChanged(t *testing.T) {
|
|||||||
}, func(_ context.Context, ts *types.TipSet) error {
|
}, func(_ context.Context, ts *types.TipSet) error {
|
||||||
reverted = true
|
reverted = true
|
||||||
return nil
|
return nil
|
||||||
}, confidence, timeout, func(oldTs, newTs *types.TipSet) (bool, stateData, error) {
|
}, confidence, timeout, func(oldTs, newTs *types.TipSet) (bool, *stateChange, error) {
|
||||||
if matchData == nil {
|
if matchData == nil {
|
||||||
return false, matchData, nil
|
return false, matchData, nil
|
||||||
}
|
}
|
||||||
@ -1121,7 +1121,7 @@ func TestStateChanged(t *testing.T) {
|
|||||||
require.Equal(t, false, reverted)
|
require.Equal(t, false, reverted)
|
||||||
|
|
||||||
// create state change (but below confidence threshold)
|
// create state change (but below confidence threshold)
|
||||||
matchData = []stateChange{"a", "b"}
|
matchData = &stateChange{from: "a", to: "b"}
|
||||||
fcs.advance(0, 3, nil)
|
fcs.advance(0, 3, nil)
|
||||||
|
|
||||||
require.Equal(t, false, applied)
|
require.Equal(t, false, applied)
|
||||||
@ -1149,8 +1149,8 @@ func TestStateChanged(t *testing.T) {
|
|||||||
require.Equal(t, abi.ChainEpoch(9), appliedH)
|
require.Equal(t, abi.ChainEpoch(9), appliedH)
|
||||||
|
|
||||||
// Make sure the state change was correctly passed through
|
// Make sure the state change was correctly passed through
|
||||||
require.Equal(t, "a", appliedData[0])
|
require.Equal(t, "a", appliedData.from)
|
||||||
require.Equal(t, "b", appliedData[1])
|
require.Equal(t, "b", appliedData.to)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStateChangedRevert(t *testing.T) {
|
func TestStateChangedRevert(t *testing.T) {
|
||||||
@ -1168,21 +1168,21 @@ func TestStateChangedRevert(t *testing.T) {
|
|||||||
|
|
||||||
more := true
|
more := true
|
||||||
var applied, reverted bool
|
var applied, reverted bool
|
||||||
var matchData stateData
|
var matchData *stateChange
|
||||||
|
|
||||||
confidence := 1
|
confidence := 1
|
||||||
timeout := abi.ChainEpoch(20)
|
timeout := abi.ChainEpoch(20)
|
||||||
|
|
||||||
err := events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
err := events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
||||||
return false, true, nil
|
return false, true, nil
|
||||||
}, func(oldTs, newTs *types.TipSet, data stateData, curH abi.ChainEpoch) (bool, error) {
|
}, func(oldTs, newTs *types.TipSet, data *stateChange, curH abi.ChainEpoch) (bool, error) {
|
||||||
require.Equal(t, false, applied)
|
require.Equal(t, false, applied)
|
||||||
applied = true
|
applied = true
|
||||||
return more, nil
|
return more, nil
|
||||||
}, func(_ context.Context, ts *types.TipSet) error {
|
}, func(_ context.Context, ts *types.TipSet) error {
|
||||||
reverted = true
|
reverted = true
|
||||||
return nil
|
return nil
|
||||||
}, confidence, timeout, func(oldTs, newTs *types.TipSet) (bool, stateData, error) {
|
}, confidence, timeout, func(oldTs, newTs *types.TipSet) (bool, *stateChange, error) {
|
||||||
if matchData == nil {
|
if matchData == nil {
|
||||||
return false, matchData, nil
|
return false, matchData, nil
|
||||||
}
|
}
|
||||||
@ -1196,7 +1196,7 @@ func TestStateChangedRevert(t *testing.T) {
|
|||||||
fcs.advance(0, 2, nil) // H=3
|
fcs.advance(0, 2, nil) // H=3
|
||||||
|
|
||||||
// Make a state change from TS at height 3 to TS at height 4
|
// Make a state change from TS at height 3 to TS at height 4
|
||||||
matchData = []stateChange{"a", "b"}
|
matchData = &stateChange{from: "a", to: "b"}
|
||||||
fcs.advance(0, 1, nil) // H=4
|
fcs.advance(0, 1, nil) // H=4
|
||||||
|
|
||||||
// Haven't yet reached confidence
|
// Haven't yet reached confidence
|
||||||
@ -1248,7 +1248,7 @@ func TestStateChangedTimeout(t *testing.T) {
|
|||||||
|
|
||||||
err := events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
err := events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
||||||
return false, true, nil
|
return false, true, nil
|
||||||
}, func(oldTs, newTs *types.TipSet, data stateData, curH abi.ChainEpoch) (bool, error) {
|
}, func(oldTs, newTs *types.TipSet, data *stateChange, curH abi.ChainEpoch) (bool, error) {
|
||||||
called = true
|
called = true
|
||||||
require.Nil(t, data)
|
require.Nil(t, data)
|
||||||
require.Equal(t, abi.ChainEpoch(20), newTs.Height())
|
require.Equal(t, abi.ChainEpoch(20), newTs.Height())
|
||||||
@ -1257,8 +1257,8 @@ func TestStateChangedTimeout(t *testing.T) {
|
|||||||
}, func(_ context.Context, ts *types.TipSet) error {
|
}, func(_ context.Context, ts *types.TipSet) error {
|
||||||
t.Fatal("revert on timeout")
|
t.Fatal("revert on timeout")
|
||||||
return nil
|
return nil
|
||||||
}, 3, 20, func(oldTs, newTs *types.TipSet) (bool, stateData, error) {
|
}, 3, 20, func(oldTs, newTs *types.TipSet) (bool, *stateChange, error) {
|
||||||
return false, stateData{}, nil
|
return false, nil, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -1286,7 +1286,7 @@ func TestStateChangedTimeout(t *testing.T) {
|
|||||||
|
|
||||||
err = events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
err = events.StateChanged(func(ts *types.TipSet) (d bool, m bool, e error) {
|
||||||
return true, true, nil
|
return true, true, nil
|
||||||
}, func(oldTs, newTs *types.TipSet, data stateData, curH abi.ChainEpoch) (bool, error) {
|
}, func(oldTs, newTs *types.TipSet, data *stateChange, curH abi.ChainEpoch) (bool, error) {
|
||||||
called = true
|
called = true
|
||||||
require.Nil(t, data)
|
require.Nil(t, data)
|
||||||
require.Equal(t, abi.ChainEpoch(20), newTs.Height())
|
require.Equal(t, abi.ChainEpoch(20), newTs.Height())
|
||||||
@ -1295,8 +1295,8 @@ func TestStateChangedTimeout(t *testing.T) {
|
|||||||
}, func(_ context.Context, ts *types.TipSet) error {
|
}, func(_ context.Context, ts *types.TipSet) error {
|
||||||
t.Fatal("revert on timeout")
|
t.Fatal("revert on timeout")
|
||||||
return nil
|
return nil
|
||||||
}, 3, 20, func(oldTs, newTs *types.TipSet) (bool, stateData, error) {
|
}, 3, 20, func(oldTs, newTs *types.TipSet) (bool, *stateChange, error) {
|
||||||
return false, stateData{}, nil
|
return false, nil, nil
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user