Skip to content

Commit a85d0ac

Browse files
committed
Client: Adapt ForceUpdate to Update
Update allowed to cancel the update process by letting the update function return an error. ForceUpdate didn't allow that. Since the two functions are otherwise very similar, we now also make their function signature consistent. Signed-off-by: Matthias Geihs <matthias@perun.network>
1 parent 2f51214 commit a85d0ac

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

client/adjudicate.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,16 @@ func (c *Channel) registerDispute(ctx context.Context) error {
178178
return nil
179179
}
180180

181-
// ForceUpdate enforces a channel update on the adjudicator.
181+
// ForceUpdate enforces a channel update on the adjudicator. `update` gets as
182+
// input a copy of the current state and is expected to modify the state as
183+
// intended. It should not to increment the version number as this is
184+
// automatically.
182185
//
183186
// Returns TxTimedoutError when the program times out waiting for a transaction
184-
// to be mined.
185-
// Returns ChainNotReachableError if the connection to the blockchain network
186-
// fails when sending a transaction to / reading from the blockchain.
187-
func (c *Channel) ForceUpdate(ctx context.Context, update func(*channel.State)) error {
187+
// to be mined. Returns ChainNotReachableError if the connection to the
188+
// blockchain network fails when sending a transaction to / reading from the
189+
// blockchain.
190+
func (c *Channel) ForceUpdate(ctx context.Context, update func(*channel.State) error) error {
188191
err := c.ensureRegistered(ctx)
189192
if err != nil {
190193
return err
@@ -200,9 +203,12 @@ func (c *Channel) ForceUpdate(ctx context.Context, update func(*channel.State))
200203
ar := c.machine.AdjudicatorReq()
201204

202205
// Update state
203-
state := c.machine.State().Clone()
206+
state := ar.Tx.State.Clone()
204207
state.Version++
205-
update(state)
208+
err = update(state)
209+
if err != nil {
210+
return errors.WithMessage(err, "updating state")
211+
}
206212

207213
// Apply state in machine and generate signature
208214
if err := c.machine.SetProgressing(ctx, state); err != nil {

client/test/progression.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ func (r *Paul) exec(_cfg ExecConfig, ch *paymentChannel) {
9595
r.waitStage() // wait for setup complete
9696

9797
// progress
98-
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) {
98+
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) error {
9999
bal := func(user channel.Index) int64 {
100100
return s.Balances[assetIdx][user].Int64()
101101
}
102102
half := (bal(0) + bal(1)) / 2 //nolint:gomnd
103103
s.Balances[assetIdx][0] = big.NewInt(half)
104104
s.Balances[assetIdx][1] = big.NewInt(half)
105+
return nil
105106
}))
106107

107108
// await our progression confirmation
@@ -166,13 +167,14 @@ func (r *Paula) exec(_cfg ExecConfig, ch *paymentChannel, _ *acceptNextPropHandl
166167
r.t.Logf("%v received progression confirmation 1", r.setup.Name)
167168

168169
// we progress
169-
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) {
170+
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) error {
170171
bal := func(user channel.Index) int64 {
171172
return s.Balances[assetIdx][user].Int64()
172173
}
173174
half := (bal(0) + bal(1)) / 2 //nolint:gomnd
174175
s.Balances[assetIdx][0] = big.NewInt(half + paulPaulaBalTransferAmount)
175176
s.Balances[assetIdx][1] = big.NewInt(half - paulPaulaBalTransferAmount)
177+
return nil
176178
}))
177179

178180
// await our progression confirmation

0 commit comments

Comments
 (0)