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
git clone https://github.com/gabrocheleau/merkle-patricia-tree-examples.git | |
cd merkle-patricia-tree-examples/ | |
npm install |
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
await trie1.put(Buffer.from('testKey'), Buffer.from('testValue')) | |
await trie1.put(Buffer.from('testKey0001'), Buffer.from('testValue1')) | |
await trie1.put(Buffer.from('testKey000A'), Buffer.from('testValueA')) // What Marie claims | |
await trie2.put(Buffer.from('testKey'), Buffer.from('testValue')) | |
await trie2.put(Buffer.from('testKey0001'), Buffer.from('testValue1')) | |
await trie2.put(Buffer.from('testKey000z'), Buffer.from('testValuez')) // What John claims | |
var temp1 = await trie1.findPath(Buffer.from('testKey')) | |
var temp2 = await trie2.findPath(Buffer.from('testKey')) |
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
console.log('Value of branch at index 3: ', node1.node._branches[3][1].toString()) | |
console.log('Value of branch at index 4: ', node1.node._branches[4][1].toString()) | |
// RESULT | |
Value of branch at index 3: testValue0 | |
Value of branch at index 4: testValueA |
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
async function test() { | |
var node1 = await trie.findPath(Buffer.from('testKey')) // We attempt to retrieve the node using the key "testKey" | |
console.log('Node 1: ', node1.node) // null | |
} | |
test() | |
// RESULT | |
Node 1: null |
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
var node1 = await trie.findPath(Buffer.from('testKey0')) // We retrieve one of the leaf nodes | |
console.log('Node 1: ', node1.node) // A leaf node! We can see that it contains 2 items: the encodedPath and the value | |
console.log('Node 1 value: ', node1.node._value.toString()) // The leaf node's value | |
// RESULT | |
Node 1: LeafNode { | |
_nibbles: [ 0 ], | |
_value: | |
} | |
Node 1 value: testValue0 |
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
console.log('Node value: ', node1.node._value.toString()) // The branch node's value | |
// RESULT | |
Node value: testValue |
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
await trie.put(Buffer.from('testKey0'), Buffer.from('testValue0')) | |
await trie.put(Buffer.from('testKeyA'), Buffer.from('testValueA')) | |
var node1 = await trie.findPath(Buffer.from('testKey')) // We retrieve the node at the "branching" off of the keys | |
console.log('Node: ', node1.node) // A branch node! | |
console.log('Node value: ', node1.node._value) // The branch node's value, in this case an empty buffer since the key "testValue" isn't assigned a value | |
// RESULT | |
Node: BranchNode { |
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
console.log(Buffer.from('testKey0001')) | |
console.log(Buffer.from('testKey000A')) | |
// RESULT | |
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
// <--path--> <------------- value -----------------> | |
[ , ] // going down the path of "testKey0" | |
[ , ] // going down the path of "testKeyA" |
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
// <------ same ------> <-> (different) | |
// "testKey0", "0" = 30 in bytes | |
// "testKeyA", "A" = 41 in bytes |
OlderNewer