From 0e3c743481c47310c6f3c0ae3c3c069143acaf47 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 23:01:54 +0100 Subject: [PATCH 1/4] topsort functionality added --- package.json | 3 ++- src/index.js | 1 + src/utils/topologySorting.js | 27 +++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/utils/topologySorting.js diff --git a/package.json b/package.json index 360236da..19cb74d5 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/index.js b/src/index.js index bbc98ee2..e81cf1c0 100644 --- a/src/index.js +++ b/src/index.js @@ -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' diff --git a/src/utils/topologySorting.js b/src/utils/topologySorting.js new file mode 100644 index 00000000..54085e97 --- /dev/null +++ b/src/utils/topologySorting.js @@ -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() +} From 0edb8cd835d41d65629003bfb8e91bef7617a9e0 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sat, 20 Jan 2018 23:19:40 +0100 Subject: [PATCH 2/4] Top sort docs --- docs/source/quickstart.rst | 2 +- docs/source/usage.rst | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index 19d0fb4a..93845ad7 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -6,4 +6,4 @@ Installation with package manager npm: .. code-block:: bash - $ npm install bigchaindb-driver + $ npm install bigchaindb-driver --save diff --git a/docs/source/usage.rst b/docs/source/usage.rst index eebc5fec..5381c065 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -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. +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' }) @@ -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: @@ -779,6 +781,14 @@ 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`. +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. + + .. TODO: .. - Add lexer: https://stackoverflow.com/questions/4259105/which-sphinx-code-block-language-to-use-for-json From 03a014ff60abcb12bf89b0f437bf673839073e2e Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sun, 21 Jan 2018 00:34:22 +0100 Subject: [PATCH 3/4] Docs updated --- .vscode/settings.json | 3 +++ docs/source/usage.rst | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..e8c013f6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "restructuredtext.confPath": "/home/michiel/projects/bigchaindb-driver/js-bigchaindb-driver" +} \ No newline at end of file diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 5381c065..b741a851 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -783,10 +783,16 @@ Here is a better overview of the flow of the tokens. Topology Sorting ---------------- -It is possible to retrieve all transactions for a specific `asset_id`. -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. +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)) From c941742a63155a35b360bcd55150e14cef84990f Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Sun, 21 Jan 2018 00:36:40 +0100 Subject: [PATCH 4/4] remove folder --- .vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e8c013f6..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "restructuredtext.confPath": "/home/michiel/projects/bigchaindb-driver/js-bigchaindb-driver" -} \ No newline at end of file