-
Notifications
You must be signed in to change notification settings - Fork 92
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
michielmulders
wants to merge
4
commits into
bigchaindb:master
Choose a base branch
from
michielmulders:topology-sorting
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
|
@@ -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,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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.