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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: bring back tests; add better comment
- Loading branch information
1 parent
321db32
commit 5788912
Showing
4 changed files
with
136 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/libsv/go-bt/v2" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,132 @@ | ||
package bux | ||
|
||
//func Test_kahnTopologicalSortTransaction(t *testing.T) { | ||
// // create related transactions from oldest to newest | ||
// txsFromOldestToNewest := []*Transaction{ | ||
// createTx("0"), | ||
// createTx("1", "0"), | ||
// createTx("2", "1"), | ||
// createTx("3", "2", "1"), | ||
// createTx("4", "3", "1"), | ||
// createTx("5", "3", "2"), | ||
// createTx("6", "4", "2", "0"), | ||
// createTx("7", "6", "5", "3", "1"), | ||
// createTx("8", "7"), | ||
// } | ||
// | ||
// txsFromOldestToNewestWithUnnecessaryInputs := []*Transaction{ | ||
// createTx("0"), | ||
// createTx("1", "0"), | ||
// createTx("2", "1", "101", "102"), | ||
// createTx("3", "2", "1"), | ||
// createTx("4", "3", "1"), | ||
// createTx("5", "3", "2", "100"), | ||
// createTx("6", "4", "2", "0"), | ||
// createTx("7", "6", "5", "3", "1", "103", "105", "106"), | ||
// createTx("8", "7"), | ||
// } | ||
// | ||
// tCases := []struct { | ||
// name string | ||
// expectedSortedTransactions []*Transaction | ||
// }{{ | ||
// name: "txs with necessary data only", | ||
// expectedSortedTransactions: txsFromOldestToNewest, | ||
// }, | ||
// { | ||
// name: "txs with inputs from other txs", | ||
// expectedSortedTransactions: txsFromOldestToNewestWithUnnecessaryInputs, | ||
// }, | ||
// } | ||
// | ||
// 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 txsFromOldestToNewest { | ||
// assert.Equal(t, tx.ID, sortedGraph[i].ID) | ||
// } | ||
// }) | ||
// } | ||
//} | ||
// | ||
//func createTx(txID string, inputsTxIDs ...string) *Transaction { | ||
// inputs := make([]*TransactionInput, 0) | ||
// for _, inTxID := range inputsTxIDs { | ||
// in := &TransactionInput{ | ||
// Utxo: Utxo{ | ||
// UtxoPointer: UtxoPointer{ | ||
// TransactionID: inTxID, | ||
// }, | ||
// }, | ||
// } | ||
// | ||
// inputs = append(inputs, in) | ||
// } | ||
// | ||
// transaction := &Transaction{ | ||
// draftTransaction: &DraftTransaction{ | ||
// Configuration: TransactionConfig{ | ||
// Inputs: inputs, | ||
// }, | ||
// }, | ||
// } | ||
// | ||
// transaction.ID = txID | ||
// | ||
// return transaction | ||
//} | ||
// | ||
//func shuffleTransactions(txs []*Transaction) []*Transaction { | ||
// n := len(txs) | ||
// result := make([]*Transaction, 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 | ||
//} | ||
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: getTxsFromOldestToNewestWithUnecessaryData(), | ||
}, | ||
} | ||
|
||
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 getTxsFromOldestToNewestWithUnecessaryData() []*bt.Tx { | ||
unnecessaryParentTx_1 := createTx() | ||
unnecessaryParentTx_2 := createTx() | ||
unnecessaryParentTx_3 := createTx() | ||
unnecessaryParentTx_4 := createTx() | ||
|
||
// create related transactions from oldest to newest | ||
oldestTx := createTx() | ||
secondTx := createTx(oldestTx) | ||
thirdTx := createTx(secondTx) | ||
fourthTx := createTx(thirdTx, secondTx, unnecessaryParentTx_1, unnecessaryParentTx_4) | ||
fifthTx := createTx(fourthTx, secondTx) | ||
sixthTx := createTx(fourthTx, thirdTx, unnecessaryParentTx_3, unnecessaryParentTx_2, unnecessaryParentTx_1) | ||
seventhTx := createTx(fifthTx, thirdTx, oldestTx) | ||
eightTx := createTx(seventhTx, sixthTx, fourthTx, secondTx, unnecessaryParentTx_1) | ||
|
||
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 | ||
} |