This commit is contained in:
Michiel Mulders 2018-01-31 10:47:52 +00:00 committed by GitHub
commit fbf8e9f890
5 changed files with 51 additions and 6 deletions

View File

@ -6,4 +6,4 @@ Installation with package manager npm:
.. code-block:: bash
$ npm install bigchaindb-driver
$ npm install bigchaindb-driver --save

View File

@ -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 @@ Lets 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,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

View File

@ -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": [

View File

@ -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'

View File

@ -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()
}