Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
added encoding test
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderer committed Sep 23, 2015
1 parent df3eddf commit 0117362
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 11 additions & 3 deletions baseTrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ function Trie (db, root) {
Object.defineProperty(this, 'root', {
set: function (value) {
if (value) {
if (!Buffer.isBuffer(value) && typeof value === 'string') {
value = new Buffer(value, 'hex')
}
value = ethUtil.toBuffer(value)
assert(value.length === 32, 'Invalid root length. Roots are 32 bytes')
} else {
value = self.EMPTY_TRIE_ROOT
Expand All @@ -57,6 +55,8 @@ function Trie (db, root) {
Trie.prototype.get = function (key, cb) {
var self = this

key = ethUtil.toBuffer(key)

self._findPath(key, function (err, node, remainder, stack) {
var value = null
if (node && remainder.length === 0) {
Expand All @@ -76,6 +76,9 @@ Trie.prototype.get = function (key, cb) {
Trie.prototype.put = function (key, value, cb) {
var self = this

key = ethUtil.toBuffer(key)
value = ethUtil.toBuffer(value)

if (!value || value.toString() === '') {
self.del(key, cb)
} else {
Expand All @@ -101,6 +104,8 @@ Trie.prototype.put = function (key, value, cb) {
// deletes a value
Trie.prototype.del = function (key, cb) {
var self = this

key = ethUtil.toBuffer(key)
cb = callTogether(cb, self.sem.leave)

self.sem.take(function () {
Expand All @@ -123,6 +128,8 @@ Trie.prototype.del = function (key, cb) {
* @param {Buffer} key
*/
Trie.prototype.getRaw = function (key, cb) {
key = ethUtil.toBuffer(key)

function dbGet (db, cb2) {
db.get(key, {
keyEncoding: 'binary',
Expand Down Expand Up @@ -683,6 +690,7 @@ Trie.prototype.batch = function (ops, cb) {
* @param {Function} cb
*/
Trie.prototype.checkRoot = function (root, cb) {
root = ethUtil.toBuffer(root)
this._lookupNode(root, function (err, value) {
cb(err, !!value)
})
Expand Down
16 changes: 16 additions & 0 deletions test/encodeing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var Trie = require('../index.js')
var assert = require('assert')
var trie = new Trie()
var trie2 = new Trie()

var hex = 'FF44A3B3'
describe('encoding', function () {
it('hexprefixes ', function (done) {
trie.put(new Buffer(hex, 'hex'), 'test', function () {
trie2.put('0x' + hex, 'test', function () {
assert.equal(trie.root.toString('hex'), trie2.root.toString('hex'))
done()
})
})
})
})
6 changes: 2 additions & 4 deletions trieNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ TrieNode.prototype.getValue = function (key) {

TrieNode.prototype.setKey = function (key) {
if (this.type !== 'branch') {
if (Buffer.isBuffer(key) || typeof key === 'string') {
if (Buffer.isBuffer(key)) {
key = stringToNibbles(key)
} else {
key = key.slice(0) // copy the key
Expand Down Expand Up @@ -238,9 +238,7 @@ function nibblesToBuffer (arr) {
* - unknown - if somehting fucked up
*/
function getNodeType (node) {
if (Buffer.isBuffer(node) || typeof node === 'string' || node instanceof String) {
return 'unknown'
} else if (node.length === 17) {
if (node.length === 17) {
return 'branch'
} else if (node.length === 2) {
var key = stringToNibbles(node[0])
Expand Down

0 comments on commit 0117362

Please sign in to comment.