refactor: rename tx functions. more es6 features.

This commit is contained in:
Christopher Jeffrey 2017-07-01 03:41:57 -07:00
parent c651136d83
commit 10672784e5
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
29 changed files with 159 additions and 130 deletions

View File

@ -20,9 +20,8 @@ const LRU = require('../utils/lru');
const ChainEntry = require('./chainentry');
const CoinView = require('../coins/coinview');
const Script = require('../script/script');
const errors = require('../protocol/errors');
const {VerifyError} = require('../protocol/errors');
const co = require('../utils/co');
const VerifyError = errors.VerifyError;
/**
* Represents a blockchain.
@ -531,7 +530,8 @@ Chain.prototype.verifyDuplicates = async function verifyDuplicates(block, prev,
* for historical data.
* @method
* @private
* @see TX#checkInputs
* @see TX#verifyInputs
* @see TX#verify
* @param {Block} block
* @param {ChainEntry} prev
* @param {DeploymentState} state
@ -595,7 +595,7 @@ Chain.prototype.verifyInputs = async function verifyInputs(block, prev, state) {
// Contextual sanity checks.
if (!tx.isCoinbase()) {
let [fee, reason, score] = tx.checkContext(view, height);
let [fee, reason, score] = tx.checkInputs(view, height);
if (fee === -1) {
throw new VerifyError(block,

View File

@ -8,18 +8,17 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const URL = require('url');
const StringDecoder = require('string_decoder').StringDecoder;
const {StringDecoder} = require('string_decoder');
const AsyncObject = require('../utils/asyncobject');
const util = require('../utils/util');
const co = require('../utils/co');
const Validator = require('../utils/validator');
const List = require('../utils/list');
const {List, ListItem} = require('../utils/list');
const fs = require('../utils/fs');
const digest = require('../crypto/digest');
const ccmp = require('../crypto/ccmp');
const ListItem = List.Item;
/**
* HTTPBase
@ -1401,23 +1400,16 @@ Response.prototype.end = function end(data, enc) {
};
Response.prototype.error = function error(code, err) {
let msg;
if (this.sent)
return;
msg = err.message;
if (!code)
code = 400;
if (typeof msg !== 'string')
msg += '';
this.send(code, {
error: {
type: err.type || 'Error',
message: err.message || msg,
message: err.message,
code: err.code
}
});
@ -1435,6 +1427,7 @@ Response.prototype.redirect = function redirect(code, url) {
url = code;
code = 301;
}
this.setStatus(code);
this.setHeader('Location', url);
this.end();

View File

@ -393,10 +393,16 @@ RPC.prototype.getPeerInfo = async function getPeerInfo(args, help) {
for (let peer = this.pool.peers.head(); peer; peer = peer.next) {
let offset = this.network.time.known.get(peer.hostname());
let hashes = [];
if (offset == null)
offset = 0;
for (let hash in peer.blockMap.keys()) {
hash = util.revHex(hash);
hashes.push(hash);
}
peers.push({
id: peer.id,
addr: peer.hostname(),
@ -422,7 +428,7 @@ RPC.prototype.getPeerInfo = async function getPeerInfo(args, help) {
besthash: peer.bestHash ? util.revHex(peer.bestHash) : null,
bestheight: peer.bestHeight,
banscore: peer.banScore,
inflight: peer.blockMap.keys().map(util.revHex),
inflight: hashes,
whitelisted: false
});
}

View File

@ -7,7 +7,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const Lock = require('../utils/lock');
const Logger = require('../node/logger');

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const Network = require('../protocol/network');
const util = require('../utils/util');
const Client = require('./client');
@ -442,7 +442,7 @@ HTTPWallet.prototype.unlock = function unlock(passphrase, timeout) {
/**
* Get wallet key.
* @param {Base58Address} address
* @param {String} address
* @returns {Promise}
*/
@ -452,7 +452,7 @@ HTTPWallet.prototype.getKey = function getKey(address) {
/**
* Get wallet key WIF dump.
* @param {Base58Address} address
* @param {String} address
* @param {String?} passphrase
* @returns {Promise}
*/

View File

@ -12,7 +12,7 @@ const common = require('../blockchain/common');
const policy = require('../protocol/policy');
const util = require('../utils/util');
const random = require('../crypto/random');
const errors = require('../protocol/errors');
const {VerifyError} = require('../protocol/errors');
const RollingFilter = require('../utils/rollingfilter');
const Address = require('../primitives/address');
const Script = require('../script/script');
@ -29,7 +29,6 @@ const Fees = require('./fees');
const CoinView = require('../coins/coinview');
const Coins = require('../coins/coins');
const Heap = require('../utils/heap');
const VerifyError = errors.VerifyError;
/**
* Represents a mempool.
@ -949,7 +948,7 @@ Mempool.prototype.verify = async function verify(entry, view) {
}
// Contextual sanity checks.
[fee, reason, score] = tx.checkContext(view, height);
[fee, reason, score] = tx.checkInputs(view, height);
if (fee === -1)
throw new VerifyError(tx, 'invalid', reason, score);

View File

@ -10,7 +10,7 @@
const assert = require('assert');
const path = require('path');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const co = require('../utils/co');
const digest = require('../crypto/digest');

View File

@ -13,7 +13,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const co = require('../utils/co');
const digest = require('../crypto/digest');

View File

@ -38,7 +38,12 @@ external.getIPv4 = async function getIPv4() {
ip = res.body.trim();
try {
ip = IP.normalize(ip);
ip = IP.toBuffer(ip);
if (!IP.isMapped(ip))
throw new Error('Could not find IPv4.');
ip = IP.toString(ip);
} catch (e) {
return await external.getIPv42();
}
@ -66,13 +71,13 @@ external.getIPv42 = async function getIPv42() {
match = /IP Address:\s*([0-9a-f.:]+)/i.exec(res.body);
if (!match)
throw new Error('Could not find IP.');
throw new Error('Could not find IPv4.');
ip = match[1];
raw = IP.toBuffer(ip);
if (!IP.isMapped(raw))
throw new Error('Could not find IP.');
throw new Error('Could not find IPv4.');
return IP.toString(raw);
};
@ -94,6 +99,10 @@ external.getIPv6 = async function getIPv6() {
});
ip = res.body.trim();
ip = IP.toBuffer(ip);
return IP.normalize(ip);
if (IP.isMapped(ip))
throw new Error('Could not find IPv6.');
return IP.toString(ip);
};

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const Network = require('../protocol/network');
const util = require('../utils/util');
const digest = require('../crypto/digest');

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const co = require('../utils/co');
const Parser = require('./parser');

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const AsyncObject = require('../utils/asyncobject');
const util = require('../utils/util');
const IP = require('../utils/ip');

View File

@ -6,12 +6,12 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events');
const IOClient = require('socket.io-client');
const util = require('../utils/util');
const digest = require('../crypto/digest');
const BufferWriter = require('../utils/writer');
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const IOClient = require('socket.io-client');
function ProxySocket(uri) {
if (!(this instanceof ProxySocket))

View File

@ -11,7 +11,7 @@
*/
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const net = require('net');
const util = require('../utils/util');
const IP = require('../utils/ip');

View File

@ -7,7 +7,7 @@
'use strict';
const ProxySocket = require('./proxysocket');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const tcp = exports;
tcp.createSocket = function createSocket(port, host, proxy) {

View File

@ -6,7 +6,7 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const net = require('net');
const socks = require('./socks');
@ -84,7 +84,7 @@ tcp.createServer = function createServer() {
*/
function wrap(resolve, reject) {
return (err, result) => {
return function(err, result) {
if (err) {
reject(err);
return;

View File

@ -703,15 +703,14 @@ LoggerContext.prototype.setLevel = function setLevel(name) {
this.logger.setLevel(name);
};
/**
* Output a log to the `error` log level.
* @param {String|Object|Error} err
* @param {...Object} args
*/
LoggerContext.prototype.error = function error(err) {
let args;
LoggerContext.prototype.error = function error(...args) {
let err = args[0];
if (this.logger.level < Logger.levels.ERROR)
return;
@ -722,18 +721,6 @@ LoggerContext.prototype.error = function error(err) {
this.log(Logger.levels.ERROR, args);
};
/**
* Helper to parse an error into a nicer
* format. Call's `log` internally.
* @private
* @param {Number} level
* @param {Error} err
*/
LoggerContext.prototype.logError = function logError(level, err) {
this.logger.logError(level, this.module, err);
};
/**
* Output a log to the `warning` log level.
* @param {String|Object} obj
@ -827,6 +814,18 @@ LoggerContext.prototype.context = function context(module) {
return new LoggerContext(this.logger, module);
};
/**
* Helper to parse an error into a nicer
* format. Call's `log` internally.
* @private
* @param {Number} level
* @param {Error} err
*/
LoggerContext.prototype.logError = function logError(level, err) {
this.logger.logError(level, this.module, err);
};
/**
* Log the current memory usage.
*/

View File

@ -242,6 +242,17 @@ MTX.prototype.addOutput = function addOutput(script, value) {
return output;
};
/**
* Verify all transaction inputs.
* @param {VerifyFlags} [flags=STANDARD_VERIFY_FLAGS]
* @returns {Boolean} Whether the inputs are valid.
* @throws {ScriptError} on invalid inputs
*/
MTX.prototype.check = function check(flags) {
return TX.prototype.check.call(this, this.view, flags);
};
/**
* Verify all transaction inputs.
* @param {VerifyFlags} [flags=STANDARD_VERIFY_FLAGS]
@ -364,16 +375,30 @@ MTX.prototype.getSigopsSize = function getSigopsSize() {
* (coinbases can only be spent 100 blocks or more
* after they're created). Note that this function is
* consensus critical.
* @param {Number} spendHeight - Height at which the
* @param {Number} height - Height at which the
* transaction is being spent. In the mempool this is
* the chain height plus one at the time it entered the pool.
* @param {Object?} ret - Return object, may be
* set with properties `reason` and `score`.
* @returns {Boolean}
*/
MTX.prototype.checkInputs = function checkInputs(height, ret) {
return TX.prototype.checkInputs.call(this, this.view, height, ret);
MTX.prototype.verifyInputs = function verifyInputs(height) {
return TX.prototype.verifyInputs.call(this, this.view, height);
};
/**
* Perform contextual checks to verify input, output,
* and fee values, as well as coinbase spend maturity
* (coinbases can only be spent 100 blocks or more
* after they're created). Note that this function is
* consensus critical.
* @param {Number} height - Height at which the
* transaction is being spent. In the mempool this is
* the chain height plus one at the time it entered the pool.
* @returns {Array} [fee, reason, score]
*/
MTX.prototype.checkInputs = function checkInputs(height) {
return TX.prototype.checkInputs.call(this, this.view, height);
};
/**

View File

@ -24,7 +24,7 @@ const InvItem = require('./invitem');
const Bloom = require('../utils/bloom');
const consensus = require('../protocol/consensus');
const policy = require('../protocol/policy');
const ScriptError = require('../script/common').ScriptError;
const {ScriptError} = require('../script/common');
/**
* A static transaction object.
@ -754,45 +754,7 @@ TX.prototype.signature = function signature(index, prev, value, key, type, versi
* Verify all transaction inputs.
* @param {CoinView} view
* @param {VerifyFlags?} [flags=STANDARD_VERIFY_FLAGS]
* @returns {Boolean} Whether the inputs are valid.
*/
TX.prototype.verify = function verify(view, flags) {
try {
this.check(view, flags);
} catch (e) {
if (e.type === 'ScriptError')
return false;
throw e;
}
return true;
};
/**
* Verify a transaction input.
* @param {Number} index - Index of output being
* verified.
* @param {Coin|Output} coin - Previous output.
* @param {VerifyFlags} [flags=STANDARD_VERIFY_FLAGS]
* @returns {Boolean} Whether the input is valid.
*/
TX.prototype.verifyInput = function verifyInput(index, coin, flags) {
try {
this.checkInput(index, coin, flags);
} catch (e) {
if (e.type === 'ScriptError')
return false;
throw e;
}
return true;
};
/**
* Verify all transaction inputs.
* @param {CoinView} view
* @param {VerifyFlags?} [flags=STANDARD_VERIFY_FLAGS]
* @throws {ScriptError} on invalid input
* @throws {ScriptError} on invalid inputs
*/
TX.prototype.check = function check(view, flags) {
@ -839,6 +801,44 @@ TX.prototype.checkInput = function checkInput(index, coin, flags) {
);
};
/**
* Verify all transaction inputs.
* @param {CoinView} view
* @param {VerifyFlags?} [flags=STANDARD_VERIFY_FLAGS]
* @returns {Boolean} Whether the inputs are valid.
*/
TX.prototype.verify = function verify(view, flags) {
try {
this.check(view, flags);
} catch (e) {
if (e.type === 'ScriptError')
return false;
throw e;
}
return true;
};
/**
* Verify a transaction input.
* @param {Number} index - Index of output being
* verified.
* @param {Coin|Output} coin - Previous output.
* @param {VerifyFlags} [flags=STANDARD_VERIFY_FLAGS]
* @returns {Boolean} Whether the input is valid.
*/
TX.prototype.verifyInput = function verifyInput(index, coin, flags) {
try {
this.checkInput(index, coin, flags);
} catch (e) {
if (e.type === 'ScriptError')
return false;
throw e;
}
return true;
};
/**
* Verify the transaction inputs on the worker pool
* (if workers are enabled).
@ -1657,8 +1657,8 @@ TX.prototype.hasStandardWitness = function hasStandardWitness(view) {
* @returns {Boolean}
*/
TX.prototype.checkInputs = function checkInputs(view, height) {
let [fee] = this.checkContext(view, height);
TX.prototype.verifyInputs = function verifyInputs(view, height) {
let [fee] = this.checkInputs(view, height);
return fee !== -1;
};
@ -1675,7 +1675,7 @@ TX.prototype.checkInputs = function checkInputs(view, height) {
* @returns {Array} [fee, reason, score]
*/
TX.prototype.checkContext = function checkContext(view, height) {
TX.prototype.checkInputs = function checkInputs(view, height) {
let total = 0;
let fee, value;
@ -1958,10 +1958,6 @@ TX.prototype.isWatched = function isWatched(filter) {
// Test the input script
if (input.script.test(filter))
return true;
// Test the witness
if (input.witness.test(filter))
return true;
}
// 5. No match

View File

@ -8,7 +8,7 @@
'use strict';
const util = require('../utils/util');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
/**
* An object which handles "adjusted time". This may not

View File

@ -7,7 +7,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('./util');
const Lock = require('./lock');

View File

@ -273,6 +273,8 @@ function ListItem(value) {
*/
exports = List;
exports.List = List;
exports.ListItem = ListItem;
exports.Item = ListItem;
module.exports = exports;

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const Network = require('../protocol/network');
const util = require('../utils/util');
const encoding = require('../utils/encoding');
@ -1574,7 +1574,7 @@ Wallet.prototype.createTX = async function createTX(options, force) {
// Consensus sanity checks.
assert(mtx.isSane(), 'TX failed sanity check.');
assert(mtx.checkInputs(this.db.state.height + 1), 'CheckInputs failed.');
assert(mtx.verifyInputs(this.db.state.height + 1), 'TX failed context check.');
total = await this.template(mtx);

View File

@ -7,7 +7,7 @@
'use strict';
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const StaticWriter = require('../utils/staticwriter');

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const Network = require('../protocol/network');
const jobs = require('./jobs');

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const util = require('../utils/util');
const packets = require('./packets');

View File

@ -8,7 +8,7 @@
'use strict';
const assert = require('assert');
const EventEmitter = require('events').EventEmitter;
const EventEmitter = require('events');
const os = require('os');
const path = require('path');
const cp = require('./cp');

View File

@ -218,7 +218,7 @@ describe('Block', function() {
for (let i = 1; i < block.txs.length; i++) {
let tx = block.txs[i];
assert(tx.isSane());
assert(tx.checkInputs(view, height));
assert(tx.verifyInputs(view, height));
assert(tx.verify(view, flags));
assert(!tx.hasWitness());
sigops += tx.getSigopsCost(view, flags);

View File

@ -380,7 +380,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should handle 51 bit coin values', () => {
@ -396,7 +396,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(tx.checkInputs(view, 0));
assert.ok(tx.verifyInputs(view, 0));
});
it('should fail on >51 bit output values', () => {
@ -412,7 +412,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should handle 51 bit output values', () => {
@ -428,7 +428,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(tx.checkInputs(view, 0));
assert.ok(tx.verifyInputs(view, 0));
});
it('should fail on >51 bit fees', () => {
@ -444,7 +444,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >51 bit values from multiple', () => {
@ -464,7 +464,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >51 bit output values from multiple', () => {
@ -490,7 +490,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >51 bit fees from multiple', () => {
@ -510,7 +510,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail to parse >53 bit values', () => {
@ -562,7 +562,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on 53 bit output values', () => {
@ -578,7 +578,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on 53 bit fees', () => {
@ -594,7 +594,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
[MAX_SAFE_ADDITION, MAX_SAFE_INTEGER].forEach((MAX) => {
@ -615,7 +615,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >53 bit output values from multiple', () => {
@ -641,7 +641,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(!tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
it('should fail on >53 bit fees from multiple', () => {
@ -661,7 +661,7 @@ describe('TX', function() {
locktime: 0
});
assert.ok(tx.isSane());
assert.ok(!tx.checkInputs(view, 0));
assert.ok(!tx.verifyInputs(view, 0));
});
});