Skip to content

Commit 658fd62

Browse files
authored
Fix ambrosus node could fail in certain scenarios (#449)
* Revert "Fix memory leak (#447)" * Update package version * Use updated wrappers from ambrosus-node-contracts * Fix tests
1 parent 4ff503f commit 658fd62

File tree

11 files changed

+25
-50
lines changed

11 files changed

+25
-50
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ambrosus-node",
33
"description": "",
4-
"version": "1.1.16",
4+
"version": "1.1.17",
55
"author": "Ambrosus",
66
"repository": "[email protected]:ambrosus/ambrosus-node.git",
77
"private": false,
@@ -34,7 +34,7 @@
3434
"dependencies": {
3535
"@sentry/node": "^6.7.1",
3636
"ajv": "^8.6.0",
37-
"ambrosus-node-contracts": "^0.0.83",
37+
"ambrosus-node-contracts": "^0.0.88",
3838
"base64url": "^3.0.1",
3939
"bn.js": "^5.2.0",
4040
"body-parser": "^1.19.0",

src/builder.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,6 @@ class Builder {
215215
bundleStoreWrapper: this.bundleStoreWrapper,
216216
shelteringWrapper: this.shelteringWrapper
217217
});
218-
219-
if (dependencies !== 'testmode') {
220-
this.contracts = {
221-
bundleStoreWrapperContract: await this.bundleStoreWrapper.contract(),
222-
shelteringWrapperContract: await this.shelteringWrapper.contract()
223-
};
224-
}
225-
226218
return {dataModelEngine: this.dataModelEngine, client: this.client, kycWhitelistWrapper: this.kycWhitelistWrapper};
227219
}
228220
}

src/services/bundles_restorer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v.
77
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0.
88
*/
99

