-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwatch_set_test.go
More file actions
139 lines (116 loc) · 3.56 KB
/
Copy pathwatch_set_test.go
File metadata and controls
139 lines (116 loc) · 3.56 KB
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
package fraud
import (
"testing"
"github.com/btcsuite/btcd/wire/v2"
"github.com/stretchr/testify/require"
)
// TestAncestorWatchesRefcounting verifies the refcounted lifecycle:
// the first addTarget reports firstRefFor=true, subsequent retains by
// other targets do not, and dropTarget only signals "empty" once the
// last target releases.
func TestAncestorWatchesRefcounting(t *testing.T) {
t.Parallel()
w := newAncestorWatches()
op := wire.OutPoint{Hash: [32]byte{0xaa}}
point := WatchPoint{
Outpoint: op,
PkScript: []byte{
0x51,
0x20,
},
HeightHint: 100,
}
targetA := wire.OutPoint{Hash: [32]byte{0xa1}}
targetB := wire.OutPoint{Hash: [32]byte{0xb1}}
require.True(
t, w.firstRefFor(op),
"empty collection must report first reference",
)
w.addTarget(point, targetA)
require.False(
t, w.firstRefFor(op),
"after addTarget the outpoint is tracked",
)
require.Equal(t, map[wire.OutPoint]struct{}{
targetA: {},
}, w.targetsOf(op))
w.addTarget(point, targetB)
require.Equal(t, map[wire.OutPoint]struct{}{
targetA: {},
targetB: {},
}, w.targetsOf(op))
// Re-adding the same target is idempotent.
w.addTarget(point, targetA)
require.Len(t, w.targetsOf(op), 2)
// Drop the first target: watch must stay armed.
gotPoint, emptied := w.dropTarget(op, targetA)
require.False(
t, emptied,
"watch must remain armed while other targets reference it",
)
require.Equal(t, WatchPoint{}, gotPoint)
require.Equal(t, map[wire.OutPoint]struct{}{
targetB: {},
}, w.targetsOf(op))
// Drop the last target: collection returns the stored point so
// the caller can unregister, and the entry is removed.
gotPoint, emptied = w.dropTarget(op, targetB)
require.True(
t, emptied,
"watch must signal empty when the last target releases",
)
require.Equal(
t, point, gotPoint,
"stored WatchPoint must round-trip to the caller",
)
require.Nil(
t, w.targetsOf(op),
"removed entry must not appear via targetsOf",
)
require.True(
t, w.firstRefFor(op),
"after the last release, the outpoint is fresh again",
)
}
// TestAncestorWatchesDropTargetMissing verifies that dropTarget on an
// untracked outpoint is a quiet no-op rather than a panic.
func TestAncestorWatchesDropTargetMissing(t *testing.T) {
t.Parallel()
w := newAncestorWatches()
op := wire.OutPoint{Hash: [32]byte{0xaa}}
target := wire.OutPoint{Hash: [32]byte{0xa1}}
gotPoint, emptied := w.dropTarget(op, target)
require.False(t, emptied)
require.Equal(t, WatchPoint{}, gotPoint)
}
// TestAncestorWatchesOutpointsAndPointAt verifies the iteration and
// lookup helpers used by the shutdown path (OnStop must walk every
// tracked outpoint and read its stored point without dropping the
// entry).
func TestAncestorWatchesOutpointsAndPointAt(t *testing.T) {
t.Parallel()
w := newAncestorWatches()
opA := wire.OutPoint{Hash: [32]byte{0x01}}
opB := wire.OutPoint{Hash: [32]byte{0x02}}
pointA := WatchPoint{Outpoint: opA, HeightHint: 100}
pointB := WatchPoint{Outpoint: opB, HeightHint: 200}
target := wire.OutPoint{Hash: [32]byte{0xff}}
w.addTarget(pointA, target)
w.addTarget(pointB, target)
require.ElementsMatch(
t, []wire.OutPoint{opA, opB}, w.outpoints(),
)
gotA, ok := w.pointAt(opA)
require.True(t, ok)
require.Equal(t, pointA, gotA)
gotB, ok := w.pointAt(opB)
require.True(t, ok)
require.Equal(t, pointB, gotB)
_, ok = w.pointAt(wire.OutPoint{Hash: [32]byte{0x09}})
require.False(t, ok)
// pointAt does not drop the entry — the outpoints list must
// still report both after multiple reads.
require.ElementsMatch(
t, []wire.OutPoint{opA, opB}, w.outpoints(),
)
}