Skip to content

Commit

Permalink
refactor: move constants around.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jan 6, 2017
1 parent d1a4e53 commit 5caf621
Show file tree
Hide file tree
Showing 78 changed files with 2,317 additions and 2,025 deletions.
2 changes: 1 addition & 1 deletion bench/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var constants = require('../lib/protocol/constants');
var crypto = require('../lib/crypto/crypto');
var Script = require('../lib/script/script');
var bench = require('./bench');
var opcodes = constants.opcodes;
var opcodes = Script.opcodes;
var i, hashes, end;

Script.prototype.fromPubkeyhashOld = function fromScripthash(hash) {
Expand Down
8 changes: 5 additions & 3 deletions bench/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ var fs = require('fs');
var Block = require('../lib/primitives/block');
var Address = require('../lib/primitives/address');
var TX = require('../lib/primitives/tx');
var Script = require('../lib/script/script');
var MTX = require('../lib/primitives/mtx');
var Coin = require('../lib/primitives/coin');
var CoinView = require('../lib/coins/coinview');
var constants = require('../lib/protocol/constants');
var encoding = require('../lib/utils/encoding');
var crypto = require('../lib/crypto/crypto');
var bench = require('./bench');

Expand Down Expand Up @@ -92,15 +94,15 @@ end(i);

end = bench('verify');
for (i = 0; i < 3000; i++)
tx3.tx.verify(tx3.view, constants.flags.VERIFY_P2SH);
tx3.tx.verify(tx3.view, Script.flags.VERIFY_P2SH);
end(i * tx3.tx.inputs.length);

end = bench('fee');
for (i = 0; i < 1000; i++)
tx3.tx.getFee(tx3.view);
end(i);

flags = constants.flags.VERIFY_P2SH | constants.flags.VERIFY_DERSIG;
flags = Script.flags.VERIFY_P2SH | Script.flags.VERIFY_DERSIG;
end = bench('verify multisig');
for (i = 0; i < 3000; i++)
btx.tx.verify(btx.view, flags);
Expand All @@ -111,7 +113,7 @@ tx = new MTX();
for (i = 0; i < 100; i++) {
tx.addInput({
prevout: {
hash: constants.NULL_HASH,
hash: encoding.NULL_HASH,
index: 0
},
script: [
Expand Down
67 changes: 34 additions & 33 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ var Network = require('../protocol/network');
var Logger = require('../node/logger');
var ChainDB = require('./chaindb');
var constants = require('../protocol/constants');
var consensus = require('../protocol/consensus');
var util = require('../utils/util');
var btcutils = require('../btc/utils');
var Locker = require('../utils/locker');
var LRU = require('../utils/lru');
var ChainEntry = require('./chainentry');
var CoinView = require('../coins/coinview');
var Script = require('../script/script');
var assert = require('assert');
var errors = require('../btc/errors');
var VerifyError = errors.VerifyError;
Expand Down Expand Up @@ -414,7 +415,7 @@ Chain.prototype.verify = co(function* verify(block, prev) {

// Check block weight (different from block size
// check in non-contextual verification).
if (block.getWeight() > constants.block.MAX_WEIGHT) {
if (block.getWeight() > consensus.MAX_BLOCK_WEIGHT) {
throw new VerifyError(block,
'invalid',
'bad-blk-weight',
Expand Down Expand Up @@ -468,25 +469,25 @@ Chain.prototype.getDeployments = co(function* getDeployments(block, prev) {
// not have a signature. See:
// 6a26d2ecb67f27d1fa5524763b49029d7106e91e3cc05743073461a719776192
// 9c08a4d78931342b37fd5f72900fb9983087e6f46c4a097d8a1f52c74e28eaf6
if (block.ts >= constants.block.BIP16_TIME)
state.flags |= constants.flags.VERIFY_P2SH;
if (block.ts >= consensus.BIP16_TIME)
state.flags |= Script.flags.VERIFY_P2SH;

// Coinbase heights are now enforced (bip34).
if (height >= this.network.block.bip34height)
state.bip34 = true;

// Signature validation is now enforced (bip66).
if (height >= this.network.block.bip66height)
state.flags |= constants.flags.VERIFY_DERSIG;
state.flags |= Script.flags.VERIFY_DERSIG;

// CHECKLOCKTIMEVERIFY is now usable (bip65)
if (height >= this.network.block.bip65height)
state.flags |= constants.flags.VERIFY_CHECKLOCKTIMEVERIFY;
state.flags |= Script.flags.VERIFY_CHECKLOCKTIMEVERIFY;

// Segregrated witness is now usable (bip141 - segnet3)
if (this.options.witness && this.network.oldWitness) {
if (height >= this.network.block.bip141height)
state.flags |= constants.flags.VERIFY_WITNESS;
state.flags |= Script.flags.VERIFY_WITNESS;
}

if (this.network.oldWitness)
Expand All @@ -496,18 +497,18 @@ Chain.prototype.getDeployments = co(function* getDeployments(block, prev) {
// past locktimes are now usable (bip9 & bip113).
active = yield this.isActive(prev, deployments.csv);
if (active) {
state.flags |= constants.flags.VERIFY_CHECKSEQUENCEVERIFY;
state.lockFlags |= constants.flags.VERIFY_SEQUENCE;
state.lockFlags |= constants.flags.MEDIAN_TIME_PAST;
state.flags |= Script.flags.VERIFY_CHECKSEQUENCEVERIFY;
state.lockFlags |= constants.lockFlags.VERIFY_SEQUENCE;
state.lockFlags |= constants.lockFlags.MEDIAN_TIME_PAST;
}

// Segregrated witness is now usable (bip141 - segnet4)
active = yield this.isActive(prev, deployments.segwit);
if (active) {
if (this.options.witness)
state.flags |= constants.flags.VERIFY_WITNESS;
state.flags |= Script.flags.VERIFY_WITNESS;
// BIP147
state.flags |= constants.flags.VERIFY_NULLDUMMY;
state.flags |= Script.flags.VERIFY_NULLDUMMY;
}

return state;
Expand Down Expand Up @@ -575,8 +576,8 @@ Chain.prototype.verifyDuplicates = co(function* verifyDuplicates(block, prev, st
// Blocks 91842 and 91880 created duplicate
// txids by using the same exact output script
// and extraNonce.
if (constants.bip30[height]) {
if (block.hash('hex') === constants.bip30[height])
if (this.network.bip30[height]) {
if (block.hash('hex') === this.network.bip30[height])
continue;
}
throw new VerifyError(block, 'invalid', 'bad-txns-BIP30', 100);
Expand Down Expand Up @@ -648,7 +649,7 @@ Chain.prototype.verifyInputs = co(function* verifyInputs(block, prev, state) {
// Count sigops (legacy + scripthash? + witness?)
sigops += tx.getSigopsCost(view, state.flags);

if (sigops > constants.block.MAX_SIGOPS_COST) {
if (sigops > consensus.MAX_BLOCK_SIGOPS_COST) {
throw new VerifyError(block,
'invalid',
'bad-blk-sigops',
Expand Down Expand Up @@ -1988,7 +1989,7 @@ Chain.prototype.retarget = function retarget(prev, first) {
return prev.bits;

actualTimespan = prev.ts - first.ts;
target = btcutils.fromCompact(prev.bits);
target = consensus.fromCompact(prev.bits);

if (actualTimespan < targetTimespan / 4 | 0)
actualTimespan = targetTimespan / 4 | 0;
Expand All @@ -2002,7 +2003,7 @@ Chain.prototype.retarget = function retarget(prev, first) {
if (target.cmp(pow.limit) > 0)
return pow.bits;

return btcutils.toCompact(target);
return consensus.toCompact(target);
};

/**
Expand Down Expand Up @@ -2222,10 +2223,10 @@ Chain.prototype.verifyFinal = co(function* verifyFinal(prev, tx, flags) {
var ts;

// We can skip MTP if the locktime is height.
if (tx.locktime < constants.LOCKTIME_THRESHOLD)
if (tx.locktime < consensus.LOCKTIME_THRESHOLD)
return tx.isFinal(height, -1);

if (flags & constants.flags.MEDIAN_TIME_PAST) {
if (flags & constants.lockFlags.MEDIAN_TIME_PAST) {
ts = yield prev.getMedianTimeAsync();
return tx.isFinal(height, ts);
}
Expand All @@ -2244,11 +2245,11 @@ Chain.prototype.verifyFinal = co(function* verifyFinal(prev, tx, flags) {
*/

Chain.prototype.getLocks = co(function* getLocks(prev, tx, view, flags) {
var mask = constants.sequence.MASK;
var granularity = constants.sequence.GRANULARITY;
var disableFlag = constants.sequence.DISABLE_FLAG;
var typeFlag = constants.sequence.TYPE_FLAG;
var hasFlag = flags & constants.flags.VERIFY_SEQUENCE;
var mask = consensus.SEQUENCE_MASK;
var granularity = consensus.SEQUENCE_GRANULARITY;
var disableFlag = consensus.SEQUENCE_DISABLE_FLAG;
var typeFlag = consensus.SEQUENCE_TYPE_FLAG;
var hasFlag = flags & constants.lockFlags.VERIFY_SEQUENCE;
var nextHeight = this.height + 1;
var minHeight = -1;
var minTime = -1;
Expand Down Expand Up @@ -2325,9 +2326,9 @@ function DeploymentState() {
if (!(this instanceof DeploymentState))
return new DeploymentState();

this.flags = constants.flags.MANDATORY_VERIFY_FLAGS;
this.flags &= ~constants.flags.VERIFY_P2SH;
this.lockFlags = constants.flags.MANDATORY_LOCKTIME_FLAGS;
this.flags = Script.flags.MANDATORY_VERIFY_FLAGS;
this.flags &= ~Script.flags.VERIFY_P2SH;
this.lockFlags = constants.lockFlags.MANDATORY_LOCKTIME_FLAGS;
this.bip34 = false;
}

Expand All @@ -2337,7 +2338,7 @@ function DeploymentState() {
*/

DeploymentState.prototype.hasP2SH = function hasP2SH() {
return (this.flags & constants.flags.VERIFY_P2SH) !== 0;
return (this.flags & Script.flags.VERIFY_P2SH) !== 0;
};

/**
Expand All @@ -2355,7 +2356,7 @@ DeploymentState.prototype.hasBIP34 = function hasBIP34() {
*/

DeploymentState.prototype.hasBIP66 = function hasBIP66() {
return (this.flags & constants.flags.VERIFY_DERSIG) !== 0;
return (this.flags & Script.flags.VERIFY_DERSIG) !== 0;
};

/**
Expand All @@ -2364,7 +2365,7 @@ DeploymentState.prototype.hasBIP66 = function hasBIP66() {
*/

DeploymentState.prototype.hasCLTV = function hasCLTV() {
return (this.flags & constants.flags.VERIFY_CHECKLOCKTIMEVERIFY) !== 0;
return (this.flags & Script.flags.VERIFY_CHECKLOCKTIMEVERIFY) !== 0;
};

/**
Expand All @@ -2373,7 +2374,7 @@ DeploymentState.prototype.hasCLTV = function hasCLTV() {
*/

DeploymentState.prototype.hasMTP = function hasMTP() {
return (this.lockFlags & constants.flags.MEDIAN_TIME_PAST) !== 0;
return (this.lockFlags & constants.lockFlags.MEDIAN_TIME_PAST) !== 0;
};

/**
Expand All @@ -2382,7 +2383,7 @@ DeploymentState.prototype.hasMTP = function hasMTP() {
*/

DeploymentState.prototype.hasCSV = function hasCSV() {
return (this.flags & constants.flags.VERIFY_CHECKSEQUENCEVERIFY) !== 0;
return (this.flags & Script.flags.VERIFY_CHECKSEQUENCEVERIFY) !== 0;
};

/**
Expand All @@ -2391,7 +2392,7 @@ DeploymentState.prototype.hasCSV = function hasCSV() {
*/

DeploymentState.prototype.hasWitness = function hasWitness() {
return (this.flags & constants.flags.VERIFY_WITNESS) !== 0;
return (this.flags & Script.flags.VERIFY_WITNESS) !== 0;
};

/**
Expand Down
35 changes: 17 additions & 18 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

'use strict';

var assert = require('assert');
var AsyncObject = require('../utils/async');
var constants = require('../protocol/constants');
var util = require('../utils/util');
var assert = require('assert');
var BufferReader = require('../utils/reader');
var StaticWriter = require('../utils/staticwriter');
var Amount = require('../btc/amount');
Expand Down Expand Up @@ -312,7 +311,7 @@ ChainDB.prototype.getHeight = co(function* getHeight(hash) {

assert(typeof hash === 'string');

if (hash === constants.NULL_HASH)
if (hash === encoding.NULL_HASH)
return -1;

entry = this.cacheHash.get(hash);
Expand Down Expand Up @@ -441,7 +440,7 @@ ChainDB.prototype.getEntryByHash = co(function* getEntryByHash(hash) {

assert(typeof hash === 'string');

if (hash === constants.NULL_HASH)
if (hash === encoding.NULL_HASH)
return;

entry = this.cacheHash.get(hash);
Expand Down Expand Up @@ -567,8 +566,8 @@ ChainDB.prototype.getStateCache = co(function* getStateCache() {
var i, items, item, key, bit, hash, state;

items = yield this.db.range({
gte: layout.v(0, constants.ZERO_HASH),
lte: layout.v(255, constants.MAX_HASH),
gte: layout.v(0, encoding.ZERO_HASH),
lte: layout.v(255, encoding.MAX_HASH),
values: true
});

Expand Down Expand Up @@ -690,8 +689,8 @@ ChainDB.prototype.invalidateCache = co(function* invalidateCache(bit, batch) {
var i, keys, key;

keys = yield this.db.keys({
gte: layout.v(bit, constants.ZERO_HASH),
lte: layout.v(bit, constants.MAX_HASH)
gte: layout.v(bit, encoding.ZERO_HASH),
lte: layout.v(bit, encoding.MAX_HASH)
});

for (i = 0; i < keys.length; i++) {
Expand Down Expand Up @@ -731,7 +730,7 @@ ChainDB.prototype.isMainChain = co(function* isMainChain(hash) {
return true;
}

if (hash === constants.NULL_HASH)
if (hash === encoding.NULL_HASH)
return false;

entry = this.cacheHash.get(hash);
Expand All @@ -756,8 +755,8 @@ ChainDB.prototype.isMainChain = co(function* isMainChain(hash) {
ChainDB.prototype.getEntries = function getEntries() {
var self = this;
return this.db.values({
gte: layout.e(constants.ZERO_HASH),
lte: layout.e(constants.MAX_HASH),
gte: layout.e(encoding.ZERO_HASH),
lte: layout.e(encoding.MAX_HASH),
parse: function(value) {
return ChainEntry.fromRaw(self.chain, value);
}
Expand All @@ -771,8 +770,8 @@ ChainDB.prototype.getEntries = function getEntries() {

ChainDB.prototype.getTips = function getTips() {
return this.db.keys({
gte: layout.p(constants.ZERO_HASH),
lte: layout.p(constants.MAX_HASH),
gte: layout.p(encoding.ZERO_HASH),
lte: layout.p(encoding.MAX_HASH),
parse: layout.pp
});
};
Expand Down Expand Up @@ -1058,8 +1057,8 @@ ChainDB.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses)
continue;

keys = yield this.db.keys({
gte: layout.C(hash, constants.ZERO_HASH, 0),
lte: layout.C(hash, constants.MAX_HASH, 0xffffffff),
gte: layout.C(hash, encoding.ZERO_HASH, 0),
lte: layout.C(hash, encoding.MAX_HASH, 0xffffffff),
parse: layout.Cc
});

Expand Down Expand Up @@ -1096,8 +1095,8 @@ ChainDB.prototype.getHashesByAddress = co(function* getHashesByAddress(addresses
continue;

yield this.db.keys({
gte: layout.T(hash, constants.ZERO_HASH),
lte: layout.T(hash, constants.MAX_HASH),
gte: layout.T(hash, encoding.ZERO_HASH),
lte: layout.T(hash, encoding.MAX_HASH),
parse: function(key) {
var hash = layout.Tt(key);
hashes[hash] = true;
Expand Down Expand Up @@ -2062,7 +2061,7 @@ ChainOptions.fromRaw = function fromRaw(data) {
*/

function ChainState() {
this.tip = constants.ZERO_HASH;
this.tip = encoding.ZERO_HASH;
this.tx = 0;
this.coin = 0;
this.value = 0;
Expand Down
Loading

0 comments on commit 5caf621

Please sign in to comment.