Skip to content

Commit 71b6c08

Browse files
authored
Merge pull request bitpay#1867 from micahriggan/fix/syncing-node-issue
fix(node): config should use merge, findOneAndUpdate should return new
2 parents ea0bc2c + f9447a1 commit 71b6c08

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

packages/bitcore-node/src/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { homedir, cpus } from 'os';
22
import parseArgv from './utils/parseArgv';
33
import { ConfigType } from './types/Config';
4+
import * as _ from 'lodash';
45
let program = parseArgv([], ['config']);
56

67
function findConfig(): ConfigType | undefined {
@@ -76,7 +77,7 @@ const Config = function(): ConfigType {
7677
};
7778

7879
let foundConfig = findConfig();
79-
Object.assign(config, foundConfig, {});
80+
config = _.merge(config, foundConfig, {});
8081
if (!Object.keys(config.chains).length) {
8182
Object.assign(config.chains, {
8283
BTC: {

packages/bitcore-node/src/models/block.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { IBlock } from '../types/Block';
1010
import { SpentHeightIndicators } from '../types/Coin';
1111
import { EventStorage } from './events';
1212
import config from '../config';
13-
import { Event } from '../services/event';
14-
import { StorageService } from "../services/storage";
13+
import { StorageService } from '../services/storage';
1514

1615
export { IBlock };
1716

@@ -47,17 +46,6 @@ export class BlockModel extends BaseModel<IBlock> {
4746
}
4847
}
4948
}
50-
51-
Event.blockStream.on('data', (block: IBlock) => {
52-
if (block) {
53-
const { chain, network, height } = block;
54-
this.chainTips[chain] = valueOrDefault(this.chainTips[chain], {});
55-
this.chainTips[chain][network] = valueOrDefault(this.chainTips[chain][network], block);
56-
if (this.chainTips[chain][network].height < height) {
57-
this.chainTips[chain][network] = block;
58-
}
59-
}
60-
});
6149
}
6250

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

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

138+
updateCachedChainTip(params: { block; chain; network; height }) {
139+
const { chain, network, block, height } = params;
140+
this.chainTips[chain] = valueOrDefault(this.chainTips[chain], {});
141+
this.chainTips[chain][network] = valueOrDefault(this.chainTips[chain][network], block);
142+
if (this.chainTips[chain][network].height < height) {
143+
this.chainTips[chain][network] = block;
144+
}
145+
}
146+
149147
getPoolInfo(coinbase: string) {
150148
//TODO need to make this actually parse the coinbase input and map to miner strings
151149
// also should go somewhere else

packages/bitcore-node/src/models/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class StateModel extends BaseModel<IState> {
2020
return this.collection.findOneAndUpdate(
2121
{},
2222
{ $setOnInsert: { created: new Date()}},
23-
{ upsert: true }
23+
{ upsert: true, returnOriginal: false }
2424
);
2525
}
2626

packages/bitcore-node/test/unit/models/block.unit.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import { ObjectID } from 'mongodb';
1212
import { MongoBound } from '../../../src/models/base';
1313

1414
describe('Block Model', function() {
15+
let addBlockParams = {
16+
chain: 'BTC',
17+
network: 'regtest',
18+
block: TEST_BLOCK,
19+
height: 1355,
20+
initialSyncComplete: false
21+
};
22+
1523
describe('addBlock', () => {
16-
let addBlockParams = {
17-
chain: 'BTC',
18-
network: 'regtest',
19-
block: TEST_BLOCK,
20-
height: 1355,
21-
initialSyncComplete: false
22-
};
2324
let sandbox;
2425
beforeEach(() => {
2526
sandbox = sinon.sandbox.create();
@@ -58,7 +59,7 @@ describe('Block Model', function() {
5859
const { query, options } = Storage.getFindOptions<MongoBound<IBlock>>(BlockStorage, {
5960
since: id,
6061
paging: '_id',
61-
limit: 100,
62+
limit: 100
6263
});
6364
expect(options.sort).to.be.deep.eq({ _id: -1 });
6465
expect(options.limit).to.be.eq(100);
@@ -87,11 +88,13 @@ describe('Block Model', function() {
8788
afterEach(() => {
8889
sandbox.restore();
8990
});
90-
it('should return null if there are no blocks', async () => {
91+
it('should return the new tip', async () => {
9192
mockStorage(null);
9293
const params = { chain: 'BTC', network: 'regtest' };
9394
const result = await ChainStateProvider.getLocalTip(params);
94-
expect(result).to.deep.equal(null);
95+
expect(result.height).to.deep.equal(addBlockParams.height + 1);
96+
expect(result.chain).to.deep.equal(addBlockParams.chain);
97+
expect(result.network).to.deep.equal(addBlockParams.network);
9598
});
9699
});
97100

0 commit comments

Comments
 (0)