Skip to content

Commit 5e97684

Browse files
authored
Merge pull request #4082 from OriginTrail/v8/develop
8.2.5 Prerelease Testnet
2 parents 2bd0cfa + 4e25e00 commit 5e97684

File tree

6 files changed

+182
-4
lines changed

6 files changed

+182
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "origintrail_node",
3-
"version": "8.2.4",
3+
"version": "8.2.5",
44
"description": "OTNode V8",
55
"main": "index.js",
66
"type": "module",

src/controllers/http-api/v1/publish-http-api-controller-v1.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
OPERATION_STATUS,
66
LOCAL_STORE_TYPES,
77
COMMAND_PRIORITY,
8+
PUBLISH_MIN_NUM_OF_NODE_REPLICATIONS,
89
} from '../../../constants/constants.js';
910

1011
class PublishController extends BaseController {
@@ -16,6 +17,7 @@ class PublishController extends BaseController {
1617
this.repositoryModuleManager = ctx.repositoryModuleManager;
1718
this.pendingStorageService = ctx.pendingStorageService;
1819
this.networkModuleManager = ctx.networkModuleManager;
20+
this.blockchainModuleManager = ctx.blockchainModuleManager;
1921
}
2022

2123
async handleRequest(req, res) {
@@ -62,6 +64,37 @@ class PublishController extends BaseController {
6264
datasetRoot,
6365
});
6466

67+
let effectiveMinReplications = minimumNumberOfNodeReplications;
68+
let chainMinNumber = null;
69+
try {
70+
const chainMin = await this.blockchainModuleManager.getMinimumRequiredSignatures(
71+
blockchain,
72+
);
73+
chainMinNumber = Number(chainMin);
74+
} catch (err) {
75+
this.logger.warn(
76+
`Failed to fetch on-chain minimumRequiredSignatures for ${blockchain}: ${err.message}`,
77+
);
78+
}
79+
80+
const userMinNumber = Number(effectiveMinReplications);
81+
const resolvedUserMin =
82+
!Number.isNaN(userMinNumber) && userMinNumber > 0
83+
? userMinNumber
84+
: PUBLISH_MIN_NUM_OF_NODE_REPLICATIONS;
85+
86+
if (!Number.isNaN(chainMinNumber) && chainMinNumber > 0) {
87+
effectiveMinReplications = Math.max(chainMinNumber, resolvedUserMin);
88+
} else {
89+
effectiveMinReplications = resolvedUserMin;
90+
}
91+
92+
if (effectiveMinReplications === 0) {
93+
this.logger.error(
94+
`Effective minimum replications resolved to 0 for operationId: ${operationId}, blockchain: ${blockchain}. This should never happen.`,
95+
);
96+
}
97+
6598
const publisherNodePeerId = this.networkModuleManager.getPeerId().toB58String();
6699
await this.pendingStorageService.cacheDataset(
67100
operationId,
@@ -80,7 +113,7 @@ class PublishController extends BaseController {
80113
blockchain,
81114
operationId,
82115
storeType: LOCAL_STORE_TYPES.TRIPLE,
83-
minimumNumberOfNodeReplications,
116+
minimumNumberOfNodeReplications: effectiveMinReplications,
84117
},
85118
transactional: false,
86119
priority: COMMAND_PRIORITY.HIGHEST,

src/modules/blockchain/blockchain-module-manager.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ class BlockchainModuleManager extends BaseModuleManager {
211211
return this.callImplementationFunction(blockchain, 'getMaximumStake');
212212
}
213213

214+
async getMinimumRequiredSignatures(blockchain) {
215+
return this.callImplementationFunction(blockchain, 'getMinimumRequiredSignatures');
216+
}
217+
214218
async getLatestBlock(blockchain) {
215219
return this.callImplementationFunction(blockchain, 'getLatestBlock');
216220
}

src/modules/blockchain/implementation/web3-service.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,15 @@ class Web3Service {
10241024
return Number(ethers.utils.formatEther(maximumStake));
10251025
}
10261026

1027+
async getMinimumRequiredSignatures() {
1028+
return this.callContractFunction(
1029+
this.contracts.ParametersStorage,
1030+
'minimumRequiredSignatures',
1031+
[],
1032+
CONTRACTS.PARAMETERS_STORAGE,
1033+
);
1034+
}
1035+
10271036
async getShardingTableHead() {
10281037
return this.callContractFunction(this.contracts.ShardingTableStorage, 'head', []);
10291038
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import { describe, it } from 'mocha';
2+
import { expect } from 'chai';
3+
4+
import PublishController from '../../../src/controllers/http-api/v1/publish-http-api-controller-v1.js';
5+
import { PUBLISH_MIN_NUM_OF_NODE_REPLICATIONS } from '../../../src/constants/constants.js';
6+
7+
const createRes = () => {
8+
const res = {
9+
statusCode: null,
10+
body: null,
11+
status(code) {
12+
this.statusCode = code;
13+
return this;
14+
},
15+
json(payload) {
16+
this.body = payload;
17+
return this;
18+
},
19+
send(payload) {
20+
this.body = payload;
21+
return this;
22+
},
23+
};
24+
return res;
25+
};
26+
27+
describe('publish-http-api-controller-v1', () => {
28+
const baseCtx = () => {
29+
const addedCommands = [];
30+
return {
31+
commandExecutor: {
32+
add: async (cmd) => {
33+
addedCommands.push(cmd);
34+
},
35+
_added: addedCommands,
36+
},
37+
publishService: {
38+
getOperationName: () => 'publish',
39+
},
40+
operationIdService: {
41+
generateOperationId: async () => 'op-id-123',
42+
emitChangeEvent: () => {},
43+
updateOperationIdStatus: async () => {},
44+
cacheOperationIdDataToMemory: async () => {},
45+
cacheOperationIdDataToFile: async () => {},
46+
},
47+
repositoryModuleManager: {
48+
createOperationRecord: async () => {},
49+
},
50+
pendingStorageService: {
51+
cacheDataset: async () => {},
52+
},
53+
networkModuleManager: {
54+
getPeerId: () => ({ toB58String: () => 'peer-self' }),
55+
},
56+
blockchainModuleManager: {
57+
getMinimumRequiredSignatures: async () => PUBLISH_MIN_NUM_OF_NODE_REPLICATIONS,
58+
},
59+
logger: {
60+
info: () => {},
61+
warn: () => {},
62+
error: () => {},
63+
},
64+
};
65+
};
66+
67+
it('clamps minimumNumberOfNodeReplications to on-chain minimum', async () => {
68+
const ctx = baseCtx();
69+
ctx.blockchainModuleManager.getMinimumRequiredSignatures = async () => 5; // on-chain min
70+
const controller = new PublishController(ctx);
71+
72+
const req = {
73+
body: {
74+
dataset: { public: {} },
75+
datasetRoot: '0xroot',
76+
blockchain: 'hardhat',
77+
minimumNumberOfNodeReplications: 2, // below chain min
78+
},
79+
};
80+
const res = createRes();
81+
82+
await controller.handleRequest(req, res);
83+
84+
expect(res.statusCode).to.equal(202);
85+
const added = ctx.commandExecutor._added[0];
86+
expect(added.data.minimumNumberOfNodeReplications).to.equal(5);
87+
});
88+
89+
it('allows higher user override than on-chain minimum', async () => {
90+
const ctx = baseCtx();
91+
ctx.blockchainModuleManager.getMinimumRequiredSignatures = async () => 3; // on-chain min
92+
const controller = new PublishController(ctx);
93+
94+
const req = {
95+
body: {
96+
dataset: { public: {} },
97+
datasetRoot: '0xroot',
98+
blockchain: 'hardhat',
99+
minimumNumberOfNodeReplications: 7, // above chain min
100+
},
101+
};
102+
const res = createRes();
103+
104+
await controller.handleRequest(req, res);
105+
106+
expect(res.statusCode).to.equal(202);
107+
const added = ctx.commandExecutor._added[0];
108+
expect(added.data.minimumNumberOfNodeReplications).to.equal(7);
109+
});
110+
111+
it('falls back to on-chain minimum when user value is zero or invalid', async () => {
112+
const ctx = baseCtx();
113+
ctx.blockchainModuleManager.getMinimumRequiredSignatures = async () => 4; // on-chain min
114+
const controller = new PublishController(ctx);
115+
116+
const req = {
117+
body: {
118+
dataset: { public: {} },
119+
datasetRoot: '0xroot',
120+
blockchain: 'hardhat',
121+
minimumNumberOfNodeReplications: 0, // invalid/zero
122+
},
123+
};
124+
const res = createRes();
125+
126+
await controller.handleRequest(req, res);
127+
128+
expect(res.statusCode).to.equal(202);
129+
const added = ctx.commandExecutor._added[0];
130+
expect(added.data.minimumNumberOfNodeReplications).to.equal(4);
131+
});
132+
});

0 commit comments

Comments
 (0)