-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathdeduplicator_test.go
153 lines (125 loc) · 3.88 KB
/
deduplicator_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
151
152
153
package tbtc
import (
"encoding/hex"
"math/big"
"testing"
"time"
"github.com/keep-network/keep-common/pkg/cache"
)
const (
testDKGSeedCachePeriod = 1 * time.Second
testDKGResultHashCachePeriod = 1 * time.Second
testWalletClosedCachePeriod = 1 * time.Second
)
func TestNotifyDKGStarted(t *testing.T) {
deduplicator := deduplicator{
dkgSeedCache: cache.NewTimeCache(testDKGSeedCachePeriod),
}
seed1 := big.NewInt(100)
seed2 := big.NewInt(200)
// Add the first seed.
canJoinDKG := deduplicator.notifyDKGStarted(seed1)
if !canJoinDKG {
t.Fatal("should be allowed to join DKG")
}
// Add the second seed.
canJoinDKG = deduplicator.notifyDKGStarted(seed2)
if !canJoinDKG {
t.Fatal("should be allowed to join DKG")
}
// Add the first seed before caching period elapses.
canJoinDKG = deduplicator.notifyDKGStarted(seed1)
if canJoinDKG {
t.Fatal("should not be allowed to join DKG")
}
// Wait until caching period elapses.
time.Sleep(testDKGSeedCachePeriod)
// Add the first seed again.
canJoinDKG = deduplicator.notifyDKGStarted(seed1)
if !canJoinDKG {
t.Fatal("should be allowed to join DKG")
}
}
func TestNotifyDKGResultSubmitted(t *testing.T) {
deduplicator := deduplicator{
dkgResultHashCache: cache.NewTimeCache(testDKGResultHashCachePeriod),
}
hash1Bytes, err := hex.DecodeString("92327ddff69a2b8c7ae787c5d590a2f14586089e6339e942d56e82aa42052cd9")
if err != nil {
t.Fatal(err)
}
var hash1 [32]byte
copy(hash1[:], hash1Bytes)
hash2Bytes, err := hex.DecodeString("23c0062913c4614bdff07f94475ceb4c585df53f71611776c3521ed8f8785913")
if err != nil {
t.Fatal(err)
}
var hash2 [32]byte
copy(hash2[:], hash2Bytes)
// Add the original parameters.
canProcess := deduplicator.notifyDKGResultSubmitted(big.NewInt(100), hash1, 500)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add with different seed.
canProcess = deduplicator.notifyDKGResultSubmitted(big.NewInt(101), hash1, 500)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add with different result hash.
canProcess = deduplicator.notifyDKGResultSubmitted(big.NewInt(100), hash2, 500)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add with different result block.
canProcess = deduplicator.notifyDKGResultSubmitted(big.NewInt(100), hash1, 501)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add with all different parameters.
canProcess = deduplicator.notifyDKGResultSubmitted(big.NewInt(101), hash2, 501)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add the original parameters before caching period elapses.
canProcess = deduplicator.notifyDKGResultSubmitted(big.NewInt(100), hash1, 500)
if canProcess {
t.Fatal("should not be allowed to process")
}
// Wait until caching period elapses.
time.Sleep(testDKGResultHashCachePeriod)
// Add the original parameters again.
canProcess = deduplicator.notifyDKGResultSubmitted(big.NewInt(100), hash1, 500)
if !canProcess {
t.Fatal("should be allowed to process")
}
}
func TestNotifyWalletClosed(t *testing.T) {
deduplicator := deduplicator{
walletClosedCache: cache.NewTimeCache(testWalletClosedCachePeriod),
}
wallet1 := [32]byte{1}
wallet2 := [32]byte{2}
// Add the first wallet ID.
canProcess := deduplicator.notifyWalletClosed(wallet1)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add the second wallet ID.
canProcess = deduplicator.notifyWalletClosed(wallet2)
if !canProcess {
t.Fatal("should be allowed to process")
}
// Add the first wallet ID before caching period elapses.
canProcess = deduplicator.notifyWalletClosed(wallet1)
if canProcess {
t.Fatal("should not be allowed to process")
}
// Wait until caching period elapses.
time.Sleep(testWalletClosedCachePeriod)
// Add the first wallet ID again.
canProcess = deduplicator.notifyWalletClosed(wallet1)
if !canProcess {
t.Fatal("should be allowed to process")
}
}