From 90d0136069d9f69fd711bdeaae8bcb98d46371e6 Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 27 Jan 2022 17:39:20 +0100 Subject: [PATCH] client: Validate state transition on force update Signed-off-by: Matthias Geihs --- channel/machine.go | 4 ++-- channel/statemachine.go | 2 +- client/adjudicate.go | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/channel/machine.go b/channel/machine.go index d1ef1b61..7a733915 100644 --- a/channel/machine.go +++ b/channel/machine.go @@ -475,7 +475,7 @@ func (m *machine) expect(tr PhaseTransition) error { return nil } -// validTransition checks that the transition from the current to the provided +// ValidTransition checks that the transition from the current to the provided // state is valid. The following checks are run: // * matching channel ids // * no transition from final state @@ -483,7 +483,7 @@ func (m *machine) expect(tr PhaseTransition) error { // * preservation of balances // A StateMachine will additionally check the validity of the app-specific // transition whereas an ActionMachine checks each Action as being valid. -func (m *machine) validTransition(to *State) error { +func (m *machine) ValidTransition(to *State) error { if to.ID != m.params.id { return errors.New("new state's ID doesn't match") } diff --git a/channel/statemachine.go b/channel/statemachine.go index f2ad6c4b..80a94c60 100644 --- a/channel/statemachine.go +++ b/channel/statemachine.go @@ -134,7 +134,7 @@ func (m *StateMachine) validTransition(to *State, actor Index) (err error) { if actor >= m.N() { return errors.New("actor index is out of range") } - if err := m.machine.validTransition(to); err != nil { + if err := m.machine.ValidTransition(to); err != nil { return err } diff --git a/client/adjudicate.go b/client/adjudicate.go index 60004a7d..afb77231 100644 --- a/client/adjudicate.go +++ b/client/adjudicate.go @@ -210,6 +210,11 @@ func (c *Channel) ForceUpdate(ctx context.Context, updater func(*channel.State)) updater(state) state.Version++ + // Check state transition. + if err := c.machine.ValidTransition(state); err != nil { + return errors.WithMessage(err, "validating state transition") + } + // Apply state in machine and generate signature if err := c.machine.SetProgressing(ctx, state); err != nil { return errors.WithMessage(err, "updating machine")