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

Topology sorting functionality added with docs #143

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Installation with package manager npm:

.. code-block:: bash

$ npm install bigchaindb-driver
$ npm install bigchaindb-driver --save
24 changes: 20 additions & 4 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ A simple connection with BigchainDB can be established like this.

const conn = new driver.Connection(API_PATH)

It is also possible to connect to a BigchainDB node of the IPDB test net.
To do so, you need to pass the **app_id and app_key**.
Previously you could connect to IPDB testnet, but it has been shutdown.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to explain about IPDB history. I would say that we are connection to BigchainDB node.

Today, you can create an account for the 'BigchainDB Testnet' here_.
.. _here: https://testnet.bigchaindb.com/?utm_source=3Scale&utm_campaign=21a328d6cd-EMAIL_CAMPAIGN_2018_01_18&utm_medium=email&utm_term=0_b98508b5a9-21a328d6cd-67355241
To use the tesnet, you need to pass the **app_id and app_key** to get access.

.. code-block:: js

let bdb = new driver.Connection('https://test.ipdb.io/api/v1/', {
let bdb = new driver.Connection('https://test.bigchaindb.com/api/v1/', {
app_id: 'dgi829l9',
app_key: 'u008ik1bf83b43ce3a95uu0727e66fb9'
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess those credentials were valid for IPDB, not for the BigchainDB Test Network anymore. Maybe they could be avoided with

{
        app_id: "Get one from testnet.bigchaindb.com",
        app_key: "Get one from testnet.bigchaindb.com"
    }

Expand Down Expand Up @@ -291,7 +293,7 @@ Let’s perform a text search for all metadata that contains the word '1.32':
.. code-block:: js

conn.searchMetadata('1.32')
.then(assets => console.log('Found assets with serial number Bicycle Inc.:', assets))
.then(assets => console.log('Found assets with with metadata equal to "1.32"', assets))

Which leads to following result:

Expand Down Expand Up @@ -779,6 +781,20 @@ Here is a better overview of the flow of the tokens.
| ``Carly`` | 1 | ``TRANSFER 2`` |
+-----------+------------+-----------------+

Topology Sorting
----------------
It is possible to retrieve all transactions for a specific `asset_id` with the API Endpoint_ `/transactions?asset_id`
.. _Endpoint: https://docs.bigchaindb.com/projects/server/en/latest/http-client-server-api.html#get--api-v1-transactions?asset_id=asset_id&operation=CREATE|TRANSFER
But, there is no guarantee that the transactions are listed in the correct order. We've provided a util method which helps you sorting these transactions in the correct order. The method uses inputs and outputs to build a so-called graph of transactions and returns an object with all ordered transactions.
In code, it looks like this:

.. code-block:: js

conn.listTransactions(tx.id, 'TRANSFER'))
.then(txArray => driver.Transaction.topsortTransactions(txArray))
.then(sortedTxs => console.log(sortedTxs))



.. TODO:
.. - Add lexer: https://stackoverflow.com/questions/4259105/which-sphinx-code-block-language-to-use-for-json
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@
"buffer": "^5.0.2",
"clone": "^2.1.0",
"core-js": "^2.4.1",
"crypto-conditions": "^2.0.1",
"decamelize": "^2.0.0",
"es6-promise": "^4.0.5",
"fetch-ponyfill": "^4.0.0",
"crypto-conditions": "^2.0.1",
"isomorphic-fetch": "^2.2.1",
"js-sha3": "^0.7.0",
"js-utility-belt": "^1.5.0",
"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
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export Connection from './connection'
export Transaction from './transaction'
export ccJsonLoad from './utils/ccJsonLoad'
export ccJsonify from './utils/ccJsonify'
export topsortTransactions from './utils/topologySorting'
27 changes: 27 additions & 0 deletions src/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()
}