Skip to content

Commit

Permalink
💥 Channel.Update/ForceUpdate: updater no longer returns error
Browse files Browse the repository at this point in the history
There seemed to be no use case and usage is more concise now.

Signed-off-by: Matthias Geihs <[email protected]>
  • Loading branch information
matthiasgeihs committed Jan 27, 2022
1 parent ba076a9 commit 37dfdbf
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func main() {
}()

// send a channel update request to the other channel peer(s)
err = ch.Update(ctx, func(s *channel.State) error {
err = ch.Update(ctx, func(s *channel.State) {
// update state s, e.g., moving funds or changing app data
})
if err != nil { /* handle error */ }
Expand Down
18 changes: 9 additions & 9 deletions client/adjudicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,19 @@ func (c *Channel) registerDispute(ctx context.Context) error {
return nil
}

// ForceUpdate enforces a channel update on the adjudicator. `update` gets as
// input a copy of the current state and is expected to modify the state as
// intended. It should not to increment the version number as this is
// automatically.
// ForceUpdate enforces a state update through the adjudicator as specified by
// the `updater` function.
//
// The updater function must adhere to the following rules:
// - The version must not be changed. It is incremented automatically.
// - The assets must not be changed.
// - The locked allocation must not be changed.
//
// Returns TxTimedoutError when the program times out waiting for a transaction
// to be mined. Returns ChainNotReachableError if the connection to the
// blockchain network fails when sending a transaction to / reading from the
// blockchain.
func (c *Channel) ForceUpdate(ctx context.Context, update func(*channel.State) error) error {
func (c *Channel) ForceUpdate(ctx context.Context, updater func(*channel.State)) error {
err := c.ensureRegistered(ctx)
if err != nil {
return err
Expand All @@ -204,11 +207,8 @@ func (c *Channel) ForceUpdate(ctx context.Context, update func(*channel.State) e

// Update state
state := ar.Tx.State.Clone()
updater(state)
state.Version++
err = update(state)
if err != nil {
return errors.WithMessage(err, "updating state")
}

// Apply state in machine and generate signature
if err := c.machine.SetProgressing(ctx, state); err != nil {
Expand Down
10 changes: 4 additions & 6 deletions client/test/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,20 @@ func (ch *paymentChannel) acceptSubchannel(
return subchannel
}

func (ch *paymentChannel) sendUpdate(update func(*channel.State) error, desc string) {
func (ch *paymentChannel) sendUpdate(updater func(*channel.State), desc string) {
ch.log.Debugf("Sending update: %s", desc)
ctx, cancel := context.WithTimeout(context.Background(), ch.r.timeout)
defer cancel()

err := ch.Update(ctx, update)
err := ch.Update(ctx, updater)
ch.log.Infof("Sent update: %s, err: %v", desc, err)
assert.NoError(ch.r.t, err)
}

func (ch *paymentChannel) sendTransfer(amount channel.Bal, desc string) {
ch.sendUpdate(
func(state *channel.State) error {
func(state *channel.State) {
transferBal(stateBals(state), ch.Idx(), amount)
return nil
},
desc,
)
Expand Down Expand Up @@ -157,9 +156,8 @@ func (ch *paymentChannel) assertBals(state *channel.State) {
}

func (ch *paymentChannel) sendFinal() {
ch.sendUpdate(func(state *channel.State) error {
ch.sendUpdate(func(state *channel.State) {
state.IsFinal = true
return nil
}, "final")
assert.True(ch.r.t, ch.State().IsFinal)
}
Expand Down
6 changes: 2 additions & 4 deletions client/test/progression.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ func (r *Paul) exec(_cfg ExecConfig, ch *paymentChannel) {
r.waitStage() // wait for setup complete

// progress
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) error {
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) {
bal := func(user channel.Index) int64 {
return s.Balances[assetIdx][user].Int64()
}
half := (bal(0) + bal(1)) / 2 //nolint:gomnd
s.Balances[assetIdx][0] = big.NewInt(half)
s.Balances[assetIdx][1] = big.NewInt(half)
return nil
}))

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

// we progress
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) error {
assert.NoError(ch.ForceUpdate(ctx, func(s *channel.State) {
bal := func(user channel.Index) int64 {
return s.Balances[assetIdx][user].Int64()
}
half := (bal(0) + bal(1)) / 2 //nolint:gomnd
s.Balances[assetIdx][0] = big.NewInt(half + paulPaulaBalTransferAmount)
s.Balances[assetIdx][1] = big.NewInt(half - paulPaulaBalTransferAmount)
return nil
}))

// await our progression confirmation
Expand Down
20 changes: 11 additions & 9 deletions client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,18 @@ func (r *UpdateResponder) Reject(ctx context.Context, reason string) error {
return r.channel.handleUpdateRej(ctx, r.pidx, r.req, reason)
}

// Update updates the channel state using the update function and proposes the
// new state to all other channel participants. The update function must not
// update the version counter.
// Update proposes a state update to the channel participants as
// specified by the `updater` function.
//
// The updater function must adhere to the following rules:
// - The version must not be changed. It is incremented automatically.
// - The assets must not be changed.
// - The locked allocation must not be changed.
//
// Returns nil if all peers accept the update. Returns RequestTimedOutError if
// any peer did not respond before the context expires or is cancelled. Returns
// an error if any runtime error occurs or any peer rejects the update.
func (c *Channel) Update(ctx context.Context, update func(*channel.State) error) (err error) {
func (c *Channel) Update(ctx context.Context, updater func(*channel.State)) error {
if ctx == nil {
return errors.New("context must not be nil")
}
Expand All @@ -152,9 +156,7 @@ func (c *Channel) Update(ctx context.Context, update func(*channel.State) error)
return c.update(ctx,
func(state *channel.State) error {
// apply update
if err := update(state); err != nil {
return err
}
updater(state)

// validate
return c.validTwoPartyUpdateState(state)
Expand Down Expand Up @@ -239,9 +241,9 @@ func (c *Channel) checkUpdateError(ctx context.Context, updateErr error) {
}
}

func (c *Channel) update(ctx context.Context, update func(*channel.State) error) (err error) {
func (c *Channel) update(ctx context.Context, updater func(*channel.State) error) (err error) {
state := c.machine.State().Clone()
if err := update(state); err != nil {
if err := updater(state); err != nil {
return err
}
state.Version++
Expand Down
6 changes: 2 additions & 4 deletions client/virtual_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,13 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
t.Fatalf("Error in go-routine: %v", err)
}

err = vct.chAliceBob.Update(ctx, func(s *channel.State) error {
err = vct.chAliceBob.Update(ctx, func(s *channel.State) {
s.Balances = channel.Balances{vct.virtualBalsUpdated}
return nil
})
require.NoError(err, "updating virtual channel")

err = vct.chAliceBob.Update(ctx, func(s *channel.State) error {
err = vct.chAliceBob.Update(ctx, func(s *channel.State) {
s.IsFinal = true
return nil
})
require.NoError(err, "updating virtual channel")
return vct
Expand Down

0 comments on commit 37dfdbf

Please sign in to comment.