forked from hyperledger-labs/perun-eth-backend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjudicator_internal_test.go
106 lines (96 loc) · 3.36 KB
/
adjudicator_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2024 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package channel
import (
"math/rand"
"testing"
"github.com/perun-network/perun-eth-backend/wallet"
"github.com/perun-network/perun-eth-backend/bindings/adjudicator"
"github.com/stretchr/testify/assert"
"perun.network/go-perun/channel"
channeltest "perun.network/go-perun/channel/test"
pkgtest "polycry.pt/poly-go/test"
)
func Test_toEthSubStates(t *testing.T) {
var (
rng = pkgtest.Prng(t)
assert = assert.New(t)
)
tests := []struct {
title string
setup func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState)
}{
{
title: "nil map gives nil slice",
setup: func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState) {
return channeltest.NewRandomState(rng, channeltest.WithBackend(wallet.BackendID)), nil, nil
},
},
{
title: "fresh map gives nil slice",
setup: func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState) {
return channeltest.NewRandomState(rng, channeltest.WithBackend(wallet.BackendID)), nil, nil
},
},
{
title: "1 layer of sub-channels",
setup: func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState) {
// ch[0]( ch[1], ch[2], ch[3] )
ch := genStates(rng, 4)
ch[0].AddSubAlloc(*ch[1].ToSubAlloc())
ch[0].AddSubAlloc(*ch[2].ToSubAlloc())
ch[0].AddSubAlloc(*ch[3].ToSubAlloc())
return ch[0], toStateMap(ch[1:]...), toEthStates(ch[1:]...)
},
},
{
title: "2 layers of sub-channels",
setup: func() (state *channel.State, subStates channel.StateMap, expected []adjudicator.ChannelState) {
// ch[0]( ch[1]( ch[2], ch[3] ), ch[4], ch[5] (ch[6] ) )
ch := genStates(rng, 7)
ch[0].AddSubAlloc(*ch[1].ToSubAlloc())
ch[0].AddSubAlloc(*ch[4].ToSubAlloc())
ch[0].AddSubAlloc(*ch[5].ToSubAlloc())
ch[1].AddSubAlloc(*ch[2].ToSubAlloc())
ch[1].AddSubAlloc(*ch[3].ToSubAlloc())
ch[5].AddSubAlloc(*ch[6].ToSubAlloc())
return ch[0], toStateMap(ch[1:]...), toEthStates(ch[1:]...)
},
},
}
for _, tc := range tests {
state, subStates, expected := tc.setup()
got := toEthSubStates(state, subStates)
assert.Equal(expected, got, tc.title)
}
}
func genStates(rng *rand.Rand, n int) (states []*channel.State) {
states = make([]*channel.State, n)
for i := range states {
states[i] = channeltest.NewRandomState(rng, channeltest.WithBackend(wallet.BackendID))
}
return
}
func toStateMap(states ...*channel.State) (_states channel.StateMap) {
_states = channel.MakeStateMap()
_states.Add(states...)
return
}
func toEthStates(states ...*channel.State) (_states []adjudicator.ChannelState) {
_states = make([]adjudicator.ChannelState, len(states))
for i, s := range states {
_states[i] = ToEthState(s)
}
return
}