-
Notifications
You must be signed in to change notification settings - Fork 52
/
bss_test.go
150 lines (133 loc) · 3.59 KB
/
bss_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.
package bss
import (
"math/big"
"slices"
"testing"
"github.com/go-test/deep"
"github.com/hemilabs/heminetwork/api/bfgapi"
"github.com/hemilabs/heminetwork/api/bssapi"
"github.com/hemilabs/heminetwork/ethereum"
"github.com/hemilabs/heminetwork/hemi"
)
func TestConvertPopTxsToPopPayouts(t *testing.T) {
type testCaseDef struct {
name string
popTxs []bfgapi.PopTx
expectedPopPayouts []bssapi.PopPayout
extraSetup func(*testCaseDef)
}
popMinerPublicKeyOne := []byte("popMinerPublicKeyOne")
popMinerPublicKeyTwo := []byte("popMinerPublicKeyTwo")
popMinerAddressOne := ethereum.PublicKeyToAddress(popMinerPublicKeyOne)
popMinerAddressTwo := ethereum.PublicKeyToAddress(popMinerPublicKeyTwo)
testTable := []testCaseDef{
{
name: "convert empty poptxs",
popTxs: []bfgapi.PopTx{},
expectedPopPayouts: []bssapi.PopPayout{},
},
{
name: "convert no duplicate miner addresses",
popTxs: []bfgapi.PopTx{
{
PopMinerPublicKey: popMinerPublicKeyOne,
},
{
PopMinerPublicKey: popMinerPublicKeyTwo,
},
},
expectedPopPayouts: []bssapi.PopPayout{
{
MinerAddress: popMinerAddressOne,
Amount: big.NewInt(hemi.HEMIBase),
},
{
MinerAddress: popMinerAddressTwo,
Amount: big.NewInt(hemi.HEMIBase),
},
},
},
{
name: "convert reduce duplicate miner addresses",
popTxs: []bfgapi.PopTx{
{
PopMinerPublicKey: popMinerPublicKeyOne,
},
{
PopMinerPublicKey: popMinerPublicKeyTwo,
},
{
PopMinerPublicKey: popMinerPublicKeyOne,
},
},
expectedPopPayouts: []bssapi.PopPayout{
{
MinerAddress: popMinerAddressOne,
Amount: big.NewInt(2 * hemi.HEMIBase),
},
{
MinerAddress: popMinerAddressTwo,
Amount: big.NewInt(hemi.HEMIBase),
},
},
},
{
name: "convert reduce duplicate miner addresses large number",
popTxs: []bfgapi.PopTx{},
expectedPopPayouts: []bssapi.PopPayout{
{
MinerAddress: popMinerAddressOne,
Amount: big.NewInt(0).Mul(big.NewInt(1*hemi.HEMIBase), big.NewInt(360)),
},
},
extraSetup: func(tcd *testCaseDef) {
for range 360 {
tcd.popTxs = append(tcd.popTxs, bfgapi.PopTx{
PopMinerPublicKey: popMinerPublicKeyOne,
})
}
},
},
}
for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
if testCase.extraSetup != nil {
testCase.extraSetup(&testCase)
}
popPayouts := ConvertPopTxsToPopPayouts(testCase.popTxs)
sortFn := func(a, b bssapi.PopPayout) int {
// find first differing byte in miner addresses and sort by that,
// this should lead to predictable ordering as
// miner addresses are unique here
var ab byte = 0
var bb byte = 0
for i := range len(a.MinerAddress) {
ab = a.MinerAddress[i]
bb = b.MinerAddress[i]
if ab != bb {
break
}
}
if ab > bb {
return -1
}
return 1
}
// sort to ensure expected order
slices.SortFunc(popPayouts, sortFn)
slices.SortFunc(testCase.expectedPopPayouts, sortFn)
diff := deep.Equal(popPayouts, testCase.expectedPopPayouts)
if len(diff) != 0 {
t.Fatalf("unexpected diff %s", diff)
}
for i := range popPayouts {
if popPayouts[i].Amount.Cmp(testCase.expectedPopPayouts[i].Amount) != 0 {
t.Fatalf("amounts not equal: %v != %v", popPayouts[i].Amount, testCase.expectedPopPayouts[i].Amount)
}
}
})
}
}