refactor: start moving away from getters.

This commit is contained in:
Christopher Jeffrey 2016-11-30 21:31:52 -08:00
parent 0e234c7c3a
commit ded3bc34f3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
22 changed files with 148 additions and 103 deletions

View File

@ -122,8 +122,8 @@ Chain.prototype._init = function _init() {
entry.height, entry.height,
self.tip.height, self.tip.height,
entry.height, entry.height,
self.tip.rhash, self.tip.rhash(),
entry.rhash, entry.rhash(),
self.tip.chainwork.toString(), self.tip.chainwork.toString(),
entry.chainwork.toString(), entry.chainwork.toString(),
self.tip.chainwork.sub(entry.chainwork).toString()); self.tip.chainwork.sub(entry.chainwork).toString());
@ -131,7 +131,7 @@ Chain.prototype._init = function _init() {
this.on('resolved', function(block, entry) { this.on('resolved', function(block, entry) {
self.logger.debug('Orphan %s (%d) was resolved.', self.logger.debug('Orphan %s (%d) was resolved.',
block.rhash, entry.height); block.rhash(), entry.height);
}); });
this.on('checkpoint', function(hash, height) { this.on('checkpoint', function(hash, height) {
@ -153,21 +153,21 @@ Chain.prototype._init = function _init() {
'Reorg at height %d: old=%s new=%s', 'Reorg at height %d: old=%s new=%s',
height, height,
util.revHex(expected), util.revHex(expected),
block.rhash block.rhash()
); );
}); });
this.on('invalid', function(block, height) { this.on('invalid', function(block, height) {
self.logger.warning('Invalid block at height %d: hash=%s', self.logger.warning('Invalid block at height %d: hash=%s',
height, block.rhash); height, block.rhash());
}); });
this.on('exists', function(block, height) { this.on('exists', function(block, height) {
self.logger.debug('Already have block %s (%d).', block.rhash, height); self.logger.debug('Already have block %s (%d).', block.rhash(), height);
}); });
this.on('orphan', function(block, height) { this.on('orphan', function(block, height) {
self.logger.debug('Handled orphan %s (%d).', block.rhash, height); self.logger.debug('Handled orphan %s (%d).', block.rhash(), height);
}); });
this.on('purge', function(count, size) { this.on('purge', function(count, size) {
@ -1406,7 +1406,7 @@ Chain.prototype.finish = function finish(block, entry) {
this.logger.info( this.logger.info(
'Block %s (%d) added to chain (size=%d txs=%d time=%d).', 'Block %s (%d) added to chain (size=%d txs=%d time=%d).',
entry.rhash, entry.rhash(),
entry.height, entry.height,
block.getSize(), block.getSize(),
block.txs.length, block.txs.length,

View File

@ -148,7 +148,7 @@ ChainDB.prototype._open = co(function* open() {
this.logger.info( this.logger.info(
'Chain State: hash=%s tx=%d coin=%d value=%s.', 'Chain State: hash=%s tx=%d coin=%d value=%s.',
this.state.rhash, this.state.rhash(),
this.state.tx, this.state.tx,
this.state.coin, this.state.coin,
Amount.btc(this.state.value)); Amount.btc(this.state.value));
@ -500,7 +500,7 @@ ChainDB.prototype.hasEntry = co(function* hasEntry(block) {
*/ */
ChainDB.prototype.getTip = function getTip() { ChainDB.prototype.getTip = function getTip() {
return this.getEntry(this.state.hash); return this.getEntry(this.state.hash());
}; };
/** /**
@ -1243,7 +1243,7 @@ ChainDB.prototype.scan = co(function* scan(start, filter, iter) {
this.logger.info( this.logger.info(
'Scanning block %s (%d).', 'Scanning block %s (%d).',
entry.rhash, entry.height); entry.rhash(), entry.height);
for (i = 0; i < block.txs.length; i++) { for (i = 0; i < block.txs.length; i++) {
tx = block.txs[i]; tx = block.txs[i];
@ -1510,7 +1510,7 @@ ChainDB.prototype.reset = co(function* reset(block) {
tip = yield this.getTip(); tip = yield this.getTip();
assert(tip); assert(tip);
this.logger.debug('Resetting main chain to: %s', entry.rhash); this.logger.debug('Resetting main chain to: %s', entry.rhash());
for (;;) { for (;;) {
this.start(); this.start();
@ -1596,7 +1596,7 @@ ChainDB.prototype._removeChain = co(function* removeChain(hash) {
if (!tip) if (!tip)
throw new Error('Alternate chain tip not found.'); throw new Error('Alternate chain tip not found.');
this.logger.debug('Removing alternate chain: %s.', tip.rhash); this.logger.debug('Removing alternate chain: %s.', tip.rhash());
for (;;) { for (;;) {
if (yield tip.isMainChain()) if (yield tip.isMainChain())
@ -2092,13 +2092,13 @@ function ChainState() {
this.committed = false; this.committed = false;
} }
ChainState.prototype.__defineGetter__('hash', function() { ChainState.prototype.hash = function() {
return this.tip.toString('hex'); return this.tip.toString('hex');
}); };
ChainState.prototype.__defineGetter__('rhash', function() { ChainState.prototype.rhash = function() {
return util.revHex(this.hash); return util.revHex(this.hash());
}); };
ChainState.prototype.clone = function clone() { ChainState.prototype.clone = function clone() {
var state = new ChainState(); var state = new ChainState();

View File

@ -383,9 +383,14 @@ ChainEntry.prototype.hasBit = function hasBit(bit) {
return (bits >>> 0) === topBits && (this.version & mask) !== 0; return (bits >>> 0) === topBits && (this.version & mask) !== 0;
}; };
ChainEntry.prototype.__defineGetter__('rhash', function() { /**
* Get little-endian block hash.
* @returns {Hash}
*/
ChainEntry.prototype.rhash = function() {
return util.revHex(this.hash); return util.revHex(this.hash);
}); };
/** /**
* Inject properties from block. * Inject properties from block.

View File

@ -711,7 +711,7 @@ RPC.prototype.getbestblockhash = function getbestblockhash(args) {
if (args.help || args.length !== 0) if (args.help || args.length !== 0)
return Promise.reject(new RPCError('getbestblockhash')); return Promise.reject(new RPCError('getbestblockhash'));
return Promise.resolve(this.chain.tip.rhash); return Promise.resolve(this.chain.tip.rhash());
}; };
RPC.prototype.getblockcount = function getblockcount(args) { RPC.prototype.getblockcount = function getblockcount(args) {
@ -763,8 +763,8 @@ RPC.prototype.getblock = co(function* getblock(args) {
RPC.prototype._txToJSON = function _txToJSON(tx) { RPC.prototype._txToJSON = function _txToJSON(tx) {
var self = this; var self = this;
return { return {
txid: tx.txid, txid: tx.txid(),
hash: tx.wtxid, hash: tx.wtxid(),
size: tx.getSize(), size: tx.getSize(),
vsize: tx.getVirtualSize(), vsize: tx.getVirtualSize(),
version: tx.version, version: tx.version,
@ -840,7 +840,7 @@ RPC.prototype.getblockhash = co(function* getblockhash(args) {
if (!entry) if (!entry)
throw new RPCError('Not found.'); throw new RPCError('Not found.');
return entry.rhash; return entry.rhash();
}); });
RPC.prototype.getblockheader = co(function* getblockheader(args) { RPC.prototype.getblockheader = co(function* getblockheader(args) {
@ -909,7 +909,7 @@ RPC.prototype._blockToJSON = co(function* _blockToJSON(entry, block, txDetails)
tx: block.txs.map(function(tx) { tx: block.txs.map(function(tx) {
if (txDetails) if (txDetails)
return self._txToJSON(tx); return self._txToJSON(tx);
return tx.rhash; return tx.txid();
}), }),
time: entry.ts, time: entry.ts,
mediantime: medianTime, mediantime: medianTime,
@ -942,7 +942,7 @@ RPC.prototype.getchaintips = co(function* getchaintips(args) {
result.push({ result.push({
height: entry.height, height: entry.height,
hash: entry.rhash, hash: entry.rhash(),
branchlen: entry.height - fork.height, branchlen: entry.height - fork.height,
status: main ? 'active' : 'valid-headers' status: main ? 'active' : 'valid-headers'
}); });
@ -1012,7 +1012,7 @@ RPC.prototype.getmempoolancestors = function getmempoolancestors(args) {
entries[i] = this._entryToJSON(entries[i]); entries[i] = this._entryToJSON(entries[i]);
} else { } else {
for (i = 0; i < entries.length; i++) for (i = 0; i < entries.length; i++)
entries[i] = entries[i].tx.rhash; entries[i] = entries[i].tx.txid();
} }
return Promise.resolve(entries); return Promise.resolve(entries);
@ -1047,7 +1047,7 @@ RPC.prototype.getmempooldescendants = function getmempooldescendants(args) {
entries[i] = this._entryToJSON(entries[i]); entries[i] = this._entryToJSON(entries[i]);
} else { } else {
for (i = 0; i < entries.length; i++) for (i = 0; i < entries.length; i++)
entries[i] = entries[i].tx.rhash; entries[i] = entries[i].tx.txid();
} }
return Promise.resolve(entries); return Promise.resolve(entries);
@ -1103,7 +1103,7 @@ RPC.prototype._mempoolToJSON = function _mempoolToJSON(verbose) {
if (!entry) if (!entry)
continue; continue;
out[entry.tx.rhash] = this._entryToJSON(entry); out[entry.tx.txid()] = this._entryToJSON(entry);
} }
return out; return out;
@ -1166,7 +1166,7 @@ RPC.prototype.gettxout = co(function* gettxout(args) {
return null; return null;
return { return {
bestblock: this.chain.tip.rhash, bestblock: this.chain.tip.rhash(),
confirmations: coin.getConfirmations(this.chain.height), confirmations: coin.getConfirmations(this.chain.height),
value: Amount.btc(coin.value, true), value: Amount.btc(coin.value, true),
scriptPubKey: this._scriptToJSON(coin.script, true), scriptPubKey: this._scriptToJSON(coin.script, true),
@ -1274,7 +1274,7 @@ RPC.prototype.gettxoutsetinfo = function gettxoutsetinfo(args) {
return Promise.resolve({ return Promise.resolve({
height: this.chain.height, height: this.chain.height,
bestblock: this.chain.tip.rhash, bestblock: this.chain.tip.rhash(),
transactions: this.chain.db.state.tx, transactions: this.chain.db.state.tx,
txouts: this.chain.db.state.coin, txouts: this.chain.db.state.coin,
bytes_serialized: 0, bytes_serialized: 0,
@ -1560,8 +1560,8 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
txs.push({ txs.push({
data: tx.toRaw().toString('hex'), data: tx.toRaw().toString('hex'),
txid: tx.rhash, txid: tx.txid(),
hash: tx.rwhash, hash: tx.rwhash(),
depends: deps, depends: deps,
fee: tx.getFee(), fee: tx.getFee(),
sigops: tx.getSigops(), sigops: tx.getSigops(),
@ -1615,7 +1615,7 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
vbrequired: 0, vbrequired: 0,
previousblockhash: util.revHex(block.prevBlock), previousblockhash: util.revHex(block.prevBlock),
transactions: txs, transactions: txs,
longpollid: this.chain.tip.rhash + util.pad32(this._totalTX()), longpollid: this.chain.tip.rhash() + util.pad32(this._totalTX()),
target: util.revHex(attempt.target.toString('hex')), target: util.revHex(attempt.target.toString('hex')),
submitold: false, submitold: false,
mintime: block.ts, mintime: block.ts,
@ -1641,16 +1641,16 @@ RPC.prototype.__template = co(function* _template(version, coinbase, rules) {
output = tx.outputs.pop(); output = tx.outputs.pop();
assert(output.script.isCommitment()); assert(output.script.isCommitment());
raw = tx.toRaw(); raw = tx.toRaw();
rwhash = tx.rwhash; rwhash = tx.rwhash();
tx.outputs.push(output); tx.outputs.push(output);
} else { } else {
raw = tx.toRaw(); raw = tx.toRaw();
rwhash = tx.rwhash; rwhash = tx.rwhash();
} }
template.coinbasetxn = { template.coinbasetxn = {
data: raw.toString('hex'), data: raw.toString('hex'),
txid: tx.rhash, txid: tx.txid(),
hash: rwhash, hash: rwhash,
depends: [], depends: [],
fee: 0, fee: 0,
@ -1933,7 +1933,7 @@ RPC.prototype._generateBlocks = co(function* _generateBlocks(blocks, address) {
for (i = 0; i < blocks; i++) { for (i = 0; i < blocks; i++) {
block = yield this.miner.mineBlock(null, address); block = yield this.miner.mineBlock(null, address);
hashes.push(block.rhash); hashes.push(block.rhash());
yield this.chain.add(block); yield this.chain.add(block);
} }
@ -2141,7 +2141,7 @@ RPC.prototype.sendrawtransaction = function sendrawtransaction(args) {
this.node.sendTX(tx); this.node.sendTX(tx);
return tx.rhash; return tx.txid();
}; };
RPC.prototype.signrawtransaction = co(function* signrawtransaction(args) { RPC.prototype.signrawtransaction = co(function* signrawtransaction(args) {
@ -2658,7 +2658,7 @@ RPC.prototype.resendwallettransactions = co(function* resendwallettransactions(a
for (i = 0; i < txs.length; i++) { for (i = 0; i < txs.length; i++) {
tx = txs[i]; tx = txs[i];
hashes.push(tx.rhash); hashes.push(tx.txid());
} }
return hashes; return hashes;
@ -2730,7 +2730,7 @@ RPC.prototype.dumpwallet = co(function* dumpwallet(args) {
util.fmt('# Wallet Dump created by BCoin %s', constants.USER_VERSION), util.fmt('# Wallet Dump created by BCoin %s', constants.USER_VERSION),
util.fmt('# * Created on %s', time), util.fmt('# * Created on %s', time),
util.fmt('# * Best block at time of backup was %d (%s),', util.fmt('# * Best block at time of backup was %d (%s),',
this.chain.height, this.chain.tip.rhash), this.chain.height, this.chain.tip.rhash()),
util.fmt('# mined on %s', util.date(this.chain.tip.ts)), util.fmt('# mined on %s', util.date(this.chain.tip.ts)),
util.fmt('# * File: %s', file), util.fmt('# * File: %s', file),
'' ''
@ -3767,7 +3767,7 @@ RPC.prototype.lockunspent = function lockunspent(args) {
outpoint.hash = toHash(output.txid); outpoint.hash = toHash(output.txid);
outpoint.index = toNumber(output.vout); outpoint.index = toNumber(output.vout);
if (!outpoint.txid) if (!outpoint.hash)
return Promise.reject(new RPCError('Invalid paremeter.')); return Promise.reject(new RPCError('Invalid paremeter.'));
if (outpoint.index < 0) if (outpoint.index < 0)
@ -3802,7 +3802,7 @@ RPC.prototype._send = co(function* _send(account, address, amount, subtractFee)
tx = yield this.wallet.send(options); tx = yield this.wallet.send(options);
return tx.rhash; return tx.txid();
}); });
RPC.prototype.sendfrom = function sendfrom(args) { RPC.prototype.sendfrom = function sendfrom(args) {
@ -3884,7 +3884,7 @@ RPC.prototype.sendmany = co(function* sendmany(args) {
tx = yield this.wallet.send(options); tx = yield this.wallet.send(options);
return tx.rhash; return tx.txid();
}); });
RPC.prototype.sendtoaddress = function sendtoaddress(args) { RPC.prototype.sendtoaddress = function sendtoaddress(args) {

View File

@ -579,7 +579,7 @@ HTTPServer.prototype._init = function _init() {
services: this.pool.services.toString(2), services: this.pool.services.toString(2),
network: this.network.type, network: this.network.type,
height: this.chain.height, height: this.chain.height,
tip: this.chain.tip.rhash, tip: this.chain.tip.rhash(),
outbound: this.pool.peers.outbound.length + loader, outbound: this.pool.peers.outbound.length + loader,
pending: this.pool.peers.pending.length, pending: this.pool.peers.pending.length,
inbound: this.pool.peers.inbound.length, inbound: this.pool.peers.inbound.length,

View File

@ -560,7 +560,7 @@ PolicyEstimator.prototype.processTX = function processTX(entry, current) {
if (this.map[hash]) { if (this.map[hash]) {
this.logger.debug( this.logger.debug(
'estimatefee: Mempool tx %s already tracked.', 'estimatefee: Mempool tx %s already tracked.',
entry.tx.rhash); entry.tx.txid());
return; return;
} }
@ -580,7 +580,7 @@ PolicyEstimator.prototype.processTX = function processTX(entry, current) {
rate = entry.getRate(); rate = entry.getRate();
priority = entry.getPriority(height); priority = entry.getPriority(height);
this.logger.spam('estimatefee: Processing mempool tx %s.', entry.tx.rhash); this.logger.spam('estimatefee: Processing mempool tx %s.', entry.tx.txid());
if (fee === 0 || this.isPriPoint(rate, priority)) { if (fee === 0 || this.isPriPoint(rate, priority)) {
item = new StatEntry(); item = new StatEntry();
@ -593,7 +593,7 @@ PolicyEstimator.prototype.processTX = function processTX(entry, current) {
} }
if (!item) { if (!item) {
this.logger.spam('estimatefee: Not adding tx %s.', entry.tx.rhash); this.logger.spam('estimatefee: Not adding tx %s.', entry.tx.txid());
return; return;
} }
@ -618,7 +618,7 @@ PolicyEstimator.prototype.processBlockTX = function processBlockTX(height, entry
if (blocks <= 0) { if (blocks <= 0) {
this.logger.debug( this.logger.debug(
'estimatefee: Block tx %s had negative blocks to confirm (%d, %d).', 'estimatefee: Block tx %s had negative blocks to confirm (%d, %d).',
entry.tx.rhash, entry.tx.txid(),
height, height,
entry.height); entry.height);
return; return;

View File

@ -803,7 +803,7 @@ Mempool.prototype._addUnchecked = co(function* addUnchecked(entry) {
if (this.fees) if (this.fees)
this.fees.processTX(entry, this.chain.isFull()); this.fees.processTX(entry, this.chain.isFull());
this.logger.debug('Added tx %s to mempool.', entry.tx.rhash); this.logger.debug('Added tx %s to mempool.', entry.tx.txid());
resolved = this.resolveOrphans(entry.tx); resolved = this.resolveOrphans(entry.tx);
@ -816,7 +816,7 @@ Mempool.prototype._addUnchecked = co(function* addUnchecked(entry) {
} catch (err) { } catch (err) {
if (err.type === 'VerifyError') { if (err.type === 'VerifyError') {
this.logger.debug('Could not resolve orphan %s: %s.', this.logger.debug('Could not resolve orphan %s: %s.',
tx.rhash, tx.txid(),
err.message); err.message);
if (!tx.hasWitness() && !err.malleated) if (!tx.hasWitness() && !err.malleated)
@ -835,7 +835,7 @@ Mempool.prototype._addUnchecked = co(function* addUnchecked(entry) {
continue; continue;
} }
this.logger.spam('Resolved orphan %s in mempool.', tx.rhash); this.logger.spam('Resolved orphan %s in mempool.', tx.txid());
} }
}); });
@ -858,9 +858,9 @@ Mempool.prototype.removeUnchecked = function removeUnchecked(entry, limit) {
// now exist on the blockchain). // now exist on the blockchain).
if (limit) { if (limit) {
this.removeSpenders(entry); this.removeSpenders(entry);
this.logger.debug('Evicting %s from the mempool.', tx.rhash); this.logger.debug('Evicting %s from the mempool.', tx.txid());
} else { } else {
this.logger.spam('Removing block tx %s from mempool.', tx.rhash); this.logger.spam('Removing block tx %s from mempool.', tx.txid());
} }
this.untrackEntry(entry); this.untrackEntry(entry);
@ -1275,7 +1275,7 @@ Mempool.prototype.storeOrphan = function storeOrphan(tx) {
var i, hash, input, prev; var i, hash, input, prev;
if (tx.getWeight() > constants.tx.MAX_WEIGHT) { if (tx.getWeight() > constants.tx.MAX_WEIGHT) {
this.logger.debug('Ignoring large orphan: %s', tx.rhash); this.logger.debug('Ignoring large orphan: %s', tx.txid());
if (!tx.hasWitness()) if (!tx.hasWitness())
this.rejects.add(tx.hash()); this.rejects.add(tx.hash());
return; return;
@ -1288,7 +1288,7 @@ Mempool.prototype.storeOrphan = function storeOrphan(tx) {
continue; continue;
if (this.hasReject(input.prevout.hash)) { if (this.hasReject(input.prevout.hash)) {
this.logger.debug('Not storing orphan %s (rejected parents).', tx.rhash); this.logger.debug('Not storing orphan %s (rejected parents).', tx.txid());
this.rejects.add(tx.hash()); this.rejects.add(tx.hash());
return; return;
} }
@ -1313,7 +1313,7 @@ Mempool.prototype.storeOrphan = function storeOrphan(tx) {
this.orphans[hash] = tx.toExtended(true); this.orphans[hash] = tx.toExtended(true);
this.totalOrphans++; this.totalOrphans++;
this.logger.debug('Added orphan %s to mempool.', tx.rhash); this.logger.debug('Added orphan %s to mempool.', tx.txid());
this.emit('add orphan', tx); this.emit('add orphan', tx);

View File

@ -138,7 +138,7 @@ Miner.prototype._init = function _init() {
this.on('block', function(block) { this.on('block', function(block) {
// Emit the block hex as a failsafe (in case we can't send it) // Emit the block hex as a failsafe (in case we can't send it)
self.logger.info('Found block: %d (%s).', block.height, block.rhash); self.logger.info('Found block: %d (%s).', block.height, block.rhash());
self.logger.debug('Raw: %s', block.toRaw().toString('hex')); self.logger.debug('Raw: %s', block.toRaw().toString('hex'));
}); });

View File

@ -88,13 +88,23 @@ util.inherits(MinerBlock, EventEmitter);
MinerBlock.INTERVAL = 0xffffffff / 1500 | 0; MinerBlock.INTERVAL = 0xffffffff / 1500 | 0;
MinerBlock.prototype.__defineGetter__('hashes', function() { /**
return this.iterations * 0xffffffff + this.block.nonce; * Calculate number of hashes.
}); * @returns {Number}
*/
MinerBlock.prototype.__defineGetter__('rate', function() { MinerBlock.prototype.getHashes = function() {
return this.iterations * 0xffffffff + this.block.nonce;
};
/**
* Calculate hashrate.
* @returns {Number}
*/
MinerBlock.prototype.getRate = function() {
return (this.block.nonce / (util.now() - this.begin)) | 0; return (this.block.nonce / (util.now() - this.begin)) | 0;
}); };
/** /**
* Initialize the block. * Initialize the block.
@ -487,8 +497,8 @@ MinerBlock.prototype.sendStatus = function sendStatus() {
this.emit('status', { this.emit('status', {
block: this.block, block: this.block,
target: this.block.bits, target: this.block.bits,
hashes: this.hashes, hashes: this.getHashes(),
hashrate: this.rate, hashrate: this.getRate(),
height: this.height, height: this.height,
best: util.revHex(this.tip.hash) best: util.revHex(this.tip.hash)
}); });

View File

@ -2304,7 +2304,7 @@ Peer.prototype._handleCmpctBlock = co(function* _handleCmpctBlock(packet) {
this.fire('block', block.toBlock()); this.fire('block', block.toBlock());
this.logger.debug( this.logger.debug(
'Received full compact block %s (%s).', 'Received full compact block %s (%s).',
block.rhash, this.hostname); block.rhash(), this.hostname);
return; return;
} }
@ -2314,14 +2314,14 @@ Peer.prototype._handleCmpctBlock = co(function* _handleCmpctBlock(packet) {
this.logger.debug( this.logger.debug(
'Received semi-full compact block %s (%s).', 'Received semi-full compact block %s (%s).',
block.rhash, this.hostname); block.rhash(), this.hostname);
try { try {
yield block.wait(10000); yield block.wait(10000);
} catch (e) { } catch (e) {
this.logger.debug( this.logger.debug(
'Compact block timed out: %s (%s).', 'Compact block timed out: %s (%s).',
block.rhash, this.hostname); block.rhash(), this.hostname);
delete this.compactBlocks[hash]; delete this.compactBlocks[hash];
} }
@ -2399,7 +2399,7 @@ Peer.prototype._handleBlockTxn = function _handleBlockTxn(packet) {
this.logger.debug( this.logger.debug(
'Filled compact block %s (%s).', 'Filled compact block %s (%s).',
block.rhash, this.hostname); block.rhash(), this.hostname);
this.fire('block', block.toBlock()); this.fire('block', block.toBlock());
this.fire('getblocktxn', res); this.fire('getblocktxn', res);
@ -2512,7 +2512,7 @@ Peer.prototype.sendReject = function sendReject(code, reason, obj) {
if (obj) { if (obj) {
this.logger.debug('Rejecting %s %s (%s): ccode=%s reason=%s.', this.logger.debug('Rejecting %s %s (%s): ccode=%s reason=%s.',
reject.message, obj.rhash, this.hostname, code, reason); reject.message, obj.rhash(), this.hostname, code, reason);
} else { } else {
this.logger.debug('Rejecting packet from %s: ccode=%s reason=%s.', this.logger.debug('Rejecting packet from %s: ccode=%s reason=%s.',
this.hostname, code, reason); this.hostname, code, reason);

View File

@ -946,7 +946,7 @@ Pool.prototype._handleBlock = co(function* _handleBlock(block, peer) {
peer.invFilter.add(block.hash()); peer.invFilter.add(block.hash());
this.logger.warning( this.logger.warning(
'Received unrequested block: %s (%s).', 'Received unrequested block: %s (%s).',
block.rhash, peer.hostname); block.rhash(), peer.hostname);
return yield co.wait(); return yield co.wait();
} }
@ -1004,7 +1004,7 @@ Pool.prototype._handleBlock = co(function* _handleBlock(block, peer) {
this.logger.info( this.logger.info(
'Received 2000 more blocks (height=%d, hash=%s).', 'Received 2000 more blocks (height=%d, hash=%s).',
this.chain.height, this.chain.height,
block.rhash); block.rhash());
} }
}); });
@ -1363,7 +1363,7 @@ Pool.prototype._handleTX = co(function* _handleTX(tx, peer) {
this.txFilter.add(tx.hash()); this.txFilter.add(tx.hash());
this.logger.warning('Peer sent unrequested tx: %s (%s).', this.logger.warning('Peer sent unrequested tx: %s (%s).',
tx.rhash, peer.hostname); tx.txid(), peer.hostname);
if (this.hasReject(tx.hash())) { if (this.hasReject(tx.hash())) {
throw new VerifyError(tx, throw new VerifyError(tx,

View File

@ -316,7 +316,7 @@ FullNode.prototype.sendTX = co(function* sendTX(tx) {
} catch (err) { } catch (err) {
if (err.type === 'VerifyError') { if (err.type === 'VerifyError') {
this._error(err); this._error(err);
this.logger.warning('Verification failed for tx: %s.', tx.rhash); this.logger.warning('Verification failed for tx: %s.', tx.txid());
this.logger.warning('Attempting to broadcast anyway...'); this.logger.warning('Attempting to broadcast anyway...');
yield this.pool.broadcast(tx); yield this.pool.broadcast(tx);
return; return;

View File

@ -282,9 +282,14 @@ AbstractBlock.prototype.setHeight = function setHeight(height) {
this.txs[i].height = height; this.txs[i].height = height;
}; };
AbstractBlock.prototype.__defineGetter__('rhash', function() { /**
* Get little-endian block hash.
* @returns {Hash}
*/
AbstractBlock.prototype.rhash = function() {
return util.revHex(this.hash('hex')); return util.revHex(this.hash('hex'));
}); };
/** /**
* Convert the block to an inv item. * Convert the block to an inv item.

View File

@ -604,7 +604,7 @@ Block.prototype.getPrevout = function getPrevout() {
Block.prototype.inspect = function inspect() { Block.prototype.inspect = function inspect() {
var commitmentHash = this.getCommitmentHash('hex'); var commitmentHash = this.getCommitmentHash('hex');
return { return {
hash: this.rhash, hash: this.rhash(),
height: this.height, height: this.height,
size: this.getSize(), size: this.getSize(),
virtualSize: this.getVirtualSize(), virtualSize: this.getVirtualSize(),
@ -633,7 +633,7 @@ Block.prototype.inspect = function inspect() {
Block.prototype.toJSON = function toJSON(network) { Block.prototype.toJSON = function toJSON(network) {
network = Network.get(network); network = Network.get(network);
return { return {
hash: this.rhash, hash: this.rhash(),
height: this.height, height: this.height,
version: this.version, version: this.version,
prevBlock: util.revHex(this.prevBlock), prevBlock: util.revHex(this.prevBlock),

View File

@ -61,7 +61,7 @@ Headers.prototype.getSize = function getSize() {
Headers.prototype.inspect = function inspect() { Headers.prototype.inspect = function inspect() {
return { return {
type: 'headers', type: 'headers',
hash: this.rhash, hash: this.rhash(),
height: this.height, height: this.height,
date: util.date(this.ts), date: util.date(this.ts),
version: util.hex32(this.version), version: util.hex32(this.version),

View File

@ -318,7 +318,7 @@ MerkleBlock.prototype.getCoinbaseHeight = function getCoinbaseHeight() {
MerkleBlock.prototype.inspect = function inspect() { MerkleBlock.prototype.inspect = function inspect() {
return { return {
type: 'merkleblock', type: 'merkleblock',
hash: this.rhash, hash: this.rhash(),
height: this.height, height: this.height,
date: util.date(this.ts), date: util.date(this.ts),
version: util.hex32(this.version), version: util.hex32(this.version),
@ -420,7 +420,7 @@ MerkleBlock.fromRaw = function fromRaw(data, enc) {
MerkleBlock.prototype.toJSON = function toJSON() { MerkleBlock.prototype.toJSON = function toJSON() {
return { return {
type: 'merkleblock', type: 'merkleblock',
hash: this.rhash, hash: this.rhash(),
height: this.height, height: this.height,
version: this.version, version: this.version,
prevBlock: util.revHex(this.prevBlock), prevBlock: util.revHex(this.prevBlock),

View File

@ -1992,27 +1992,52 @@ TX.prototype.isWatched = function isWatched(filter) {
return false; return false;
}; };
TX.prototype.__defineGetter__('rblock', function() { /**
* Get little-endian block hash.
* @returns {Hash|null}
*/
TX.prototype.rblock = function() {
return this.block return this.block
? util.revHex(this.block) ? util.revHex(this.block)
: null; : null;
}); };
TX.prototype.__defineGetter__('rhash', function() { /**
* Get little-endian tx hash.
* @returns {Hash}
*/
TX.prototype.rhash = function() {
return util.revHex(this.hash('hex')); return util.revHex(this.hash('hex'));
}); };
TX.prototype.__defineGetter__('rwhash', function() { /**
* Get little-endian wtx hash.
* @returns {Hash}
*/
TX.prototype.rwhash = function() {
return util.revHex(this.witnessHash('hex')); return util.revHex(this.witnessHash('hex'));
}); };
TX.prototype.__defineGetter__('txid', function() { /**
return this.rhash; * Get little-endian tx hash.
}); * @returns {Hash}
*/
TX.prototype.__defineGetter__('wtxid', function() { TX.prototype.txid = function() {
return this.rwhash; return this.rhash();
}); };
/**
* Get little-endian wtx hash.
* @returns {Hash}
*/
TX.prototype.wtxid = function() {
return this.rwhash();
};
/** /**
* Convert the tx to an inv item. * Convert the tx to an inv item.
@ -2037,8 +2062,8 @@ TX.prototype.inspect = function inspect() {
rate = 0; rate = 0;
return { return {
hash: this.rhash, hash: this.rhash(),
witnessHash: this.rwhash, witnessHash: this.rwhash(),
size: this.getSize(), size: this.getSize(),
virtualSize: this.maxSize(), virtualSize: this.maxSize(),
height: this.height, height: this.height,

View File

@ -1683,7 +1683,7 @@ TXDB.prototype.disconnect = co(function* disconnect(tx) {
TXDB.prototype.removeConflict = co(function* removeConflict(tx) { TXDB.prototype.removeConflict = co(function* removeConflict(tx) {
var details; var details;
this.logger.warning('Handling conflicting tx: %s.', tx.rhash); this.logger.warning('Handling conflicting tx: %s.', tx.txid());
this.drop(); this.drop();
@ -1691,7 +1691,7 @@ TXDB.prototype.removeConflict = co(function* removeConflict(tx) {
this.start(); this.start();
this.logger.warning('Removed conflict: %s.', tx.rhash); this.logger.warning('Removed conflict: %s.', tx.txid());
// Emit the _removed_ transaction. // Emit the _removed_ transaction.
this.emit('conflict', tx, details); this.emit('conflict', tx, details);

View File

@ -1541,7 +1541,7 @@ Wallet.prototype._send = co(function* send(options, passphrase) {
yield this.db.addTX(tx); yield this.db.addTX(tx);
this.logger.debug('Sending wallet tx (%s): %s', this.id, tx.rhash); this.logger.debug('Sending wallet tx (%s): %s', this.id, tx.txid());
yield this.db.send(tx); yield this.db.send(tx);
@ -1633,7 +1633,7 @@ Wallet.prototype.increaseFee = co(function* increaseFee(hash, rate, passphrase)
tx = tx.toTX(); tx = tx.toTX();
this.logger.debug('Increasing fee for wallet tx (%s): %s', this.id, tx.rhash); this.logger.debug('Increasing fee for wallet tx (%s): %s', this.id, tx.txid());
yield this.db.addTX(tx); yield this.db.addTX(tx);
yield this.db.send(tx); yield this.db.send(tx);

View File

@ -2114,7 +2114,7 @@ WalletDB.prototype._insert = co(function* insert(tx, block) {
this.logger.info( this.logger.info(
'Incoming transaction for %d wallets in WalletDB (%s).', 'Incoming transaction for %d wallets in WalletDB (%s).',
wids.length, tx.rhash); wids.length, tx.txid());
// If this is our first transaction // If this is our first transaction
// in a block, set the start block here. // in a block, set the start block here.

View File

@ -67,7 +67,7 @@ describe('Block', function() {
assert.equal(mblock.matches.length, 2); assert.equal(mblock.matches.length, 2);
assert.equal(mblock.hash('hex'), assert.equal(mblock.hash('hex'),
'8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000'); '8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000');
assert.equal(mblock.rhash, assert.equal(mblock.rhash(),
'0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c'); '0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c');
assert.equal( assert.equal(
mblock.matches[0].toString('hex'), mblock.matches[0].toString('hex'),
@ -124,7 +124,7 @@ describe('Block', function() {
block = bcoin.block.fromJSON(block300025); block = bcoin.block.fromJSON(block300025);
assert.equal(block.hash('hex'), assert.equal(block.hash('hex'),
'8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000'); '8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000');
assert.equal(block.rhash, assert.equal(block.rhash(),
'0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c'); '0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c');
assert.equal(block.merkleRoot, block.createMerkleRoot('hex')); assert.equal(block.merkleRoot, block.createMerkleRoot('hex'));
}); });

View File

@ -112,7 +112,7 @@ describe('HTTP', function() {
assert.equal(Amount.value(balance.confirmed), 0); assert.equal(Amount.value(balance.confirmed), 0);
assert.equal(Amount.value(balance.unconfirmed), 201840); assert.equal(Amount.value(balance.unconfirmed), 201840);
assert(details); assert(details);
assert.equal(details.hash, t1.rhash); assert.equal(details.hash, t1.rhash());
})); }));
it('should get balance', cob(function* () { it('should get balance', cob(function* () {