Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Utility method for topological sort #117

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"json-stable-stringify": "^1.0.1",
"query-string": "^5.0.0",
"sprintf-js": "^1.0.3",
"toposort": "^1.0.6",
"tweetnacl": "^1.0.0"
},
"keywords": [
Expand Down
27 changes: 27 additions & 0 deletions src/transaction/utils/topologySorting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import toposort from 'toposort'

function generateGraph(txs) {
const graph = []
txs.forEach(tx => {
tx.inputs.forEach(input => {
if (input.fulfills) {
graph.push([tx.id, input.fulfills.transaction_id])
}
})
})

return graph
}


/**
* @public
* Topological sort algorithm for transactions
* Used for endpoint: /transactions?assetId
* @param {object} txs Array of transactions
* @returns {object} Returns transactions in order of dependency
*/
export default function topsortTransactions(txs) {
const graph = generateGraph(txs)
return toposort(graph).reverse()
}