forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkahnsort_test.go
148 lines (126 loc) · 3.28 KB
/
kahnsort_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
package wtxmgr_test
import (
"math/rand"
"reflect"
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wtxmgr"
)
// createTx is a helper method to create random transactions that spend
// particular inputs.
func createTx(t *testing.T, numOutputs int, inputs ...wire.OutPoint) *wire.MsgTx {
t.Helper()
tx := wire.NewMsgTx(1)
if len(inputs) == 0 {
tx.AddTxIn(&wire.TxIn{})
} else {
for _, input := range inputs {
tx.AddTxIn(&wire.TxIn{PreviousOutPoint: input})
}
}
for i := 0; i < numOutputs; i++ {
var pkScript [32]byte
if _, err := rand.Read(pkScript[:]); err != nil {
t.Fatal(err)
}
tx.AddTxOut(&wire.TxOut{
Value: rand.Int63(),
PkScript: pkScript[:],
})
}
return tx
}
// getOutPoint returns the outpoint for the output with the given index in the
// transaction.
func getOutPoint(tx *wire.MsgTx, index uint32) wire.OutPoint {
return wire.OutPoint{Hash: tx.TxHash(), Index: index}
}
// TestDependencySort ensures that transactions are topologically sorted by
// their dependency order under multiple scenarios. A transaction (a) can depend
// on another (b) as long as (a) spends an output created in (b).
func TestDependencySort(t *testing.T) {
t.Parallel()
tests := []struct {
name string
// setup is in charge of setting the dependency graph and
// returning the transactions in their expected sorted order.
setup func(t *testing.T) []*wire.MsgTx
}{
{
name: "single dependency chain",
setup: func(t *testing.T) []*wire.MsgTx {
// a -> b -> c
a := createTx(t, 1)
b := createTx(t, 1, getOutPoint(a, 0))
c := createTx(t, 1, getOutPoint(b, 0))
return []*wire.MsgTx{a, b, c}
},
},
{
name: "double dependency chain",
setup: func(t *testing.T) []*wire.MsgTx {
// a -> b
// a -> c
// c -> d
// d -> b
a := createTx(t, 2)
c := createTx(t, 1, getOutPoint(a, 1))
d := createTx(t, 1, getOutPoint(c, 0))
b := createTx(t, 1, getOutPoint(a, 0), getOutPoint(d, 0))
return []*wire.MsgTx{a, c, d, b}
},
},
{
name: "multi dependency chain",
setup: func(t *testing.T) []*wire.MsgTx {
// a -> e
// a -> c
// e -> c
// c -> g
// a -> b
// g -> b
// e -> f
// c -> f
// g -> f
// b -> f
// b -> d
// f -> d
a := createTx(t, 3)
a0 := getOutPoint(a, 0)
e := createTx(t, 2, a0)
a1 := getOutPoint(a, 1)
e0 := getOutPoint(e, 0)
c := createTx(t, 2, a1, e0)
c0 := getOutPoint(c, 0)
g := createTx(t, 2, c0)
a2 := getOutPoint(a, 2)
g0 := getOutPoint(g, 0)
b := createTx(t, 1, a2, g0)
e1 := getOutPoint(e, 1)
c1 := getOutPoint(c, 1)
g1 := getOutPoint(g, 1)
b0 := getOutPoint(b, 0)
f := createTx(t, 1, e1, c1, g1, b0)
f0 := getOutPoint(f, 0)
d := createTx(t, 1, b0, f0)
return []*wire.MsgTx{a, e, c, g, b, f, d}
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
exp := test.setup(t)
txSet := make(map[chainhash.Hash]*wire.MsgTx, len(exp))
for _, tx := range exp {
txSet[tx.TxHash()] = tx
}
sortedTxs := wtxmgr.DependencySort(txSet)
if !reflect.DeepEqual(sortedTxs, exp) {
t.Fatalf("expected %v, got %v", exp, sortedTxs)
}
})
}
}