From 3c82ac0486a6f49c8b4e75150b8c95451af093ff Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 17 Nov 2017 00:00:36 -0800 Subject: [PATCH] bcoin: use util.revHex again. --- lib/blockchain/chain.js | 7 +++---- lib/blockchain/chaindb.js | 5 +++-- lib/blockchain/chainentry.js | 15 ++++++++------- lib/mempool/fees.js | 3 ++- lib/mempool/mempool.js | 6 +++--- lib/mining/cpuminer.js | 3 +-- lib/mining/template.js | 3 ++- lib/net/packets.js | 4 ++-- lib/net/peer.js | 12 ++++++------ lib/net/pool.js | 15 +++++++-------- lib/node/http.js | 3 +-- lib/node/rpc.js | 30 +++++++++++++++--------------- lib/primitives/abstractblock.js | 7 ++++--- lib/primitives/block.js | 10 +++++----- lib/primitives/coin.js | 7 ++++--- lib/primitives/headers.js | 9 ++++----- lib/primitives/invitem.js | 4 ++-- lib/primitives/merkleblock.js | 12 ++++++------ lib/primitives/outpoint.js | 7 ++++--- lib/primitives/tx.js | 8 ++++---- lib/primitives/txmeta.js | 7 +++---- lib/script/script.js | 1 - lib/utils/util.js | 2 ++ lib/wallet/client.js | 6 +++--- lib/wallet/records.js | 4 ++-- lib/wallet/rpc.js | 12 ++++++------ lib/wallet/txdb.js | 8 ++++---- lib/wallet/walletdb.js | 5 +++-- migrate/chaindb2to3.js | 11 ++++++----- migrate/ensure-tip-index.js | 3 ++- test/chain-test.js | 1 - test/input-test.js | 4 ++-- test/mempool-test.js | 1 - test/tx-test.js | 5 +++-- 34 files changed, 122 insertions(+), 118 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index a1825455..7fec51d6 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -12,7 +12,6 @@ const path = require('path'); const AsyncEmitter = require('bevent'); const Logger = require('blgr'); const {Lock} = require('bmutex'); -const {encoding} = require('bufio'); const Network = require('../protocol/network'); const ChainDB = require('./chaindb'); const common = require('./common'); @@ -1562,7 +1561,7 @@ class Chain extends AsyncEmitter { if (hash === checkpoint) { this.logger.debug('Hit checkpoint block %s (%d).', - encoding.revHex(hash), height); + util.revHex(hash), height); this.emit('checkpoint', hash, height); return true; } @@ -1574,8 +1573,8 @@ class Chain extends AsyncEmitter { this.logger.warning( 'Checkpoint mismatch at height %d: expected=%s received=%s', height, - encoding.revHex(checkpoint), - encoding.revHex(hash) + util.revHex(checkpoint), + util.revHex(hash) ); this.purgeOrphans(); diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 7a864d50..bd2f6bc4 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -16,6 +16,7 @@ const CoinView = require('../coins/coinview'); const UndoCoins = require('../coins/undocoins'); const layout = require('./layout'); const LRU = require('../utils/lru'); +const util = require('../utils/util'); const Block = require('../primitives/block'); const Outpoint = require('../primitives/outpoint'); const Address = require('../primitives/address'); @@ -1275,7 +1276,7 @@ class ChainDB { if (typeof start === 'number') this.logger.info('Scanning from height %d.', start); else - this.logger.info('Scanning from block %s.', encoding.revHex(start)); + this.logger.info('Scanning from block %s.', util.revHex(start)); let entry = await this.getEntry(start); @@ -2135,7 +2136,7 @@ class ChainState { } rhash() { - return encoding.revHex(this.tip); + return util.revHex(this.tip); } clone() { diff --git a/lib/blockchain/chainentry.js b/lib/blockchain/chainentry.js index 34f3885b..b2b24238 100644 --- a/lib/blockchain/chainentry.js +++ b/lib/blockchain/chainentry.js @@ -12,6 +12,7 @@ const bio = require('bufio'); const BN = require('bcrypto/lib/bn'); const consensus = require('../protocol/consensus'); const hash256 = require('bcrypto/lib/hash256'); +const util = require('../utils/util'); const Headers = require('../primitives/headers'); const InvItem = require('../primitives/invitem'); const {encoding} = bio; @@ -177,7 +178,7 @@ class ChainEntry { */ rhash() { - return encoding.revHex(this.hash); + return util.revHex(this.hash); } /** @@ -274,10 +275,10 @@ class ChainEntry { toJSON() { return { - hash: encoding.revHex(this.hash), + hash: util.revHex(this.hash), version: this.version, - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), time: this.time, bits: this.bits, nonce: this.nonce, @@ -303,10 +304,10 @@ class ChainEntry { assert((json.nonce >>> 0) === json.nonce); assert(typeof json.chainwork === 'string'); - this.hash = encoding.revHex(json.hash); + this.hash = util.revHex(json.hash); this.version = json.version; - this.prevBlock = encoding.revHex(json.prevBlock); - this.merkleRoot = encoding.revHex(json.merkleRoot); + this.prevBlock = util.revHex(json.prevBlock); + this.merkleRoot = util.revHex(json.merkleRoot); this.time = json.time; this.bits = json.bits; this.nonce = json.nonce; diff --git a/lib/mempool/fees.js b/lib/mempool/fees.js index 22d888d3..3f65d372 100644 --- a/lib/mempool/fees.js +++ b/lib/mempool/fees.js @@ -11,6 +11,7 @@ const assert = require('assert'); const bio = require('bufio'); const Logger = require('blgr'); +const util = require('../utils/util'); const binary = require('../utils/binary'); const consensus = require('../protocol/consensus'); const policy = require('../protocol/policy'); @@ -488,7 +489,7 @@ class PolicyEstimator { const item = this.map.get(hash); if (!item) { - this.logger.spam('Mempool tx %s not found.', encoding.revHex(hash)); + this.logger.spam('Mempool tx %s not found.', util.revHex(hash)); return; } diff --git a/lib/mempool/mempool.js b/lib/mempool/mempool.js index 1a8e691c..a0bc5572 100644 --- a/lib/mempool/mempool.js +++ b/lib/mempool/mempool.js @@ -1661,7 +1661,7 @@ class Mempool extends EventEmitter { assert(hash); this.logger.debug('Removing orphan %s from mempool.', - encoding.revHex(hash)); + util.revHex(hash)); this.removeOrphan(hash); @@ -2518,7 +2518,7 @@ class MempoolCache { this.logger.info( 'Mempool cache is empty. Writing tip %s.', - encoding.revHex(tip)); + util.revHex(tip)); await this.init(tip); } @@ -2538,7 +2538,7 @@ class MempoolCache { if (tip !== this.chain.tip.hash) { this.logger.warning( 'Mempool tip not consistent with chain tip (%s != %s)!', - encoding.revHex(tip), + util.revHex(tip), this.chain.tip.rhash()); this.logger.warning('Invalidating mempool cache.'); await this.wipe(); diff --git a/lib/mining/cpuminer.js b/lib/mining/cpuminer.js index 0834c80f..98171428 100644 --- a/lib/mining/cpuminer.js +++ b/lib/mining/cpuminer.js @@ -9,7 +9,6 @@ const assert = require('assert'); const EventEmitter = require('events'); -const {encoding} = require('bufio'); const {Lock} = require('bmutex'); const util = require('../utils/util'); const mine = require('./mine'); @@ -402,7 +401,7 @@ class CPUMiner extends EventEmitter { sendStatus(job, nonce) { const attempt = job.attempt; - const tip = encoding.revHex(attempt.prevBlock); + const tip = util.revHex(attempt.prevBlock); const hashes = job.getHashes(nonce); const hashrate = job.getRate(nonce); diff --git a/lib/mining/template.js b/lib/mining/template.js index 33cc9e4c..ffb25b1e 100644 --- a/lib/mining/template.js +++ b/lib/mining/template.js @@ -11,6 +11,7 @@ const assert = require('assert'); const bio = require('bufio'); const hash256 = require('bcrypto/lib/hash256'); const merkle = require('bcrypto/lib/merkle'); +const util = require('../utils/util'); const Address = require('../primitives/address'); const TX = require('../primitives/tx'); const Block = require('../primitives/block'); @@ -677,7 +678,7 @@ class BlockProof { } rhash() { - return encoding.revHex(this.hash.toString('hex')); + return util.revHex(this.hash.toString('hex')); } verify(target) { diff --git a/lib/net/packets.js b/lib/net/packets.js index b742bf74..7ca9fbdd 100644 --- a/lib/net/packets.js +++ b/lib/net/packets.js @@ -1557,7 +1557,7 @@ class RejectPacket extends Packet { */ rhash() { - return this.hash ? encoding.revHex(this.hash) : null; + return this.hash ? util.revHex(this.hash) : null; } /** @@ -1741,7 +1741,7 @@ class RejectPacket extends Packet { inspect() { const code = RejectPacket.codesByVal[this.code] || this.code; - const hash = this.hash ? encoding.revHex(this.hash) : null; + const hash = this.hash ? util.revHex(this.hash) : null; return ' 0) - hash = encoding.revHex(packet.locator[0]); + hash = util.revHex(packet.locator[0]); let end = null; if (stop) - end = encoding.revHex(stop); + end = util.revHex(stop); this.logger.debug( 'Requesting headers packet from peer with getheaders (%s).', @@ -1951,11 +1951,11 @@ class Peer extends EventEmitter { let hash = null; if (packet.locator.length > 0) - hash = encoding.revHex(packet.locator[0]); + hash = util.revHex(packet.locator[0]); let end = null; if (stop) - end = encoding.revHex(stop); + end = util.revHex(stop); this.logger.debug( 'Requesting inv packet from peer with getblocks (%s).', @@ -2003,7 +2003,7 @@ class Peer extends EventEmitter { if (msg) { this.logger.debug('Rejecting %s %s (%s): code=%s reason=%s.', - msg, encoding.revHex(hash), this.hostname(), code, reason); + msg, util.revHex(hash), this.hostname(), code, reason); } else { this.logger.debug('Rejecting packet from %s: code=%s reason=%s.', this.hostname(), code, reason); diff --git a/lib/net/pool.js b/lib/net/pool.js index d658fda2..3ebd6fc1 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -9,7 +9,6 @@ const assert = require('assert'); const EventEmitter = require('events'); -const {encoding} = require('bufio'); const {Lock} = require('bmutex'); const IP = require('binet'); const dns = require('bdns'); @@ -227,7 +226,7 @@ class Pool extends EventEmitter { this.headerChain.push(new HeaderEntry(tip.hash, tip.height)); this.logger.info( 'Initialized header chain to height %d (checkpoint=%s).', - tip.height, encoding.revHex(this.headerTip.hash)); + tip.height, util.revHex(this.headerTip.hash)); } } @@ -2285,8 +2284,8 @@ class Pool extends EventEmitter { if (hash !== node.hash) { this.logger.warning( 'Header hash mismatch %s != %s (%s).', - encoding.revHex(hash), - encoding.revHex(node.hash), + util.revHex(hash), + util.revHex(node.hash), peer.hostname()); peer.destroy(); @@ -2298,7 +2297,7 @@ class Pool extends EventEmitter { if (node.height === this.headerTip.height) { this.logger.info( 'Received checkpoint %s (%d).', - encoding.revHex(node.hash), node.height); + util.revHex(node.hash), node.height); this.headerTip = this.getNextTip(node.height); @@ -2355,7 +2354,7 @@ class Pool extends EventEmitter { if (!peer) { this.logger.warning( 'Could not find offending peer for orphan: %s (%d).', - encoding.revHex(err.hash), id); + util.revHex(err.hash), id); return; } @@ -3443,7 +3442,7 @@ class Pool extends EventEmitter { // If we recently rejected this item. Ignore. if (this.mempool.hasReject(hash)) { - this.logger.spam('Saw known reject of %s.', encoding.revHex(hash)); + this.logger.spam('Saw known reject of %s.', util.revHex(hash)); return true; } } @@ -4336,7 +4335,7 @@ class BroadcastItem extends EventEmitter { inspect() { const type = this.type === invTypes.TX ? 'tx' : 'block'; - const hash = encoding.revHex(this.hash); + const hash = util.revHex(this.hash); return ``; } } diff --git a/lib/node/http.js b/lib/node/http.js index 3f631ea9..ad53571b 100644 --- a/lib/node/http.js +++ b/lib/node/http.js @@ -12,7 +12,6 @@ const path = require('path'); const {Server} = require('bweb'); const Validator = require('bval'); const {base58} = require('bstring'); -const {encoding} = require('bufio'); const {BloomFilter} = require('bfilter'); const sha256 = require('bcrypto/lib/sha256'); const random = require('bcrypto/lib/random'); @@ -296,7 +295,7 @@ class HTTP extends Server { const result = []; for (const hash of hashes) - result.push(encoding.revHex(hash)); + result.push(util.revHex(hash)); res.json(200, result); }); diff --git a/lib/node/rpc.js b/lib/node/rpc.js index bf90c140..4287bce3 100644 --- a/lib/node/rpc.js +++ b/lib/node/rpc.js @@ -464,7 +464,7 @@ class RPC extends RPCBase { offset = 0; for (const hash in peer.blockMap.keys()) { - const str = encoding.revHex(hash); + const str = util.revHex(hash); hashes.push(str); } @@ -490,7 +490,7 @@ class RPC extends RPCBase { subver: peer.agent, inbound: !peer.outbound, startingheight: peer.height, - besthash: peer.bestHash ? encoding.revHex(peer.bestHash) : null, + besthash: peer.bestHash ? util.revHex(peer.bestHash) : null, bestheight: peer.bestHeight, banscore: peer.banScore, inflight: hashes, @@ -694,7 +694,7 @@ class RPC extends RPCBase { if (!hash) throw new RPCError(errs.MISC_ERROR, 'Not found.'); - return encoding.revHex(hash); + return util.revHex(hash); } async getBlockHeader(args, help) { @@ -878,7 +878,7 @@ class RPC extends RPCBase { const hashes = this.mempool.getSnapshot(); - return hashes.map(encoding.revHex); + return hashes.map(util.revHex); } async getTXOut(args, help) { @@ -1017,7 +1017,7 @@ class RPC extends RPCBase { const out = []; for (const hash of tree.matches) - out.push(encoding.revHex(hash.toString('hex'))); + out.push(util.revHex(hash.toString('hex'))); return out; } @@ -1461,8 +1461,8 @@ class RPC extends RPCBase { vbavailable: vbavailable, vbrequired: 0, height: attempt.height, - previousblockhash: encoding.revHex(attempt.prevBlock), - target: encoding.revHex(attempt.target.toString('hex')), + previousblockhash: util.revHex(attempt.prevBlock), + target: util.revHex(attempt.target.toString('hex')), bits: hex32(attempt.bits), noncerange: '00000000ffffffff', curtime: attempt.time, @@ -2286,7 +2286,7 @@ class RPC extends RPCBase { if ((lastTX >>> 0) !== lastTX) throw new RPCError(errs.INVALID_PARAMETER, 'Invalid longpoll ID.'); - const hash = encoding.revHex(watched); + const hash = util.revHex(watched); if (this.chain.tip.hash !== hash) return; @@ -2699,16 +2699,16 @@ class RPC extends RPCBase { height: entry.height, version: entry.version, versionHex: hex32(entry.version), - merkleroot: encoding.revHex(entry.merkleRoot), + merkleroot: util.revHex(entry.merkleRoot), time: entry.time, mediantime: mtp, bits: entry.bits, difficulty: toDifficulty(entry.bits), chainwork: entry.chainwork.toString('hex', 64), previousblockhash: entry.prevBlock !== encoding.NULL_HASH - ? encoding.revHex(entry.prevBlock) + ? util.revHex(entry.prevBlock) : null, - nextblockhash: next ? encoding.revHex(next) : null + nextblockhash: next ? util.revHex(next) : null }; } @@ -2735,7 +2735,7 @@ class RPC extends RPCBase { height: entry.height, version: entry.version, versionHex: hex32(entry.version), - merkleroot: encoding.revHex(entry.merkleRoot), + merkleroot: util.revHex(entry.merkleRoot), coinbase: block.txs[0].inputs[0].script.toJSON(), tx: txs, time: entry.time, @@ -2744,9 +2744,9 @@ class RPC extends RPCBase { difficulty: toDifficulty(entry.bits), chainwork: entry.chainwork.toString('hex', 64), previousblockhash: entry.prevBlock !== encoding.NULL_HASH - ? encoding.revHex(entry.prevBlock) + ? util.revHex(entry.prevBlock) : null, - nextblockhash: next ? encoding.revHex(next) : null + nextblockhash: next ? util.revHex(next) : null }; } @@ -2765,7 +2765,7 @@ class RPC extends RPCBase { ancestorcount: this.mempool.countAncestors(entry), ancestorsize: 0, ancestorfees: 0, - depends: this.mempool.getDepends(entry.tx).map(encoding.revHex) + depends: this.mempool.getDepends(entry.tx).map(util.revHex) }; } } diff --git a/lib/primitives/abstractblock.js b/lib/primitives/abstractblock.js index 761528fd..756a2d5e 100644 --- a/lib/primitives/abstractblock.js +++ b/lib/primitives/abstractblock.js @@ -10,6 +10,7 @@ const assert = require('assert'); const hash256 = require('bcrypto/lib/hash256'); const bio = require('bufio'); +const util = require('../utils/util'); const InvItem = require('./invitem'); const consensus = require('../protocol/consensus'); const {encoding} = bio; @@ -93,8 +94,8 @@ class AbstractBlock { assert((json.nonce >>> 0) === json.nonce); this.version = json.version; - this.prevBlock = encoding.revHex(json.prevBlock); - this.merkleRoot = encoding.revHex(json.merkleRoot); + this.prevBlock = util.revHex(json.prevBlock); + this.merkleRoot = util.revHex(json.merkleRoot); this.time = json.time; this.bits = json.bits; this.nonce = json.nonce; @@ -244,7 +245,7 @@ class AbstractBlock { */ rhash() { - return encoding.revHex(this.hash('hex')); + return util.revHex(this.hash('hex')); } /** diff --git a/lib/primitives/block.js b/lib/primitives/block.js index 1f1cd9de..d6a13cb6 100644 --- a/lib/primitives/block.js +++ b/lib/primitives/block.js @@ -540,10 +540,10 @@ class Block extends AbstractBlock { virtualSize: this.getVirtualSize(), date: util.date(this.time), version: this.version.toString(16), - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), commitmentHash: commitmentHash - ? encoding.revHex(commitmentHash) + ? util.revHex(commitmentHash) : null, time: this.time, bits: this.bits, @@ -583,8 +583,8 @@ class Block extends AbstractBlock { height: height, depth: depth, version: this.version, - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), time: this.time, bits: this.bits, nonce: this.nonce, diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index 9c4b2462..994a79a2 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -9,6 +9,7 @@ const assert = require('assert'); const bio = require('bufio'); +const util = require('../utils/util'); const Amount = require('../btc/amount'); const Output = require('./output'); const Network = require('../protocol/network'); @@ -184,7 +185,7 @@ class Coin extends Output { */ rhash() { - return encoding.revHex(this.hash); + return util.revHex(this.hash); } /** @@ -209,7 +210,7 @@ class Coin extends Output { value: Amount.btc(this.value), script: this.script, coinbase: this.coinbase, - hash: this.hash ? encoding.revHex(this.hash) : null, + hash: this.hash ? util.revHex(this.hash) : null, index: this.index, address: this.getAddress() }; @@ -280,7 +281,7 @@ class Coin extends Output { assert(typeof json.hash === 'string', 'Hash must be a string.'); assert(json.hash.length === 64, 'Hash must be a string.'); assert((json.index >>> 0) === json.index, 'Index must be a uint32.'); - this.hash = encoding.revHex(json.hash); + this.hash = util.revHex(json.hash); this.index = json.index; } diff --git a/lib/primitives/headers.js b/lib/primitives/headers.js index a02a83ba..1e52440a 100644 --- a/lib/primitives/headers.js +++ b/lib/primitives/headers.js @@ -10,7 +10,6 @@ const bio = require('bufio'); const util = require('../utils/util'); const AbstractBlock = require('./abstractblock'); -const {encoding} = bio; /** * Headers @@ -199,8 +198,8 @@ class Headers extends AbstractBlock { hash: this.rhash(), height: height, version: this.version, - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), time: this.time, bits: this.bits, nonce: this.nonce @@ -252,8 +251,8 @@ class Headers extends AbstractBlock { height: height != null ? height : -1, date: util.date(this.time), version: this.version.toString(16), - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), time: this.time, bits: this.bits, nonce: this.nonce diff --git a/lib/primitives/invitem.js b/lib/primitives/invitem.js index 29468b48..b077c2e7 100644 --- a/lib/primitives/invitem.js +++ b/lib/primitives/invitem.js @@ -8,7 +8,7 @@ 'use strict'; const bio = require('bufio'); -const {encoding} = bio; +const util = require('../utils/util'); /** * Inv Item @@ -152,7 +152,7 @@ class InvItem { */ rhash() { - return encoding.revHex(this.hash); + return util.revHex(this.hash); } } diff --git a/lib/primitives/merkleblock.js b/lib/primitives/merkleblock.js index 6225c328..57b93bca 100644 --- a/lib/primitives/merkleblock.js +++ b/lib/primitives/merkleblock.js @@ -306,8 +306,8 @@ class MerkleBlock extends AbstractBlock { height: height != null ? height : -1, date: util.date(this.time), version: this.version.toString(16), - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), time: this.time, bits: this.bits, nonce: this.nonce, @@ -448,14 +448,14 @@ class MerkleBlock extends AbstractBlock { hash: this.rhash(), height: height, version: this.version, - prevBlock: encoding.revHex(this.prevBlock), - merkleRoot: encoding.revHex(this.merkleRoot), + prevBlock: util.revHex(this.prevBlock), + merkleRoot: util.revHex(this.merkleRoot), time: this.time, bits: this.bits, nonce: this.nonce, totalTX: this.totalTX, hashes: this.hashes.map((hash) => { - return encoding.revHex(hash.toString('hex')); + return util.revHex(hash.toString('hex')); }), flags: this.flags.toString('hex') }; @@ -476,7 +476,7 @@ class MerkleBlock extends AbstractBlock { this.parseJSON(json); for (let hash of json.hashes) { - hash = encoding.revHex(hash); + hash = util.revHex(hash); this.hashes.push(Buffer.from(hash, 'hex')); } diff --git a/lib/primitives/outpoint.js b/lib/primitives/outpoint.js index 27ff4671..1ee4e6b4 100644 --- a/lib/primitives/outpoint.js +++ b/lib/primitives/outpoint.js @@ -8,6 +8,7 @@ const assert = require('assert'); const bio = require('bufio'); +const util = require('../utils/util'); const {encoding} = bio; /** @@ -120,7 +121,7 @@ class Outpoint { */ rhash() { - return encoding.revHex(this.hash); + return util.revHex(this.hash); } /** @@ -247,7 +248,7 @@ class Outpoint { assert(json, 'Outpoint data is required.'); assert(typeof json.hash === 'string', 'Hash must be a string.'); assert((json.index >>> 0) === json.index, 'Index must be a uint32.'); - this.hash = encoding.revHex(json.hash); + this.hash = util.revHex(json.hash); this.index = json.index; return this; } @@ -262,7 +263,7 @@ class Outpoint { toJSON() { return { - hash: encoding.revHex(this.hash), + hash: util.revHex(this.hash), index: this.index }; } diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 52325cc4..ad357dc6 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -2012,7 +2012,7 @@ class TX { */ rhash() { - return encoding.revHex(this.hash('hex')); + return util.revHex(this.hash('hex')); } /** @@ -2021,7 +2021,7 @@ class TX { */ rwhash() { - return encoding.revHex(this.witnessHash('hex')); + return util.revHex(this.witnessHash('hex')); } /** @@ -2089,7 +2089,7 @@ class TX { if (entry) { height = entry.height; - block = encoding.revHex(entry.hash); + block = util.revHex(entry.hash); time = entry.time; date = util.date(time); } @@ -2157,7 +2157,7 @@ class TX { if (entry) { height = entry.height; - block = encoding.revHex(entry.hash); + block = util.revHex(entry.hash); time = entry.time; date = util.date(time); } diff --git a/lib/primitives/txmeta.js b/lib/primitives/txmeta.js index 6bdd8b5f..195838c6 100644 --- a/lib/primitives/txmeta.js +++ b/lib/primitives/txmeta.js @@ -10,7 +10,6 @@ const assert = require('assert'); const bio = require('bufio'); const util = require('../utils/util'); const TX = require('./tx'); -const {encoding} = bio; /** * TXMeta @@ -132,7 +131,7 @@ class TXMeta { const data = this.tx.format(view, null, this.index); data.mtime = this.mtime; data.height = this.height; - data.block = this.block ? encoding.revHex(this.block) : null; + data.block = this.block ? util.revHex(this.block) : null; data.time = this.time; return data; } @@ -158,7 +157,7 @@ class TXMeta { const json = this.tx.getJSON(network, view, null, this.index); json.mtime = this.mtime; json.height = this.height; - json.block = this.block ? encoding.revHex(this.block) : null; + json.block = this.block ? util.revHex(this.block) : null; json.time = this.time; json.confirmations = 0; @@ -185,7 +184,7 @@ class TXMeta { this.mtime = json.mtime; this.height = json.height; - this.block = encoding.revHex(json.block); + this.block = util.revHex(json.block); this.index = json.index; return this; diff --git a/lib/script/script.js b/lib/script/script.js index e59063d2..1feb2867 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -14,7 +14,6 @@ const sha1 = require('bcrypto/lib/sha1'); const sha256 = require('bcrypto/lib/sha256'); const hash160 = require('bcrypto/lib/hash160'); const hash256 = require('bcrypto/lib/hash256'); -const merkle = require('bcrypto/lib/merkle'); const secp256k1 = require('bcrypto/lib/secp256k1'); const consensus = require('../protocol/consensus'); const policy = require('../protocol/policy'); diff --git a/lib/utils/util.js b/lib/utils/util.js index 11fac487..bed8f375 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -7,6 +7,8 @@ 'use strict'; +const assert = require('assert'); + /** * @exports utils/util */ diff --git a/lib/wallet/client.js b/lib/wallet/client.js index ec810433..1bdd52a3 100644 --- a/lib/wallet/client.js +++ b/lib/wallet/client.js @@ -8,8 +8,8 @@ 'use strict'; const assert = require('assert'); -const {encoding} = require('bufio'); const {NodeClient} = require('bclient'); +const util = require('../utils/util'); const TX = require('../primitives/tx'); const hash256 = require('bcrypto/lib/hash256'); @@ -58,7 +58,7 @@ class WalletClient extends NodeClient { async getEntry(block) { if (typeof block === 'string') - block = encoding.revHex(block); + block = util.revHex(block); return parseEntry(await super.getEntry(block)); } @@ -73,7 +73,7 @@ class WalletClient extends NodeClient { async rescan(start) { if (typeof start === 'string') - start = encoding.revHex(start); + start = util.revHex(start); return super.rescan(start); } diff --git a/lib/wallet/records.js b/lib/wallet/records.js index 4bab3ae4..04e09527 100644 --- a/lib/wallet/records.js +++ b/lib/wallet/records.js @@ -149,7 +149,7 @@ class BlockMeta { */ fromJSON(json) { - this.hash = encoding.revHex(json.hash); + this.hash = util.revHex(json.hash); this.height = json.height; this.time = json.time; return this; @@ -220,7 +220,7 @@ class BlockMeta { toJSON() { return { - hash: encoding.revHex(this.hash), + hash: util.revHex(this.hash), height: this.height, time: this.time }; diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index d297a459..3fc700dc 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -316,7 +316,7 @@ class RPC extends RPCBase { format('# Wallet Dump created by Bcoin %s', pkg.version), format('# * Created on %s', time), format('# * Best block at time of backup was %d (%s).', - tip.height, encoding.revHex(tip.hash)), + tip.height, util.revHex(tip.hash)), format('# * File: %s', file), '' ]; @@ -632,10 +632,10 @@ class RPC extends RPCBase { return { amount: Amount.btc(receive ? received : -sent, true), confirmations: details.confirmations, - blockhash: details.block ? encoding.revHex(details.block) : null, + blockhash: details.block ? util.revHex(details.block) : null, blockindex: details.index, blocktime: details.time, - txid: encoding.revHex(details.hash), + txid: util.revHex(details.hash), walletconflicts: [], time: details.mtime, timereceived: details.mtime, @@ -1063,7 +1063,7 @@ class RPC extends RPCBase { return { transactions: out, lastblock: highest && highest.block - ? encoding.revHex(highest.block) + ? util.revHex(highest.block) : encoding.NULL_HASH }; } @@ -1129,10 +1129,10 @@ class RPC extends RPCBase { label: member.path ? member.path.name : undefined, vout: index, confirmations: details.getDepth(), - blockhash: details.block ? encoding.revHex(details.block) : null, + blockhash: details.block ? util.revHex(details.block) : null, blockindex: details.index, blocktime: details.time, - txid: encoding.revHex(details.hash), + txid: util.revHex(details.hash), walletconflicts: [], time: details.mtime, timereceived: details.mtime, diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 9671d425..506b7c28 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -2525,9 +2525,9 @@ class Details { const rate = this.getRate(fee); return { - hash: encoding.revHex(this.hash), + hash: util.revHex(this.hash), height: this.height, - block: this.block ? encoding.revHex(this.block) : null, + block: this.block ? util.revHex(this.block) : null, time: this.time, mtime: this.mtime, date: util.date(this.time), @@ -2723,10 +2723,10 @@ class BlockRecord { toJSON() { return { - hash: encoding.revHex(this.hash), + hash: util.revHex(this.hash), height: this.height, time: this.time, - hashes: this.toArray().map(encoding.revHex) + hashes: this.toArray().map(util.revHex) }; } diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 4fb8647c..fcd47a9a 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -17,6 +17,7 @@ const BDB = require('bdb'); const Logger = require('blgr'); const ccmp = require('bcrypto/lib/ccmp'); const aes = require('bcrypto/lib/aes'); +const util = require('../utils/util'); const Network = require('../protocol/network'); const Path = require('./path'); const common = require('./common'); @@ -1748,7 +1749,7 @@ class WalletDB extends EventEmitter { if (total > 0) { this.logger.info('Connected WalletDB block %s (tx=%d).', - encoding.revHex(tip.hash), total); + util.revHex(tip.hash), total); } return total; @@ -1816,7 +1817,7 @@ class WalletDB extends EventEmitter { await this.setTip(prev); this.logger.warning('Disconnected wallet block %s (tx=%d).', - encoding.revHex(tip.hash), total); + util.revHex(tip.hash), total); return total; } diff --git a/migrate/chaindb2to3.js b/migrate/chaindb2to3.js index ed434bdc..2ae30d54 100644 --- a/migrate/chaindb2to3.js +++ b/migrate/chaindb2to3.js @@ -20,6 +20,7 @@ const BDB = require('bdb'); const hash256 = require('bcrypto/lib/hash256'); const BN = require('bcrypto/lib/bn'); const bio = require('bufio'); +const util = require('../lib/utils/util'); const OldCoins = require('./coins/coins'); const OldUndoCoins = require('./coins/undocoins'); const CoinEntry = require('../lib/coins/coinentry'); @@ -131,7 +132,7 @@ async function reserializeUndo(hash) { tip = await getEntry(hash); console.log('Reserializing undo coins from tip %s.', - encoding.revHex(tip.hash)); + util.revHex(tip.hash)); let batch = db.batch(); let pruning = false; @@ -142,7 +143,7 @@ async function reserializeUndo(hash) { if (shouldPrune) { if (tip.height < height - 288) { console.log('Pruning block %s (%d).', - encoding.revHex(tip.hash), tip.height); + util.revHex(tip.hash), tip.height); batch.del(pair('u', tip.hash)); batch.del(pair('b', tip.hash)); @@ -185,7 +186,7 @@ async function reserializeUndo(hash) { console.log( 'Reserializing coins for block %s (%d).', - encoding.revHex(tip.hash), tip.height); + util.revHex(tip.hash), tip.height); for (let i = block.txs.length - 1; i >= 1; i--) { const tx = block.txs[i]; @@ -313,7 +314,7 @@ async function reserializeCoins(hash) { start = false; } - console.log('Reserializing coins from %s.', encoding.revHex(hash)); + console.log('Reserializing coins from %s.', util.revHex(hash)); let batch = db.batch(); let total = 0; @@ -388,7 +389,7 @@ async function reserializeEntries(hash) { assert(item.key.equals(pair('e', hash))); } - console.log('Reserializing entries from %s.', encoding.revHex(hash)); + console.log('Reserializing entries from %s.', util.revHex(hash)); const tip = await getTipHash(); diff --git a/migrate/ensure-tip-index.js b/migrate/ensure-tip-index.js index be4e7f8c..7d16a2a1 100644 --- a/migrate/ensure-tip-index.js +++ b/migrate/ensure-tip-index.js @@ -5,6 +5,7 @@ const BDB = require('bdb'); const bio = require('bufio'); const hash256 = require('bcrypto/lib/hash256'); const BN = require('bcrypto/lib/bn'); +const util = require('../lib/utils/util'); const {encoding} = bio; const DUMMY = Buffer.from([0]); @@ -114,7 +115,7 @@ async function indexTips() { for (let i = 0; i < tips.length; i++) { const tip = tips[i]; - console.log('Indexing chain tip: %s.', encoding.revHex(tip)); + console.log('Indexing chain tip: %s.', util.revHex(tip)); batch.put(pair('p', tip), DUMMY); } } diff --git a/test/chain-test.js b/test/chain-test.js index 27f5bd9f..428c4d61 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -5,7 +5,6 @@ const assert = require('./util/assert'); const consensus = require('../lib/protocol/consensus'); -const {encoding} = require('bufio'); const Coin = require('../lib/primitives/coin'); const Script = require('../lib/script/script'); const Chain = require('../lib/blockchain/chain'); diff --git a/test/input-test.js b/test/input-test.js index 25054d5b..83e7e2f6 100644 --- a/test/input-test.js +++ b/test/input-test.js @@ -4,10 +4,10 @@ 'use strict'; const bio = require('bufio'); +const util = require('../lib/utils/util'); const Input = require('../lib/primitives/input'); const assert = require('./util/assert'); const common = require('./util/common'); -const {encoding} = bio; // Take input rawbytes from the raw data format // p2pkh @@ -236,7 +236,7 @@ describe('Input', function() { const inputs = test.inputs.map((prevout, i) => { const input = Input.fromOptions({ prevout: { - hash: encoding.revHex(prevout.txId), + hash: util.revHex(prevout.txId), index: prevout.vout } }); diff --git a/test/mempool-test.js b/test/mempool-test.js index 674a43c5..995f9539 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -4,7 +4,6 @@ 'use strict'; const assert = require('./util/assert'); -const {encoding} = require('bufio'); const random = require('bcrypto/lib/random'); const MempoolEntry = require('../lib/mempool/mempoolentry'); const Mempool = require('../lib/mempool/mempool'); diff --git a/test/tx-test.js b/test/tx-test.js index 88862eeb..ca1aad38 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -7,6 +7,7 @@ const {inspect} = require('util'); const {encoding} = require('bufio'); const assert = require('./util/assert'); const random = require('bcrypto/lib/random'); +const util = require('../lib/utils/util'); const consensus = require('../lib/protocol/consensus'); const TX = require('../lib/primitives/tx'); const Output = require('../lib/primitives/output'); @@ -65,7 +66,7 @@ function parseTXTest(data) { const view = new CoinView(); for (const [txid, index, str, amount] of coins) { - const hash = encoding.revHex(txid); + const hash = util.revHex(txid); const script = Script.fromString(str); const value = parseInt(amount || '0', 10); @@ -102,7 +103,7 @@ function parseSighashTest(data) { const tx = TX.fromRaw(txHex, 'hex'); const script = Script.fromRaw(scriptHex, 'hex'); - const expected = encoding.revHex(hash); + const expected = util.revHex(hash); let hex = type & 3;