From 945c2f95646f372403c124367eec0ce51700a4cd Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 30 Mar 2016 14:43:06 -0700 Subject: [PATCH] more standardness. --- lib/bcoin/block.js | 2 +- lib/bcoin/chain.js | 2 +- lib/bcoin/chainblock.js | 2 +- lib/bcoin/mempool.js | 28 ++++++++++++++++++++++------ lib/bcoin/protocol/parser.js | 17 +++++++++-------- lib/bcoin/tx.js | 16 +++------------- 6 files changed, 37 insertions(+), 30 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index dcad272a..17704013 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -393,7 +393,7 @@ Block.prototype.toCompact = function toCompact() { Block.fromCompact = function fromCompact(buf) { var p = new BufferReader(buf); var hashes = []; - var version = p.read32(); + var version = p.readU32(); // Technically signed var prevBlock = p.readHash('hex'); var merkleRoot = p.readHash('hex'); var ts = p.readU32(); diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 92b6fda9..6bf25d00 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -247,7 +247,7 @@ Chain.prototype._preload = function _preload(callback) { return { hash: utils.toHex(hash), - version: p.read32(), + version: p.readU32(), // Technically signed prevBlock: p.readHash('hex'), merkleRoot: p.readHash('hex'), ts: p.readU32(), diff --git a/lib/bcoin/chainblock.js b/lib/bcoin/chainblock.js index bb532a3c..e44aa390 100644 --- a/lib/bcoin/chainblock.js +++ b/lib/bcoin/chainblock.js @@ -267,7 +267,7 @@ ChainBlock.fromRaw = function fromRaw(chain, buf) { return new ChainBlock(chain, { hash: utils.toHex(hash), - version: p.read32(), + version: p.readU32(), // Technically signed prevBlock: p.readHash('hex'), merkleRoot: p.readHash('hex'), ts: p.readU32(), diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 370297c1..ee102227 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -308,11 +308,17 @@ Mempool.prototype.addTX = function addTX(tx, peer, callback, force) { hash = tx.hash('hex'); - assert(tx.ts === 0); - callback = utils.wrap(callback, unlock); callback = utils.asyncify(callback); + if (tx.ts !== 0) { + peer.sendReject(tx, 'alreadyknown', 'txn-already-in-mempool', 0); + return callback(new VerifyError( + 'alreadyknown', + 'txn-already-in-mempool', + 0)); + } + if (!this.chain.segwitActive) { if (tx.hasWitness()) { peer.sendReject(tx, 'nonstandard', 'no-witness-yet', 0); @@ -330,10 +336,20 @@ Mempool.prototype.addTX = function addTX(tx, peer, callback, force) { return callback(new VerifyError('invalid', 'coinbase', 100)); } + // ts = locktimeFlags & LOCKTIME_MEDIAN_PAST + // ? self.chain.tip.getMedianTime() + // : utils.now(); + + ts = utils.now(); + height = this.chain.height + 1; + + if (!tx.isFinal(ts, height)) { + peer.sendReject(tx, 'nonstandard', 'non-final', 0); + return callback(new VerifyError('nonstandard', 'non-final', 0)); + } + if (this.requireStandard) { - ts = utils.now(); - height = this.chain.height + 1; - if (!tx.isStandard(flags, ts, height, ret)) { + if (!tx.isStandard(flags, ret)) { peer.sendReject(tx, 'nonstandard', ret.reason, 0); return callback(new VerifyError(ret.reason, 0)); } @@ -817,7 +833,7 @@ Mempool.prototype.getLocks = function getLocks(tx, flags, entry, callback) { var minTime = -1; var coinHeight; - if ((tx.version >>> 0) < 2 || !hasFlag) + if (tx.version < 2 || !hasFlag) return utils.asyncify(callback)(null, minHeight, minTime); utils.forEachSerial(tx.inputs, function(input, next) { diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 9c5cbd61..14407df4 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -251,7 +251,7 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) { p = new BufferReader(p); p.start(); - version = p.read32(); + version = p.readU32(); // Technically signed prevBlock = p.readHash(); merkleRoot = p.readHash(); ts = p.readU32(); @@ -293,7 +293,7 @@ Parser.parseHeaders = function parseHeaders(p) { for (i = 0; i < count; i++) { headers.push({ - version: p.read32(), + version: p.readU32(), // Technically signed prevBlock: p.readHash(), merkleRoot: p.readHash(), ts: p.readU32(), @@ -317,7 +317,7 @@ Parser.parseBlock = function parseBlock(p) { p = new BufferReader(p); p.start(); - version = p.read32(); + version = p.readU32(); // Technically signed prevBlock = p.readHash(); merkleRoot = p.readHash(); ts = p.readU32(); @@ -352,7 +352,7 @@ Parser.parseBlockCompact = function parseBlockCompact(p) { p = new BufferReader(p); p.start(); - version = p.read32(); + version = p.readU32(); // Technically signed prevBlock = p.readHash(); merkleRoot = p.readHash(); ts = p.readU32(); @@ -362,7 +362,7 @@ Parser.parseBlockCompact = function parseBlockCompact(p) { totalTX = p.readVarint(); if (version > 1 && totalTX > 0) { - p.read32(); + p.readU32(); // Technically signed inCount = p.readVarint(); if (inCount === 0) { @@ -505,7 +505,7 @@ Parser.parseTX = function parseTX(p) { if (Parser.isWitnessTX(p)) return Parser.parseWitnessTX(p); - version = p.readU32(); + version = p.readU32(); // Technically signed inCount = p.readVarint(); txIn = new Array(inCount); @@ -565,13 +565,14 @@ Parser.parseWitnessTX = function parseWitnessTX(p) { p = new BufferReader(p); p.start(); - version = p.readU32(); + version = p.readU32(); // Technically signed marker = p.readU8(); - flag = p.readU8(); if (marker !== 0) throw new Error('Invalid witness tx (marker != 0)'); + flag = p.readU8(); + if (flag === 0) throw new Error('Invalid witness tx (flag == 0)'); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 9c2e1fb7..339fc1ab 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -804,7 +804,7 @@ TX.prototype.isSane = function isSane(ret) { }; // IsStandardTx -TX.prototype.isStandard = function isStandard(flags, ts, height, ret) { +TX.prototype.isStandard = function isStandard(flags, ret) { var i, input, output, type; var nulldata = 0; var maxVersion = constants.tx.version; @@ -815,21 +815,11 @@ TX.prototype.isStandard = function isStandard(flags, ts, height, ret) { if (flags == null) flags = constants.flags.STANDARD_VERIFY_FLAGS; - if (flags & constants.flags.VERIFY_CHECKSEQUENCEVERIFY) - maxVersion = Math.max(maxVersion, 2); - - if ((this.version >>> 0) < 1 || (this.version >>> 0) > maxVersion) { + if (this.version < 1 || this.version > constants.tx.version) { ret.reason = 'version'; return false; } - if (ts != null) { - if (!this.isFinal(ts, height)) { - ret.reason = 'non-final'; - return false; - } - } - if (this.getVirtualSize() > constants.tx.maxSize) { ret.reason = 'tx-size'; return false; @@ -843,7 +833,7 @@ TX.prototype.isStandard = function isStandard(flags, ts, height, ret) { return false; } - // XXX Not accurate + // Not accurate if (this.isCoinbase()) continue;