Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat(BUX-177): minor sort refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Sep 15, 2023
1 parent 10180b2 commit 24c0b5e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion beef_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type beefTx struct {
}

func newBeefTx(version uint32, tx *Transaction) (*beefTx, error) {
// get inputs previous transactions
// get inputs parent transactions
inputs := tx.draftTransaction.Configuration.Inputs
transactions := make([]*Transaction, 0, len(inputs)+1)

Expand Down
16 changes: 7 additions & 9 deletions beef_tx_sorting.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package bux

func kahnTopologicalSortTransactions(transactions []*Transaction) []*Transaction {
randomAccessMap, incomingEdgesMap, zeroIncomingEdgeQueue := prepareSort(transactions)

txById, incomingEdgesMap, zeroIncomingEdgeQueue := prepareSortStructures(transactions)
result := make([]*Transaction, 0, len(transactions))

for len(zeroIncomingEdgeQueue) > 0 {
txID := zeroIncomingEdgeQueue[0]
zeroIncomingEdgeQueue = zeroIncomingEdgeQueue[1:]

tx := randomAccessMap[txID]
tx := txById[txID]
result = append(result, tx)

zeroIncomingEdgeQueue = removeTxFromIncomingEdges(tx, incomingEdgesMap, zeroIncomingEdgeQueue)
Expand All @@ -19,19 +18,18 @@ func kahnTopologicalSortTransactions(transactions []*Transaction) []*Transaction
return result
}

func prepareSort(dag []*Transaction) (randomAccessMap map[string]*Transaction, incomingEdgesMap map[string]int, zeroIncomingEdgeQueue []string) {
func prepareSortStructures(dag []*Transaction) (txById map[string]*Transaction, incomingEdgesMap map[string]int, zeroIncomingEdgeQueue []string) {
dagLen := len(dag)

randomAccessMap = make(map[string]*Transaction, dagLen)
txById = make(map[string]*Transaction, dagLen)
incomingEdgesMap = make(map[string]int, dagLen)

for _, tx := range dag {
randomAccessMap[tx.ID] = tx
txById[tx.ID] = tx
incomingEdgesMap[tx.ID] = 0
}

calculateIncomingEdges(incomingEdgesMap, dag)
zeroIncomingEdgeQueue = prepareStartNodesQueue(incomingEdgesMap)
zeroIncomingEdgeQueue = getTxWithZeroIncomingEdges(incomingEdgesMap)

return
}
Expand All @@ -44,7 +42,7 @@ func calculateIncomingEdges(inDegree map[string]int, transactions []*Transaction
}
}

func prepareStartNodesQueue(incomingEdgesMap map[string]int) []string {
func getTxWithZeroIncomingEdges(incomingEdgesMap map[string]int) []string {
zeroIncomingEdgeQueue := make([]string, 0, len(incomingEdgesMap))

for txID, edgeNum := range incomingEdgesMap {
Expand Down
26 changes: 15 additions & 11 deletions beef_tx_sorting_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bux

import (
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -20,17 +21,7 @@ func Test_kahnTopologicalSortTransaction(t *testing.T) {
createTx("8", "7"),
}

unsortedTxs := []*Transaction{
txsFromOldestToNewest[2],
txsFromOldestToNewest[3],
txsFromOldestToNewest[0],
txsFromOldestToNewest[1],
txsFromOldestToNewest[4],
txsFromOldestToNewest[7],
txsFromOldestToNewest[5],
txsFromOldestToNewest[6],
txsFromOldestToNewest[8],
}
unsortedTxs := shuffleTransactions(txsFromOldestToNewest)

t.Run("kahnTopologicalSortTransaction sort from oldest to newest", func(t *testing.T) {
sortedGraph := kahnTopologicalSortTransactions(unsortedTxs)
Expand Down Expand Up @@ -68,3 +59,16 @@ func createTx(txID string, inputsTxIDs ...string) *Transaction {

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
}

0 comments on commit 24c0b5e

Please sign in to comment.