Package Exports
- urkel
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (urkel) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Urkel Tree
An optimized and cryptographically provable key-value store.
Design
The urkel tree was created for the Handshake protocol, and is implemented as a base-2 merkelized trie. It was created as an alternative to Ethereum's base-16 trie (which was the initial choice for Handshake name proofs). The design was inspired by Amaury Séchet's Merklix tree and shares many similarities to earlier work done by Bram Cohen.
Urkel stores nodes in a series of append-only files for snapshotting and crash consistency capabilities. Due to these presence of these features, Urkel has the ability to expose a fully transactional database.
The primary advantages in using an urkel tree over something like Ethereum's trie are:
- Performance - Stores nodes in flat files instead of an existing key-value store like LevelDB. Urkel is its own database. In benchmarks, this results in a 100x+ speedup.
- Simplicity - Maintains only two types of nodes: internal nodes and leaf nodes.
- Storage - Internal nodes are small (a constant size of 77 bytes on disk). This is important as internal nodes are frequently rewritten during updates to the tree.
- Proof Size - Sibling nodes required for proofs are a constant size of 32 bytes, similar to a typical merkle tree proof. This results in an extremely compact proof size.
The final benefit was the primary focus of the Handshake protocol. As name resolutions are a frequently requested operation, Handshake required proof sizes less than 1kb even after hundreds of millions of leaves are present in the tree.
History independence and non-destruction are also inherent properties of the urkel tree, just the same as the Ethereum trie. Note that urkel should only be used with uniformally distributed keys (i.e. hashed).
Compaction, while available, is currently inefficient and requires user intervention. This will be optimized in a future C implementation of the urkel tree. In the meantime, we don't see this as a problem as long as frequent commissions are avoided in consensus applications of the tree (i.e. avoid committing the tree on every block).
A more in-depth description is available in the Handshake Whitepaper.
Usage
const assert = require('assert');
const crypto = require('bcrypto');
const {Tree} = require('urkel');
const {Blake2b, randomBytes} = crypto;
const tree = new Tree(Blake2b, 256, '/path/to/my/db');
await tree.open();
let last;
// APIv1 - v2 will use transaction objects.
for (let i = 0; i < 500; i++) {
const key = randomBytes(32);
const val = randomBytes(300);
await tree.insert(key, val);
last = key;
}
await tree.commit();
const root = tree.rootHash();
const proof = await tree.prove(root, last);
const [code, value] = tree.verify(root, last, proof);
assert(code === 0);
if (value) {
console.log('Valid proof for %s: %s',
last.toString('hex'), value.toString('hex'));
} else {
console.log('Absence proof for %s.', last.toString('hex'));
}
await tree.values((key, value) => {
console.log('Iterated over item:');
console.log('%s: %s', key.toString('hex'), value.toString('hex'));
});
await tree.close();Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your code
to be distributed under the MIT license. You are also implicitly verifying that
all code is your original work. </legalese>
License
- Copyright (c) 2018, Christopher Jeffrey (MIT License).
See LICENSE for more info.