Skip to content

Commit 946ef50

Browse files
committed
👌 [backend/eth/channel|sub] Apply review fixes.
Signed-off-by: Oliver Tale-Yazdi <oliver@perun.network>
1 parent 4fe84c8 commit 946ef50

File tree

7 files changed

+70
-36
lines changed

7 files changed

+70
-36
lines changed

backend/ethereum/bindings/abi.go

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package bindings
1616

1717
import (
18+
"fmt"
1819
"strings"
1920

2021
"github.com/ethereum/go-ethereum/accounts/abi"
@@ -27,37 +28,69 @@ import (
2728
"perun.network/go-perun/backend/ethereum/bindings/trivialapp"
2829
)
2930

30-
// This file contains all the parsed ABI definitions of our contracts.
31+
// ABI contains all the parsed ABI definitions of our contracts.
3132
// Use it together with `bind.NewBoundContract` to create a bound contract.
33+
var ABI = struct {
34+
// ERC20Token is the parsed ABI definition of contract ERC20Token.
35+
ERC20Token abi.ABI
36+
// Adjudicator is the parsed ABI definition of contract Adjudicator.
37+
Adjudicator abi.ABI
38+
// AssetHolder is the parsed ABI definition of contract AssetHolder.
39+
AssetHolder abi.ABI
40+
// ETHAssetHolder is the parsed ABI definition of contract ETHAssetHolder.
41+
ETHAssetHolder abi.ABI
42+
// ERC20AssetHolder is the parsed ABI definition of contract ERC20AssetHolder.
43+
ERC20AssetHolder abi.ABI
44+
// TrivialApp is the parsed ABI definition of contract TrivialApp.
45+
TrivialApp abi.ABI
46+
}{}
3247

33-
var (
34-
// ERC20TokenABI is the parsed ABI definition of contract ERC20Token.
35-
ERC20TokenABI abi.ABI
36-
// AdjudicatorABI is the parsed ABI definition of contract Adjudicator.
37-
AdjudicatorABI abi.ABI
38-
// AssetHolderABI is the parsed ABI definition of contract AssetHolder.
39-
AssetHolderABI abi.ABI
40-
// ETHAssetHolderABI is the parsed ABI definition of contract ETHAssetHolder.
41-
ETHAssetHolderABI abi.ABI
42-
// ERC20AssetHolderABI is the parsed ABI definition of contract ERC20AssetHolder.
43-
ERC20AssetHolderABI abi.ABI
44-
// TrivialAppABI is the parsed ABI definition of contract TrivialApp.
45-
TrivialAppABI abi.ABI
46-
)
48+
// Events contains the event names for specific events.
49+
var Events = struct {
50+
// AdjChannelUpdate is the ChannelUpdate event of the Adjudicator contract.
51+
AdjChannelUpdate string
52+
// AhDeposited is the Deposited event of the Assetholder contract.
53+
AhDeposited string
54+
// AhWithdrawn is the Withdrawn event of the Assetholder contract.
55+
AhWithdrawn string
56+
// ERC20Approval is the Approval event of the ERC20Token contract.
57+
ERC20Approval string
58+
}{}
4759

4860
func init() {
49-
parseABI := func(raw string) abi.ABI {
61+
parseABIs()
62+
extractEvents()
63+
}
64+
65+
func parseABIs() {
66+
parse := func(raw string) abi.ABI {
5067
abi, err := abi.JSON(strings.NewReader(raw))
5168
if err != nil {
5269
panic(err)
5370
}
5471
return abi
5572
}
5673

57-
ERC20TokenABI = parseABI(peruntoken.ERC20ABI)
58-
AdjudicatorABI = parseABI(adjudicator.AdjudicatorABI)
59-
AssetHolderABI = parseABI(assetholder.AssetHolderABI)
60-
ETHAssetHolderABI = parseABI(assetholdereth.AssetHolderETHABI)
61-
ERC20AssetHolderABI = parseABI(assetholdererc20.AssetHolderERC20ABI)
62-
TrivialAppABI = parseABI(trivialapp.TrivialAppABI)
74+
ABI.ERC20Token = parse(peruntoken.ERC20ABI)
75+
ABI.Adjudicator = parse(adjudicator.AdjudicatorABI)
76+
ABI.AssetHolder = parse(assetholder.AssetHolderABI)
77+
ABI.ETHAssetHolder = parse(assetholdereth.AssetHolderETHABI)
78+
ABI.ERC20AssetHolder = parse(assetholdererc20.AssetHolderERC20ABI)
79+
ABI.TrivialApp = parse(trivialapp.TrivialAppABI)
80+
}
81+
82+
// extractEvents sets the event names and panics if any event does not exist.
83+
func extractEvents() {
84+
extract := func(abi abi.ABI, eName string) string {
85+
e, ok := abi.Events[eName]
86+
if !ok {
87+
panic(fmt.Sprintf("Event '%s' not found.", eName))
88+
}
89+
return e.Name
90+
}
91+
92+
Events.AdjChannelUpdate = extract(ABI.Adjudicator, "ChannelUpdate")
93+
Events.AhDeposited = extract(ABI.AssetHolder, "Deposited")
94+
Events.AhWithdrawn = extract(ABI.AssetHolder, "Withdrawn")
95+
Events.ERC20Approval = extract(ABI.ERC20Token, "Approval")
6396
}

backend/ethereum/channel/adjudicator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func NewAdjudicator(backend ContractBackend, contract common.Address, receiver c
5959
if err != nil {
6060
panic("Could not create a new instance of adjudicator")
6161
}
62-
bound := bind.NewBoundContract(contract, bindings.AdjudicatorABI, backend, backend, backend)
62+
bound := bind.NewBoundContract(contract, bindings.ABI.Adjudicator, backend, backend, backend)
6363
return &Adjudicator{
6464
ContractBackend: backend,
6565
contract: contr,

backend/ethereum/channel/conclude.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/ethereum/go-ethereum/core/types"
2222
"github.com/pkg/errors"
2323

24+
"perun.network/go-perun/backend/ethereum/bindings"
2425
"perun.network/go-perun/backend/ethereum/bindings/adjudicator"
2526
cherrors "perun.network/go-perun/backend/ethereum/channel/errors"
2627
"perun.network/go-perun/backend/ethereum/subscription"
@@ -79,11 +80,9 @@ func (a *Adjudicator) ensureConcluded(ctx context.Context, req channel.Adjudicat
7980

8081
// No conclude event found in the past, send transaction.
8182
if req.Tx.IsFinal {
82-
err = cherrors.CheckIsChainNotReachableError(a.callConcludeFinal(ctx, req))
83-
err = errors.WithMessage(err, "calling concludeFinal")
83+
err = errors.WithMessage(a.callConcludeFinal(ctx, req), "calling concludeFinal")
8484
} else {
85-
err = cherrors.CheckIsChainNotReachableError(a.callConclude(ctx, req, subStates))
86-
err = errors.WithMessage(err, "calling conclude")
85+
err = errors.WithMessage(a.callConclude(ctx, req, subStates), "calling conclude")
8786
}
8887
if IsErrTxFailed(err) {
8988
a.log.Warn("Calling conclude(Final) failed, waiting for event anyways...")
@@ -112,7 +111,7 @@ func (a *Adjudicator) ensureConcluded(ctx context.Context, req channel.Adjudicat
112111
func updateEventType(channelID [32]byte) subscription.EventFactory {
113112
return func() *subscription.Event {
114113
return &subscription.Event{
115-
Name: "ChannelUpdate",
114+
Name: bindings.Events.AdjChannelUpdate,
116115
Data: new(adjudicator.AdjudicatorChannelUpdate),
117116
// In the best case we could already filter for 'Concluded' phase only here.
118117
Filter: [][]interface{}{{channelID}},

backend/ethereum/channel/funder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/crypto"
2727
"github.com/pkg/errors"
2828

29+
"perun.network/go-perun/backend/ethereum/bindings"
2930
"perun.network/go-perun/backend/ethereum/bindings/assetholder"
3031
"perun.network/go-perun/backend/ethereum/subscription"
3132
"perun.network/go-perun/backend/ethereum/wallet"
@@ -240,7 +241,7 @@ func (f *Funder) depositedSub(ctx context.Context, contract *bind.BoundContract,
240241
}
241242
event := func() *subscription.Event {
242243
return &subscription.Event{
243-
Name: "Deposited",
244+
Name: bindings.Events.AhDeposited,
244245
Data: new(assetholder.AssetHolderDeposited),
245246
Filter: [][]interface{}{filter},
246247
}

backend/ethereum/channel/subscription.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/pkg/errors"
2525

26+
"perun.network/go-perun/backend/ethereum/bindings"
2627
"perun.network/go-perun/backend/ethereum/bindings/adjudicator"
2728
cherrors "perun.network/go-perun/backend/ethereum/channel/errors"
2829
"perun.network/go-perun/backend/ethereum/subscription"
@@ -36,7 +37,7 @@ func (a *Adjudicator) Subscribe(ctx context.Context, params *channel.Params) (ch
3637
events := make(chan *subscription.Event, 10)
3738
eFact := func() *subscription.Event {
3839
return &subscription.Event{
39-
Name: "ChannelUpdate",
40+
Name: bindings.Events.AdjChannelUpdate,
4041
Data: new(adjudicator.AdjudicatorChannelUpdate),
4142
Filter: [][]interface{}{{params.ID()}},
4243
}

backend/ethereum/channel/withdraw.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (a *Adjudicator) ensureWithdrawn(ctx context.Context, req channel.Adjudicat
105105
func withdrawnEventType(fundingID [32]byte) subscription.EventFactory {
106106
return func() *subscription.Event {
107107
return &subscription.Event{
108-
Name: "Withdrawn",
108+
Name: bindings.Events.AhWithdrawn,
109109
Data: new(assetholder.AssetHolderWithdrawn),
110110
Filter: [][]interface{}{{fundingID}},
111111
}
@@ -119,7 +119,7 @@ func bindAssetHolder(cb ContractBackend, asset channel.Asset, assetIndex channel
119119
if err != nil {
120120
log.Panic("Invalid AssetHolder ABI definition.")
121121
}
122-
contract := bind.NewBoundContract(assetAddr, bindings.AssetHolderABI, cb, cb, cb)
122+
contract := bind.NewBoundContract(assetAddr, bindings.ABI.AssetHolder, cb, cb, cb)
123123
return assetHolder{ctr, &assetAddr, contract, assetIndex}
124124
}
125125

backend/ethereum/subscription/eventsub_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ func TestEventSub(t *testing.T) {
8989
sink := make(chan *subscription.Event, 10)
9090
eFact := func() *subscription.Event {
9191
return &subscription.Event{
92-
Name: "Approval",
92+
Name: bindings.Events.ERC20Approval,
9393
Data: new(peruntoken.ERC20Approval),
9494
}
9595
}
9696
// Setup the event sub after some events have been sent.
9797
<-waitSent
98-
contract := bind.NewBoundContract(tokenAddr, bindings.ERC20TokenABI, cb, cb, cb)
98+
contract := bind.NewBoundContract(tokenAddr, bindings.ABI.ERC20Token, cb, cb, cb)
9999
sub, err := subscription.NewEventSub(ctx, cb, contract, eFact, 10000)
100100
require.NoError(t, err)
101101
go ct.Stage("sub", func(t pkgtest.ConcT) {
@@ -171,12 +171,12 @@ func TestEventSub_Filter(t *testing.T) {
171171
sink := make(chan *subscription.Event, 1)
172172
eFact := func() *subscription.Event {
173173
return &subscription.Event{
174-
Name: "Deposited",
174+
Name: bindings.Events.AhDeposited,
175175
Data: new(assetholder.AssetHolderDeposited),
176176
Filter: [][]interface{}{Filter},
177177
}
178178
}
179-
contract := bind.NewBoundContract(ahAddr, bindings.AssetHolderABI, cb, cb, cb)
179+
contract := bind.NewBoundContract(ahAddr, bindings.ABI.AssetHolder, cb, cb, cb)
180180
sub, err := subscription.NewEventSub(ctx, cb, contract, eFact, 100)
181181
require.NoError(t, err)
182182
go ct.Stage("sub", func(t pkgtest.ConcT) {

0 commit comments

Comments
 (0)