From 87d985fcdba09dfc624b8bb0931957f0c1fe47ef Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 17 Apr 2016 15:58:12 -0700 Subject: [PATCH] constants. network. docs. --- lib/bcoin/chain.js | 10 +- lib/bcoin/chainblock.js | 2 +- lib/bcoin/chaindb.js | 2 +- lib/bcoin/protocol/constants.js | 133 +++++++++++++++++++- lib/bcoin/protocol/network.js | 211 ++++++++++++++++++++------------ 5 files changed, 269 insertions(+), 89 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index d6f474c2..6551225d 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -2103,12 +2103,12 @@ Chain.prototype.getCurrentTarget = function getCurrentTarget(callback) { Chain.prototype.getTargetAsync = function getTargetAsync(last, block, callback) { var self = this; - if ((last.height + 1) % network.pow.diffInterval !== 0) { + if ((last.height + 1) % network.pow.retargetInterval !== 0) { if (!network.pow.allowMinDifficultyBlocks) return utils.asyncify(callback)(null, this.getTarget(last, block)); } - return last.getAncestors(network.pow.diffInterval, function(err, ancestors) { + return last.getAncestors(network.pow.retargetInterval, function(err, ancestors) { if (err) return callback(err); @@ -2138,7 +2138,7 @@ Chain.prototype.getTarget = function getTarget(last, block, ancestors) { ancestors = last.ancestors; // Do not retarget - if ((last.height + 1) % network.pow.diffInterval !== 0) { + if ((last.height + 1) % network.pow.retargetInterval !== 0) { if (network.pow.allowMinDifficultyBlocks) { // Special behavior for testnet: ts = block ? (block.ts || block) : utils.now(); @@ -2147,7 +2147,7 @@ Chain.prototype.getTarget = function getTarget(last, block, ancestors) { i = 1; while (ancestors[i] - && last.height % network.pow.diffInterval !== 0 + && last.height % network.pow.retargetInterval !== 0 && last.bits === powLimit) { last = ancestors[i++]; } @@ -2156,7 +2156,7 @@ Chain.prototype.getTarget = function getTarget(last, block, ancestors) { } // Back 2 weeks - first = ancestors[network.pow.diffInterval - 1]; + first = ancestors[network.pow.retargetInterval - 1]; assert(first); diff --git a/lib/bcoin/chainblock.js b/lib/bcoin/chainblock.js index 0d993f3d..97bab479 100644 --- a/lib/bcoin/chainblock.js +++ b/lib/bcoin/chainblock.js @@ -102,7 +102,7 @@ ChainBlock.prototype.isGenesis = function isGenesis() { ChainBlock.prototype.ensureAncestors = function ensureAncestors(callback) { var majorityWindow = network.block.majorityWindow; var medianTimespan = constants.block.MEDIAN_TIMESPAN; - var powDiffInterval = network.pow.diffInterval; + var powDiffInterval = network.pow.retargetInterval; var allowMinDiff = network.pow.allowMinDifficultyBlocks; var max = Math.max(majorityWindow, medianTimespan); if ((this.height + 1) % powDiffInterval === 0 || allowMinDiff) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index d6987804..e909fd5f 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -61,7 +61,7 @@ function ChainDB(chain, options) { // if we're going to be checking the damn // target all the time. if (network.pow.allowMinDifficultyBlocks) - this._cacheWindow = network.pow.diffInterval + 1; + this._cacheWindow = network.pow.retargetInterval + 1; else this._cacheWindow = network.block.majorityWindow + 1; diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 55670559..73ba1d41 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -43,9 +43,28 @@ exports.MAX_MESSAGE = 4 * 1000 * 1000; */ exports.services = { + /** + * Whether network services are enabled. + */ + NETWORK: (1 << 0), + + /** + * Whether the peer supports the getutxos packet. + */ + GETUTXO: (1 << 1), + + /** + * Whether the peer supports BIP37. + */ + BLOOM: (1 << 2), + + /** + * Whether the peer supports segregated witness. + */ + WITNESS: (1 << 3) }; @@ -101,8 +120,23 @@ exports.WITNESS_MASK = 1 << 30; */ exports.filterFlags = { + /** + * Never update the filter with outpoints. + */ + NONE: 0, + + /** + * Always update the filter with outpoints. + */ + ALL: 1, + + /** + * Only update the filter with outpoints if it is + * "asymmetric" in terms of addresses (pubkey/multisig). + */ + PUBKEY_ONLY: 2 }; @@ -303,9 +337,28 @@ exports.MAX_MONEY = new bn(21000000).mul(exports.COIN); */ exports.hashType = { + /** + * Sign all outputs. + */ + ALL: 1, + + /** + * Do not sign outputs (zero sequences). + */ + NONE: 2, + + /** + * Sign output at the same index (zero sequences). + */ + SINGLE: 3, + + /** + * Sign only the current input (mask). + */ + ANYONECANPAY: 0x80 }; @@ -384,9 +437,29 @@ exports.script = { */ exports.mempool = { + /** + * Ancestor limit. + */ + ANCESTOR_LIMIT: 25, + + /** + * Maximum mempool size in bytes. + */ + MAX_MEMPOOL_SIZE: 300 << 20, + + /** + * The time at which transactions + * fall out of the mempool. + */ + MEMPOOL_EXPIRY: 72 * 60 * 60, + + /** + * Maximum number of orphan transactions. + */ + MAX_ORPHAN_TX: 100 }; @@ -452,9 +525,28 @@ exports.LOCKTIME_THRESHOLD = 500000000; */ exports.sequence = { + /** + * Highest nSequence bit (disables sequence locktimes). + */ + DISABLE_FLAG: (1 << 31) >>> 0, + + /** + * Type (height or time). + */ + TYPE_FLAG: 1 << 22, + + /** + * Sequence granularity. + */ + GRANULARITY: 9, + + /** + * Mask. + */ + MASK: 0x0000ffff }; @@ -599,11 +691,22 @@ exports.flags.STANDARD_LOCKTIME_FLAGS = 0 */ exports.versionbits = { - // What block version to use for new blocks (pre versionbits) + /** + * What block version to use for new blocks (pre versionbits) + */ + LAST_OLD_BLOCK_VERSION: 4, - // What bits to set in version for versionbits blocks + + /** + * What bits to set in version for versionbits blocks + */ + TOP_BITS: 0x20000000, - // What bitmask determines whether versionbits is in use + + /** + * What bitmask determines whether versionbits is in use + */ + TOP_MASK: 0xe0000000 }; @@ -628,9 +731,33 @@ exports.thresholdStates = { */ exports.confidence = { + /** + * Transaction is in the main chain. + */ + BUILDING: 1, + + /** + * Transaction is valid and in the mempool. + */ + PENDING: 2, + + /** + * Transaction is on a side chain. + */ + DEAD: 4, + + /** + * Transaction is double-spent. + */ + INCONFLICT: 5, + + /** + * Transaction is not in the mempool or chain. + */ + UNKNOWN: 0 }; diff --git a/lib/bcoin/protocol/network.js b/lib/bcoin/protocol/network.js index be83cade..8e982724 100644 --- a/lib/bcoin/protocol/network.js +++ b/lib/bcoin/protocol/network.js @@ -169,33 +169,55 @@ main.genesisBlock = + '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f' + 'ac00000000'; -main.pow = {}; - /** - * Default target. - * @const {Buffer} - */ - -main.pow.limit = new bn( - '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff', - 'hex' -); - -/** - * Default retarget interval. - * @const {Number} + * POW-related constants. + * @enum {Number} * @default */ -main.pow.targetTimespan = 14 * 24 * 60 * 60; // two weeks +main.pow = { + /** + * Default target. + * @const {Buffer} + */ -/** - * Average block time. - * @const {Number} - * @default - */ + limit: new bn( + '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff', + 'hex' + ), -main.pow.targetSpacing = 10 * 60; + /** + * Default retarget interval. + * @const {Number} + * @default + */ + + targetTimespan: 14 * 24 * 60 * 60, // two weeks + + /** + * Average block time. + * @const {Number} + * @default + */ + + targetSpacing: 10 * 60, + + /** + * Allow constant retargetting on testnet. + * @const {Boolean} + * @default + */ + + allowMinDifficultyBlocks: false, + + /** + * Do not allow retargetting. + * @const {Boolean} + * @default + */ + + noRetargeting: false +}; /** * Retarget interval in blocks. @@ -203,21 +225,7 @@ main.pow.targetSpacing = 10 * 60; * @default */ -main.pow.diffInterval = main.pow.targetTimespan / main.pow.targetSpacing | 0; - -/** - * @const {Boolean} - * @default - */ - -main.pow.allowMinDifficultyBlocks = false; - -/** - * @const {Boolean} - * @default - */ - -main.pow.noRetargeting = false; +main.pow.retargetInterval = main.pow.targetTimespan / main.pow.targetSpacing | 0; /** * Block constants. @@ -226,12 +234,50 @@ main.pow.noRetargeting = false; */ main.block = { + /** + * Required versions to upgrade (see {@link ChainBlock#IsUpgraded}). + */ + majorityEnforceUpgrade: 750, + + /** + * Required versions to consider block + * outdated (see {@link ChainBlock#IsOutdated}). + */ + majorityRejectOutdated: 950, + + /** + * Majority window to check for upgraded and outdated + * blocks (see {@link ChainBlock#IsSuperMajority}). + */ + majorityWindow: 1000, + + /** + * Height at which bip34 was activated. + * Used for avoiding bip30 checks. + */ + bip34height: 227931, + + /** + * Hash of the block that activated bip34. + */ + bip34hash: 'b808089c756add1591b1d17bab44bba3fed9e02f942ab4894b02000000000000', + + /** + * Safe height to start pruning. + */ + pruneAfterHeight: 100000, + + /** + * Age used for the time delta to + * determine whether the chain is synced. + */ + maxTipAge: 24 * 60 * 60 }; @@ -312,26 +358,27 @@ main.prefixes = { /** * {@link Base58Address} constants. - * @const {Object} + * @enum {Object} */ main.address = { /** * {@link Base58Address} prefixes. * @enum {Number} - * @default */ + prefixes: { pubkeyhash: 0, scripthash: 5, witnesspubkeyhash: 6, witnessscripthash: 10 }, + /** * {@link Base58Address} versions. * @enum {Number} - * @default */ + versions: { witnesspubkeyhash: 0, witnessscripthash: 0 @@ -415,17 +462,18 @@ testnet.genesisBlock = + '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f' + 'ac00000000'; -testnet.pow = {}; +testnet.pow = { + limit: new bn( + '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff', + 'hex' + ), + targetTimespan: 14 * 24 * 60 * 60, // two weeks + targetSpacing: 10 * 60, + allowMinDifficultyBlocks: true, + noRetargeting: false +}; -testnet.pow.limit = new bn( - '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff', - 'hex' -); -testnet.pow.targetTimespan = 14 * 24 * 60 * 60; // two weeks -testnet.pow.targetSpacing = 10 * 60; -testnet.pow.diffInterval = testnet.pow.targetTimespan / testnet.pow.targetSpacing | 0; -testnet.pow.allowMinDifficultyBlocks = true; -testnet.pow.noRetargeting = false; +testnet.pow.retargetInterval = testnet.pow.targetTimespan / testnet.pow.targetSpacing | 0; testnet.block = { majorityEnforceUpgrade: 51, @@ -541,15 +589,18 @@ regtest.genesisBlock = regtest.pow = {}; -regtest.pow.limit = new bn( - '7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', - 'hex' -); -regtest.pow.targetTimespan = 14 * 24 * 60 * 60; // two weeks -regtest.pow.targetSpacing = 10 * 60; -regtest.pow.diffInterval = regtest.pow.targetTimespan / regtest.pow.targetSpacing | 0; -regtest.pow.allowMinDifficultyBlocks = true; -regtest.pow.noRetargeting = true; +regtest.pow = { + limit: new bn( + '7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', + 'hex' + ), + targetTimespan: 14 * 24 * 60 * 60, // two weeks + targetSpacing: 10 * 60, + allowMinDifficultyBlocks: true, + noRetargeting: true +}; + +regtest.pow.retargetInterval = regtest.pow.targetTimespan / regtest.pow.targetSpacing | 0; regtest.block = { majorityEnforceUpgrade: 750, @@ -662,17 +713,18 @@ segnet3.genesisBlock = + '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f' + 'ac00000000'; -segnet3.pow = {}; +segnet3.pow = { + limit: new bn( + '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff', + 'hex' + ), + targetTimespan: 14 * 24 * 60 * 60, // two weeks + targetSpacing: 10 * 60, + allowMinDifficultyBlocks: true, + noRetargeting: false +}; -segnet3.pow.limit = new bn( - '00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff', - 'hex' -); -segnet3.pow.targetTimespan = 14 * 24 * 60 * 60; // two weeks -segnet3.pow.targetSpacing = 10 * 60; -segnet3.pow.diffInterval = segnet3.pow.targetTimespan / segnet3.pow.targetSpacing | 0; -segnet3.pow.allowMinDifficultyBlocks = true; -segnet3.pow.noRetargeting = false; +segnet3.pow.retargetInterval = segnet3.pow.targetTimespan / segnet3.pow.targetSpacing | 0; segnet3.block = { majorityEnforceUpgrade: 7, @@ -766,18 +818,19 @@ segnet4.genesisBlock = + '61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f' + 'ac00000000'; -segnet4.pow = {}; - -segnet4.pow.limit = new bn( +segnet4.pow = { // 512x lower min difficulty than mainnet - '000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', - 'hex' -); -segnet4.pow.targetTimespan = 14 * 24 * 60 * 60; // two weeks -segnet4.pow.targetSpacing = 10 * 60; -segnet4.pow.diffInterval = segnet4.pow.targetTimespan / segnet4.pow.targetSpacing | 0; -segnet4.pow.allowMinDifficultyBlocks = true; -segnet4.pow.noRetargeting = false; + limit: new bn( + '000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', + 'hex' + ), + targetTimespan: 14 * 24 * 60 * 60, // two weeks + targetSpacing: 10 * 60, + allowMinDifficultyBlocks: true, + noRetargeting: false +}; + +segnet4.pow.retargetInterval = segnet4.pow.targetTimespan / segnet4.pow.targetSpacing | 0; segnet4.block = { majorityEnforceUpgrade: 7,