db: stop relying on encoding.

This commit is contained in:
Christopher Jeffrey 2017-12-05 02:32:54 -08:00
parent dbde501444
commit 83e1de2e98
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 38 additions and 15 deletions

View File

@ -24,8 +24,6 @@ const ChainEntry = require('./chainentry');
const TXMeta = require('../primitives/txmeta'); const TXMeta = require('../primitives/txmeta');
const CoinEntry = require('../coins/coinentry'); const CoinEntry = require('../coins/coinentry');
const {encoding} = bio; const {encoding} = bio;
const u8 = encoding.u8;
const u32 = encoding.u32;
/** /**
* ChainDB * ChainDB
@ -1386,7 +1384,7 @@ class ChainDB {
const hash = block.hash(); const hash = block.hash();
// Hash->height index. // Hash->height index.
this.put(layout.h.build(hash), u32(entry.height)); this.put(layout.h.build(hash), fromU32(entry.height));
// Entry data. // Entry data.
this.put(layout.e.build(hash), entry.toRaw()); this.put(layout.e.build(hash), entry.toRaw());
@ -2288,7 +2286,9 @@ class CacheUpdate {
} }
toRaw() { toRaw() {
return u8(this.state); const data = Buffer.allocUnsafe(1);
data[0] = this.state;
return data;
} }
} }
@ -2300,6 +2300,12 @@ function getSize(value) {
return value.length + 80; return value.length + 80;
} }
function fromU32(num) {
const data = Buffer.allocUnsafe(4);
data.writeUInt32LE(num, 0, true);
return data;
}
/* /*
* Expose * Expose
*/ */

View File

@ -27,8 +27,6 @@ const Outpoint = require('../primitives/outpoint');
const layouts = require('./layout'); const layouts = require('./layout');
const records = require('./records'); const records = require('./records');
const NullClient = require('./nullclient'); const NullClient = require('./nullclient');
const {encoding} = bio;
const {u32} = encoding;
const layout = layouts.wdb; const layout = layouts.wdb;
const tlayout = layouts.txdb; const tlayout = layouts.txdb;
@ -216,7 +214,7 @@ class WalletDB extends EventEmitter {
if (!raw) { if (!raw) {
const b = this.db.batch(); const b = this.db.batch();
b.put(layout.O.build(), u32(this.network.magic)); b.put(layout.O.build(), fromU32(this.network.magic));
return b.write(); return b.write();
} }
@ -750,7 +748,7 @@ class WalletDB extends EventEmitter {
const id = wallet.id; const id = wallet.id;
b.put(layout.w.build(wid), wallet.toRaw()); b.put(layout.w.build(wid), wallet.toRaw());
b.put(layout.l.build(id), u32(wid)); b.put(layout.l.build(id), fromU32(wid));
} }
/** /**
@ -760,7 +758,7 @@ class WalletDB extends EventEmitter {
*/ */
increment(b, wid) { increment(b, wid) {
b.put(layout.D.build(), u32(wid + 1)); b.put(layout.D.build(), fromU32(wid + 1));
} }
/** /**
@ -1125,10 +1123,10 @@ class WalletDB extends EventEmitter {
b.put(layout.a.build(wid, index), account.toRaw()); b.put(layout.a.build(wid, index), account.toRaw());
// Name->Index lookups // Name->Index lookups
b.put(layout.i.build(wid, name), u32(index)); b.put(layout.i.build(wid, name), fromU32(index));
// Index->Name lookups // Index->Name lookups
b.put(layout.n.build(wid, index), Buffer.from(name, 'ascii')); b.put(layout.n.build(wid, index), fromString(name));
} }
/** /**
@ -2264,6 +2262,20 @@ class WalletOptions {
} }
} }
/*
* Helpers
*/
function fromU32(num) {
const data = Buffer.allocUnsafe(4);
data.writeUInt32LE(num, 0, true);
return data;
}
function fromString(str) {
return Buffer.from(str, 'ascii');
}
/* /*
* Expose * Expose
*/ */

View File

@ -19,7 +19,6 @@ const Input = require('../lib/primitives/input');
const Outpoint = require('../lib/primitives/outpoint'); const Outpoint = require('../lib/primitives/outpoint');
const Script = require('../lib/script/script'); const Script = require('../lib/script/script');
const HD = require('../lib/hd'); const HD = require('../lib/hd');
const u32 = encoding.u32;
const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
+ 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ'; + 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ';
@ -37,6 +36,12 @@ let importedKey = null;
let doubleSpendWallet = null; let doubleSpendWallet = null;
let doubleSpendCoin = null; let doubleSpendCoin = null;
function fromU32(num) {
const data = Buffer.allocUnsafe(4);
data.writeUInt32LE(num, 0, true);
return data;
}
function curBlock(wdb) { function curBlock(wdb) {
return fakeBlock(wdb.state.height); return fakeBlock(wdb.state.height);
}; };
@ -46,9 +51,9 @@ function nextBlock(wdb) {
} }
function fakeBlock(height) { function fakeBlock(height) {
const prev = hash256.digest(u32((height - 1) >>> 0)); const prev = hash256.digest(fromU32((height - 1) >>> 0));
const hash = hash256.digest(u32(height >>> 0)); const hash = hash256.digest(fromU32(height >>> 0));
const root = hash256.digest(u32((height | 0x80000000) >>> 0)); const root = hash256.digest(fromU32((height | 0x80000000) >>> 0));
return { return {
hash: hash.toString('hex'), hash: hash.toString('hex'),