Skip to content

Commit

Permalink
Merge pull request bitpay#1867 from micahriggan/fix/syncing-node-issue
Browse files Browse the repository at this point in the history
fix(node): config should use merge, findOneAndUpdate should return new
  • Loading branch information
micahriggan authored Jan 7, 2019
2 parents ea0bc2c + f9447a1 commit 71b6c08
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
3 changes: 2 additions & 1 deletion packages/bitcore-node/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { homedir, cpus } from 'os';
import parseArgv from './utils/parseArgv';
import { ConfigType } from './types/Config';
import * as _ from 'lodash';
let program = parseArgv([], ['config']);

function findConfig(): ConfigType | undefined {
Expand Down Expand Up @@ -76,7 +77,7 @@ const Config = function(): ConfigType {
};

let foundConfig = findConfig();
Object.assign(config, foundConfig, {});
config = _.merge(config, foundConfig, {});
if (!Object.keys(config.chains).length) {
Object.assign(config.chains, {
BTC: {
Expand Down
24 changes: 11 additions & 13 deletions packages/bitcore-node/src/models/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { IBlock } from '../types/Block';
import { SpentHeightIndicators } from '../types/Coin';
import { EventStorage } from './events';
import config from '../config';
import { Event } from '../services/event';
import { StorageService } from "../services/storage";
import { StorageService } from '../services/storage';

export { IBlock };

Expand Down Expand Up @@ -47,17 +46,6 @@ export class BlockModel extends BaseModel<IBlock> {
}
}
}

Event.blockStream.on('data', (block: IBlock) => {
if (block) {
const { chain, network, height } = block;
this.chainTips[chain] = valueOrDefault(this.chainTips[chain], {});
this.chainTips[chain][network] = valueOrDefault(this.chainTips[chain][network], block);
if (this.chainTips[chain][network].height < height) {
this.chainTips[chain][network] = block;
}
}
});
}

async addBlock(params: {
Expand Down Expand Up @@ -143,9 +131,19 @@ export class BlockModel extends BaseModel<IBlock> {
EventStorage.signalBlock(convertedBlock);
}

this.updateCachedChainTip({ block: convertedBlock, height, chain, network });
return this.collection.updateOne({ hash: header.hash, chain, network }, { $set: { processed: true } });
}

updateCachedChainTip(params: { block; chain; network; height }) {
const { chain, network, block, height } = params;
this.chainTips[chain] = valueOrDefault(this.chainTips[chain], {});
this.chainTips[chain][network] = valueOrDefault(this.chainTips[chain][network], block);
if (this.chainTips[chain][network].height < height) {
this.chainTips[chain][network] = block;
}
}

getPoolInfo(coinbase: string) {
//TODO need to make this actually parse the coinbase input and map to miner strings
// also should go somewhere else
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-node/src/models/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class StateModel extends BaseModel<IState> {
return this.collection.findOneAndUpdate(
{},
{ $setOnInsert: { created: new Date()}},
{ upsert: true }
{ upsert: true, returnOriginal: false }
);
}

Expand Down
23 changes: 13 additions & 10 deletions packages/bitcore-node/test/unit/models/block.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { ObjectID } from 'mongodb';
import { MongoBound } from '../../../src/models/base';

describe('Block Model', function() {
let addBlockParams = {
chain: 'BTC',
network: 'regtest',
block: TEST_BLOCK,
height: 1355,
initialSyncComplete: false
};

describe('addBlock', () => {
let addBlockParams = {
chain: 'BTC',
network: 'regtest',
block: TEST_BLOCK,
height: 1355,
initialSyncComplete: false
};
let sandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
Expand Down Expand Up @@ -58,7 +59,7 @@ describe('Block Model', function() {
const { query, options } = Storage.getFindOptions<MongoBound<IBlock>>(BlockStorage, {
since: id,
paging: '_id',
limit: 100,
limit: 100
});
expect(options.sort).to.be.deep.eq({ _id: -1 });
expect(options.limit).to.be.eq(100);
Expand Down Expand Up @@ -87,11 +88,13 @@ describe('Block Model', function() {
afterEach(() => {
sandbox.restore();
});
it('should return null if there are no blocks', async () => {
it('should return the new tip', async () => {
mockStorage(null);
const params = { chain: 'BTC', network: 'regtest' };
const result = await ChainStateProvider.getLocalTip(params);
expect(result).to.deep.equal(null);
expect(result.height).to.deep.equal(addBlockParams.height + 1);
expect(result.chain).to.deep.equal(addBlockParams.chain);
expect(result.network).to.deep.equal(addBlockParams.network);
});
});

Expand Down

0 comments on commit 71b6c08

Please sign in to comment.