10-
import Builder from '../builder';
11-
1210
export default class BundlesRestorer {
1311
constructor(bundleStoreWrapper, shelteringWrapper, shelteringTransfersWrapper, dataModelEngine, bundleRepository, shelteredBundlesRepository, workerLogger) {
1412
this.bundleStoreWrapper = bundleStoreWrapper;
@@ -75,13 +73,13 @@ export default class BundlesRestorer {
7573
}
7674

7775
async getBundleDonors(bundle) {
78-
const shelterers = await Builder.contracts.bundleStoreWrapperContract.methods.getShelterers(bundle.bundleId).call();
76+
const shelterers = await this.bundleStoreWrapper.getShelterers(bundle.bundleId);
7977
let pos = shelterers.indexOf(bundle.shelterer);
8078
while (-1 !== pos) {
8179
shelterers.splice(pos, 1);
8280
pos = shelterers.indexOf(bundle.shelterer);
8381
}
84-
shelterers.push(await Builder.contracts.bundleStoreWrapperContract.methods.getUploader(bundle.bundleId).call());
82+
shelterers.push(await this.bundleStoreWrapper.getUploader(bundle.bundleId));
8583
return shelterers;
8684
}
8785
}

src/services/bundles_restorer_hermes.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v.
77
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0.
88
*/
99

10-
import Builder from '../builder';
11-
1210
export default class BundlesRestorerHermes {
1311
constructor(
1412
bundleStoreWrapper,
@@ -139,7 +137,7 @@ export default class BundlesRestorerHermes {
139137
}
140138

141139
async getBundleDonors(bundle) {
142-
const shelterers = await Builder.contracts.bundleStoreWrapperContract.methods.getShelterers(bundle.bundleId).call();
140+
const shelterers = await this.bundleStoreWrapper.getShelterers(bundle.bundleId);
143141
let pos = shelterers.indexOf(bundle.shelterer);
144142

145143
while (-1 !== pos) {

src/services/data_model_engine.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {pick, put} from '../utils/dict_utils';
1313
import allPermissions from '../utils/all_permissions';
1414
import BundleStatuses from '../utils/bundle_statuses';
1515
import removeDuplicates from '../utils/sutils.js';
16-
import Builder from '../builder';
1716

1817
export default class DataModelEngine {
1918
constructor(
@@ -436,15 +435,15 @@ export default class DataModelEngine {
436435
}
437436

438437
async getBundleDonors(bundleId, nodeId = null) {
439-
const donors = await Builder.contracts.bundleStoreWrapperContract.methods.getShelterers(bundleId).call();
438+
const donors = await this.bundleStoreWrapper.getShelterers(bundleId);
440439
if (nodeId) {
441440
let pos = donors.indexOf(nodeId);
442441
while (-1 !== pos) {
443442
donors.splice(pos, 1);
444443
pos = donors.indexOf(nodeId);
445444
}
446445
}
447-
const uploaderId = await Builder.contracts.bundleStoreWrapperContract.methods.getUploader(bundleId).call();
446+
const uploaderId = await this.bundleStoreWrapper.getUploader(bundleId);
448447
if (uploaderId !== nodeId) {
449448
donors.push(uploaderId);
450449
}

src/workers/atlas_resolvers/atlas_challenge_resolver.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
1010
import promClient from 'prom-client';
1111
import BundleShelteringResolver from './bundle_sheltering_resolver';
1212
import {atlasResolutionStatus} from './atlas_resolver';
13-
import Builder from '../../builder';
1413

1514
export default class AtlasChallengeResolver extends BundleShelteringResolver {
1615
constructor(
@@ -83,13 +82,13 @@ export default class AtlasChallengeResolver extends BundleShelteringResolver {
8382
}
8483

8584
async getBundleDonors(proposition) {
86-
const shelterers = await Builder.contracts.bundleStoreWrapperContract.methods.getShelterers(proposition.bundleId).call();
85+
const shelterers = await this.bundleStoreWrapper.getShelterers(proposition.bundleId);
8786
let pos = shelterers.indexOf(proposition.sheltererId);
8887
while (-1 !== pos) {
8988
shelterers.splice(pos, 1);
9089
pos = shelterers.indexOf(proposition.sheltererId);
9190
}
92-
shelterers.push(await Builder.contracts.bundleStoreWrapperContract.methods.getUploader(proposition.bundleId).call());
91+
shelterers.push(await this.bundleStoreWrapper.getUploader(proposition.bundleId));
9392
return shelterers;
9493
}
9594
}

src/workers/atlas_resolvers/atlas_transfer_resolver.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ This Source Code Form is “Incompatible With Secondary Licenses”, as defined
1010
import promClient from 'prom-client';
1111
import BundleShelteringResolver from './bundle_sheltering_resolver';
1212
import {atlasResolutionStatus} from './atlas_resolver';
13-
import Builder from '../../builder';
1413

1514
export default class AtlaTransferResolver extends BundleShelteringResolver {
1615
constructor(
@@ -83,13 +82,13 @@ export default class AtlaTransferResolver extends BundleShelteringResolver {
8382
}
8483

8584
async getBundleDonors(proposition) {
86-
const shelterers = await Builder.contracts.bundleStoreWrapperContract.methods.getShelterers(proposition.bundleId).call();
85+
const shelterers = await this.bundleStoreWrapper.getShelterers(proposition.bundleId);
8786
let pos = shelterers.indexOf(proposition.donorId);
8887
while (-1 !== pos) {
8988
shelterers.splice(pos, 1);
9089
pos = shelterers.indexOf(proposition.donorId);
9190
}
92-
shelterers.push(await Builder.contracts.bundleStoreWrapperContract.methods.getUploader(proposition.bundleId).call());
91+
shelterers.push(await this.bundleStoreWrapper.getUploader(proposition.bundleId));
9392
return shelterers;
9493
}
9594
}

src/workers/validator_worker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v.
77
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0.
88
*/
99
import PeriodicWorker from './periodic_worker';
10-
import Builder from '../builder';
1110

1211
const BUNDLES_VERIFY_WORK_TYPE = 'BundlesVerify';
1312
const STORAGE_PERIOD_DURATION = 13 * 28 * 86400; // in seconds
@@ -32,6 +31,7 @@ export default class HermesBundlesValidatorWorker extends PeriodicWorker {
3231
}
3332
try {
3433
this.logInfo(`Validation start`);
34+
this.shelteringContract = await this.shelteringWrapper.contract();
3535

3636
const hermresBundles = await this.bundleRepository.getHermesBundles(0);
3737
this.logInfo(`Hermes bundles count ${hermresBundles.length}`);
@@ -41,7 +41,7 @@ export default class HermesBundlesValidatorWorker extends PeriodicWorker {
4141
if (this.now() > expirationTime) {
4242
continue; // skip expired bundles
4343
}
44-
const shelterers = await Builder.contracts.bundleStoreWrapperContract.methods.getShelterers(bundleId).call();
44+
const shelterers = await this.bundleStoreWrapper.getShelterers(bundleId);
4545
if (shelterers.length === 0) {
4646
this.logInfo(`No shelterers: ${bundleId}`);
4747
}
@@ -59,7 +59,7 @@ export default class HermesBundlesValidatorWorker extends PeriodicWorker {
5959

6060
async validateAndRestoreBundle(bundleId, shelterer) {
6161
try {
62-
const sheltererExpirationTime = await Builder.contracts.shelteringWrapperContract.methods.getShelteringExpirationDate(bundleId, shelterer).call();
62+
const sheltererExpirationTime = await this.shelteringContract.methods.getShelteringExpirationDate(bundleId, shelterer).call();
6363
if (this.now() > sheltererExpirationTime) {
6464
throw new Error('Bundle expired'); // skip expired bundles (when sheltererExpirationTime < expirationTime)
6565
}

test/integration/workers/cleanup_worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Cleanup worker – integration', () => {
2929

3030
before(async () => {
3131
builder = new Builder();
32-
await builder.build({...config, headContractAddress: '0x0000000000000000000000000000000000000F10'}, 'testmode');
32+
await builder.build({...config, headContractAddress: '0x0000000000000000000000000000000000000F10'});
3333
});
3434

3535
beforeEach(async () => {

test/services/bundles_restorer.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,13 @@ describe('Bundles Restorer', () => {
2929
let dataModelEngineMock;
3030
let shelteredBundlesRepositoryMock;
3131
let workerLoggerMock;
32-
let bundleStoreContractMock;
3332

3433
const timestamp = 1500000000;
3534

3635
beforeEach(async () => {
37-
getShelterersCall = {
38-
call: sinon.stub().resolves(['donor1', 'donor2', 'donor3', 'shelterer'])
39-
};
40-
getUploaderCall = {
41-
call: sinon.stub().resolves('uploader')
42-
};
43-
bundleStoreContractMock = {
44-
methods: {
45-
getShelterers: sinon.stub().returns(getShelterersCall),
46-
getUploader: sinon.stub().returns(getUploaderCall)
47-
}
48-
};
4936
bundleStoreWrapperMock = {
50-
contract: sinon.stub().resolves(bundleStoreContractMock)
37+
getShelterers: sinon.stub().returns(['donor1', 'donor2', 'donor3', 'shelterer']),
38+
getUploader: sinon.stub().returns('uploader')
5139
};
5240
shelteringWrapperMock = {
5341
isSheltering: sinon.stub().resolves(true),
@@ -87,10 +75,10 @@ describe('Bundles Restorer', () => {
8775
expect(dataModelEngineMock.markBundleAsSheltered).to.not.have.been.called;
8876
});
8977

90-
// it('get bundle donors works', async () => {
91-
// sinon.spy(restorer, 'getBundleDonors');
92-
// expect(await restorer.getBundleDonors({bundleId: 1, shelterer: 'shelterer'})).to.deep.equal(['donor1', 'donor2', 'donor3', 'uploader']);
93-
// });
78+
it('get bundle donors works', async () => {
79+
sinon.spy(restorer, 'getBundleDonors');
80+
expect(await restorer.getBundleDonors({bundleId: 1, shelterer: 'shelterer'})).to.deep.equal(['donor1', 'donor2', 'donor3', 'uploader']);
81+
});
9482

9583
it('restore works', async () => {
9684
const shelterer = 'shelterer';

0 commit comments

Comments
 (0)