forked from lightningnetwork/lightning-onion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaylog_test.go
132 lines (113 loc) · 3.5 KB
/
replaylog_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
package sphinx
import (
"testing"
)
// TestMemoryReplayLogStorageAndRetrieval tests that the non-batch methods on
// MemoryReplayLog work as expected.
func TestMemoryReplayLogStorageAndRetrieval(t *testing.T) {
rl := NewMemoryReplayLog()
rl.Start()
defer rl.Stop()
var hashPrefix HashPrefix
hashPrefix[0] = 1
var cltv1 uint32 = 1
// Attempt to lookup unknown sphinx packet.
_, err := rl.Get(&hashPrefix)
if err == nil {
t.Fatalf("Expected ErrLogEntryNotFound")
}
if err != ErrLogEntryNotFound {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
// Log incoming sphinx packet.
err = rl.Put(&hashPrefix, cltv1)
if err != nil {
t.Fatalf("Put failed - received unexpected error upon Put: %v", err)
}
// Attempt to replay sphinx packet.
err = rl.Put(&hashPrefix, cltv1)
if err == nil {
t.Fatalf("Expected ErrReplayedPacket")
}
if err != ErrReplayedPacket {
t.Fatalf("Put failed - received unexpected error upon Put: %v", err)
}
// Lookup logged sphinx packet.
cltv, err := rl.Get(&hashPrefix)
if err != nil {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
if cltv != cltv1 {
t.Fatalf("Get returned wrong value: expected %v, got %v", cltv1, cltv)
}
// Delete sphinx packet from log.
err = rl.Delete(&hashPrefix)
if err != nil {
t.Fatalf("Delete failed - received unexpected error upon Delete: %v", err)
}
// Attempt to lookup deleted sphinx packet.
_, err = rl.Get(&hashPrefix)
if err == nil {
t.Fatalf("Expected ErrLogEntryNotFound")
}
if err != ErrLogEntryNotFound {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
// Reinsert incoming sphinx packet into the log.
var cltv2 uint32 = 2
err = rl.Put(&hashPrefix, cltv2)
if err != nil {
t.Fatalf("Put failed - received unexpected error upon Put: %v", err)
}
// Lookup logged sphinx packet.
cltv, err = rl.Get(&hashPrefix)
if err != nil {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
if cltv != cltv2 {
t.Fatalf("Get returned wrong value: expected %v, got %v", cltv2, cltv)
}
}
// TestMemoryReplayLogPutBatch tests that the batch adding of packets to a log
// works as expected.
func TestMemoryReplayLogPutBatch(t *testing.T) {
rl := NewMemoryReplayLog()
rl.Start()
defer rl.Stop()
var hashPrefix1, hashPrefix2 HashPrefix
hashPrefix1[0] = 1
hashPrefix2[0] = 2
// Create a batch with a duplicated packet.
batch1 := NewBatch([]byte{1})
err := batch1.Put(1, &hashPrefix1, 1)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
err = batch1.Put(1, &hashPrefix1, 1)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
replays, err := rl.PutBatch(batch1)
if replays.Size() != 1 || !replays.Contains(1) {
t.Fatalf("Unexpected replay set after adding batch 1 to log: %v", err)
}
// Create a batch with one replayed packet and one valid one.
batch2 := NewBatch([]byte{2})
err = batch2.Put(1, &hashPrefix1, 1)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
err = batch2.Put(2, &hashPrefix2, 2)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
replays, err = rl.PutBatch(batch2)
if replays.Size() != 1 || !replays.Contains(1) {
t.Fatalf("Unexpected replay set after adding batch 2 to log: %v", err)
}
// Reprocess batch 2, which should be idempotent.
replays, err = rl.PutBatch(batch2)
if replays.Size() != 1 || !replays.Contains(1) {
t.Fatalf("Unexpected replay set after adding batch 2 to log: %v", err)
}
}