Skip to content

Commit

Permalink
✨ [channel, backend/eth] Add state and sigs to RegisteredEvent
Browse files Browse the repository at this point in the history
Preparation for virtual channel watching.

Signed-off-by: Matthias Geihs <[email protected]>
  • Loading branch information
matthiasgeihs committed Jun 16, 2021
1 parent e097d03 commit 7fd86bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions backend/ethereum/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
abiParams abi.Type
abiState abi.Type
abiProgress abi.Method
abiRegister abi.Method
)

func init() {
Expand All @@ -74,6 +75,10 @@ func init() {
if abiProgress, ok = adj.Methods["progress"]; !ok {
panic("Could not find method progress in adjudicator contract.")
}

if abiRegister, ok = adj.Methods["register"]; !ok {
panic("Could not find method register in adjudicator contract.")
}
}

// Backend implements the interface defined in channel/Backend.go.
Expand Down
70 changes: 69 additions & 1 deletion backend/ethereum/channel/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package channel

import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -179,7 +180,33 @@ func (a *Adjudicator) convertEvent(ctx context.Context, e *adjudicator.Adjudicat
base := channel.NewAdjudicatorEventBase(e.ChannelID, NewBlockTimeout(a.ContractInterface, e.Timeout), e.Version)
switch e.Phase {
case phaseDispute:
return &channel.RegisteredEvent{AdjudicatorEventBase: *base}, nil
args, err := a.fetchRegisterCallData(ctx, e.Raw.TxHash)
if err != nil {
return nil, errors.WithMessage(err, "fetching call data")
}

ch, ok := args.get(e.ChannelID)
if !ok {
return nil, fmt.Errorf("channel not found in calldata: %v", e.ChannelID)
}

var app channel.App
var zeroAddress common.Address
if ch.Params.App == zeroAddress {
app = channel.NoApp()
} else {
app, err = channel.Resolve(wallet.AsWalletAddr(ch.Params.App))
if err != nil {
return nil, err
}
}
state := FromEthState(app, &ch.State)

return &channel.RegisteredEvent{
AdjudicatorEventBase: *base,
State: &state,
Sigs: ch.Sigs,
}, nil

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

return &args, nil
}

type registerCallData struct {
Channel adjudicator.AdjudicatorSignedState
SubChannels []adjudicator.AdjudicatorSignedState
}

func (args *registerCallData) get(id channel.ID) (*adjudicator.AdjudicatorSignedState, bool) {
ch := &args.Channel
if ch.State.ChannelID == id {
return ch, true
}
for _, ch := range args.SubChannels {
if ch.State.ChannelID == id {
return &ch, true
}
}
return nil, false
}

func (a *Adjudicator) fetchRegisterCallData(ctx context.Context, txHash common.Hash) (*registerCallData, error) {
tx, _, err := a.ContractBackend.TransactionByHash(ctx, txHash)
if err != nil {
err = cherrors.CheckIsChainNotReachableError(err)
return nil, errors.WithMessage(err, "getting transaction")
}

argsData := tx.Data()[len(abiRegister.ID):]

argsI, err := abiRegister.Inputs.UnpackValues(argsData)
if err != nil {
return nil, errors.WithMessage(err, "unpacking")
}

var args registerCallData
err = abiRegister.Inputs.Copy(&args, argsI)
if err != nil {
return nil, errors.WithMessage(err, "copying into struct")
}

return &args, nil
}
2 changes: 2 additions & 0 deletions channel/adjudicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ type (
// registration on the blockchain.
RegisteredEvent struct {
AdjudicatorEventBase // Channel ID and Refutation phase timeout
State *State
Sigs []wallet.Sig
}

// ConcludedEvent signals channel conclusion.
Expand Down

0 comments on commit 7fd86bb

Please sign in to comment.