chain: refactor deps.
This commit is contained in:
parent
376d6303b7
commit
d842b5639b
@ -7,13 +7,18 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bcoin = require('../env');
|
|
||||||
var AsyncObject = require('../utils/async');
|
var AsyncObject = require('../utils/async');
|
||||||
var constants = bcoin.constants;
|
var Network = require('../protocol/network');
|
||||||
|
var Logger = require('../node/logger');
|
||||||
|
var ChainDB = require('./chaindb');
|
||||||
|
var constants = require('../protocol/constants');
|
||||||
var utils = require('../utils/utils');
|
var utils = require('../utils/utils');
|
||||||
|
var Locker = require('../utils/locker');
|
||||||
|
var ChainEntry = require('./chainentry');
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
var VerifyError = bcoin.errors.VerifyError;
|
var VerifyError = require('../utils/errors').VerifyError;
|
||||||
var VerifyResult = utils.VerifyResult;
|
var VerifyResult = utils.VerifyResult;
|
||||||
|
var time = require('../net/timedata');
|
||||||
var spawn = require('../utils/spawn');
|
var spawn = require('../utils/spawn');
|
||||||
var co = spawn.co;
|
var co = spawn.co;
|
||||||
|
|
||||||
@ -67,13 +72,13 @@ function Chain(options) {
|
|||||||
|
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
this.network = bcoin.network.get(options.network);
|
this.network = Network.get(options.network);
|
||||||
this.logger = options.logger || bcoin.defaultLogger;
|
this.logger = options.logger || Logger.global;
|
||||||
this.db = new bcoin.chaindb(this);
|
this.db = new ChainDB(this);
|
||||||
this.total = 0;
|
this.total = 0;
|
||||||
this.currentBlock = null;
|
this.currentBlock = null;
|
||||||
this.orphanLimit = options.orphanLimit || (20 << 20);
|
this.orphanLimit = options.orphanLimit || (20 << 20);
|
||||||
this.locker = new bcoin.locker(true);
|
this.locker = new Locker(true);
|
||||||
this.invalid = {};
|
this.invalid = {};
|
||||||
this.bestHeight = -1;
|
this.bestHeight = -1;
|
||||||
this.tip = null;
|
this.tip = null;
|
||||||
@ -1173,7 +1178,7 @@ Chain.prototype._add = co(function* add(block) {
|
|||||||
block.setHeight(height);
|
block.setHeight(height);
|
||||||
|
|
||||||
// Create a new chain entry.
|
// Create a new chain entry.
|
||||||
entry = bcoin.chainentry.fromBlock(this, block, prev);
|
entry = ChainEntry.fromBlock(this, block, prev);
|
||||||
|
|
||||||
// The block is on a alternate chain if the
|
// The block is on a alternate chain if the
|
||||||
// chainwork is less than or equal to
|
// chainwork is less than or equal to
|
||||||
@ -1658,7 +1663,7 @@ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) {
|
|||||||
if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) {
|
if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) {
|
||||||
if (this.network.pow.difficultyReset) {
|
if (this.network.pow.difficultyReset) {
|
||||||
// Special behavior for testnet:
|
// Special behavior for testnet:
|
||||||
ts = block ? (block.ts || block) : bcoin.now();
|
ts = block ? (block.ts || block) : time.now();
|
||||||
if (ts > prev.ts + this.network.pow.targetSpacing * 2)
|
if (ts > prev.ts + this.network.pow.targetSpacing * 2)
|
||||||
return this.network.pow.bits;
|
return this.network.pow.bits;
|
||||||
|
|
||||||
@ -1945,7 +1950,7 @@ Chain.prototype.checkFinal = co(function* checkFinal(prev, tx, flags) {
|
|||||||
return tx.isFinal(height, ts);
|
return tx.isFinal(height, ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx.isFinal(height, bcoin.now());
|
return tx.isFinal(height, time.now());
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -7,9 +7,8 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bcoin = require('../env');
|
|
||||||
var AsyncObject = require('../utils/async');
|
var AsyncObject = require('../utils/async');
|
||||||
var constants = bcoin.constants;
|
var constants = require('../protocol/constants');
|
||||||
var utils = require('../utils/utils');
|
var utils = require('../utils/utils');
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
var DUMMY = new Buffer([0]);
|
var DUMMY = new Buffer([0]);
|
||||||
@ -18,6 +17,13 @@ var BufferReader = require('../utils/reader');
|
|||||||
var spawn = require('../utils/spawn');
|
var spawn = require('../utils/spawn');
|
||||||
var CoinView = require('./coinview');
|
var CoinView = require('./coinview');
|
||||||
var Coins = require('./coins');
|
var Coins = require('./coins');
|
||||||
|
var ldb = require('../db/ldb');
|
||||||
|
var LRU = require('../utils/lru');
|
||||||
|
var Block = require('../primitives/block');
|
||||||
|
var Coin = require('../primitives/coin');
|
||||||
|
var TX = require('../primitives/tx');
|
||||||
|
var Address = require('../primitives/address');
|
||||||
|
var ChainEntry = require('./chainentry');
|
||||||
var co = spawn.co;
|
var co = spawn.co;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -164,7 +170,7 @@ function ChainDB(chain) {
|
|||||||
this.logger = chain.logger;
|
this.logger = chain.logger;
|
||||||
this.network = chain.network;
|
this.network = chain.network;
|
||||||
|
|
||||||
this.db = bcoin.ldb({
|
this.db = ldb({
|
||||||
location: this.options.location,
|
location: this.options.location,
|
||||||
db: this.options.db,
|
db: this.options.db,
|
||||||
maxOpenFiles: this.options.maxFiles,
|
maxOpenFiles: this.options.maxFiles,
|
||||||
@ -191,12 +197,12 @@ function ChainDB(chain) {
|
|||||||
// We want to keep the last 5 blocks of unspents in memory.
|
// We want to keep the last 5 blocks of unspents in memory.
|
||||||
this.coinWindow = 25 << 20;
|
this.coinWindow = 25 << 20;
|
||||||
|
|
||||||
this.coinCache = new bcoin.lru.nil();
|
this.coinCache = new LRU.nil();
|
||||||
this.cacheHash = new bcoin.lru(this.cacheWindow);
|
this.cacheHash = new LRU(this.cacheWindow);
|
||||||
this.cacheHeight = new bcoin.lru(this.cacheWindow);
|
this.cacheHeight = new LRU(this.cacheWindow);
|
||||||
|
|
||||||
if (this.options.coinCache)
|
if (this.options.coinCache)
|
||||||
this.coinCache = new bcoin.lru(this.coinWindow, getSize);
|
this.coinCache = new LRU(this.coinWindow, getSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.inherits(ChainDB, AsyncObject);
|
utils.inherits(ChainDB, AsyncObject);
|
||||||
@ -231,9 +237,9 @@ ChainDB.prototype._open = co(function* open() {
|
|||||||
} else {
|
} else {
|
||||||
// Otherwise write the genesis block.
|
// Otherwise write the genesis block.
|
||||||
// (We assume this database is fresh).
|
// (We assume this database is fresh).
|
||||||
block = bcoin.block.fromRaw(this.network.genesisBlock, 'hex');
|
block = Block.fromRaw(this.network.genesisBlock, 'hex');
|
||||||
block.setHeight(0);
|
block.setHeight(0);
|
||||||
entry = bcoin.chainentry.fromBlock(this.chain, block);
|
entry = ChainEntry.fromBlock(this.chain, block);
|
||||||
yield this.save(entry, block, new CoinView());
|
yield this.save(entry, block, new CoinView());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -514,7 +520,7 @@ ChainDB.prototype.getEntry = co(function* getEntry(hash) {
|
|||||||
if (!entry)
|
if (!entry)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
return bcoin.chainentry.fromRaw(this.chain, entry);
|
return ChainEntry.fromRaw(this.chain, entry);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -703,7 +709,7 @@ ChainDB.prototype.getNextHash = co(function* getNextHash(hash) {
|
|||||||
ChainDB.prototype.isMainChain = co(function* isMainChain(hash) {
|
ChainDB.prototype.isMainChain = co(function* isMainChain(hash) {
|
||||||
var query, height, existing;
|
var query, height, existing;
|
||||||
|
|
||||||
if (hash instanceof bcoin.chainentry) {
|
if (hash instanceof ChainEntry) {
|
||||||
query = hash.height;
|
query = hash.height;
|
||||||
hash = hash.hash;
|
hash = hash.hash;
|
||||||
} else {
|
} else {
|
||||||
@ -1071,7 +1077,7 @@ ChainDB.prototype.fillHistory = co(function* fillHistory(tx) {
|
|||||||
ptx = yield this.getTX(input.prevout.hash);
|
ptx = yield this.getTX(input.prevout.hash);
|
||||||
|
|
||||||
if (ptx)
|
if (ptx)
|
||||||
input.coin = bcoin.coin.fromTX(ptx, input.prevout.index);
|
input.coin = Coin.fromTX(ptx, input.prevout.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
return tx;
|
return tx;
|
||||||
@ -1200,7 +1206,7 @@ ChainDB.prototype.getTX = co(function* getTX(hash) {
|
|||||||
if (!data)
|
if (!data)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
return bcoin.tx.fromExtended(data);
|
return TX.fromExtended(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1233,7 +1239,7 @@ ChainDB.prototype.getCoinsByAddress = co(function* getCoinsByAddress(addresses)
|
|||||||
|
|
||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < addresses.length; i++) {
|
||||||
address = addresses[i];
|
address = addresses[i];
|
||||||
hash = bcoin.address.getHash(address);
|
hash = Address.getHash(address);
|
||||||
|
|
||||||
if (!hash)
|
if (!hash)
|
||||||
continue;
|
continue;
|
||||||
@ -1267,7 +1273,7 @@ ChainDB.prototype.getEntries = function getEntries() {
|
|||||||
gte: layout.e(constants.ZERO_HASH),
|
gte: layout.e(constants.ZERO_HASH),
|
||||||
lte: layout.e(constants.MAX_HASH),
|
lte: layout.e(constants.MAX_HASH),
|
||||||
parse: function(key, value) {
|
parse: function(key, value) {
|
||||||
return bcoin.chainentry.fromRaw(self.chain, value);
|
return ChainEntry.fromRaw(self.chain, value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -1287,7 +1293,7 @@ ChainDB.prototype.getHashesByAddress = co(function* getHashesByAddress(addresses
|
|||||||
|
|
||||||
for (i = 0; i < addresses.length; i++) {
|
for (i = 0; i < addresses.length; i++) {
|
||||||
address = addresses[i];
|
address = addresses[i];
|
||||||
hash = bcoin.address.getHash(address);
|
hash = Address.getHash(address);
|
||||||
|
|
||||||
if (!hash)
|
if (!hash)
|
||||||
continue;
|
continue;
|
||||||
@ -1410,7 +1416,7 @@ ChainDB.prototype.getUndoCoins = co(function* getUndoCoins(hash) {
|
|||||||
coins = [];
|
coins = [];
|
||||||
|
|
||||||
while (p.left())
|
while (p.left())
|
||||||
coins.push(bcoin.coin.fromRaw(p));
|
coins.push(Coin.fromRaw(p));
|
||||||
|
|
||||||
return coins;
|
return coins;
|
||||||
});
|
});
|
||||||
@ -1472,7 +1478,7 @@ ChainDB.prototype.getBlock = co(function* getBlock(hash) {
|
|||||||
if (!data)
|
if (!data)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
block = bcoin.block.fromRaw(data);
|
block = Block.fromRaw(data);
|
||||||
block.setHeight(height);
|
block.setHeight(height);
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
|
|||||||
@ -7,14 +7,16 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bcoin = require('../env');
|
|
||||||
var bn = require('bn.js');
|
var bn = require('bn.js');
|
||||||
var constants = bcoin.constants;
|
var Network = require('../protocol/network');
|
||||||
|
var constants = require('../protocol/constants');
|
||||||
var utils = require('../utils/utils');
|
var utils = require('../utils/utils');
|
||||||
var crypto = require('../crypto/crypto');
|
var crypto = require('../crypto/crypto');
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
var BufferWriter = require('../utils/writer');
|
var BufferWriter = require('../utils/writer');
|
||||||
var BufferReader = require('../utils/reader');
|
var BufferReader = require('../utils/reader');
|
||||||
|
var Header = require('../primitives/headers');
|
||||||
|
var InvItem = require('../primitives/invitem');
|
||||||
var spawn = require('../utils/spawn');
|
var spawn = require('../utils/spawn');
|
||||||
var co = spawn.co;
|
var co = spawn.co;
|
||||||
|
|
||||||
@ -48,7 +50,7 @@ function ChainEntry(chain, options, prev) {
|
|||||||
return new ChainEntry(chain, options, prev);
|
return new ChainEntry(chain, options, prev);
|
||||||
|
|
||||||
this.chain = chain;
|
this.chain = chain;
|
||||||
this.network = chain ? chain.network : bcoin.network.get();
|
this.network = chain ? chain.network : Network.primary;
|
||||||
|
|
||||||
this.hash = constants.NULL_HASH;
|
this.hash = constants.NULL_HASH;
|
||||||
this.version = 1;
|
this.version = 1;
|
||||||
@ -608,7 +610,7 @@ ChainEntry.fromJSON = function fromJSON(chain, json) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
ChainEntry.prototype.toHeaders = function toHeaders() {
|
ChainEntry.prototype.toHeaders = function toHeaders() {
|
||||||
return bcoin.headers.fromEntry(this);
|
return Headers.fromEntry(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -617,7 +619,7 @@ ChainEntry.prototype.toHeaders = function toHeaders() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
ChainEntry.prototype.toInv = function toInv() {
|
ChainEntry.prototype.toInv = function toInv() {
|
||||||
return new bcoin.invitem(constants.inv.BLOCK, this.hash);
|
return new InvItem(constants.inv.BLOCK, this.hash);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -7,10 +7,11 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bcoin = require('../env');
|
var utils = require('../utils/utils');
|
||||||
var utils = bcoin.utils;
|
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
var constants = bcoin.constants;
|
var constants = require('../protocol/constants');
|
||||||
|
var Coin = require('../primitives/coin');
|
||||||
|
var ec = require('../crypto/ec');
|
||||||
var BufferReader = require('../utils/reader');
|
var BufferReader = require('../utils/reader');
|
||||||
var BufferWriter = require('../utils/writer');
|
var BufferWriter = require('../utils/writer');
|
||||||
|
|
||||||
@ -449,7 +450,7 @@ Coins.prototype.fromTX = function fromTX(tx) {
|
|||||||
this.outputs.push(null);
|
this.outputs.push(null);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
this.outputs.push(bcoin.coin.fromTX(tx, i));
|
this.outputs.push(Coin.fromTX(tx, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@ -502,7 +503,7 @@ function CompressedCoin(offset, size, raw) {
|
|||||||
|
|
||||||
CompressedCoin.prototype.toCoin = function toCoin(coins, index) {
|
CompressedCoin.prototype.toCoin = function toCoin(coins, index) {
|
||||||
var p = new BufferReader(this.raw);
|
var p = new BufferReader(this.raw);
|
||||||
var coin = new bcoin.coin();
|
var coin = new Coin();
|
||||||
|
|
||||||
// Load in all necessary properties
|
// Load in all necessary properties
|
||||||
// from the parent Coins object.
|
// from the parent Coins object.
|
||||||
@ -681,7 +682,7 @@ function compressKey(key) {
|
|||||||
var out;
|
var out;
|
||||||
|
|
||||||
// We can't compress it if it's not valid.
|
// We can't compress it if it's not valid.
|
||||||
if (!bcoin.ec.publicKeyVerify(key))
|
if (!ec.publicKeyVerify(key))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (key[0]) {
|
switch (key[0]) {
|
||||||
@ -694,7 +695,7 @@ function compressKey(key) {
|
|||||||
case 0x06:
|
case 0x06:
|
||||||
case 0x07:
|
case 0x07:
|
||||||
// Compress the key normally.
|
// Compress the key normally.
|
||||||
out = bcoin.ec.publicKeyConvert(key, true);
|
out = ec.publicKeyConvert(key, true);
|
||||||
// Store the original format (which
|
// Store the original format (which
|
||||||
// may be a hybrid byte) in the hi
|
// may be a hybrid byte) in the hi
|
||||||
// 3 bits so we can restore it later.
|
// 3 bits so we can restore it later.
|
||||||
@ -733,7 +734,7 @@ function decompressKey(key) {
|
|||||||
// low bits so publicKeyConvert
|
// low bits so publicKeyConvert
|
||||||
// actually understands it.
|
// actually understands it.
|
||||||
key[0] &= 0x03;
|
key[0] &= 0x03;
|
||||||
out = bcoin.ec.publicKeyConvert(key, false);
|
out = ec.publicKeyConvert(key, false);
|
||||||
|
|
||||||
// Reset the hi bits so as not to
|
// Reset the hi bits so as not to
|
||||||
// mutate the original buffer.
|
// mutate the original buffer.
|
||||||
|
|||||||
@ -7,9 +7,9 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bcoin = require('../env');
|
var utils = require('../utils/utils');
|
||||||
var utils = bcoin.utils;
|
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
|
var Coins = require('./coins');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A collections of {@link Coins} objects.
|
* A collections of {@link Coins} objects.
|
||||||
@ -43,7 +43,7 @@ CoinView.prototype.add = function add(coins) {
|
|||||||
CoinView.prototype.addCoin = function addCoin(coin) {
|
CoinView.prototype.addCoin = function addCoin(coin) {
|
||||||
assert(typeof coin.hash === 'string');
|
assert(typeof coin.hash === 'string');
|
||||||
if (!this.coins[coin.hash])
|
if (!this.coins[coin.hash])
|
||||||
this.coins[coin.hash] = new bcoin.coins();
|
this.coins[coin.hash] = new Coins();
|
||||||
this.coins[coin.hash].add(coin);
|
this.coins[coin.hash].add(coin);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ CoinView.prototype.addCoin = function addCoin(coin) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
CoinView.prototype.addTX = function addTX(tx) {
|
CoinView.prototype.addTX = function addTX(tx) {
|
||||||
this.add(bcoin.coins.fromTX(tx));
|
this.add(Coins.fromTX(tx));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user