Skip to content

Commit 8aad187

Browse files
committed
✅ [backend/eth/glue] Add EventSub test.
Signed-off-by: Oliver Tale-Yazdi <oliver@perun.network>
1 parent 88e2ec4 commit 8aad187

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2021 - See NOTICE file for copyright holders.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package glue_test
16+
17+
import (
18+
"context"
19+
"math/big"
20+
"testing"
21+
"time"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/types"
25+
"github.com/sirupsen/logrus"
26+
"github.com/stretchr/testify/require"
27+
28+
"perun.network/go-perun/backend/ethereum/bindings/peruntoken"
29+
ethchannel "perun.network/go-perun/backend/ethereum/channel"
30+
"perun.network/go-perun/backend/ethereum/channel/test"
31+
"perun.network/go-perun/backend/ethereum/glue"
32+
"perun.network/go-perun/backend/ethereum/wallet/keystore"
33+
channeltest "perun.network/go-perun/channel/test"
34+
plogrus "perun.network/go-perun/log/logrus"
35+
pkgtest "perun.network/go-perun/pkg/test"
36+
wallettest "perun.network/go-perun/wallet/test"
37+
)
38+
39+
const (
40+
txGasLimit = 100000
41+
)
42+
43+
func init() {
44+
plogrus.Set(logrus.TraceLevel, &logrus.TextFormatter{})
45+
}
46+
47+
// TestEventSub tests the `EventSub` by:
48+
// 1. Emit `n-x` events with x <= n
49+
// 2. Starting up the `EventSub`
50+
// 3. Emit `x` events
51+
// 4. Checking that `EventSub` contains all events
52+
func TestEventSub(t *testing.T) {
53+
n := 1000
54+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
55+
defer cancel()
56+
rng := pkgtest.Prng(t)
57+
58+
// Simulated chain setup.
59+
sb := test.NewSimulatedBackend()
60+
ksWallet := wallettest.RandomWallet().(*keystore.Wallet)
61+
account := &ksWallet.NewRandomAccount(rng).(*keystore.Account).Account
62+
sb.FundAddress(ctx, account.Address)
63+
cb := ethchannel.NewContractBackend(sb, keystore.NewTransactor(*ksWallet, types.NewEIP155Signer(big.NewInt(1337))))
64+
65+
// Setup Perun Token.
66+
tokenAddr, err := ethchannel.DeployPerunToken(ctx, cb, *account, []common.Address{account.Address}, channeltest.MaxBalance)
67+
require.NoError(t, err)
68+
token, err := peruntoken.NewERC20(tokenAddr, cb)
69+
require.NoError(t, err)
70+
ct := pkgtest.NewConcurrent(t)
71+
72+
go ct.Stage("emitter", func(t pkgtest.ConcT) {
73+
for i := 0; i < n; i++ {
74+
// Send the transaction.
75+
opts, err := cb.NewTransactor(ctx, txGasLimit, *account)
76+
require.NoError(t, err)
77+
tx, err := token.IncreaseAllowance(opts, account.Address, big.NewInt(1))
78+
require.NoError(t, err)
79+
// Wait for the TX to be mined.
80+
_, err = cb.ConfirmTransaction(ctx, tx, *account)
81+
require.NoError(t, err)
82+
}
83+
})
84+
// Random wait to get some events before and some after the event sub is setup.
85+
time.Sleep(time.Duration(rng.Int31n(500)+100) * time.Millisecond)
86+
sink := make(chan *glue.Event, 10)
87+
eFact := func() glue.EventType {
88+
return glue.EventType{
89+
Name: "Approval",
90+
Data: new(peruntoken.ERC20Approval),
91+
}
92+
}
93+
// Setup the event sub.
94+
sub, err := glue.NewEventSub(token.Contract(), eFact, nil, nil)
95+
require.NoError(t, err)
96+
go ct.Stage("sub", func(t pkgtest.ConcT) {
97+
require.NoError(t, sub.Read(context.Background(), sink))
98+
})
99+
100+
go ct.Stage("receiver", func(t pkgtest.ConcT) {
101+
// Receive `n` events.
102+
for i := 0; i < n; i++ {
103+
e := <-sink
104+
require.NotNil(t, e)
105+
want := &peruntoken.ERC20Approval{
106+
Owner: account.Address,
107+
Spender: account.Address,
108+
Value: big.NewInt(int64(i + 1)),
109+
}
110+
require.Equal(t, want, e.Data)
111+
require.False(t, e.Log.Removed)
112+
}
113+
sub.Close()
114+
})
115+
116+
ct.Wait("emitter", "sub", "receiver")
117+
require.Nil(t, <-sink)
118+
}

0 commit comments

Comments
 (0)