From 9edbf2c1034064f11a0cd17f60da5412a9a7963c Mon Sep 17 00:00:00 2001 From: arkadiuszos4chain Date: Tue, 3 Oct 2023 18:26:53 +0200 Subject: [PATCH] fix(BUX-255): kahn sorting: ignore inputs we do not use for the transaction --- beef_tx.go | 4 ++-- beef_tx_sorting.go | 11 +++++++---- beef_tx_sorting_test.go | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/beef_tx.go b/beef_tx.go index 34b4c93f..0d01670a 100644 --- a/beef_tx.go +++ b/beef_tx.go @@ -28,8 +28,8 @@ type beefTx struct { } func newBeefTx(version uint32, tx *Transaction) (*beefTx, error) { - if version > 65_535 { - return nil, errors.New("version above 65.535") + if version > 0xFFFF { + return nil, errors.New("version above 0xFFFF") } // get inputs parent transactions diff --git a/beef_tx_sorting.go b/beef_tx_sorting.go index de2dc836..129b063e 100644 --- a/beef_tx_sorting.go +++ b/beef_tx_sorting.go @@ -28,16 +28,19 @@ func prepareSortStructures(dag []*Transaction) (txByID map[string]*Transaction, incomingEdgesMap[tx.ID] = 0 } - calculateIncomingEdges(incomingEdgesMap, dag) + calculateIncomingEdges(incomingEdgesMap, txByID) zeroIncomingEdgeQueue = getTxWithZeroIncomingEdges(incomingEdgesMap) return } -func calculateIncomingEdges(inDegree map[string]int, transactions []*Transaction) { - for _, tx := range transactions { +func calculateIncomingEdges(inDegree map[string]int, txByID map[string]*Transaction) { + for _, tx := range txByID { for _, input := range tx.draftTransaction.Configuration.Inputs { - inDegree[input.UtxoPointer.TransactionID]++ + inputUtxoTxID := input.UtxoPointer.TransactionID + if _, ok := txByID[inputUtxoTxID]; ok { // transaction can contains inputs we are not interested in + inDegree[inputUtxoTxID]++ + } } } } diff --git a/beef_tx_sorting_test.go b/beef_tx_sorting_test.go index dec96e01..0fb5f1cd 100644 --- a/beef_tx_sorting_test.go +++ b/beef_tx_sorting_test.go @@ -12,10 +12,10 @@ func Test_kahnTopologicalSortTransaction(t *testing.T) { txsFromOldestToNewest := []*Transaction{ createTx("0"), createTx("1", "0"), - createTx("2", "1"), + createTx("2", "1", "101"), createTx("3", "2", "1"), createTx("4", "3", "1"), - createTx("5", "3", "2"), + createTx("5", "3", "2", "100"), createTx("6", "4", "2", "0"), createTx("7", "6", "5", "3", "1"), createTx("8", "7"),