2016-03-17 00:07:38 +01:00
|
|
|
// Copyright (c) 2016 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package wtxmgr
|
|
|
|
|
2019-02-01 01:41:19 +01:00
|
|
|
import (
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
)
|
2016-03-17 00:07:38 +01:00
|
|
|
|
|
|
|
type graphNode struct {
|
2019-02-01 01:41:19 +01:00
|
|
|
value *wire.MsgTx
|
2016-08-08 21:49:09 +02:00
|
|
|
outEdges []*chainhash.Hash
|
2016-03-17 00:07:38 +01:00
|
|
|
inDegree int
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:49:09 +02:00
|
|
|
type hashGraph map[chainhash.Hash]graphNode
|
2016-03-17 00:07:38 +01:00
|
|
|
|
2019-02-01 01:41:19 +01:00
|
|
|
func makeGraph(set map[chainhash.Hash]*wire.MsgTx) hashGraph {
|
2016-03-17 00:07:38 +01:00
|
|
|
graph := make(hashGraph)
|
|
|
|
|
2019-02-01 01:41:19 +01:00
|
|
|
for _, tx := range set {
|
|
|
|
// Add a node for every transaction. The output edges and input
|
|
|
|
// degree are set by iterating over each transaction's inputs
|
|
|
|
// below.
|
|
|
|
txHash := tx.TxHash()
|
|
|
|
if _, ok := graph[txHash]; !ok {
|
|
|
|
graph[txHash] = graphNode{value: tx}
|
2016-03-17 00:07:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inputLoop:
|
2019-02-01 01:41:19 +01:00
|
|
|
for _, input := range tx.TxIn {
|
2016-03-17 00:07:38 +01:00
|
|
|
// Transaction inputs that reference transactions not
|
|
|
|
// included in the set do not create any (local) graph
|
|
|
|
// edges.
|
|
|
|
if _, ok := set[input.PreviousOutPoint.Hash]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
inputNode := graph[input.PreviousOutPoint.Hash]
|
|
|
|
|
|
|
|
// Skip duplicate edges.
|
|
|
|
for _, outEdge := range inputNode.outEdges {
|
|
|
|
if *outEdge == input.PreviousOutPoint.Hash {
|
|
|
|
continue inputLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark a directed edge from the previous transaction
|
2019-02-01 01:41:19 +01:00
|
|
|
// hash to this transaction and increase the input
|
|
|
|
// degree for this transaction's node.
|
|
|
|
inputTx := inputNode.value
|
|
|
|
if inputTx == nil {
|
|
|
|
inputTx = set[input.PreviousOutPoint.Hash]
|
2016-03-17 00:07:38 +01:00
|
|
|
}
|
|
|
|
graph[input.PreviousOutPoint.Hash] = graphNode{
|
2019-02-01 01:41:19 +01:00
|
|
|
value: inputTx,
|
|
|
|
outEdges: append(inputNode.outEdges, &txHash),
|
2016-03-17 00:07:38 +01:00
|
|
|
inDegree: inputNode.inDegree,
|
|
|
|
}
|
2019-02-01 01:41:19 +01:00
|
|
|
node := graph[txHash]
|
|
|
|
graph[txHash] = graphNode{
|
|
|
|
value: tx,
|
2016-03-17 00:07:38 +01:00
|
|
|
outEdges: node.outEdges,
|
|
|
|
inDegree: node.inDegree + 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return graph
|
|
|
|
}
|
|
|
|
|
|
|
|
// graphRoots returns the roots of the graph. That is, it returns the node's
|
|
|
|
// values for all nodes which contain an input degree of 0.
|
2019-02-01 01:41:19 +01:00
|
|
|
func graphRoots(graph hashGraph) []*wire.MsgTx {
|
|
|
|
roots := make([]*wire.MsgTx, 0, len(graph))
|
2016-03-17 00:07:38 +01:00
|
|
|
for _, node := range graph {
|
|
|
|
if node.inDegree == 0 {
|
|
|
|
roots = append(roots, node.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return roots
|
|
|
|
}
|
|
|
|
|
2019-02-01 01:42:53 +01:00
|
|
|
// DependencySort topologically sorts a set of transactions by their dependency
|
2019-02-01 01:41:19 +01:00
|
|
|
// order. It is implemented using Kahn's algorithm.
|
2019-02-01 01:42:53 +01:00
|
|
|
func DependencySort(txs map[chainhash.Hash]*wire.MsgTx) []*wire.MsgTx {
|
2016-03-17 00:07:38 +01:00
|
|
|
graph := makeGraph(txs)
|
|
|
|
s := graphRoots(graph)
|
|
|
|
|
|
|
|
// If there are no edges (no transactions from the map reference each
|
|
|
|
// other), then Kahn's algorithm is unnecessary.
|
|
|
|
if len(s) == len(txs) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-02-01 01:41:19 +01:00
|
|
|
sorted := make([]*wire.MsgTx, 0, len(txs))
|
2016-03-17 00:07:38 +01:00
|
|
|
for len(s) != 0 {
|
2019-02-01 01:41:19 +01:00
|
|
|
tx := s[0]
|
2016-03-17 00:07:38 +01:00
|
|
|
s = s[1:]
|
2019-02-01 01:41:19 +01:00
|
|
|
sorted = append(sorted, tx)
|
2016-03-17 00:07:38 +01:00
|
|
|
|
2019-02-01 01:41:19 +01:00
|
|
|
n := graph[tx.TxHash()]
|
2016-03-17 00:07:38 +01:00
|
|
|
for _, mHash := range n.outEdges {
|
|
|
|
m := graph[*mHash]
|
|
|
|
if m.inDegree != 0 {
|
|
|
|
m.inDegree--
|
|
|
|
graph[*mHash] = m
|
|
|
|
if m.inDegree == 0 {
|
|
|
|
s = append(s, m.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sorted
|
|
|
|
}
|