Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit c6aaecd

Browse files
committed
* Add missing null check in Client._onHeadChanged()
* Fix memory leak on block/tx request timeout * Fix a few warnings
1 parent a0917cd commit c6aaecd

File tree

11 files changed

+37
-32
lines changed

11 files changed

+37
-32
lines changed

clients/nodejs/modules/MetricsServer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class MetricsServer {
141141
async _networkMetrics(res) {
142142
/** @type {Map.<string, number>} */
143143
const peers = new Map();
144-
for (let peer of (await this._client.network.getPeers())) {
145-
let o = {};
144+
for (const peer of (await this._client.network.getPeers())) {
145+
const o = {};
146146
switch (peer.peerAddress ? peer.peerAddress.protocol : -1) {
147147
case Nimiq.Protocol.DUMB:
148148
o.type = 'dumb';
@@ -191,7 +191,7 @@ class MetricsServer {
191191
}
192192
/** @type {Map.<string, number>} */
193193
const addresses = new Map();
194-
for (let address of (await this._client.network.getAddresses())) {
194+
for (const address of (await this._client.network.getAddresses())) {
195195
let type = 'unknown';
196196
switch (address.peerAddress.protocol) {
197197
case Nimiq.Protocol.DUMB:
@@ -213,10 +213,10 @@ class MetricsServer {
213213
}
214214
}
215215

216-
for (let os of peers.keys()) {
216+
for (const os of peers.keys()) {
217217
MetricsServer._metric(res, 'network_peers', this._with(JSON.parse(os)), peers.get(os));
218218
}
219-
for (let type of addresses.keys()) {
219+
for (const type of addresses.keys()) {
220220
MetricsServer._metric(res, 'network_known_addresses', this._with({type: type}), addresses.get(type));
221221
}
222222
const stats = await this._client.network.getStatistics();

src/main/generic/api/Client.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,13 @@ class Client {
266266
if (block.isFull()) {
267267
revertedTxs.addAll(block.transactions);
268268
}
269-
// FIXME
270-
for (const tx of this._transactionConfirmWaiting.get(this._txConfirmsAt(block.height)).valueIterator()) {
271-
revertedTxs.add(tx);
269+
const set = this._transactionConfirmWaiting.get(this._txConfirmsAt(block.height));
270+
if (set) {
271+
for (const tx of set.valueIterator()) {
272+
revertedTxs.add(tx);
273+
}
274+
this._transactionConfirmWaiting.remove(this._txConfirmsAt(block.height));
272275
}
273-
this._transactionConfirmWaiting.remove(this._txConfirmsAt(block.height));
274276
}
275277

276278
// Gather applied transactions

src/main/generic/api/NetworkClient.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Client.Network = class Network {
1414
async getPeers() {
1515
const consensus = await this._client._consensus;
1616
const infos = [];
17-
for (let connection of consensus.network.connections.valueIterator()) {
17+
for (const connection of consensus.network.connections.valueIterator()) {
1818
infos.push(new Client.PeerInfo(connection));
1919
}
2020
return infos;
@@ -39,7 +39,7 @@ Client.Network = class Network {
3939
async getAddresses() {
4040
const consensus = await this._client._consensus;
4141
const infos = [];
42-
for (let addressState of consensus.network.addresses.iterator()) {
42+
for (const addressState of consensus.network.addresses.iterator()) {
4343
infos.push(new Client.AddressInfo(addressState));
4444
}
4545
return infos;
@@ -129,7 +129,7 @@ Client.Network = class Network {
129129
} else if (address instanceof Client.BasicAddress) {
130130
peerAddress = consensus.network.addresses.get(address.peerAddress);
131131
} else if (typeof address === 'string') {
132-
for (let peerAddressState of consensus.network.addresses.iterator()) {
132+
for (const peerAddressState of consensus.network.addresses.iterator()) {
133133
if (peerAddressState.peerAddress.toString() === address) {
134134
peerAddress = peerAddressState.peerAddress;
135135
break;

src/main/generic/consensus/BaseConsensusAgent.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,16 @@ class BaseConsensusAgent extends Observable {
243243
this._pendingRequests.get(vector).push({resolve, reject});
244244
} else {
245245
this._pendingRequests.put(vector, [{resolve, reject}]);
246+
246247
this._peer.channel.getData([vector]);
248+
247249
this._timers.setTimeout(`block-request-${vector.hash.toBase64()}`, () => {
248-
if (!this._pendingRequests.get(vector)) return;
249-
for (const {reject} of this._pendingRequests.get(vector)) {
250+
const requests = this._pendingRequests.get(vector);
251+
if (!requests) return;
252+
253+
this._pendingRequests.remove(vector);
254+
255+
for (const {reject} of requests) {
250256
reject(new Error('Timeout'));
251257
}
252258
}, BaseConsensusAgent.REQUEST_TIMEOUT);
@@ -265,12 +271,19 @@ class BaseConsensusAgent extends Observable {
265271
this._pendingRequests.get(vector).push({resolve, reject});
266272
} else {
267273
this._pendingRequests.put(vector, [{resolve, reject}]);
274+
268275
if (!this._objectsInFlight.contains(vector)) {
269276
this._peer.channel.getData([vector]);
270277
this._objectsInFlight.add(vector);
271278
}
279+
272280
this._timers.setTimeout(`tx-request-${vector.hash.toBase64()}`, () => {
273-
for (const {reject} of this._pendingRequests.get(vector)) {
281+
const requests = this._pendingRequests.get(vector);
282+
if (!requests) return;
283+
284+
this._pendingRequests.remove(vector);
285+
286+
for (const {reject} of requests) {
274287
reject(new Error('Timeout'));
275288
}
276289
}, BaseConsensusAgent.REQUEST_TIMEOUT);
@@ -1518,7 +1531,7 @@ BaseConsensusAgent.TRANSACTION_RELAY_FEE_MIN = 1;
15181531
*/
15191532
BaseConsensusAgent.SUBSCRIPTION_CHANGE_GRACE_PERIOD = 1000 * 2;
15201533
BaseConsensusAgent.HEAD_REQUEST_INTERVAL = 100 * 1000; // 100 seconds, give client time to announce new head without request
1521-
BaseConsensusAgent.KNOWS_OBJECT_AFTER_INV_DELAY = 1000 * 5;
1534+
BaseConsensusAgent.KNOWS_OBJECT_AFTER_INV_DELAY = 1000 * 3;
15221535

15231536
BaseConsensusAgent.KNOWN_OBJECTS_COUNT_MAX = 40000;
15241537
Class.register(BaseConsensusAgent);

src/main/generic/consensus/BaseMiniConsensus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class BaseMiniConsensus extends BaseConsensus {
132132
// Try agents first that (we think) know the block hash.
133133
agents.sort((a, b) =>
134134
a.knowsBlock(blockHash) !== b.knowsBlock(blockHash)
135-
? -a.knowsBlock(hash) + 0.5
135+
? -a.knowsBlock(blockHash) + 0.5
136136
: Math.random() - 0.5);
137137

138138
for (const agent of agents) {

src/main/generic/consensus/Policy.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ Policy.TOTAL_SUPPLY = 21e14;
178178

179179
/**
180180
* Initial supply before genesis block in lunas.
181-
* FIXME: Change for main net.
182181
* @type {number}
183182
* @constant
184183
*/

src/main/generic/consensus/base/block/BlockHeader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ class BlockHeader {
238238
this._pow = null;
239239
}
240240
}
241-
// FIXME: Clean up for mainnet.
242241
BlockHeader.Version = {
243242
V1: 1
244243
};

src/main/generic/consensus/base/blockchain/BaseChain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class BaseChain extends IBlockchain {
572572
promises.push(worker.computeArgon2dBatch(part));
573573
}
574574
const pows = (await Promise.all(promises)).reduce((a, b) => [...a, ...b], []);
575-
for(let i = 0; i < headers.length; ++i) {
575+
for (let i = 0; i < headers.length; ++i) {
576576
headers[i]._pow = new Hash(pows[i]);
577577
}
578578
}

src/main/generic/consensus/nano/NanoConsensusAgent.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ class NanoConsensusAgent extends BaseMiniConsensusAgent {
9494
async _onChainProof(msg) {
9595
Log.d(NanoConsensusAgent, `[CHAIN-PROOF] Received from ${this._peer.peerAddress}: ${msg.proof}`);
9696

97-
// Check if we have requested a chain proof, reject unsolicited ones.
98-
// FIXME
97+
// Check if we have requested a chain proof, discard unsolicited ones.
9998
if (!this._requestedChainProof) {
10099
Log.w(NanoConsensusAgent, `Unsolicited chain proof received from ${this._peer.peerAddress}`);
101-
// TODO close/ban?
102100
return;
103101
}
104102
this._requestedChainProof = false;

src/test/specs/generic/Policy.spec.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,17 @@ describe('Policy', () => {
2222
});
2323

2424
it('reaches total supply after ~100 years', () => {
25-
// FIXME: replace initialSupply by Policy.INITIAL_SUPPLY for main net
26-
const initialSupply = 252e12;
27-
2825
// Should reach 21mio NIM at block 52888984.
2926
const block = 101*365*24*60*60 / Policy.BLOCK_TIME; // 101 years
30-
const supply = Policy._supplyAfter(initialSupply, block-1);
27+
const supply = Policy._supplyAfter(Policy.INITIAL_SUPPLY, block - 1);
3128
expect(supply).toBe(Policy.TOTAL_SUPPLY, 'Wrong supply.');
3229
expect(Policy._blockRewardAt(supply, block)).toBe(0, 'Wrong reward.');
3330
});
3431

3532
it('correctly switches to tail emission', () => {
36-
// FIXME: replace initialSupply by Policy.INITIAL_SUPPLY for main net
37-
const initialSupply = 252e12;
38-
3933
// Should reach 21mio NIM at block 52888984.
4034
const block = Policy.EMISSION_TAIL_START;
41-
const supply = Policy._supplyAfter(initialSupply, block-2);
35+
const supply = Policy._supplyAfter(Policy.INITIAL_SUPPLY, block - 2);
4236
const previousReward = Policy._blockRewardAt(supply, block-1);
4337
expect(previousReward > Policy.EMISSION_TAIL_REWARD).toBe(true, 'Wrong reward before starting block.');
4438
expect(Policy._blockRewardAt(supply + previousReward, block)).toBe(Policy.EMISSION_TAIL_REWARD, 'Wrong reward at starting block.');

src/test/specs/generic/api/Client.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Client', () => {
2121
const netconfig = Dummy.NETCONFIG;
2222
const consensus = await Consensus.volatileFull(netconfig);
2323
testChain = await TestBlockchain.createVolatileTest(numBlocks);
24-
for (let block of (await testChain.getBlocks(consensus.blockchain.headHash))) {
24+
for (const block of (await testChain.getBlocks(consensus.blockchain.headHash))) {
2525
await consensus.blockchain.pushBlock(await testChain.getBlock(block.hash(), true, true));
2626
}
2727
consensus.network.connect();

0 commit comments

Comments
 (0)