Skip to content

Commit 7fd86bb

Browse files
committed
✨ [channel, backend/eth] Add state and sigs to RegisteredEvent
Preparation for virtual channel watching. Signed-off-by: Matthias Geihs <[email protected]>
1 parent e097d03 commit 7fd86bb

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

backend/ethereum/channel/backend.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
abiParams abi.Type
5050
abiState abi.Type
5151
abiProgress abi.Method
52+
abiRegister abi.Method
5253
)
5354

5455
func init() {
@@ -74,6 +75,10 @@ func init() {
7475
if abiProgress, ok = adj.Methods["progress"]; !ok {
7576
panic("Could not find method progress in adjudicator contract.")
7677
}
78+
79+
if abiRegister, ok = adj.Methods["register"]; !ok {
80+
panic("Could not find method register in adjudicator contract.")
81+
}
7782
}
7883

7984
// Backend implements the interface defined in channel/Backend.go.

backend/ethereum/channel/subscription.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package channel
1616

1717
import (
1818
"context"
19+
"fmt"
1920
"math/big"
2021

2122
"github.com/ethereum/go-ethereum"
@@ -179,7 +180,33 @@ func (a *Adjudicator) convertEvent(ctx context.Context, e *adjudicator.Adjudicat
179180
base := channel.NewAdjudicatorEventBase(e.ChannelID, NewBlockTimeout(a.ContractInterface, e.Timeout), e.Version)
180181
switch e.Phase {
181182
case phaseDispute:
182-
return &channel.RegisteredEvent{AdjudicatorEventBase: *base}, nil
183+
args, err := a.fetchRegisterCallData(ctx, e.Raw.TxHash)
184+
if err != nil {
185+
return nil, errors.WithMessage(err, "fetching call data")
186+
}
187+
188+
ch, ok := args.get(e.ChannelID)
189+
if !ok {
190+
return nil, fmt.Errorf("channel not found in calldata: %v", e.ChannelID)
191+
}
192+
193+
var app channel.App
194+
var zeroAddress common.Address
195+
if ch.Params.App == zeroAddress {
196+
app = channel.NoApp()
197+
} else {
198+
app, err = channel.Resolve(wallet.AsWalletAddr(ch.Params.App))
199+
if err != nil {
200+
return nil, err
201+
}
202+
}
203+
state := FromEthState(app, &ch.State)
204+
205+
return &channel.RegisteredEvent{
206+
AdjudicatorEventBase: *base,
207+
State: &state,
208+
Sigs: ch.Sigs,
209+
}, nil
183210

184211
case phaseForceExec:
185212
args, err := a.fetchProgressCallData(ctx, e.Raw.TxHash)
@@ -235,3 +262,44 @@ func (a *Adjudicator) fetchProgressCallData(ctx context.Context, txHash common.H
235262

236263
return &args, nil
237264
}
265+
266+
type registerCallData struct {
267+
Channel adjudicator.AdjudicatorSignedState
268+
SubChannels []adjudicator.AdjudicatorSignedState
269+
}
270+
271+
func (args *registerCallData) get(id channel.ID) (*adjudicator.AdjudicatorSignedState, bool) {
272+
ch := &args.Channel
273+
if ch.State.ChannelID == id {
274+
return ch, true
275+
}
276+
for _, ch := range args.SubChannels {
277+
if ch.State.ChannelID == id {
278+
return &ch, true
279+
}
280+
}
281+
return nil, false
282+
}
283+
284+
func (a *Adjudicator) fetchRegisterCallData(ctx context.Context, txHash common.Hash) (*registerCallData, error) {
285+
tx, _, err := a.ContractBackend.TransactionByHash(ctx, txHash)
286+
if err != nil {
287+
err = cherrors.CheckIsChainNotReachableError(err)
288+
return nil, errors.WithMessage(err, "getting transaction")
289+
}
290+
291+
argsData := tx.Data()[len(abiRegister.ID):]
292+
293+
argsI, err := abiRegister.Inputs.UnpackValues(argsData)
294+
if err != nil {
295+
return nil, errors.WithMessage(err, "unpacking")
296+
}
297+
298+
var args registerCallData
299+
err = abiRegister.Inputs.Copy(&args, argsI)
300+
if err != nil {
301+
return nil, errors.WithMessage(err, "copying into struct")
302+
}
303+
304+
return &args, nil
305+
}

channel/adjudicator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ type (
144144
// registration on the blockchain.
145145
RegisteredEvent struct {
146146
AdjudicatorEventBase // Channel ID and Refutation phase timeout
147+
State *State
148+
Sigs []wallet.Sig
147149
}
148150

149151
// ConcludedEvent signals channel conclusion.

0 commit comments

Comments
 (0)