This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
beef_tx_sorting_test.go
132 lines (108 loc) · 3.06 KB
/
beef_tx_sorting_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 bux
import (
"fmt"
"math/rand"
"testing"
"github.com/libsv/go-bt/v2"
"github.com/stretchr/testify/assert"
)
func Test_kahnTopologicalSortTransaction(t *testing.T) {
tCases := []struct {
name string
expectedSortedTransactions []*bt.Tx
}{
{
name: "txs with necessary data only",
expectedSortedTransactions: getTxsFromOldestToNewestWithNecessaryDataOnly(),
},
{
name: "txs with inputs from other txs",
expectedSortedTransactions: getTxsFromOldestToNewestWithUnnecessaryData(),
},
}
for _, tc := range tCases {
t.Run(fmt.Sprint("sort from oldest to newest ", tc.name), func(t *testing.T) {
// given
unsortedTxs := shuffleTransactions(tc.expectedSortedTransactions)
// when
sortedGraph := kahnTopologicalSortTransactions(unsortedTxs)
// then
for i, tx := range tc.expectedSortedTransactions {
assert.Equal(t, tx.TxID(), sortedGraph[i].TxID())
}
})
}
}
func getTxsFromOldestToNewestWithNecessaryDataOnly() []*bt.Tx {
// create related transactions from oldest to newest
oldestTx := createTx()
secondTx := createTx(oldestTx)
thirdTx := createTx(secondTx)
fourthTx := createTx(thirdTx, secondTx)
fifthTx := createTx(fourthTx, secondTx)
sixthTx := createTx(fourthTx, thirdTx)
seventhTx := createTx(fifthTx, thirdTx, oldestTx)
eightTx := createTx(seventhTx, sixthTx, fourthTx, secondTx)
newestTx := createTx(eightTx)
txsFromOldestToNewest := []*bt.Tx{
oldestTx,
secondTx,
thirdTx,
fourthTx,
fifthTx,
sixthTx,
seventhTx,
eightTx,
newestTx,
}
return txsFromOldestToNewest
}
func getTxsFromOldestToNewestWithUnnecessaryData() []*bt.Tx {
unnecessaryParentTx1 := createTx()
unnecessaryParentTx2 := createTx()
unnecessaryParentTx3 := createTx()
unnecessaryParentTx4 := createTx()
// create related transactions from oldest to newest
oldestTx := createTx()
secondTx := createTx(oldestTx)
thirdTx := createTx(secondTx)
fourthTx := createTx(thirdTx, secondTx, unnecessaryParentTx1, unnecessaryParentTx4)
fifthTx := createTx(fourthTx, secondTx)
sixthTx := createTx(fourthTx, thirdTx, unnecessaryParentTx3, unnecessaryParentTx2, unnecessaryParentTx1)
seventhTx := createTx(fifthTx, thirdTx, oldestTx)
eightTx := createTx(seventhTx, sixthTx, fourthTx, secondTx, unnecessaryParentTx1)
newestTx := createTx(eightTx)
txsFromOldestToNewest := []*bt.Tx{
oldestTx,
secondTx,
thirdTx,
fourthTx,
fifthTx,
sixthTx,
seventhTx,
eightTx,
newestTx,
}
return txsFromOldestToNewest
}
func createTx(inputsParents ...*bt.Tx) *bt.Tx {
inputs := make([]*bt.Input, 0)
for _, parent := range inputsParents {
in := bt.Input{}
in.PreviousTxIDAdd(parent.TxIDBytes())
inputs = append(inputs, &in)
}
transaction := bt.NewTx()
transaction.Inputs = append(transaction.Inputs, inputs...)
return transaction
}
func shuffleTransactions(txs []*bt.Tx) []*bt.Tx {
n := len(txs)
result := make([]*bt.Tx, n)
copy(result, txs)
for i := n - 1; i > 0; i-- {
j := rand.Intn(i + 1)
result[i], result[j] = result[j], result[i]
}
return result
}