Compare commits

..

89 Commits

Author SHA1 Message Date
sairajzero
8121a2ba96 Bug fix
- Fixed: mempool (unconfirmed tx) getting repeated in chain query when using before option
2023-04-27 04:15:43 +05:30
sairajzero
31344d770e Adding 'before' option to address APIs
- Similar to 'after' option but inverse of it. ie, if before txid is passed, then query list/values before the given txid
- 'before' and 'after' option can be use in combination or individually
- cache system for address summary api wont be used with 'after' and/or 'before' option is used
2023-04-27 03:48:09 +05:30
sairajzero
96677310c2 Bug fixes 2023-04-27 01:41:22 +05:30
sairajzero
e180672583 Adding reverse option to address history query
- Using option `reverse` as true will query the latest 1000 (max val) tx instead of the 1st 1000 tx.
2023-04-23 02:55:49 +05:30
sairajzero
158d5aefc2 Decrease MAX_TX_QUERY_LIMIT_SUMMARY to 500
- Summary cache is triggered for addresses with more than 500 tx
2023-04-22 23:55:11 +05:30
sairajzero
e7248320a6 bug fix: txid & blockhash not cached correctly 2023-04-22 22:19:23 +05:30
sairajzero
60289a644b Fix: Incorrect values in cache during reorg
- store the blockhash of lastTx in cache value
- If last cached tx-block was removed (during reorg), then delete the cache and recalculate the values.
2023-04-21 22:51:45 +05:30
sairajzero
d42e569008 update address-cache key prefix 2023-04-19 03:03:21 +05:30
sairajzero
6beb2ecd06 Adding function to delete cache for a given addr
Adding _deleteCache: deletes the cache for given address (useful for dev purposes or to recalculate caches)
2023-04-19 02:31:04 +05:30
sairajzero
4d023760ad Adding del (delete) function in db service 2023-04-19 02:28:44 +05:30
sairajzero
10d9459f26 Bug fix: unconfirmed-tx values corrupting cache 2023-04-19 01:24:37 +05:30
sairajzero
8b07a1e4a5 separate MAX_TX_QUERY_LIMIT for each query type 2023-04-19 01:03:45 +05:30
sairajzero
4afb0dfaaa Bug fix: cache summary not working properly 2023-04-19 00:45:44 +05:30
sairajzero
f8260541ef Fix: Cache value encoding
- Using BigInt (uInt64) for cache value (balance, sent, received) as they are large for uInt32
- Reflect the changes for BigInt (uInt64) in decode cache value
- Removed default values in cache encoding, as if a value is invalid then it must throw error and not write db
2023-04-19 00:42:47 +05:30
sairajzero
6b794aa9a3 Fixes: cache storing unconfirmed values
- removed unconfirmed values from cache store and get
-  do not update lastItem value for unconfirmed tx
2023-04-18 04:37:01 +05:30
sairajzero
adb81616aa Run cache summary
run cache storage for address summary in background when queried result is incomplete
2023-04-18 04:23:41 +05:30
sairajzero
3982807b32 Cache address-summary
Store cache of address summary in db when address has more than MAX_TX_QUERY_LIMIT
2023-04-17 20:13:29 +05:30
sairajzero
0a3a1b5ea6 changing typeof to equivalent lodash 2023-04-17 20:07:12 +05:30
sairajzero
df7710ded1 Adding option mempoolOnly in querying
- mempoolOnly option make the query in db from mempool only (ie, unconfirmed tx only
2023-04-13 03:52:02 +05:30
sairajzero
27f3993884 Fixed: inconsistency addrs API on multiple address
- Fixed: addrs API on multiple addresses when total tx is more than 1000 (MAX_TX_QUERY_LIMIT) giving inconsistent list of tx due to order messed up in parallel query
2023-04-13 02:26:33 +05:30
sairajzero
71bcafb243 Limited data responses identification
- address-query responses data has `incomplete` property set to `true` when addr has more than 1000 (MAX_TX_QUERY_LIMIT)
2023-04-11 04:04:19 +05:30
sairajzero
1b6352573f Limit all address query to MAX_TX_QUERY_LIMIT
- any address data API will now max out at 1000 (MAX_TX_QUERY_LIMIT)
- For addrs with more than 1000 tx, use chained API query to get the complete data (like balance, txid, etc)

- set parallel queue limit to 1: preserve consistency in API queries (and prevent incorrect data)
2023-04-11 03:49:21 +05:30
sairajzero
7f86e488e4 bug fix
Fixed bug: api on addr not giving response (timeoout) when it has only 1 tx
2023-04-06 22:25:03 +05:30
sairajzero
37e08b0801 Bug fix
- Fixed: Incorrect ordering of tx list with unconfirmed tx
2023-02-06 02:02:52 +05:30
Sai Raj
16bed1b811
Merge pull request #9 from ranchimall/api-improvements
API Improvements
2023-02-06 00:18:20 +05:30
sairajzero
bca4fe4f97 Bug fix
- Fixed: data inconsistency and continuity lost in chain querying of tx details
- Fixed: Not getting response when query has no tx. (ie, either address has no tx, or using the most recent tx as the key in `after` option)
2023-02-05 23:01:06 +05:30
sairajzero
7409dbb77d Bug fixes
- Fixed: incorrect data returned via `from` and `to` option
- Fixed: Missing data due to unordered items in getAddressHistory
- Fixed: callback invoked multiple items in _streamAddressSummary due to queue parallel limit
- Fixed: Queue drain being invoked before mempool txs are pushed into queue
2023-02-05 19:03:11 +05:30
sairajzero
774d830fff Improvements to API query options
- Stop request-stream process when stop-flag is on
- Changed: order of reading db to forward (so that continuity will be preserved with `after` option and ws api calls). And changes required for the same.
- Deprecating options (from and to) in calls that are not supported
- Added: Temporary support for from and to option in getAddressHistory
2023-02-05 03:12:04 +05:30
sairajzero
3a75002efc API Query options
- Fixed: option `start` to query from blockheight
- Added option `after`: pass this option in API to get list after the given txid
Note: If both `start` and `after` are given, then greater height will be used
Note: invalid or unconfirmed txid cannot be used in `after` option and will be ignored
2023-02-04 20:29:41 +05:30
sairajzero
d9579853ad Improve handling of duplicate tx query
- temporarily store txids to ignore duplication
- removed the queue pause() and resume() in _streamAddressSummary
2023-02-02 19:14:29 +05:30
sairajzero
3fbcbbe7bc Fixed: APIs giving incorrect data
- Fixed: addr API giving decimals in satoshi values
- Fixed: Incorrect balance, totalSent, totalReceived values returned in API calls (issue: duplication)
- Fixed: incorrect totalCount value in addr API and duplication of tx list
2023-01-28 21:55:58 +05:30
sairajzero
dbfe39991f Fixed: Address-summary request not responding 2023-01-28 02:04:40 +05:30
sairajzero
6c164993bf Update package.json 2023-01-27 22:33:30 +05:30
sairajzero
2145fdb056 pass 'express-ws' module to service setupRoutes
- flosight-api uses 'express-ws' module for ws api calls
2023-01-27 18:07:38 +05:30
sairajzero
4472ed8394 Changing fns to use _streamAddressSummary
Functions updated:
- getAddressHistory
- getAddressSummary

(old fns are kept as it is and renamed to __getAddressHistory and __getAddressSummary respectively)
2023-01-27 17:38:37 +05:30
sairajzero
e13bd5e3e6 Adding _streamAddressSummary
- Fn uses streamer to process data. Thus doesnt store the entire list of txid or tx details
- streamer fn can process the tx data as required
2023-01-27 17:36:39 +05:30
sairajzero
0283be05db Limit max response size 2023-01-27 17:31:02 +05:30
Sai Raj
25eb992cf1
Merge pull request #8 from ranchimall/block-subscribe-stuck-fix
Fix: Block subscribe getting stuck
2023-01-18 00:24:09 +05:30
sairajzero
69e9465b93 block service: set MAX_IGNORED_BLOCK to 16 2023-01-18 00:19:20 +05:30
sairajzero
700abe0500 hotfix: block subscription getting stuck
- Issue: Block subscription getting stuck when a (missing) block wasn't received by block service.
- Solution: Re-trigger the sync process when too many blocks ignored

- Others: fixed a typo in _reportInterval property
2023-01-17 21:54:48 +05:30
sairajzero
0572ee6b35 header: clear _syncCheckInterval upon synced
Header service: clear _syncCheckInterval to null when clearing interval after sync completed
2023-01-17 16:44:42 +05:30
Sai Raj
e6826c7dfc
Merge pull request #6 from ranchimall/startup-sync-fix
Startup sync fix
2023-01-15 12:33:50 +05:30
sairajzero
ece347c825 hotfix: best header not updating for prev fix
- updated header service best header from fcoin node directly.
- set interval check only if fcoin is started by flocore (else header best height ll not get updated and sync complete incorrectly)
2023-01-15 00:02:25 +05:30
sairajzero
b831cbce7e Update .gitignore 2023-01-14 23:56:18 +05:30
sairajzero
f9e2ed304b hotfix for unresponsive header sync 2023-01-11 03:38:38 +05:30
00515c5378
Update index.js
Added a missing return HeaderService.prototype._handleError
2023-01-09 05:12:39 +05:30
Sai Raj
e3f5de4df5
Merge pull request #3 from ranchimall/uncaught-no-shut
hotfix: Do not shutdown on unhandled exceptions
2023-01-08 17:36:33 +05:30
sairajzero
26b65d63a8 hotfix: Do not shutdown on unhandled exceptions 2023-01-08 17:13:30 +05:30
4113d9cfd0
Update index.js 2022-01-25 11:23:06 +05:30
7fa2f096df
Update reorg.js 2022-01-25 11:18:46 +05:30
fcecf08ac0
Update reorg.js 2022-01-25 11:18:13 +05:30
af6048de93
Update index.js 2022-01-25 11:13:27 +05:30
d47b6047e5
Update index.js 2022-01-25 11:00:27 +05:30
c525516a95
Update reorg.js 2022-01-25 10:57:40 +05:30
0eaa4b6fd2
Update index.js 2022-01-25 10:54:54 +05:30
2921e389df
Update block_handler.js 2022-01-25 00:31:31 +05:30
522d00bd52
Update index.js 2022-01-25 00:25:25 +05:30
821aae706d
Update index.js 2022-01-25 00:22:55 +05:30
18d1a16b0c
Update reorg.js 2022-01-25 00:07:57 +05:30
9c0ec67ccf
Update index.js 2022-01-25 00:03:52 +05:30
f0768027c9
Update index.js 2022-01-24 18:06:15 +05:30
80f22f731f
Update index.js 2022-01-24 17:57:48 +05:30
81a3d5f8ff
Update reorg.js 2022-01-24 17:56:37 +05:30
69fc6790ae
Update reorg.js 2022-01-24 17:55:39 +05:30
5db68b6bb7
Update reorg.js 2022-01-24 17:53:30 +05:30
091d7aa863
Update reorg.js 2022-01-24 17:52:14 +05:30
582bdd698a
Update reorg.js 2022-01-24 17:50:10 +05:30
726156843e
Update index.js 2022-01-24 17:45:10 +05:30
846f85e2f8
Update block_handler.js 2022-01-24 17:40:20 +05:30
ca47013c5a
Update block_handler.js 2022-01-24 15:17:50 +05:30
82357f2ecc
Update reorg.js 2022-01-24 15:17:02 +05:30
c3cc5f7465
Update index.js 2022-01-24 15:16:15 +05:30
1888b4a4ae
Throwing error removed
new Error was stopping the running instance of flosight. Substituted by log.info
2022-01-24 15:15:11 +05:30
1edc88f14b
Update node.js 2021-05-26 19:54:53 +05:30
0966ec124b
Stop the stoppage of service 2021-05-25 14:58:20 +05:30
05c6cd7739
Fixed header existence conditions 2021-05-25 11:07:56 +05:30
2c37e05ff7
Added test to check header.height 2021-05-25 11:01:31 +05:30
sairajzero
0cfb80a164 reverting log.info to log.debug 2021-05-24 17:56:13 +05:30
11b0a58351
Fixed syntax issue 2021-05-24 16:54:44 +05:30
869a7c21b4
Converted _sync log.debug to log.info 2021-05-24 16:29:15 +05:30
17dd83de10
db index.js asserts removed for resilience 2021-05-20 09:40:40 +05:30
bad1fb2552
Updated definition of index.log 2021-05-18 11:02:41 +05:30
5344d9cd5b
Fixed the conditional operator 2021-05-17 16:14:54 +05:30
89c43cd9a7
Fixing the conditional operator 2021-05-17 16:11:11 +05:30
0d4a7e3e42
Modification of condition operator 2021-05-17 16:06:06 +05:30
d689045002
Updated comparison operator 2021-05-17 16:00:27 +05:30
Vivek Teega
1efed08a39 1.0.9 Removing assertions in transction and p2p service 2021-05-17 14:02:31 +05:30
Vivek Teega
56e5cb1f25 1.0.8 Commented out assertion stops 2021-05-17 13:37:19 +05:30
Vivek Teega
453d11c64c 1.0.7 Commented out assertion stops 2021-05-17 12:59:36 +05:30
37 changed files with 1144 additions and 285 deletions

2
.gitignore vendored
View File

@ -21,6 +21,8 @@ coverage/*
**/*.config
**/*.creator
*.log
*.tmp
*.tmp.*
.DS_Store
bin/florincoin*
bin/SHA256SUMS.asc

View File

@ -12,7 +12,7 @@ var dbLocation = process.argv[2];
console.log('Using db location: ', dbLocation);
var addressPrefix = Buffer.from('0006', 'hex');
var addressPrefix = new Buffer('0006', 'hex');
var startAddress = new Array(35).join('0');
var endAddress = new Array(35).join('f');

View File

@ -7,19 +7,19 @@ var dbPath = '/Users/chrisk/.bwdb/flocore-node.db';
var flocore = require('flocore-lib');
var db = levelup(dbPath, {keyEncoding: 'binary', valueEncoding: 'binary'});
var prefix = Buffer.from('0002', 'hex');
var prefix = new Buffer('0002', 'hex');
var encoding = new Encoding(prefix);
var address = '1MfDRRVVKXUe5KNVZzu8CBzUZDHTTYZM94';
var addressLength = Buffer.alloc(1);
var addressLength = new Buffer(1);
addressLength.writeUInt8(address.length);
//var startBuffer = prefix;
//var endBuffer = Buffer.concat([prefix, Buffer.from('ff', 'hex')]);
//var endBuffer = Buffer.concat([prefix, new Buffer('ff', 'hex')]);
//var startBuffer = Buffer.concat([prefix, addressLength, Buffer.from(address, 'utf8'), Buffer.from('00', 'hex')]);
//var endBuffer = Buffer.concat([prefix, addressLength, Buffer.from(address, 'utf8'), Buffer.from('01', 'hex')]);
var start = Buffer.concat([prefix, Buffer.from('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex')]);
var end = Buffer.concat([prefix, Buffer.from('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex'), Buffer.from('01', 'hex')]);
//var startBuffer = Buffer.concat([prefix, addressLength, new Buffer(address, 'utf8'), new Buffer('00', 'hex')]);
//var endBuffer = Buffer.concat([prefix, addressLength, new Buffer(address, 'utf8'), new Buffer('01', 'hex')]);
var start = Buffer.concat([prefix, new Buffer('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex')]);
var end = Buffer.concat([prefix, new Buffer('0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9', 'hex'), new Buffer('01', 'hex')]);
var stream = db.createReadStream({
gte: start,
lt: end

View File

@ -8,6 +8,6 @@ module.exports = {
testnet: '9b7bc86236c34b5e3a39367c036b7fe8807a966c22a7a1f0da2a198a27e03731', //this is testnet3
testnet5: '000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943' //this is testnet5
},
DB_PREFIX: Buffer.from('ffff', 'hex')
DB_PREFIX: new Buffer('ffff', 'hex')
};

View File

@ -212,6 +212,7 @@ function exitHandler(options, _process, node, err) {
if(err.stack) {
log.error(err.stack);
}
if(options.exit)
node.stop(function(err) {
if(err) {
log.error('Failed to stop services: ' + err);
@ -229,7 +230,7 @@ function exitHandler(options, _process, node, err) {
}
function registerExitHandlers(_process, node) {
_process.on('uncaughtException', exitHandler.bind(null, {exit:true}, _process, node));
_process.on('uncaughtException', exitHandler.bind(null, {exit:false}, _process, node));
_process.on('SIGINT', exitHandler.bind(null, {sigint:true}, _process, node));
}

View File

@ -2,36 +2,38 @@
function Encoding(servicePrefix) {
this.servicePrefix = servicePrefix;
this.addressIndex = new Buffer('00', 'hex');
this.utxoIndex = new Buffer('01', 'hex');
this.addressCache = new Buffer('fe', 'hex');
}
Encoding.prototype.encodeAddressIndexKey = function(address, height, txid, index, input, timestamp) {
var prefix = Buffer.from('00', 'hex');
var buffers = [this.servicePrefix, prefix];
var buffers = [this.servicePrefix, this.addressIndex];
var addressSizeBuffer = Buffer.alloc(1);
var addressSizeBuffer = new Buffer(1);
addressSizeBuffer.writeUInt8(address.length);
var addressBuffer = Buffer.from(address, 'utf8');
var addressBuffer = new Buffer(address, 'utf8');
buffers.push(addressSizeBuffer);
buffers.push(addressBuffer);
var heightBuffer = Buffer.alloc(4);
var heightBuffer = new Buffer(4);
heightBuffer.writeUInt32BE(height || 0);
buffers.push(heightBuffer);
var txidBuffer = Buffer.from(txid || Array(65).join('0'), 'hex');
var txidBuffer = new Buffer(txid || Array(65).join('0'), 'hex');
buffers.push(txidBuffer);
var indexBuffer = Buffer.alloc(4);
var indexBuffer = new Buffer(4);
indexBuffer.writeUInt32BE(index || 0);
buffers.push(indexBuffer);
// this is whether the address appears in an input (1) or output (0)
var inputBuffer = Buffer.alloc(1);
var inputBuffer = new Buffer(1);
inputBuffer.writeUInt8(input || 0);
buffers.push(inputBuffer);
var timestampBuffer = Buffer.alloc(4);
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(timestamp || 0);
buffers.push(timestampBuffer);
@ -58,20 +60,19 @@ Encoding.prototype.decodeAddressIndexKey = function(buffer) {
};
Encoding.prototype.encodeUtxoIndexKey = function(address, txid, outputIndex) {
var prefix = Buffer.from('01', 'hex');
var buffers = [this.servicePrefix, prefix];
var buffers = [this.servicePrefix, this.utxoIndex];
var addressSizeBuffer = Buffer.alloc(1);
var addressSizeBuffer = new Buffer(1);
addressSizeBuffer.writeUInt8(address.length);
var addressBuffer = Buffer.from(address, 'utf8');
var addressBuffer = new Buffer(address, 'utf8');
buffers.push(addressSizeBuffer);
buffers.push(addressBuffer);
var txidBuffer = Buffer.from(txid || new Array(65).join('0'), 'hex');
var txidBuffer = new Buffer(txid || new Array(65).join('0'), 'hex');
buffers.push(txidBuffer);
var outputIndexBuffer = Buffer.alloc(4);
var outputIndexBuffer = new Buffer(4);
outputIndexBuffer.writeUInt32BE(outputIndex || 0);
buffers.push(outputIndexBuffer);
@ -92,11 +93,11 @@ Encoding.prototype.decodeUtxoIndexKey = function(buffer) {
};
Encoding.prototype.encodeUtxoIndexValue = function(height, satoshis, timestamp, scriptBuffer) {
var heightBuffer = Buffer.alloc(4);
var heightBuffer = new Buffer(4);
heightBuffer.writeUInt32BE(height);
var satoshisBuffer = Buffer.alloc(8);
var satoshisBuffer = new Buffer(8);
satoshisBuffer.writeDoubleBE(satoshis);
var timestampBuffer = Buffer.alloc(4);
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(timestamp || 0);
return Buffer.concat([heightBuffer, satoshisBuffer, timestampBuffer, scriptBuffer]);
};
@ -114,5 +115,53 @@ Encoding.prototype.decodeUtxoIndexValue = function(buffer) {
};
};
Encoding.prototype.encodeAddressCacheKey = function(address) {
return Buffer.concat([this.servicePrefix, this.addressCache, new Buffer(address, 'utf8')]);
}
Encoding.prototype.decodeAddressCacheKey = function(buffer) {
return buffer.slice(3).toString('utf8');
}
Encoding.prototype.encodeAddressCacheValue = function(lastTx, lastBlock, balance, received, sent, txApperances) {
var buffer = [];
var balanceBuffer = new Buffer(8);
balanceBuffer.writeBigUInt64BE(BigInt(balance));
buffer.push(balanceBuffer);
var receivedBuffer = new Buffer(8);
receivedBuffer.writeBigUInt64BE(BigInt(received));
buffer.push(receivedBuffer);
var sentBuffer = new Buffer(8);
sentBuffer.writeBigUInt64BE(BigInt(sent));
buffer.push(sentBuffer);
var txApperancesBuffer = new Buffer(4);
txApperancesBuffer.writeUInt32BE(txApperances);
buffer.push(txApperancesBuffer);
var txidBuffer = new Buffer(lastTx, 'hex');
buffer.push(txidBuffer);
var blkBuffer = new Buffer(lastBlock, 'hex');
buffer.push(blkBuffer);
return Buffer.concat(buffer);
}
Encoding.prototype.decodeAddressCacheValue = function(buffer) {
var balance = parseInt(buffer.readBigUInt64BE(0));
var received = parseInt(buffer.readBigUInt64BE(8));
var sent = parseInt(buffer.readBigUInt64BE(16));
var txApperances = buffer.readUInt32BE(24);
var lastTx = buffer.slice(28, 60).toString('hex'); //28 + 32 (tx hash buffer length) = 60
var lastBlock = buffer.slice(60).toString('hex');
return { lastTx, lastBlock, balance, received, sent, txApperances };
}
module.exports = Encoding;

View File

@ -16,7 +16,9 @@ var utils = require('../../utils');
var LRU = require('lru-cache');
var XXHash = require('xxhash');
const MAX_TX_QUERY_LIMIT_HISTORY = 1000;
const MAX_TX_QUERY_LIMIT_UTXO = 1000;
const MAX_TX_QUERY_LIMIT_SUMMARY = 500;
// See rationale about this cache at function getTxList(next)
const TXID_LIST_CACHE_ITEMS = 250; // nr of items (this translates to: consecutive
@ -67,8 +69,8 @@ AddressService.dependencies = [
// then I would pass back [tx1, tx2] in that order
//
// Instead of passing addresses, with from>0, options.cacheKey can be used to define the address set.
//
AddressService.prototype.getAddressHistory = function(addresses, options, callback) {
//(old one: non-optimized for large data)
AddressService.prototype.__getAddressHistory = function(addresses, options, callback) {
var self = this;
var cacheUsed = false;
@ -173,8 +175,106 @@ AddressService.prototype.getAddressHistory = function(addresses, options, callba
};
AddressService.prototype.getAddressHistory = function(addresses, options, streamer, callback) {
var self = this;
options = options || {};
//options.from = options.from || 0; //Deprecated, use `after` and `before` option
//options.to = options.to || 0xffffffff; //Deprecated, use `after` and `before` option
if(!_.isFunction(callback)){ //if only 3 args, then streamer is callback
callback = streamer;
streamer = () => null; //NULL fn
}
if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true;
}
if (_.isUndefined(options.mempoolOnly)) {
options.mempoolOnly = false;
}
if(_.isUndefined(options.reverse)) {
options.reverse = false;
}
var old_support = false;
//Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use)
if(!_.isUndefined(options.from) || !_.isUndefined(options.to)) {
old_support = true;
options.from = options.from || 0;
options.to = options.to || 0xffffffff; //Max value of to will actually be MAX_TX_QUERY_LIMIT_HISTORY
}
if (_.isString(addresses)) {
addresses = [addresses];
}
var results = {
totalCount: 0,
items: [],
}
async.eachLimit(addresses, 4, function(address, next) {
var addr_options = Object.assign({}, options), addr_count = 0;
self._streamAddressSummary(address, addr_options, function(err, tx){
if(err)
return log.error(err);
addr_count++;
if(!results.items.some(x => x.txid() === tx.txid())) {//add only if tx not already in array
if(!options.reverse)
results.items.unshift(tx); //using unshift, so that recent tx (low) are at front
else
results.items.push(tx);
}
if(results.items.length > MAX_TX_QUERY_LIMIT_HISTORY) { //remove items from array when overflown
results.items.sort((a, b) => (b.__height || 0xffffffff) - (a.__height || 0xffffffff) || b.txid().localeCompare(a.txid()));
let del_count = results.items.length - MAX_TX_QUERY_LIMIT_HISTORY;
let start_index = (old_support || options.reverse) ? MAX_TX_QUERY_LIMIT_HISTORY : 0;
results.items.splice(start_index, del_count);
results.incomplete = true;
if(!old_support && addr_count >= MAX_TX_QUERY_LIMIT_HISTORY)
addr_options.flag_stop = true; //limit has reached, stop quering db for more tx
}
streamer(null, tx);
}, next);
}, function(err) {
if (err) {
return callback(err);
}
//sort items in desc block-height, then asc txid (if same height)
results.items.sort((a, b) => (b.__height || 0xffffffff) - (a.__height || 0xffffffff) || b.txid().localeCompare(a.txid()));
results.totalCount = results.items.length ;
//Quick support for `from` and `to` options (DEPRECATED! Not recommeded to use)
if(old_support) {
results.items = results.items.slice(options.from, options.to);
}
callback(null, results);
})
}
// this is basically the same as _getAddressHistory apart from the summary
AddressService.prototype.getAddressSummary = function(address, options, callback) {
//(old one: non-optimized for large data)
AddressService.prototype.__getAddressSummary = function(address, options, callback) {
var self = this;
@ -200,7 +300,7 @@ AddressService.prototype.getAddressSummary = function(address, options, callback
txApperances: 0,
};
self.getAddressHistory(address, options, function(err, results) {
self.__getAddressHistory(address, options, function(err, results) { //old fn
if (err) {
return callback(err);
@ -218,6 +318,229 @@ AddressService.prototype.getAddressSummary = function(address, options, callback
};
AddressService.prototype.getAddressSummary = function(address, options, streamer, callback) {
var self = this;
options = options || {};
//options.from = options.from || 0; //Deprecated, use `after` and `before` option
//options.to = options.to || 0xffffffff; //Deprecated, use `after` and `before` option
if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true;
}
if(!_.isFunction(callback)){ //if only 3 args, then streamer is callback
callback = streamer;
streamer = () => null; //NULL fn
}
var count = 0;
var result = {
addrStr: address,
balance: 0,
balanceSat: 0,
totalReceived: 0,
totalReceivedSat: 0,
totalSent: 0,
totalSentSat: 0,
unconfirmedBalance: 0,
unconfirmedBalanceSat: 0,
unconfirmedTxApperances: 0,
txApperances: 0,
};
var useCache = _.isUndefined(options.after) && _.isUndefined(options.before);
var lastTx, lastBlock;
self._loadCache(address, result, useCache, function(err, lastCachedTx) {
if(err)
log.error(err);
if(!_.isUndefined(lastCachedTx))
options.after = lastCachedTx;
self._streamAddressSummary(address, options, function(err, tx) {
if(err)
return log.error(err);
if(tx) {
count++;
self._aggregateAddressSummaryResult(tx, address, result, options);
if(tx.confirmations) {
lastTx = tx.txid();
lastBlock = tx.blockhash;
}
}
if(count >= MAX_TX_QUERY_LIMIT_SUMMARY) {//stop quering db when limit reached
options.flag_stop = true;
result.incomplete = true;
}
streamer(null, tx);
}, function(err) {
if (err) {
return callback(err);
}
result.balanceSat = parseInt(result.balanceSat.toFixed());
result.totalReceivedSat = parseInt(result.totalReceivedSat.toFixed());
result.totalSentSat = parseInt(result.totalSentSat.toFixed());
result.txApperances = parseInt(result.txApperances.toFixed());
result.unconfirmedBalanceSat = parseInt(result.unconfirmedBalanceSat.toFixed());
result.unconfirmedTxApperances = parseInt(result.unconfirmedTxApperances.toFixed());
result.balance = Unit.fromSatoshis(result.balanceSat).toBTC();
result.totalReceived = Unit.fromSatoshis(result.totalReceivedSat).toBTC();
result.totalSent = Unit.fromSatoshis(result.totalSentSat).toBTC();
result.unconfirmedBalance = Unit.fromSatoshis(result.unconfirmedBalanceSat).toBTC();
result.lastItem = lastTx;
callback(null, result);
//store in cache if needed
if(useCache) {
if(result.incomplete) //full summary needs to be calculated in background
self._cacheSummaryInBackground(address, lastTx, lastBlock, result);
else if (!_.isUndefined(lastCachedTx) && !_.isUndefined(lastTx)
&& lastTx != lastCachedTx && !self._cacheInstance.has(address)) //update cache if needed
self._storeCache(address, lastTx, lastBlock, result);
}
});
})
}
AddressService.prototype._cacheInstance = new Set();
AddressService.prototype._cacheSummaryInBackground = function(address, lastTx, lastBlock, result){
const self = this;
if(self._cacheInstance.has(address))
return;
self._cacheInstance.add(address);
const cache = {
balanceSat: result.balanceSat,
totalReceivedSat: result.totalReceivedSat,
totalSentSat: result.totalSentSat,
txApperances: result.txApperances,
unconfirmedBalanceSat: 0, //unconfirmed (mempool) values should not be cached
unconfirmedTxApperances: 0
};
const options = { queryMempool: false, after: lastTx, noTxList: true };
self._streamAddressSummary(address, options, function(err, tx) {
if(err)
return log.error(err);
if(tx) {
self._aggregateAddressSummaryResult(tx, address, cache, options);
if(tx.confirmations){
lastTx = tx.txid();
lastBlock = tx.blockhash;
}
}
}, function(err) {
if (err)
return log.error(err);
cache.balanceSat = parseInt(cache.balanceSat.toFixed());
cache.totalReceivedSat = parseInt(cache.totalReceivedSat.toFixed());
cache.totalSentSat = parseInt(cache.totalSentSat.toFixed());
cache.txApperances = parseInt(cache.txApperances.toFixed());
if(!_.isUndefined(lastTx))
self._storeCache(address, lastTx, lastBlock, cache);
self._cacheInstance.delete(address); //remove from running instance
});
}
AddressService.prototype._storeCache = function(address, lastCacheTx, lastCacheBlock, result, callback) {
const self = this;
var key = self._encoding.encodeAddressCacheKey(address);
var value = self._encoding.encodeAddressCacheValue(lastCacheTx, lastCacheBlock, result.balanceSat, result.totalReceivedSat, result.totalSentSat, result.txApperances)
if(!_.isFunction(callback)) //if callback is not passed, call a empty function
callback = () => null;
self._db.put(key, value, callback);
}
AddressService.prototype._loadCache = function(address, result, useCache, callback) {
const self = this;
if(!useCache) //skip if useCache is false (cases like 'after' and/or 'before' parameter is used by client)
return callback();
var key = self._encoding.encodeAddressCacheKey(address);
self._db.get(key, function(err, value) {
if (err) {
return callback(err);
}
if (!value) {
return callback();
}
var addressCache = self._encoding.decodeAddressCacheValue(value);
var lastCacheTx = addressCache.lastTx, lastCacheBlock = addressCache.lastBlock
self._block.getBlock(lastCacheBlock, function(err, block) {
if(err) {
return callback(err);
}
if (!block) { //block not found, probably removed in reorg.
//delete the existing cache and recalc values freshly
self._deleteCache(address, function() {
callback();
});
} else {
//values are in satoshis
result.balanceSat = addressCache.balance;
result.totalReceivedSat = addressCache.received;
result.totalSentSat = addressCache.sent;
result.txApperances = addressCache.txApperances;
callback(null, lastCacheTx);
}
})
});
}
AddressService.prototype._deleteCache = function(address, callback) {
const self = this;
var key = self._encoding.encodeAddressCacheKey(address);
if(!_.isFunction(callback)) //if callback is not passed, call a empty function
callback = () => null;
self._db.del(key, callback);
}
AddressService.prototype._setOutputResults = function(tx, address, result) {
for(var j = 0; j < tx.outputs.length; j++) {
@ -265,7 +588,6 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul
var self = this;
for(var i = 0; i < txs.length; i++) {
var tx = txs[i];
self._setOutputResults(tx, address, result);
@ -283,6 +605,110 @@ AddressService.prototype._getAddressSummaryResult = function(txs, address, resul
return result;
};
AddressService.prototype._getOccurrenceCount = function(tx, address) {
let count = 0;
for(var i = 0; i < tx.inputs.length; i++) {
var input = tx.inputs[i];
if(utils.getAddress(input, this._network) === address)
count++;
}
for(var j = 0; j < tx.outputs.length; j++) {
var output = tx.outputs[j];
if(utils.getAddress(output, this._network) === address)
count++;
}
return count;
}
AddressService.prototype._getOutputResults = function(tx, address) {
let value = 0;
for(var j = 0; j < tx.outputs.length; j++) {
var output = tx.outputs[j];
if (utils.getAddress(output, this._network) === address)
value += output.value;
}
return value;
};
AddressService.prototype._getInputResults = function(tx, address) {
let value = 0;
for(var i = 0; i < tx.inputs.length; i++) {
var input = tx.inputs[i];
if (utils.getAddress(input, this._network) === address)
value += tx.__inputValues[i];
}
return value;
};
AddressService.prototype._aggregateAddressSummaryResult = function (tx, address, result, options) {
var self = this;
let output_val = self._getOutputResults(tx, address);
let input_val = self._getInputResults(tx, address);
//aggregate the result
if(tx.confirmations) {
result.txApperances++;
result.totalReceivedSat += output_val;
result.balanceSat += output_val;
result.totalSentSat += input_val;
result.balanceSat -= input_val;
} else {
result.unconfirmedTxApperances++;
result.unconfirmedBalanceSat += output_val;
result.unconfirmedBalanceSat -= input_val;
}
if (!options.noTxList) {
if (!result.transactions) {
result.transactions = [];
}
let txid = tx.txid();
if(!result.transactions.includes(txid)) { //push txid only if its not in the array
result.transactions.unshift(txid); //using unshift, so that recent tx (low confirmation) are at front
if(result.transactions.length > MAX_TX_QUERY_LIMIT_SUMMARY)
result.transactions.pop(); //pop the oldest tx in list (when list limit is maxed out)
}
}
}
AddressService.prototype.getAddressUnspentOutputs = function(address, options, callback) {
var self = this;
@ -298,7 +724,7 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
var results = [];
var start = self._encoding.encodeUtxoIndexKey(address);
var final = Buffer.from(new Array(73).join('f'), 'hex');
var final = new Buffer(new Array(73).join('f'), 'hex');
var end = Buffer.concat([ start.slice(0, -36), final ]);
var criteria = {
@ -365,6 +791,11 @@ AddressService.prototype.getAddressUnspentOutputs = function(address, options, c
utxoStream.on('data', function(data) {
if(results.length >= MAX_TX_QUERY_LIMIT_UTXO) { //Max array limit reached, end response
utxoStream.emit('end');
return;
}
var key = self._encoding.decodeUtxoIndexKey(data.key);
var value = self._encoding.decodeUtxoIndexValue(data.value);
@ -440,19 +871,21 @@ AddressService.prototype.stop = function(callback) {
AddressService.prototype._getTxidStream = function(address, options) {
var start = this._encoding.encodeAddressIndexKey(address);
var end = Buffer.concat([
start.slice(0, address.length + 4),
options.endHeightBuf,
Buffer.from(new Array(83).join('f'), 'hex')
]);
var criteria = {};
var criteria = {
gte: start,
lte: end,
reverse: true // txids stream from low confirmations to high confirmations
};
if(options.after)
criteria.gt = this._encoding.encodeAddressIndexKey(address, options.start, options.after, 0xffffffff, 1, 0xffffffff); //0xffffffff is for getting after the txid
else
criteria.gte = this._encoding.encodeAddressIndexKey(address, options.start);
if(options.before)
criteria.lt = this._encoding.encodeAddressIndexKey(address, options.end, options.before); //get before the txid
else
criteria.lte = this._encoding.encodeAddressIndexKey(address, options.end, Array(65).join('f'), 0xffffffff, 1, 0xffffffff);
//reverse option can be used explictly when latest tx are required
if(options.reverse)
criteria.reverse = true;
// txid stream
var txidStream = this._db.createKeyStream(criteria);
@ -463,6 +896,7 @@ AddressService.prototype._getTxidStream = function(address, options) {
return txidStream;
};
//(used by old fn)
AddressService.prototype._getAddressTxHistory = function(options, callback) {
var self = this;
@ -491,6 +925,7 @@ AddressService.prototype._getAddressTxHistory = function(options, callback) {
};
//(used by old fn)
AddressService.prototype._getAddressTxidHistory = function(address, options, callback) {
var self = this;
@ -500,9 +935,6 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
var results = [];
options.endHeightBuf = Buffer.alloc(4);
options.endHeightBuf.writeUInt32BE(options.end);
if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true;
}
@ -551,7 +983,15 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
txIdTransformStream._transform = function(chunk, enc, callback) {
var txInfo = self._encoding.decodeAddressIndexKey(chunk);
if(results.length >= MAX_TX_QUERY_LIMIT_HISTORY) { //Max array limit reached, end response
txIdTransformStream.emit('end');
return;
}
if(!results.some(r => r.txid == txInfo.txid)) //add txid to array only if its not already there
results.push({ txid: txInfo.txid, height: txInfo.height });
callback();
};
@ -563,6 +1003,225 @@ AddressService.prototype._getAddressTxidHistory = function(address, options, cal
};
AddressService.prototype._streamAddressSummary = function(address, options, streamer, callback) {
var self = this;
options = options || {};
options.start = options.start || 0;
options.end = options.end || 0xffffffff;
//options.from = options.from || 0; //Deprecated, use `after` and `before` option
//options.to = options.to || 0xffffffff; //Deprecated, use `after` and `before` option
if (_.isUndefined(options.queryMempool)) {
options.queryMempool = true;
}
if (_.isUndefined(options.mempoolOnly)) {
options.mempoolOnly = false;
}
if (_.isUndefined(options.reverse)) {
options.reverse = false;
}
//declare the queue to process tx data
var tmpTxList = {}; //store processed txid temporarily to ignore duplication
var q = async.queue(function(id, cb) {
//duplication finding
if(id.txid in tmpTxList){
tmpTxList[id.txid][0]++;
if(tmpTxList[id.txid][1] !== null && tmpTxList[id.txid][0] >= tmpTxList[id.txid][1]) //all duplications are found for this txid
delete tmpTxList[id.txid];
return cb();
} else tmpTxList[id.txid] = [1, null];
if (id.height === 0xffffffff) {
return self._mempool.getMempoolTransaction(id.txid, function(err, tx) {
if (err || !tx) {
return cb(err || new Error('Address Service: could not find tx: ' + id.txid));
}
self._transaction.setTxMetaInfo(tx, options, cb);
});
}
self._transaction.getDetailedTransaction(id.txid, options, cb);
}, 1);
//q.pause(); //pause and wait until queue is set (not needed)
function chunkCallback(err, tx){
if(q.killed || (!err && !tx)) //no error or tx data (duplicate calls will have empty tx value)
return;
if(tx){
let txid = tx.txid();
tmpTxList[txid][1] = self._getOccurrenceCount(tx, address);
if(tmpTxList[txid][0] >= tmpTxList[txid][1]) //all duplications are found for this txid
delete tmpTxList[txid];
}
streamer(err, tx);
if((err || options.flag_stop) && !q.killed){
q.kill();
q.killed = true;
return callback();
}
}
const waterfall_array = [];
waterfall_array.push(
//Find start height if `after` option is passed
function parse_after_id(next){
if(_.isUndefined(options.after)) {
return next();
}
self._transaction.getTransaction(options.after, options, function(err, tx) {
if(tx && tx.confirmations && tx.height >= options.start) {
options.start = tx.height;
} else {
delete options.after;
}
next();
});
});
waterfall_array.push(
//Find end height if `before` option is passed
function parse_before_id(next){
if(_.isUndefined(options.before)) {
return next();
}
self._transaction.getTransaction(options.before, options, function(err, tx) {
if(tx && tx.confirmations && tx.height <= options.end) {
options.end = tx.height;
} else {
delete options.before;
}
next();
});
});
// stream the confirmed txids out of the address index
function query_confirmed_txids(next) {
if (options.mempoolOnly) { //Option to query from mempool only (ie, unconfirmed txs only)
return next();
}
var txIdTransformStream = new Transform({ objectMode: true });
txIdTransformStream._flush = function(cb) {
txIdTransformStream.emit('end');
cb();
};
txIdTransformStream.on('error', function(err) {
log.error('Address Service: txstream err: ' + err);
txIdTransformStream.unpipe();
});
txIdTransformStream.on('end', function() {
next();
});
txIdTransformStream._transform = function(chunk, enc, cb) {
if(options.flag_stop)//stop data query
return txIdTransformStream.unpipe();
var txInfo = self._encoding.decodeAddressIndexKey(chunk);
q.push({ txid: txInfo.txid, height: txInfo.height }, chunkCallback);
cb();
};
var txidStream = self._getTxidStream(address, options);
txidStream.pipe(txIdTransformStream);
}
// query the mempool for relevant txs for this address
function query_mempool_txids(next) {
if (!options.queryMempool || !_.isUndefined(options.before)) { //if queryMempool=false or options.before is given a valid value, then do not query mempool
return next();
}
self._mempool.getTxidsByAddress(address, 'both', function(err, mempoolTxids) {
if (mempoolTxids.length <= 0) {
return next();
}
mempoolTxids.map(id => q.push(id, chunkCallback));
next();
});
}
if(options.reverse){ //when queried txs in reverse key order, mempool first then confirmed
waterfall_array.push(query_mempool_txids);
waterfall_array.push(query_confirmed_txids);
} else { //when queried tx in key order, confirmed tx 1st, then mempool
waterfall_array.push(query_confirmed_txids);
waterfall_array.push(query_mempool_txids);
}
waterfall_array.push(
//wait for queue to complete
function end_fall(next) {
if(!q.started || q.idle()) //No tx in query (or) already finished querying
return next();
else
q.drain = () => next();
});
async.waterfall(waterfall_array, callback);
}
AddressService.prototype._removeBlock = function(block, callback) {
var self = this;

View File

@ -242,7 +242,7 @@ ProcessSerial.prototype._write = function(block, enc, callback) {
self.block.once('concurrentaddblock', function() {
if(!check()) {
var err = new Error('Concurrent block ' + self.block.concurrentTip.__height + ' is less than ' + block.__height);
var err = 'Concurrent block ' + self.block.concurrentTip.__height + ' is less than ' + block.__height;
return self.emit('error', err);
}
self._process(block, callback);

View File

@ -10,7 +10,7 @@ function Encoding(servicePrefix) {
// ---- hash --> rawblock
Encoding.prototype.encodeBlockKey = function(hash) {
return Buffer.concat([ this._servicePrefix, Buffer.from(hash, 'hex') ]);
return Buffer.concat([ this._servicePrefix, new Buffer(hash, 'hex') ]);
};
Encoding.prototype.decodeBlockKey = function(buffer) {

View File

@ -13,6 +13,8 @@ var bcoin = require('fcoin');
var _ = require('lodash');
var LRU = require('lru-cache');
const MAX_IGNORED_BLOCK = 16; //Maximum ignored block allowed before trigging sync again
var BlockService = function(options) {
BaseService.call(this, options);
@ -231,7 +233,7 @@ BlockService.prototype._resetTip = function(callback) {
self._header.getAllHeaders(function(err, headers) {
if (err || !headers) {
return callback(err || new Error('headers required'));
log.error(err || 'headers required'); return callback(err);
}
log.info('Block Service: retrieved all the headers for lookups.');
@ -258,7 +260,12 @@ BlockService.prototype._resetTip = function(callback) {
block = _block;
header = headers.getIndex(--height);
assert(header, 'Header not found for reset.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (header == false) {
log.error('Header not found for reset.');
}
// assert(header, 'Header not found for reset.');
if (!block) {
log.debug('Block Service: trying block: ' + header.hash);
@ -271,8 +278,8 @@ BlockService.prototype._resetTip = function(callback) {
}, function(err) {
if (err || !block) {
return callback(err ||
new Error('Block Service: none of the blocks from the headers match what is already indexed in the block service.'));
log.error(err ||
'Block Service: none of the blocks from the headers match what is already indexed in the block service.'); return callback(err);
}
self._setTip({ hash: block.rhash(), height: height + 1 }, callback);
@ -370,8 +377,8 @@ BlockService.prototype._loadRecentBlockHashes = function(callback) {
self.getBlock(hash, function(err, block) {
if (err || !block) {
return callback(err || new Error('Block Service: attempted to retrieve block: ' + hash +
' but was not in the index.'));
log.error(err || 'Block Service: attempted to retrieve block: ' + hash +
' but was not in the index.'); return callback(err);
}
var prevHash = bcoin.util.revHex(block.prevBlock);
@ -387,7 +394,11 @@ BlockService.prototype._loadRecentBlockHashes = function(callback) {
return callback(err);
}
assert(self._recentBlockHashes.length === times, 'Block Service: did not load enough recent block hashes from the index.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (self._recentBlockHashes.length != times) {
log.error('Block Service: did not load enough recent block hashes from the index.');
}
//assert(self._recentBlockHashes.length === times, 'Block Service: did not load enough recent block hashes from the index.');
log.info('Block Service: loaded: ' + self._recentBlockHashes.length + ' hashes from the index.');
callback();
@ -406,7 +417,7 @@ BlockService.prototype._getTimeSinceLastBlock = function(callback) {
self._header.getBlockHeader(Math.max(self._tip.height - 1, 0), function(err, header) {
if(err || !header) {
return callback(err || new Error('Block Service: we should have a header in order to get time since last block.'));
log.error(err || 'Block Service: we should have a header in order to get time since last block.'); return callback(err);
}
async.map([ self._tip.hash, header.hash ], function(hash, next) {
@ -628,6 +639,7 @@ BlockService.prototype._startBlockSubscription = function() {
}
this._subscribedBlock = true;
this._ignoredBlockCount = 0; //SZ: reset the ignored count to 0 when subscription starts
log.info('Block Service: starting p2p block subscription.');
this._bus.on('p2p/block', this._queueBlock.bind(this));
@ -656,7 +668,7 @@ BlockService.prototype._findLatestValidBlockHeader = function(callback) {
if (self._reorgToBlock) {
return self._header.getBlockHeader(self._reorgToBlock, function(err, header) {
if (err || !header) {
return callback(err || new Error('Block Service: header not found to reorg to.'));
log.error(err || 'Block Service: header not found to reorg to.'); return callback(err);
}
callback(null, header);
});
@ -711,12 +723,19 @@ BlockService.prototype._findLatestValidBlockHeader = function(callback) {
// any of our recent block hashes in its indexes.
// if some joker mines a block using an orphan block as its prev block, then the effect of this will be
// us detecting a reorg, but not actually reorging anything
assert(header, 'Block Service: we could not locate any of our recent block hashes in the header service ' +
'index. Perhaps our header service sync\'ed to the wrong chain?');
assert(header.height <= self._tip.height, 'Block Service: we found a common ancestor header whose ' +
'height was greater than our current tip. This should be impossible.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (!_.isUndefined(header)) {
if (header == false) {
log.error('Block Service: we could not locate any of our recent block hashes in the header service ' + 'index. Perhaps our header service sync\'ed to the wrong chain?');
}
}
// assert(header, 'Block Service: we could not locate any of our recent block hashes in the header service ' + 'index. Perhaps our header service sync\'ed to the wrong chain?');
if (!_.isUndefined(header.height)) {
if (header.height > self._tip.height) {
log.error('Block Service: we found a common ancestor header whose ' + 'height was greater than our current tip. This should be impossible.');
}
// assert(header.height <= self._tip.height, 'Block Service: we found a common ancestor header whose ' + 'height was greater than our current tip. This should be impossible.');
}
callback(null, header);
});
@ -739,13 +758,13 @@ BlockService.prototype._findBlocksToRemove = function(commonHeader, callback) {
self._getBlock(hash, function(err, block) {
if (err || !block) {
return next(err || new Error('Block Service: block not found in index.'));
return next(err || 'Block Service: block not found in index.');
}
self._timestamp.getTimestamp(block.rhash(), function(err, timestamp) {
if (err || !timestamp) {
return callback(err || new Error('timestamp missing from reorg.'));
log.error(err || 'timestamp missing from reorg.'); return callback(err);
}
block.__height = height;
@ -815,8 +834,10 @@ BlockService.prototype._handleReorg = function(callback) {
blocksToRemove = _blocksToRemove;
assert(blocksToRemove.length >= 1 && blocksToRemove.length <= self._recentBlockHashes.length,
'Block Service: the number of blocks to remove looks to be incorrect.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (blocksToRemove.length < 1 || blocksToRemove.length > self._recentBlockHashes.length) {
log.error('Block Service: the number of blocks to remove looks to be incorrect.'); }
// assert(blocksToRemove.length >= 1 && blocksToRemove.length <= self._recentBlockHashes.length, 'Block Service: the number of blocks to remove looks to be incorrect.');
log.warn('Block Service: chain reorganization detected, current height/hash: ' + self._tip.height + '/' +
self._tip.hash + ' common ancestor hash: ' + commonAncestorHeader.hash + ' at height: ' + commonAncestorHeader.height +
@ -920,6 +941,15 @@ BlockService.prototype._processBlock = function(block, callback) {
return self._saveBlock(block, callback);
}
//SZ: count the ignored blocks. if many blocks ignored, trigger sync process
if(self._ignoredBlockCount < MAX_IGNORED_BLOCK)
self._ignoredBlockCount++;
else {
self._ignoredBlockCount = 0;
self._removeAllSubscriptions();
self._startSync();
}
// reorg -- in this case, we will not handle the reorg right away
// instead, we will skip the block and wait for the eventual call to
// "onHeaders" function. When the header service calls this function,
@ -935,6 +965,7 @@ BlockService.prototype._saveBlock = function(block, callback) {
var self = this;
block.__height = self._tip.height + 1;
self._ignoredBlockCount = 0; //SZ: a block is saved, reset the ignored count
var services = self.node.services;
@ -1079,7 +1110,7 @@ BlockService.prototype._startSync = function() {
this.on('next block', this._sync.bind(this));
this.on('synced', this._onSynced.bind(this));
clearInterval(this._reportInterval);
this._reportingInterval = setInterval(this._logProgress.bind(this), 5000);
this._reportInterval = setInterval(this._logProgress.bind(this), 5000);
return this._sync();
}

View File

@ -315,7 +315,7 @@ Reorg.prototype.findCommonAncestorAndNewHashes = function(oldTipHash, newTipHash
}
if(!mainPosition && !forkPosition) {
return next(new Error('Unknown common ancestor'));
return next('Unknown common ancestor');
}
next();

View File

@ -3,6 +3,7 @@
var util = require('util');
var fs = require('fs');
var async = require('async');
var _ = require('lodash');
var levelup = require('levelup');
var leveldown = require('leveldown');
var mkdirp = require('mkdirp');
@ -50,7 +51,6 @@ DB.prototype._onError = function(err) {
if (!this._stopping) {
log.error('Db Service: error: ' + err);
//FLO Crash Error Resolution by RanchiMall 10th May 2021
//return this.node.stop();
//this.node.stop();
}
};
@ -69,9 +69,9 @@ DB.prototype._setDataPath = function() {
};
DB.prototype._setVersion = function(callback) {
var versionBuffer = Buffer.from(new Array(4));
var versionBuffer = new Buffer(new Array(4));
versionBuffer.writeUInt32BE(this.version);
this.put(Buffer.concat([ this._dbPrefix, Buffer.from('version', 'utf8') ]), versionBuffer, callback);
this.put(Buffer.concat([ this._dbPrefix, new Buffer('version', 'utf8') ]), versionBuffer, callback);
};
DB.prototype.start = function(callback) {
@ -96,7 +96,7 @@ DB.prototype.get = function(key, options, callback) {
var cb = callback;
var opts = options;
if (typeof callback !== 'function') {
if (!_.isFunction(callback)) {
cb = options;
opts = {};
}
@ -119,7 +119,9 @@ DB.prototype.get = function(key, options, callback) {
} else {
cb(new Error('Shutdown sequence underway, not able to complete the query'));
// FLOSight Error Correction from RanchiMall 20th May 2021.
//cb(new Error('Shutdown sequence underway, not able to complete the query'));
log.error('Shutdown sequence underway, not able to complete the query');
}
};
@ -130,17 +132,40 @@ DB.prototype.put = function(key, value, callback) {
callback();
}
assert(Buffer.isBuffer(key), 'key NOT a buffer as expected.');
// FLOSight Error Correction from RanchiMall 20th May 2021. removed the unhandled assert and replaced by looging of error
if (Buffer.isBuffer(key) == false) {
log.error('key NOT a buffer as expected.');
}
// assert(Buffer.isBuffer(key), 'key NOT a buffer as expected.');
if (value) {
assert(Buffer.isBuffer(value), 'value exists but NOT a buffer as expected.');
// FLOSight Error Correction from RanchiMall 20th May 2021. removed the unhandled assert and replaced by looging of error
if (Buffer.isBuffer(value) == false) {
log.error('value exists but NOT a buffer as expected.');
}
//assert(Buffer.isBuffer(value), 'value exists but NOT a buffer as expected.');
}
this._store.put(key, value, callback);
};
DB.prototype.del = function(key, callback) {
if (this._stopping) {
callback();
}
// FLOSight Error Correction from RanchiMall 20th May 2021. removed the unhandled assert and replaced by looging of error
if (Buffer.isBuffer(key) == false) {
log.error('key NOT a buffer as expected.');
}
// assert(Buffer.isBuffer(key), 'key NOT a buffer as expected.');
this._store.del(key, callback);
}
DB.prototype.batch = function(ops, callback) {
if (this._stopping) {
@ -149,11 +174,18 @@ DB.prototype.batch = function(ops, callback) {
for(var i = 0; i < ops.length; i++) {
assert(Buffer.isBuffer(ops[i].key), 'key NOT a buffer as expected.');
// FLOSight Error Correction from RanchiMall 20th May 2021. removed the unhandled assert and replaced by looging of error
if (Buffer.isBuffer(ops[i].key) == false) {
log.error('key NOT a buffer as expected.');
}
//assert(Buffer.isBuffer(ops[i].key), 'key NOT a buffer as expected.');
if (ops[i].value) {
assert(Buffer.isBuffer(ops[i].value), 'value exists but NOT a buffer as expected.');
// FLOSight Error Correction from RanchiMall 20th May 2021. removed the unhandled assert and replaced by looging of error
if (Buffer.isBuffer(ops[i].value) == false) {
log.error('value exists but NOT a buffer as expected.');
}
//assert(Buffer.isBuffer(ops[i].value), 'value exists but NOT a buffer as expected.');
}
}
@ -207,7 +239,7 @@ DB.prototype.getPublishEvents = function() {
DB.prototype.getServiceTip = function(serviceName, callback) {
var keyBuf = Buffer.concat([ this._dbPrefix, Buffer.from('tip-' + serviceName, 'utf8') ]);
var keyBuf = Buffer.concat([ this._dbPrefix, new Buffer('tip-' + serviceName, 'utf8') ]);
var self = this;
self.get(keyBuf, function(err, tipBuf) {
@ -242,8 +274,8 @@ DB.prototype.getServiceTip = function(serviceName, callback) {
DB.prototype.getPrefix = function(service, callback) {
var self = this;
var keyBuf = Buffer.concat([ self._dbPrefix, Buffer.from('prefix-', 'utf8'), Buffer.from(service, 'utf8') ]);
var unusedBuf = Buffer.concat([ self._dbPrefix, Buffer.from('nextUnused', 'utf8') ]);
var keyBuf = Buffer.concat([ self._dbPrefix, new Buffer('prefix-', 'utf8'), new Buffer(service, 'utf8') ]);
var unusedBuf = Buffer.concat([ self._dbPrefix, new Buffer('nextUnused', 'utf8') ]);
function getPrefix(next) {
@ -270,7 +302,7 @@ DB.prototype.getPrefix = function(service, callback) {
}
if(!buffer) {
return next(null, Buffer.from('0001', 'hex'));
return next(null, new Buffer('0001', 'hex'));
}
next(null, buffer);
@ -294,7 +326,7 @@ DB.prototype.getPrefix = function(service, callback) {
function putUnused(buffer, next) {
var prefix = buffer.readUInt16BE();
var nextUnused = Buffer.alloc(2);
var nextUnused = new Buffer(2);
nextUnused.writeUInt16BE(prefix + 1);
self.put(unusedBuf, nextUnused, function(err) {

View File

@ -3,13 +3,13 @@
function Encoding(servicePrefix) {
this._servicePrefix = servicePrefix;
this._hashPrefix = Buffer.from('00', 'hex');
this._heightPrefix = Buffer.from('01', 'hex');
this._hashPrefix = new Buffer('00', 'hex');
this._heightPrefix = new Buffer('01', 'hex');
}
// ---- hash --> header
Encoding.prototype.encodeHeaderHashKey = function(hash) {
var hashBuf = Buffer.from(hash, 'hex');
var hashBuf = new Buffer(hash, 'hex');
return Buffer.concat([ this._servicePrefix, this._hashPrefix, hashBuf ]);
};
@ -19,7 +19,7 @@ Encoding.prototype.decodeHeaderHashKey = function(buffer) {
// ---- height --> header
Encoding.prototype.encodeHeaderHeightKey = function(height) {
var heightBuf = Buffer.alloc(4);
var heightBuf = new Buffer(4);
heightBuf.writeUInt32BE(height);
return Buffer.concat([ this._servicePrefix, this._heightPrefix, heightBuf ]);
};
@ -29,21 +29,21 @@ Encoding.prototype.decodeHeaderHeightKey = function(buffer) {
};
Encoding.prototype.encodeHeaderValue = function(header) {
var hashBuf = Buffer.from(header.hash, 'hex');
var versionBuf = Buffer.alloc(4);
var hashBuf = new Buffer(header.hash, 'hex');
var versionBuf = new Buffer(4);
versionBuf.writeInt32BE(header.version);
var prevHash = Buffer.from(header.prevHash, 'hex');
var merkleRoot = Buffer.from(header.merkleRoot, 'hex');
var tsBuf = Buffer.alloc(4);
var prevHash = new Buffer(header.prevHash, 'hex');
var merkleRoot = new Buffer(header.merkleRoot, 'hex');
var tsBuf = new Buffer(4);
tsBuf.writeUInt32BE(header.timestamp || header.time);
var bitsBuf = Buffer.alloc(4);
var bitsBuf = new Buffer(4);
bitsBuf.writeUInt32BE(header.bits);
var nonceBuf = Buffer.alloc(4);
var nonceBuf = new Buffer(4);
nonceBuf.writeUInt32BE(header.nonce);
var heightBuf = Buffer.alloc(4);
var heightBuf = new Buffer(4);
heightBuf.writeUInt32BE(header.height);
var chainworkBuf = Buffer.from(header.chainwork, 'hex');
var nextHash = Buffer.from(header.nextHash || new Array(65).join('0'), 'hex');
var chainworkBuf = new Buffer(header.chainwork, 'hex');
var nextHash = new Buffer(header.nextHash || new Array(65).join('0'), 'hex');
return Buffer.concat([
hashBuf,
versionBuf,

View File

@ -14,6 +14,8 @@ var assert = require('assert');
var constants = require('../../constants');
var bcoin = require('fcoin');
const SYNC_CHECK_INTERVAL = 1000 * 60 * 15; //15 mins
var HeaderService = function(options) {
BaseService.call(this, options);
@ -145,7 +147,11 @@ HeaderService.prototype._adjustTipBackToCheckpoint = function() {
HeaderService.prototype._setGenesisBlock = function(callback) {
assert(this._tip.hash === this.GENESIS_HASH, 'Expected tip hash to be genesis hash, but it was not.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (this._tip.hash != this.GENESIS_HASH) {
log.error('Expected tip hash to be genesis hash, but it was not.');
}
// assert(this._tip.hash === this.GENESIS_HASH, 'Expected tip hash to be genesis hash, but it was not.');
var genesisHeader = {
hash: this.GENESIS_HASH,
@ -412,7 +418,10 @@ HeaderService.prototype._getDBOpForLastHeader = function(nextHeader) {
this._lastHeader.nextHash = nextHeader.hash;
var keyHash = this._encoding.encodeHeaderHashKey(this._lastHeader.hash);
assert(this._lastHeader.height >= 0, 'Trying to save a header with incorrect height.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (this._lastHeader.height < 0) { log.error('Trying to save a header with incorrect height.');
}
// assert(this._lastHeader.height >= 0, 'Trying to save a header with incorrect height.');
var keyHeight = this._encoding.encodeHeaderHeightKey(this._lastHeader.height);
var value = this._encoding.encodeHeaderValue(this._lastHeader);
@ -467,8 +476,11 @@ HeaderService.prototype._onHeaders = function(headers) {
var header = transformedHeaders[i];
assert(self._lastHeader.hash === header.prevHash, 'headers not in order: ' + self._lastHeader.hash +
' -and- ' + header.prevHash + ' Last header at height: ' + self._lastHeader.height);
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (self._lastHeader.hash != header.prevHash) {
log.error('headers not in order: ' + self._lastHeader.hash + ' -and- ' + header.prevHash + ' Last header at height: ' + self._lastHeader.height);
}
// assert(self._lastHeader.hash === header.prevHash, 'headers not in order: ' + self._lastHeader.hash + ' -and- ' + header.prevHash + ' Last header at height: ' + self._lastHeader.height);
var ops = self._onHeader(header);
@ -485,8 +497,8 @@ HeaderService.prototype._onHeaders = function(headers) {
};
HeaderService.prototype._handleError = function(err) {
log.error('Header Service: ' + err);
// FLO Crash Error Resolution by RanchiMall 10th May 2021
log.error('Error in Header Service: ' + err);
return;
//this.node.stop();
};
@ -512,6 +524,7 @@ HeaderService.prototype._saveHeaders = function(dbOps, callback) {
HeaderService.prototype._onHeadersSave = function(callback) {
var self = this;
self._syncUnresponsive = false; //SZ: got response from peer
self._logProgress();
if (!self._syncComplete()) {
@ -519,6 +532,12 @@ HeaderService.prototype._onHeadersSave = function(callback) {
return callback();
}
//SZ: clear the interval check as sync is completed
if(self._syncCheckInterval){
clearInterval(self._syncCheckInterval);
self._syncCheckInterval = null;
}
self._endHeaderSubscription(); // we don't need headers any more
self._startBlockSubscription(); // we need new blocks coming tu us aynchronuously
@ -598,7 +617,7 @@ HeaderService.prototype._getHeader = function(height, hash, callback) {
/*jshint -W018 */
if (!hash && !(height >= 0)) {
/*jshint +W018 */
return callback(new Error('invalid arguments'));
return callback('invalid arguments');
}
if (height === self._lastHeader.height || hash === self._lastHeader.hash) {
@ -697,6 +716,23 @@ HeaderService.prototype._startSync = function() {
// common case
if (numNeeded > 0) {
log.info('Header Service: Gathering: ' + numNeeded + ' ' + 'header(s) from the peer-to-peer network.');
//SZ: Adding interval check for sync with peer is responsive or not
//(only if fcoin is started by flocore)
if(self._p2p._bcoin){
self._syncUnresponsive = true;
self._syncCheckInterval = setInterval(() => {
//check the best height
if(self._bestHeight < self._p2p._bcoin._bcoin.pool.chain.height)
self._bestHeight = self._p2p._bcoin._bcoin.pool.chain.height;
//call sync again if unresponsive
if(self._syncUnresponsive)
self._sync();
else //reset unresponsive as true
self._syncUnresponsive = true;
}, SYNC_CHECK_INTERVAL);
}
return self._sync();
}
@ -778,9 +814,11 @@ HeaderService.prototype._findReorgConditionInNewPeer = function(callback) {
// nothing matched...
// at this point, we should wonder if we are connected to the wrong network
assert(true, 'We tried to find a common header between current set of headers ' +
'and the new peer\'s set of headers, but there were none. This should be impossible ' +
' if the new peer is using the same genesis block.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (false) {
log.error('We tried to find a common header between current set of headers ' + 'and the new peer\'s set of headers, but there were none. This should be impossible ' + ' if the new peer is using the same genesis block.');
}
// assert(true, 'We tried to find a common header between current set of headers ' +'and the new peer\'s set of headers, but there were none. This should be impossible ' +' if the new peer is using the same genesis block.');
});
self._getP2PHeaders(self.GENESIS_HASH);
@ -881,7 +919,11 @@ HeaderService.prototype._sync = function() {
HeaderService.prototype.getEndHash = function(tip, blockCount, callback) {
assert(blockCount >= 1, 'Header Service: block count to getEndHash must be at least 1.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (blockCount < 1) {
log.error('Header Service: block count to getEndHash must be at least 1.');
}
// assert(blockCount >= 1, 'Header Service: block count to getEndHash must be at least 1.');
var self = this;
@ -892,7 +934,7 @@ HeaderService.prototype.getEndHash = function(tip, blockCount, callback) {
}
if (numResultsNeeded <= 0) {
return callback(new Error('Header Service: block service is mis-aligned '));
return callback('Header Service: block service is mis-aligned ');
}
var startingHeight = tip.height + 1;
@ -922,8 +964,11 @@ HeaderService.prototype.getEndHash = function(tip, blockCount, callback) {
if (streamErr) {
return streamErr;
}
assert(results.length === numResultsNeeded, 'getEndHash returned incorrect number of results.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (results.length != numResultsNeeded) {
log.error('getEndHash returned incorrect number of results.');
}
// assert(results.length === numResultsNeeded, 'getEndHash returned incorrect number of results.');
var index = numResultsNeeded - 1;
var endHash = index <= 0 || !results[index] ? 0 : results[index];
@ -941,7 +986,11 @@ HeaderService.prototype.getEndHash = function(tip, blockCount, callback) {
};
HeaderService.prototype.getLastHeader = function() {
assert(this._lastHeader, 'Last header should be populated.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (this._lastHeader == false) {
log.error('Last header should be populated.'); return;
}
// assert(this._lastHeader, 'Last header should be populated.');
return this._lastHeader;
};
@ -999,7 +1048,13 @@ HeaderService.prototype._adjustHeadersForCheckPointTip = function(callback) {
return streamErr;
}
assert(self._lastHeader, 'The last synced header was not in the database.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (self._lastHeader == false) {
log.error('The last synced header was not in the database.');
}
//assert(self._lastHeader, 'The last synced header was not in the database.');
self._tip.hash = self._lastHeader.hash;
self._tip.height = self._lastHeader.height;
self._db.batch(removalOps, callback);
@ -1010,7 +1065,7 @@ HeaderService.prototype._adjustHeadersForCheckPointTip = function(callback) {
HeaderService.prototype._getChainwork = function(header, prevHeader) {
var prevChainwork = new BN(Buffer.from(prevHeader.chainwork, 'hex'));
var prevChainwork = new BN(new Buffer(prevHeader.chainwork, 'hex'));
return this._computeChainwork(header.bits, prevChainwork);
};

View File

@ -4,13 +4,13 @@ var tx = require('fcoin').TX;
function Encoding(servicePrefix) {
this.servicePrefix = servicePrefix;
this.txPrefix = Buffer.from('00', 'hex');
this.addressPrefix = Buffer.from('01', 'hex');
this.txPrefix = new Buffer('00', 'hex');
this.addressPrefix = new Buffer('01', 'hex');
}
Encoding.prototype.encodeMempoolTransactionKey = function(txid) {
var buffers = [this.servicePrefix, this.txPrefix];
var txidBuffer = Buffer.from(txid, 'hex');
var txidBuffer = new Buffer(txid, 'hex');
buffers.push(txidBuffer);
return Buffer.concat(buffers);
};
@ -30,22 +30,22 @@ Encoding.prototype.decodeMempoolTransactionValue = function(buffer) {
Encoding.prototype.encodeMempoolAddressKey = function(address, txid, index, input) {
var buffers = [this.servicePrefix, this.addressPrefix];
var addressSizeBuffer = Buffer.alloc(1);
var addressSizeBuffer = new Buffer(1);
addressSizeBuffer.writeUInt8(address.length);
var addressBuffer = Buffer.from(address, 'utf8');
var addressBuffer = new Buffer(address, 'utf8');
buffers.push(addressSizeBuffer);
buffers.push(addressBuffer);
var txidBuffer = Buffer.from(txid || Array(65).join('0'), 'hex');
var txidBuffer = new Buffer(txid || Array(65).join('0'), 'hex');
buffers.push(txidBuffer);
var indexBuffer = Buffer.alloc(4);
var indexBuffer = new Buffer(4);
indexBuffer.writeUInt32BE(index || 0);
buffers.push(indexBuffer);
// this is whether the address appears in an input (1) or output (0)
var inputBuffer = Buffer.alloc(1);
var inputBuffer = new Buffer(1);
inputBuffer.writeUInt8(input || 0);
buffers.push(inputBuffer);

View File

@ -283,7 +283,7 @@ MempoolService.prototype.getTxidsByAddress = function(address, type, callback) {
var self = this;
var results = [];
var start = self._encoding.encodeMempoolAddressKey(address);
var end = Buffer.concat([ start.slice(0, -37), Buffer.from(new Array(75).join('f'), 'hex') ]);
var end = Buffer.concat([ start.slice(0, -37), new Buffer(new Array(75).join('f'), 'hex') ]);
var criteria = {
gte: start,

View File

@ -425,7 +425,12 @@ P2P.prototype._setListeners = function() {
P2P.prototype._setResourceFilter = function(filter) {
assert(filter && filter.startHash, 'A "startHash" field is required to retrieve headers or blocks');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (filter == false || filter.startHash == false) {
log.error('A "startHash" field is required to retrieve headers or blocks');
}
// assert(filter && filter.startHash, 'A "startHash" field is required to retrieve headers or blocks');
if (!filter.endHash) {
filter.endHash = 0;
}

View File

@ -2,13 +2,13 @@
function Encoding(servicePrefix) {
this._servicePrefix = servicePrefix;
this._blockPrefix = Buffer.from('00', 'hex');
this._timestampPrefix = Buffer.from('01', 'hex');
this._blockPrefix = new Buffer('00', 'hex');
this._timestampPrefix = new Buffer('01', 'hex');
}
// ---- block hash -> timestamp
Encoding.prototype.encodeBlockTimestampKey = function(hash) {
return Buffer.concat([this._servicePrefix, this._blockPrefix, Buffer.from(hash, 'hex')]);
return Buffer.concat([this._servicePrefix, this._blockPrefix, new Buffer(hash, 'hex')]);
};
Encoding.prototype.decodeBlockTimestampKey = function(buffer) {
@ -16,7 +16,7 @@ Encoding.prototype.decodeBlockTimestampKey = function(buffer) {
};
Encoding.prototype.encodeBlockTimestampValue = function(timestamp) {
var timestampBuffer = Buffer.alloc(4);
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(timestamp);
return timestampBuffer;
};
@ -28,7 +28,7 @@ Encoding.prototype.decodeBlockTimestampValue = function(buffer) {
// ---- timestamp -> block hash
Encoding.prototype.encodeTimestampBlockKey = function(timestamp) {
var timestampBuffer = Buffer.alloc(4);
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(timestamp);
return Buffer.concat([this._servicePrefix, this._timestampPrefix, timestampBuffer]);
};
@ -38,7 +38,7 @@ Encoding.prototype.decodeTimestampBlockKey = function(buffer) {
};
Encoding.prototype.encodeTimestampBlockValue = function(hash) {
return Buffer.from(hash, 'hex');
return new Buffer(hash, 'hex');
};
Encoding.prototype.decodeTimestampBlockValue = function(buffer) {

View File

@ -4,13 +4,13 @@ var Tx = require('fcoin').TX;
function Encoding(servicePrefix) {
this.servicePrefix = servicePrefix;
this.txIndex = Buffer.from('00', 'hex');
this.spentIndex = Buffer.from('01', 'hex');
this.doubleSpentIndex = Buffer.from('02', 'hex');
this.txIndex = new Buffer('00', 'hex');
this.spentIndex = new Buffer('01', 'hex');
this.doubleSpentIndex = new Buffer('02', 'hex');
}
Encoding.prototype.encodeTransactionKey = function(txid) {
return Buffer.concat([this.servicePrefix, this.txIndex, Buffer.from(txid, 'hex')]);
return Buffer.concat([this.servicePrefix, this.txIndex, new Buffer(txid, 'hex')]);
};
Encoding.prototype.decodeTransactionKey = function(buffer) {
@ -18,24 +18,24 @@ Encoding.prototype.decodeTransactionKey = function(buffer) {
};
Encoding.prototype.encodeTransactionValue = function(transaction) {
var heightBuffer = Buffer.alloc(4);
var heightBuffer = new Buffer(4);
heightBuffer.writeUInt32BE(transaction.__height);
var hashBuffer = Buffer.from(transaction.__blockhash, 'hex');
var hashBuffer = new Buffer(transaction.__blockhash, 'hex');
var timestampBuffer = Buffer.alloc(4);
var timestampBuffer = new Buffer(4);
timestampBuffer.writeUInt32BE(transaction.__timestamp);
var inputValues = transaction.__inputValues;
var inputValuesBuffer = Buffer.alloc(8 * inputValues.length);
var inputValuesBuffer = new Buffer(8 * inputValues.length);
for(var i = 0; i < inputValues.length; i++) {
inputValuesBuffer.writeDoubleBE(inputValues[i], i * 8);
}
var inputValuesLengthBuffer = Buffer.alloc(2);
var inputValuesLengthBuffer = new Buffer(2);
inputValuesLengthBuffer.writeUInt16BE(inputValues.length);
return Buffer.concat([heightBuffer, hashBuffer, timestampBuffer,
return new Buffer.concat([heightBuffer, hashBuffer, timestampBuffer,
inputValuesLengthBuffer, inputValuesBuffer, transaction.toRaw()]);
};
@ -65,9 +65,9 @@ Encoding.prototype.decodeTransactionValue = function(buffer) {
// for every input we receive, we make an entry for what output it spends
Encoding.prototype.encodeSpentKey = function(txid, outputIndex) {
var outputIndexBuffer = Buffer.alloc(4);
var outputIndexBuffer = new Buffer(4);
outputIndexBuffer.writeUInt32BE(outputIndex);
return Buffer.concat([this.servicePrefix, this.spentIndex, Buffer.from(txid, 'hex'), outputIndexBuffer]);
return Buffer.concat([this.servicePrefix, this.spentIndex, new Buffer(txid, 'hex'), outputIndexBuffer]);
};
Encoding.prototype.decodeSpentKey = function(buffer) {
@ -80,12 +80,12 @@ Encoding.prototype.decodeSpentKey = function(buffer) {
};
Encoding.prototype.encodeSpentValue = function(txid, inputIndex, blockHeight, blockHash) {
var inputIndexBuffer = Buffer.alloc(4);
var inputIndexBuffer = new Buffer(4);
inputIndexBuffer.writeUInt32BE(inputIndex);
var blockHeightBuffer = Buffer.alloc(4);
var blockHeightBuffer = new Buffer(4);
blockHeightBuffer.writeUInt32BE(blockHeight);
var blockHashBuffer = Buffer.from(blockHash, 'hex');
return Buffer.concat([Buffer.from(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]);
var blockHashBuffer = new Buffer(blockHash, 'hex');
return Buffer.concat([new Buffer(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]);
};
Encoding.prototype.decodeSpentValue = function(buffer) {
@ -102,9 +102,9 @@ Encoding.prototype.decodeSpentValue = function(buffer) {
};
Encoding.prototype.encodeDoubleSpentKey = function(txid, outputIndex) {
var outputIndexBuffer = Buffer.alloc(4);
var outputIndexBuffer = new Buffer(4);
outputIndexBuffer.writeUInt32BE(outputIndex);
return Buffer.concat([this.servicePrefix, this.spentIndex, Buffer.from(txid, 'hex'), outputIndexBuffer]);
return Buffer.concat([this.servicePrefix, this.spentIndex, new Buffer(txid, 'hex'), outputIndexBuffer]);
};
Encoding.prototype.decodeDoubleSpentKey = function(buffer) {
@ -117,12 +117,12 @@ Encoding.prototype.decodeDoubleSpentKey = function(buffer) {
};
Encoding.prototype.encodeDoubleSpentValue = function(txid, inputIndex, blockHeight, blockHash) {
var inputIndexBuffer = Buffer.alloc(4);
var inputIndexBuffer = new Buffer(4);
inputIndexBuffer.writeUInt32BE(inputIndex);
var blockHeightBuffer = Buffer.alloc(4);
var blockHeightBuffer = new Buffer(4);
blockHeightBuffer.writeUInt32BE(inputIndex);
var blockHashBuffer = Buffer.from(blockHash, 'hex');
return Buffer.concat([Buffer.from(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]);
var blockHashBuffer = new Buffer(blockHash, 'hex');
return Buffer.concat([new Buffer(txid, 'hex'), inputIndexBuffer, blockHeightBuffer, blockHashBuffer]);
};
Encoding.prototype.decodeDoubleSpentValue = function(buffer) {

View File

@ -7,6 +7,7 @@ var _ = require('lodash');
var async = require('async');
var assert = require('assert');
var LRU = require('lru-cache');
var log = require('../../index').log;
function TransactionService(options) {
BaseService.call(this, options);
@ -113,7 +114,7 @@ TransactionService.prototype.getTransaction = function(txid, options, callback)
var self = this;
if (typeof callback !== 'function') {
if (!_.isFunction(callback)) {
callback = options;
}
@ -184,8 +185,11 @@ TransactionService.prototype.setTxMetaInfo = function(tx, options, callback) {
var inputSatoshis = 0;
assert(tx.__inputValues.length === tx.inputs.length,
'Transaction Service: input values length is not the same as the number of inputs.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (tx.__inputValues.length != tx.inputs.length) {
log.error('Transaction Service: input values length is not the same as the number of inputs.');
}
// assert(tx.__inputValues.length === tx.inputs.length, 'Transaction Service: input values length is not the same as the number of inputs.');
tx.__inputValues.forEach(function(val) {
@ -308,14 +312,18 @@ TransactionService.prototype._getInputValues = function(tx, options, callback) {
// if not in mempool or tx index, we just don't have it, yet?
function(txid, tx, next) {
if (!tx) {
return next(new Error('Transaction Service: prev transacion: (' + input.prevout.txid() + ') for tx: ' +
return next(log.error('Transaction Service: prev transacion: (' + input.prevout.txid() + ') for tx: ' +
_tx.txid() + ' at input index: ' + outputIndex + ' is missing from the index or not in the memory pool. It could be' +
' that the parent tx has not yet been relayed to us, but will be relayed in the near future.'));
}
var output = tx.outputs[outputIndex];
assert(output, 'Expected an output, but did not get one for tx: ' + tx.txid() + ' outputIndex: ' + outputIndex);
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (output == false) {
log.error('Expected an output, but did not get one for tx: ' + tx.txid() + ' outputIndex: ' + outputIndex);
}
// assert(output, 'Expected an output, but did not get one for tx: ' + tx.txid() + ' outputIndex: ' + outputIndex);
next(null, output.value);
}
@ -375,7 +383,11 @@ TransactionService.prototype.onBlock = function(block, callback) {
return callback(err);
}
assert(block.txs.length === operations.length, 'It seems we are not indexing the correct number of transactions.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (block.txs.length != operations.length) {
log.error('It seems we are not indexing the correct number of transactions.');
}
// assert(block.txs.length === operations.length, 'It seems we are not indexing the correct number of transactions.');
callback(null, _.flattenDeep(operations));
});
@ -477,19 +489,30 @@ TransactionService.prototype._processTransaction = function(tx, opts, callback)
return callback(err);
}
assert(inputValues && inputValues.length === tx.inputs.length,
'Input values missing from tx.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (inputValues == false || inputValues.length != tx.inputs.length) {
log.error('Input values missing from tx.');
}
// assert(inputValues && inputValues.length === tx.inputs.length, 'Input values missing from tx.');
// inputValues
tx.__inputValues = inputValues;
// timestamp
tx.__timestamp = self._getBlockTimestamp(opts.block.rhash());
assert(tx.__timestamp, 'Timestamp is required when saving a transaction.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (tx.__timestamp == false) {
log.error('Timestamp is required when saving a transaction.');
}
// assert(tx.__timestamp, 'Timestamp is required when saving a transaction.');
// height
tx.__height = opts.block.__height;
assert(tx.__height, 'Block height is required when saving a trasnaction.');
// FLOSight Error Correction from RanchiMall 17th May 2021. removed the unhandled assert and replaced by looging of error
if (tx.__height == false) {
log.error('Block height is required when saving a trasnaction.');
}
//assert(tx.__height, 'Block height is required when saving a trasnaction.');
// block hash
tx.__blockhash = opts.block.rhash();

View File

@ -4,6 +4,7 @@ var fs = require('fs');
var http = require('http');
var https = require('https');
var express = require('express');
var express_ws = require('express-ws');
var bodyParser = require('body-parser');
var socketio = require('socket.io');
var inherits = require('util').inherits;
@ -105,7 +106,7 @@ WebService.prototype.setupAllRoutes = function() {
if(service.getRoutePrefix && service.setupRoutes) {
this.app.use('/' + this.node.services[key].getRoutePrefix(), subApp);
this.node.services[key].setupRoutes(subApp, express);
this.node.services[key].setupRoutes(subApp, express, express_ws);
} else {
log.debug('No routes defined for: ' + key);
}

View File

@ -58,12 +58,12 @@ utils.sendError = function(err, res) {
utils.encodeTip = function(tip, name) {
var key = Buffer.concat([ constants.DB_PREFIX,
Buffer.from('tip-' + name, 'utf8') ]);
new Buffer('tip-' + name, 'utf8') ]);
var heightBuf = Buffer.alloc(4);
var heightBuf = new Buffer(4);
heightBuf.writeUInt32BE(tip.height);
var value = Buffer.concat([ heightBuf, Buffer.from(tip.hash, 'hex') ]);
var value = Buffer.concat([ heightBuf, new Buffer(tip.hash, 'hex') ]);
return { key: key, value: value };
};

View File

@ -5,12 +5,12 @@
"node": ">=8.0.0"
},
"author": "BitPay <dev@bitpay.com>",
"version": "5.0.8",
"version": "5.0.9-beta-rm",
"main": "./index.js",
"repository": "git://github.com/oipwg/flocore-node.git",
"homepage": "https://github.com/oipwg/flocore-node",
"repository": "git://github.com/ranchimall/flocore-node.git",
"homepage": "https://github.com/ranchimall/flocore-node",
"bugs": {
"url": "https://github.com/oipwg/flocore-node/issues"
"url": "https://github.com/ranchimall/flocore-node/issues"
},
"bin": {
"flocore-node": "./bin/flocore-node"
@ -36,13 +36,14 @@
"commander": "^2.8.1",
"errno": "^0.1.4",
"express": "^4.13.3",
"express-ws": "^5.0.2",
"fcoin": "^1.1.4",
"flocore-lib": "^0.15.2",
"flocore-message": "^1.0.7",
"flocore-p2p": "^5.0.0-beta.8",
"florincoind-rpc": "0.7.1",
"flosight-api": "^5.0.0-beta.75",
"flosight-ui": "^5.0.0-beta.72",
"flosight-api": "github:ranchimall/flosight-api",
"flosight-ui": "github:ranchimall/flosight-ui",
"leveldown": "^2.0.0",
"levelup": "^2.0.0",
"liftoff": "^2.2.0",

View File

@ -6,27 +6,27 @@ var Encoding = require('../../../lib/services/address/encoding');
describe('Address service encoding', function() {
var servicePrefix = Buffer.from('0000', 'hex');
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var address = '1EZBqbJSHFKSkVPNKzc5v26HA6nAHiTXq6';
var height = 1;
var addressSizeBuf = Buffer.alloc(1);
var prefix0 = Buffer.from('00', 'hex');
var prefix1 = Buffer.from('01', 'hex');
var addressSizeBuf = new Buffer(1);
var prefix0 = new Buffer('00', 'hex');
var prefix1 = new Buffer('01', 'hex');
var ts = Math.floor(new Date('2017-02-28').getTime() / 1000);
var tsBuf = Buffer.alloc(4);
var tsBuf = new Buffer(4);
tsBuf.writeUInt32BE(ts);
addressSizeBuf.writeUInt8(address.length);
var addressIndexKeyBuf = Buffer.concat([
servicePrefix,
prefix0,
addressSizeBuf,
Buffer.from(address),
Buffer.from('00000001', 'hex'),
Buffer.from(txid, 'hex'),
Buffer.from('00000000', 'hex'),
Buffer.from('00', 'hex'),
new Buffer(address),
new Buffer('00000001', 'hex'),
new Buffer(txid, 'hex'),
new Buffer('00000000', 'hex'),
new Buffer('00', 'hex'),
tsBuf
]);
var outputIndex = 5;
@ -34,15 +34,15 @@ describe('Address service encoding', function() {
servicePrefix,
prefix1,
addressSizeBuf,
Buffer.from(address),
Buffer.from(txid, 'hex'),
Buffer.from('00000005', 'hex')]);
new Buffer(address),
new Buffer(txid, 'hex'),
new Buffer('00000005', 'hex')]);
var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000';
var tx = new flocore.Transaction(txHex);
var sats = tx.outputs[0].satoshis;
var satsBuf = Buffer.alloc(8);
var satsBuf = new Buffer(8);
satsBuf.writeDoubleBE(sats);
var utxoValueBuf = Buffer.concat([Buffer.from('00000001', 'hex'), satsBuf, tsBuf, tx.outputs[0]._scriptBuffer]);
var utxoValueBuf = Buffer.concat([new Buffer('00000001', 'hex'), satsBuf, tsBuf, tx.outputs[0]._scriptBuffer]);
it('should encode address key' , function() {
encoding.encodeAddressIndexKey(address, height, txid, 0, 0, ts).should.deep.equal(addressIndexKeyBuf);

View File

@ -24,7 +24,7 @@ describe('Address Service', function() {
services: []
}
});
addressService._encoding = new Encoding(Buffer.from('0000', 'hex'));
addressService._encoding = new Encoding(new Buffer('0000', 'hex'));
});
afterEach(function() {
@ -34,7 +34,7 @@ describe('Address Service', function() {
describe('#start', function() {
it('should get prefix for database', function(done) {
var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('ffee', 'hex'));
var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('ffee', 'hex'));
addressService._db = { getPrefix: getPrefix };
addressService.start(function() {
expect(getPrefix.calledOnce).to.be.true;
@ -355,7 +355,7 @@ describe('Address Service', function() {
describe('#getAddressUnspentOutputs', function() {
it('should get address utxos', function(done) {
var encoding = new Encoding(Buffer.from('0001', 'hex'));
var encoding = new Encoding(new Buffer('0001', 'hex'));
addressService._encoding = encoding;
var address = 'a';

View File

@ -7,7 +7,7 @@ var Encoding = require('../../../lib/services/block/encoding');
describe('Block service encoding', function() {
var servicePrefix = Buffer.from('0000', 'hex');
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
@ -19,14 +19,14 @@ describe('Block service encoding', function() {
it('should encode block key' , function() {
encoding.encodeBlockKey(hash).should.deep.equal(Buffer.concat([
servicePrefix,
Buffer.from(hash, 'hex')
new Buffer(hash, 'hex')
]));
});
it('should decode block key' , function() {
var buf = Buffer.concat([
servicePrefix,
Buffer.from(hash, 'hex')
new Buffer(hash, 'hex')
]);
var actual = encoding.decodeBlockKey(buf);

View File

@ -23,7 +23,7 @@ describe('Block Service', function() {
services: []
}
});
blockService._encoding = new Encoding(Buffer.from('0000', 'hex'));
blockService._encoding = new Encoding(new Buffer('0000', 'hex'));
});
afterEach(function() {

View File

@ -121,7 +121,7 @@ describe('DB', function() {
it('should put a value in the db', function(done) {
var put = sandbox.stub().callsArgWith(2, null);
dbService._store = { put: put };
dbService.put(Buffer.from('key'), Buffer.from('value'), function(err) {
dbService.put(new Buffer('key'), new Buffer('value'), function(err) {
if (err) {
return done(err);
}
@ -132,7 +132,7 @@ describe('DB', function() {
it('should not allow an operation while the node is shutting down', function(done) {
dbService._stopping = true;
dbService.put(Buffer.from('key'), Buffer.from('value'), function(err) {
dbService.put(new Buffer('key'), new Buffer('value'), function(err) {
done();
});
});
@ -256,7 +256,7 @@ describe('DB', function() {
describe('#getServiceTip', function() {
it('should get service tip for previously saved', function(done) {
var tipBuf = Buffer.concat([ Buffer.from('deadbeef', 'hex'), Buffer.from(tx.txid(), 'hex') ]);
var tipBuf = Buffer.concat([ new Buffer('deadbeef', 'hex'), new Buffer(tx.txid(), 'hex') ]);
var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, tipBuf);
dbService.getServiceTip('test', function(err, tip) {
@ -293,7 +293,7 @@ describe('DB', function() {
describe('#getPrefix', function() {
it('should get the db prefix for a service when one already exists', function(done) {
var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, Buffer.from('0000', 'hex'));
var get = sandbox.stub(dbService, 'get').callsArgWith(1, null, new Buffer('0000', 'hex'));
dbService.getPrefix('test', function(err, prefix) {
if(err) {
@ -310,7 +310,7 @@ describe('DB', function() {
var put = sandbox.stub(dbService, 'put').callsArgWith(2, null);
var get = sandbox.stub(dbService, 'get');
get.onCall(0).callsArgWith(1, null, null);
get.onCall(1).callsArgWith(1, null, Buffer.from('eeee', 'hex'));
get.onCall(1).callsArgWith(1, null, new Buffer('eeee', 'hex'));
dbService.getPrefix('test', function(err, prefix) {
if(err) {

View File

@ -6,13 +6,13 @@ var Encoding = require('../../../lib/services/header/encoding');
describe('Header service encoding', function() {
var servicePrefix = Buffer.from('0000', 'hex');
var servicePrefix = new Buffer('0000', 'hex');
var hashPrefix = Buffer.from('00', 'hex');
var heightPrefix = Buffer.from('01', 'hex');
var hashPrefix = new Buffer('00', 'hex');
var heightPrefix = new Buffer('01', 'hex');
var encoding = new Encoding(servicePrefix);
var hash = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var hashBuf = Buffer.from(hash, 'hex');
var hashBuf = new Buffer(hash, 'hex');
var header = {
hash: hash,
prevHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade',
@ -25,15 +25,15 @@ describe('Header service encoding', function() {
chainwork: '0000000000000000000000000000000000000000000000000000000200020002',
nextHash: '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03ade'
};
var versionBuf = Buffer.alloc(4);
var prevHashBuf = Buffer.from(header.prevHash, 'hex');
var nextHashBuf = Buffer.from(header.nextHash, 'hex');
var merkleRootBuf = Buffer.from(header.merkleRoot, 'hex');
var tsBuf = Buffer.alloc(4);
var bitsBuf = Buffer.alloc(4);
var nonceBuf = Buffer.alloc(4);
var heightBuf = Buffer.alloc(4);
var chainBuf = Buffer.from('0000000000000000000000000000000000000000000000000000000200020002', 'hex');
var versionBuf = new Buffer(4);
var prevHashBuf = new Buffer(header.prevHash, 'hex');
var nextHashBuf = new Buffer(header.nextHash, 'hex');
var merkleRootBuf = new Buffer(header.merkleRoot, 'hex');
var tsBuf = new Buffer(4);
var bitsBuf = new Buffer(4);
var nonceBuf = new Buffer(4);
var heightBuf = new Buffer(4);
var chainBuf = new Buffer('0000000000000000000000000000000000000000000000000000000200020002', 'hex');
heightBuf.writeUInt32BE(header.height);
it('should encode header hash key' , function() {
@ -54,7 +54,7 @@ describe('Header service encoding', function() {
.should.deep.equal(header.height);
});
it('should encode header value', function() {
var prevHashBuf = Buffer.from(header.prevHash, 'hex');
var prevHashBuf = new Buffer(header.prevHash, 'hex');
versionBuf.writeInt32BE(header.version); // signed
tsBuf.writeUInt32BE(header.timestamp);
bitsBuf.writeUInt32BE(header.bits);

View File

@ -16,8 +16,8 @@ describe('Header Service', function() {
var headerService;
var sandbox;
var prevHeader = new Block(Buffer.from('01000000b25c0849b469983b4a5b90a49e4c0e4ba3853122ed141b5bd92d14000000000021a8aaa4995e4ce3b885677730b153741feda66a08492287a45c6a131671ba5a72ff504c5a0c011c456e4d060201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c028208ffffffff0100f2052a010000004341041994d910507ec4b2135dd32a4723caf00f8567f356ffbd5e703786d856b49a89d6597c280d8981238fbde81fa3767161bc3e994c17be41b42235a61c24c73459ac0000000001000000013b517d1aebd89b4034e0cf9b25ecbe82ef162ce71284e92a1f1adebf44ea1409000000008b483045022100c7ebc62e89740ddab42a64435c996e1c91a063f9f2cc004b4f023f7a1be5234402207608837faebec16049461d4ef7de807ce217040fd2a823a29da16ec07e463d440141048f108c0da4b5be3308e2e0b521d02d341de85b36a29285b47f00bc33e57a89cf4b6e76aa4a48ddc9a5e882620779e0f1b19dc98d478052fbd544167c745be1d8ffffffff010026e85a050000001976a914f760ef90462b0a4bde26d597c1f29324f5cd0fc488ac00000000', 'hex')).header.toObject();
var preObjectHeader = new Block(Buffer.from('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex')).header;
var prevHeader = new Block(new Buffer('01000000b25c0849b469983b4a5b90a49e4c0e4ba3853122ed141b5bd92d14000000000021a8aaa4995e4ce3b885677730b153741feda66a08492287a45c6a131671ba5a72ff504c5a0c011c456e4d060201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c028208ffffffff0100f2052a010000004341041994d910507ec4b2135dd32a4723caf00f8567f356ffbd5e703786d856b49a89d6597c280d8981238fbde81fa3767161bc3e994c17be41b42235a61c24c73459ac0000000001000000013b517d1aebd89b4034e0cf9b25ecbe82ef162ce71284e92a1f1adebf44ea1409000000008b483045022100c7ebc62e89740ddab42a64435c996e1c91a063f9f2cc004b4f023f7a1be5234402207608837faebec16049461d4ef7de807ce217040fd2a823a29da16ec07e463d440141048f108c0da4b5be3308e2e0b521d02d341de85b36a29285b47f00bc33e57a89cf4b6e76aa4a48ddc9a5e882620779e0f1b19dc98d478052fbd544167c745be1d8ffffffff010026e85a050000001976a914f760ef90462b0a4bde26d597c1f29324f5cd0fc488ac00000000', 'hex')).header.toObject();
var preObjectHeader = new Block(new Buffer('010000006a39821735ec18a366d95b391a7ff10dee181a198f1789b0550e0d00000000002b0c80fa52b669022c344c3e09e6bb9698ab90707bb4bb412af3fbf31cfd2163a601514c5a0c011c572aef0f0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08045a0c011c022003ffffffff0100f2052a01000000434104c5b694d72e601091fd733c6b18b94795c13e2db6b1474747e7be914b407854cad37cee3058f85373b9f9dbb0014e541c45851d5f85e83a1fd7c45e54423718f3ac00000000', 'hex')).header;
var header = preObjectHeader.toObject();
beforeEach(function() {
sandbox = sinon.sandbox.create();
@ -26,7 +26,7 @@ describe('Header Service', function() {
services: []
}
});
headerService._encoding = new Encoding(Buffer.from('0000', 'hex'));
headerService._encoding = new Encoding(new Buffer('0000', 'hex'));
});
afterEach(function() {
@ -40,7 +40,7 @@ describe('Header Service', function() {
var getServiceTip = sandbox.stub().callsArgWith(1, null, { height: 123, hash: 'a' });
var setListeners = sandbox.stub(headerService, '_setListeners');
var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('ffee', 'hex'));
var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('ffee', 'hex'));
var adjustHeadersForCheckPointTip = sandbox.stub(headerService, '_adjustHeadersForCheckPointTip').callsArgWith(0, null);
var setGenesisBlock = sandbox.stub(headerService, '_setGenesisBlock').callsArgWith(0, null);
headerService.GENESIS_HASH = '00';
@ -159,8 +159,8 @@ describe('Header Service', function() {
describe('#_computeChainwork', function() {
it('should calculate chain work correctly', function() {
var expected = new BN(Buffer.from('000000000000000000000000000000000000000000677c7b8122f9902c79f4e0', 'hex'));
var prev = new BN(Buffer.from('000000000000000000000000000000000000000000677bd68118a98f8779ea90', 'hex'));
var expected = new BN(new Buffer('000000000000000000000000000000000000000000677c7b8122f9902c79f4e0', 'hex'));
var prev = new BN(new Buffer('000000000000000000000000000000000000000000677bd68118a98f8779ea90', 'hex'));
var actual = headerService._computeChainwork(0x18018d30, prev);
assert(actual.eq(expected), 'not equal: actual: ' + actual + ' expected: ' + expected);

View File

@ -7,36 +7,36 @@ var Encoding = require('../../../lib/services/mempool/encoding');
describe('Block service encoding', function() {
var servicePrefix = Buffer.from('0000', 'hex');
var txPrefix = Buffer.from('00', 'hex');
var addressPrefix = Buffer.from('01', 'hex');
var servicePrefix = new Buffer('0000', 'hex');
var txPrefix = new Buffer('00', 'hex');
var addressPrefix = new Buffer('01', 'hex');
var encoding = new Encoding(servicePrefix);
var hash = '25e28f9fb0ada5353b7d98d85af5524b2f8df5b0b0e2d188f05968bceca603eb';
var txString = '0100000004de9b4bb17f627096a9ee0b4528e4eae17df5b5c69edc29704c2e84a7371db29f010000006b483045022100f5b1a0d33b7be291c3953c25f8ae39d98601aa7099a8674daf638a08b86c7173022006ce372da5ad088a1cc6e5c49c2760a1b6f085eb1b51b502211b6bc9508661f9012102ec5e3731e54475dd2902326f43602a03ae3d62753324139163f81f20e787514cffffffff7a1d4e5fc2b8177ec738cd723a16cf2bf493791e55573445fc0df630fe5e2d64010000006b483045022100cf97f6cb8f126703e9768545dfb20ffb10ba78ae3d101aa46775f5a239b075fc02203150c4a89a11eaf5e404f4f96b62efa4455e9525765a025525c7105a7e47b6db012102c01e11b1d331f999bbdb83e8831de503cd52a01e3834a95ccafd615c67703d77ffffffff9e52447116415ca0d0567418a1a4ef8f27be3ff5a96bf87c922f3723d7db5d7c000000006b483045022100f6c117e536701be41a6b0b544d7c3b1091301e4e64a6265b6eb167b15d16959d022076916de4b115e700964194ce36a24cb9105f86482f4abbc63110c3f537cd5770012102ddf84cc7bee2d6a82ac09628a8ad4a26cd449fc528b81e7e6cc615707b8169dfffffffff5815d9750eb3572e30d6fd9df7afb4dbd76e042f3aa4988ac763b3fdf8397f80010000006a473044022028f4402b736066d93d2a32b28ccd3b7a21d84bb58fcd07fe392a611db94cdec5022018902ee0bf2c3c840c1b81ead4e6c87c88c48b2005bf5eea796464e561a620a8012102b6cdd1a6cd129ef796faeedb0b840fcd0ca00c57e16e38e46ee7028d59812ae7ffffffff0220a10700000000001976a914c342bcd1a7784d9842f7386b8b3b8a3d4171a06e88ac59611100000000001976a91449f8c749a9960dc29b5cbe7d2397cea7d26611bb88ac00000000';
var address = '1234567';
var now = Math.floor(Date.now() / 1000);
var nowBuf = Buffer.alloc(4);
var nowBuf = new Buffer(4);
nowBuf.writeUInt32BE(now);
describe('Mempool', function() {
it('should encode mempool transaction key', function() {
encoding.encodeMempoolTransactionKey(hash).should.deep.equal(Buffer.concat([ servicePrefix, txPrefix, Buffer.from(hash, 'hex') ]));
encoding.encodeMempoolTransactionKey(hash).should.deep.equal(Buffer.concat([ servicePrefix, txPrefix, new Buffer(hash, 'hex') ]));
});
it('should decode mempool transaction key', function() {
encoding.decodeMempoolTransactionKey(Buffer.concat([ servicePrefix, txPrefix, Buffer.from(hash, 'hex') ])).should.deep.equal(hash);
encoding.decodeMempoolTransactionKey(Buffer.concat([ servicePrefix, txPrefix, new Buffer(hash, 'hex') ])).should.deep.equal(hash);
});
it('should encode mempool transaction value', function() {
var mytx = tx.fromRaw(txString, 'hex');
mytx.__inputValues = [1012955, 447698, 446664, 391348];
encoding.encodeMempoolTransactionValue(mytx).should.deep.equal(Buffer.from(txString, 'hex'));
encoding.encodeMempoolTransactionValue(mytx).should.deep.equal(new Buffer(txString, 'hex'));
});
it('should decode mempool transaction value', function() {
var mytx = encoding.decodeMempoolTransactionValue(Buffer.from(txString, 'hex'));
var mytx = encoding.decodeMempoolTransactionValue(new Buffer(txString, 'hex'));
mytx.should.deep.equal(tx.fromRaw(txString, 'hex'));
});
@ -46,11 +46,11 @@ describe('Block service encoding', function() {
.should.deep.equal(Buffer.concat([
servicePrefix,
addressPrefix,
Buffer.from('07', 'hex'),
Buffer.from(address),
Buffer.from(hash, 'hex'),
Buffer.from('00000000', 'hex'),
Buffer.from('01', 'hex')
new Buffer('07', 'hex'),
new Buffer(address),
new Buffer(hash, 'hex'),
new Buffer('00000000', 'hex'),
new Buffer('01', 'hex')
]));
});
@ -58,11 +58,11 @@ describe('Block service encoding', function() {
encoding.decodeMempoolAddressKey(Buffer.concat([
servicePrefix,
addressPrefix,
Buffer.from('07', 'hex'),
Buffer.from(address),
Buffer.from(hash, 'hex'),
Buffer.from('00000000', 'hex'),
Buffer.from('01', 'hex') ])).should.deep.equal({
new Buffer('07', 'hex'),
new Buffer(address),
new Buffer(hash, 'hex'),
new Buffer('00000000', 'hex'),
new Buffer('01', 'hex') ])).should.deep.equal({
address: address,
txid: hash,
index: 0,

View File

@ -21,7 +21,7 @@ describe('Mempool Service', function() {
services: []
}
});
mempoolService._encoding = new Encoding(Buffer.from('0000', 'hex'));
mempoolService._encoding = new Encoding(new Buffer('0000', 'hex'));
});
afterEach(function() {
@ -31,7 +31,7 @@ describe('Mempool Service', function() {
describe('#start', function() {
it('should get the db prefix', function(done) {
var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('0001', 'hex'));
var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('0001', 'hex'));
mempoolService._db = { getPrefix: getPrefix };
mempoolService.start(function() {

View File

@ -5,21 +5,21 @@ var Encoding = require('../../../lib/services/timestamp/encoding');
describe('Timestamp service encoding', function() {
var servicePrefix = Buffer.from('0000', 'hex');
var blockPrefix = Buffer.from('00', 'hex');
var timestampPrefix = Buffer.from('01', 'hex');
var servicePrefix = new Buffer('0000', 'hex');
var blockPrefix = new Buffer('00', 'hex');
var timestampPrefix = new Buffer('01', 'hex');
var encoding = new Encoding(servicePrefix);
var blockhash = '00000000000000000115b92b1ff4377441049bff75c6c48b626eb99e8b744297';
var timestamp = 5;
var timestampBuf = Buffer.alloc(4);
var timestampBuf = new Buffer(4);
timestampBuf.writeUInt32BE(timestamp);
it('should encode block timestamp key' , function() {
encoding.encodeBlockTimestampKey(blockhash).should.deep.equal(Buffer.concat([servicePrefix, blockPrefix, Buffer.from(blockhash, 'hex')]));
encoding.encodeBlockTimestampKey(blockhash).should.deep.equal(Buffer.concat([servicePrefix, blockPrefix, new Buffer(blockhash, 'hex')]));
});
it('should decode block timestamp key', function() {
var blockTimestampKey = encoding.decodeBlockTimestampKey(Buffer.concat([servicePrefix, blockPrefix, Buffer.from(blockhash, 'hex')]));
var blockTimestampKey = encoding.decodeBlockTimestampKey(Buffer.concat([servicePrefix, blockPrefix, new Buffer(blockhash, 'hex')]));
blockTimestampKey.should.equal(blockhash);
});
@ -40,10 +40,10 @@ describe('Timestamp service encoding', function() {
});
it('should encode timestamp block value', function() {
encoding.encodeTimestampBlockValue(blockhash).should.deep.equal(Buffer.from(blockhash, 'hex'));
encoding.encodeTimestampBlockValue(blockhash).should.deep.equal(new Buffer(blockhash, 'hex'));
});
it('should decode timestamp block value', function() {
encoding.decodeTimestampBlockValue(Buffer.from(blockhash, 'hex')).should.equal(blockhash);
encoding.decodeTimestampBlockValue(new Buffer(blockhash, 'hex')).should.equal(blockhash);
});
});

View File

@ -6,24 +6,24 @@ var Encoding = require('../../../lib/services/transaction/encoding');
describe('Transaction service encoding', function() {
var servicePrefix = Buffer.from('0000', 'hex');
var servicePrefix = new Buffer('0000', 'hex');
var encoding = new Encoding(servicePrefix);
var txid = '91b58f19b6eecba94ed0f6e463e8e334ec0bcda7880e2985c82a8f32e4d03add';
var blockHash = txid;
var txHex = '0100000001cc3ffe0638792c8b39328bb490caaefe2cf418f2ce0144956e0c22515f29724d010000006a473044022030ce9fa68d1a32abf0cd4adecf90fb998375b64fe887c6987278452b068ae74c022036a7d00d1c8af19e298e04f14294c807ebda51a20389ad751b4ff3c032cf8990012103acfcb348abb526526a9f63214639d79183871311c05b2eebc727adfdd016514fffffffff02f6ae7d04000000001976a9144455183e407ee4d3423858c8a3275918aedcd18e88aca99b9b08010000001976a9140beceae2c29bfde08d2b6d80b33067451c5887be88ac00000000';
var tx = Tx.fromRaw(txHex, 'hex');
var txEncoded = Buffer.concat([Buffer.from('00000002', 'hex'), Buffer.from(blockHash, 'hex'), Buffer.from('00000001', 'hex'), Buffer.from('0002', 'hex'), Buffer.from('40000000000000004008000000000000', 'hex'), tx.toRaw()]);
var indexBuf = Buffer.alloc(4);
var txEncoded = Buffer.concat([new Buffer('00000002', 'hex'), new Buffer(blockHash, 'hex'), new Buffer('00000001', 'hex'), new Buffer('0002', 'hex'), new Buffer('40000000000000004008000000000000', 'hex'), tx.toRaw()]);
var indexBuf = new Buffer(4);
indexBuf.writeUInt32BE(3);
it('should encode transaction key' , function() {
var txBuf = Buffer.from(txid, 'hex');
encoding.encodeTransactionKey(txid).should.deep.equal(Buffer.concat([servicePrefix, Buffer.from('00', 'hex'), txBuf]));
var txBuf = new Buffer(txid, 'hex');
encoding.encodeTransactionKey(txid).should.deep.equal(Buffer.concat([servicePrefix, new Buffer('00', 'hex'), txBuf]));
});
it('should decode transaction key', function() {
encoding.decodeTransactionKey(Buffer.concat([servicePrefix, Buffer.from('00', 'hex'), Buffer.from(txid, 'hex')]))
encoding.decodeTransactionKey(Buffer.concat([servicePrefix, new Buffer('00', 'hex'), new Buffer(txid, 'hex')]))
.should.equal(txid);
});
@ -47,21 +47,21 @@ describe('Transaction service encoding', function() {
it('should encode spent key', function() {
encoding.encodeSpentKey(txid, 3).should.deep.equal(Buffer.concat([servicePrefix,
Buffer.from('01', 'hex'), Buffer.from(txid, 'hex'), indexBuf]));
new Buffer('01', 'hex'), new Buffer(txid, 'hex'), indexBuf]));
});
it('should decode spent key', function() {
encoding.decodeSpentKey(Buffer.concat([servicePrefix,
Buffer.from('01', 'hex'), Buffer.from(txid, 'hex'), indexBuf])).should.deep.equal({ txid: txid, outputIndex: 3 });
new Buffer('01', 'hex'), new Buffer(txid, 'hex'), indexBuf])).should.deep.equal({ txid: txid, outputIndex: 3 });
});
it('should encode spent value', function() {
encoding.encodeSpentValue(txid, 3, 3, txid).should.deep.equal(Buffer.concat([Buffer.from(txid, 'hex'), indexBuf, indexBuf, Buffer.from(blockHash, 'hex')]));
encoding.encodeSpentValue(txid, 3, 3, txid).should.deep.equal(Buffer.concat([new Buffer(txid, 'hex'), indexBuf, indexBuf, new Buffer(blockHash, 'hex')]));
});
it('should decode spent value', function() {
encoding.decodeSpentValue(Buffer.concat([Buffer.from(txid, 'hex'), indexBuf,
indexBuf, Buffer.from(blockHash, 'hex')]))
encoding.decodeSpentValue(Buffer.concat([new Buffer(txid, 'hex'), indexBuf,
indexBuf, new Buffer(blockHash, 'hex')]))
.should.deep.equal({ txid: txid, inputIndex: 3, blockHeight: 3, blockHash: blockHash });
});
});

View File

@ -21,7 +21,7 @@ describe('Transaction Service', function() {
services: []
}
});
txService._encoding = new Encoding(Buffer.from('0000', 'hex'));
txService._encoding = new Encoding(new Buffer('0000', 'hex'));
});
afterEach(function() {
@ -30,7 +30,7 @@ describe('Transaction Service', function() {
describe('#start', function() {
it('should get the prefix and the service tip', function(done) {
var getPrefix = sandbox.stub().callsArgWith(1, null, Buffer.from('ffee', 'hex'));
var getPrefix = sandbox.stub().callsArgWith(1, null, new Buffer('ffee', 'hex'));
txService._db = { getPrefix: getPrefix };
txService.start(function() {
getPrefix.calledOnce.should.be.true;

View File

@ -39,13 +39,13 @@ describe('Utils', function() {
describe('#getTerminalKey', function() {
it('should get the terminal key for a buffer', function() {
utils.getTerminalKey(Buffer.from('ffff', 'hex'))
.should.deep.equal(Buffer.from('010000', 'hex'));
utils.getTerminalKey(new Buffer('ffff', 'hex'))
.should.deep.equal(new Buffer('010000', 'hex'));
});
it('should get the terminal key for a large buffer', function() {
utils.getTerminalKey(Buffer.concat([ Buffer.from(new Array(64).join('f'), 'hex'), Buffer.from('fe', 'hex') ]))
.should.deep.equal(Buffer.from('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'));
utils.getTerminalKey(Buffer.concat([ new Buffer(new Array(64).join('f'), 'hex'), new Buffer('fe', 'hex') ]))
.should.deep.equal(new Buffer('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 'hex'));
});
});
@ -91,8 +91,8 @@ describe('Utils', function() {
it('should encode tip', function() {
var res = utils.encodeTip({ height: 0xdeadbeef, hash: new Array(65).join('0') }, 'test');
res.should.deep.equal({
key: Buffer.from('ffff7469702d74657374', 'hex'),
value: Buffer.from('deadbeef00000000000000000000000000000000000000000000000000000000000000000', 'hex')
key: new Buffer('ffff7469702d74657374', 'hex'),
value: new Buffer('deadbeef00000000000000000000000000000000000000000000000000000000000000000', 'hex')
});
});
});