trac-msb 0.1.77-msb-r2-dev.4 → 0.1.77-msb-r2-init-release
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/msb.mjs +8 -7
- package/package.json +22 -21
- package/rpc/handlers.mjs +1 -1
- package/src/core/network/messaging/routes/NetworkMessageRouter.js +1 -1
- package/src/core/network/messaging/validators/AdminResponse.js +4 -8
- package/src/core/network/messaging/validators/CustomNodeResponse.js +3 -6
- package/src/core/network/messaging/validators/PartialBootstrapDeployment.js +1 -4
- package/src/core/network/messaging/validators/PartialRoleAccess.js +28 -4
- package/src/core/network/messaging/validators/ValidatorResponse.js +5 -10
- package/src/core/network/messaging/validators/base/BaseResponse.js +5 -10
- package/src/core/network/services/PoolService.js +59 -22
- package/src/core/network/services/TransactionRateLimiterService.js +10 -1
- package/src/core/state/State.js +513 -405
- package/src/core/state/utils/transaction.js +1 -0
- package/src/index.js +68 -22
- package/src/utils/Scheduler.js +118 -0
- package/src/utils/cli.js +55 -38
- package/src/utils/constants.js +1 -1
- package/test/acceptance/v1/rpc.test.mjs +7 -2
- package/test/apply/addAdmin/addAdminBasic.test.js +1 -1
- package/test/apply/addAdmin/addAdminRecovery.test.js +1 -1
- package/test/apply/addIndexer.test.js +1 -1
- package/test/apply/addWhitelist.test.js +1 -1
- package/test/apply/addWriter.test.js +1 -1
- package/test/apply/banValidator.test.js +1 -1
- package/test/apply/postTx/invalidSubValues.test.js +1 -1
- package/test/apply/postTx/postTx.test.js +1 -1
- package/test/apply/removeIndexer.test.js +1 -1
- package/test/apply/removeWriter.test.js +1 -1
- package/test/apply/transfer.test.js +1 -1
- package/src/core/network/messaging/handlers/WhitelistedEventHandler.js +0 -88
- package/src/core/network/messaging/validators/WhitelistEvent.js +0 -117
package/msb.mjs
CHANGED
|
@@ -6,21 +6,22 @@ const args = isPear ? Pear.config.args : process.argv.slice(2);
|
|
|
6
6
|
const opts = {
|
|
7
7
|
stores_directory : 'stores/',
|
|
8
8
|
store_name : typeof process !== "undefined" ? process.argv[2] : Pear.config.args[0],
|
|
9
|
-
bootstrap: '
|
|
10
|
-
channel: '
|
|
11
|
-
chain_id: 1,
|
|
9
|
+
bootstrap: '3bba9ebdd5fe2104d3bc7ae0464834980a7d6195acb399f95dfe9a110e4d1da2',
|
|
10
|
+
channel: '00000000000tracnetworkmsbmainnet',
|
|
12
11
|
enable_role_requester: false,
|
|
13
|
-
enable_auto_transaction_consent: false,
|
|
14
12
|
enable_wallet: true,
|
|
15
13
|
enable_validator_observer: true,
|
|
16
14
|
enable_interactive_mode: true,
|
|
17
|
-
disable_rate_limit:
|
|
18
|
-
|
|
15
|
+
disable_rate_limit: false,
|
|
16
|
+
enable_tx_apply_logs: false,
|
|
17
|
+
enable_error_apply_logs: false,
|
|
19
18
|
};
|
|
20
19
|
|
|
21
20
|
const rpc_opts = {
|
|
22
21
|
...opts,
|
|
23
|
-
|
|
22
|
+
enable_tx_apply_logs: false,
|
|
23
|
+
enable_error_apply_logs: false,
|
|
24
|
+
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
const msb = new MainSettlementBus(args.includes('--rpc') ? rpc_opts : opts);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trac-msb",
|
|
3
3
|
"main": "msb.mjs",
|
|
4
|
-
"version": "0.1.77-msb-r2-
|
|
4
|
+
"version": "0.1.77-msb-r2-init-release",
|
|
5
5
|
"pear": {
|
|
6
6
|
"name": "trac-msb",
|
|
7
7
|
"type": "terminal"
|
|
@@ -9,41 +9,42 @@
|
|
|
9
9
|
"type": "module",
|
|
10
10
|
"scripts": {
|
|
11
11
|
"dev": "pear run -d .",
|
|
12
|
+
"prod": "NODE_OPTIONS='--max-old-space-size=4096' pear run . ${npm_config_store}",
|
|
12
13
|
"dev-rpc": "pear run -d . ${npm_config_store} --rpc --port ${npm_config_port}",
|
|
13
14
|
"prod-rpc": "pear run . ${npm_config_store} --rpc --host ${npm_config_host} --port ${npm_config_port}",
|
|
14
15
|
"protobuf": "node scripts/generate-protobufs.js",
|
|
15
16
|
"test:acceptance": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testTimeout=200000 test/acceptance/",
|
|
16
|
-
"test:node": "brittle-node -t
|
|
17
|
-
"test:bare": "brittle-bare -t
|
|
18
|
-
"test:node:cov": "brittle-node -c -t
|
|
19
|
-
"test:bare:cov": "brittle-bare -c -t
|
|
17
|
+
"test:node": "brittle-node -t 1200000 test/all.test.js",
|
|
18
|
+
"test:bare": "brittle-bare -t 1200000 test/all.test.js",
|
|
19
|
+
"test:node:cov": "brittle-node -c -t 1200000 test/all.test.js",
|
|
20
|
+
"test:bare:cov": "brittle-bare -c -t 1200000 test/all.test.js"
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@tracsystems/blake3": "
|
|
23
|
-
"autobase": "7.
|
|
23
|
+
"@tracsystems/blake3": "0.0.13",
|
|
24
|
+
"autobase": "7.20.1",
|
|
24
25
|
"b4a": "1.6.7",
|
|
25
|
-
"bare-crypto": "
|
|
26
|
-
"bare-fs": "
|
|
27
|
-
"bare-http1": "
|
|
26
|
+
"bare-crypto": "1.12.0",
|
|
27
|
+
"bare-fs": "4.5.0",
|
|
28
|
+
"bare-http1": "4.1.5",
|
|
28
29
|
"bare-readline": "1.0.7",
|
|
29
30
|
"bare-tty": "5.0.2",
|
|
30
|
-
"bech32": "
|
|
31
|
-
"compact-encoding": "
|
|
32
|
-
"corestore": "
|
|
31
|
+
"bech32": "2.0.0",
|
|
32
|
+
"compact-encoding": "2.18.0",
|
|
33
|
+
"corestore": "7.5.0",
|
|
33
34
|
"crypto": "npm:bare-node-crypto",
|
|
34
35
|
"fastest-validator": "1.19.0",
|
|
35
36
|
"http": "npm:bare-node-http",
|
|
36
|
-
"hyperbee": "
|
|
37
|
-
"hypercore": "
|
|
38
|
-
"hypercore-crypto": "
|
|
39
|
-
"hyperdht": "
|
|
40
|
-
"hyperswarm": "
|
|
41
|
-
"protocol-buffers-encodings": "
|
|
42
|
-
"protomux": "
|
|
37
|
+
"hyperbee": "2.26.5",
|
|
38
|
+
"hypercore": "11.18.3",
|
|
39
|
+
"hypercore-crypto": "3.6.1",
|
|
40
|
+
"hyperdht": "6.27.0",
|
|
41
|
+
"hyperswarm": "4.14.2",
|
|
42
|
+
"protocol-buffers-encodings": "1.2.0",
|
|
43
|
+
"protomux": "3.10.1",
|
|
43
44
|
"protomux-wakeup": "2.4.0",
|
|
44
45
|
"readline": "npm:bare-node-readline",
|
|
45
46
|
"ready-resource": "1.1.2",
|
|
46
|
-
"trac-wallet": "0.0.43-msb-r2.
|
|
47
|
+
"trac-wallet": "0.0.43-msb-r2.7",
|
|
47
48
|
"tty": "npm:bare-node-tty"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
package/rpc/handlers.mjs
CHANGED
|
@@ -129,7 +129,7 @@ export async function handleTransactionDetails({ msbInstance, respond, req }) {
|
|
|
129
129
|
const hash = req.url.split('/')[3];
|
|
130
130
|
const commandString = `/get_tx_details ${hash}`;
|
|
131
131
|
const txDetails = await msbInstance.handleCommand(commandString);
|
|
132
|
-
respond(200, { txDetails });
|
|
132
|
+
respond(txDetails === null ? 404 : 200 , { txDetails });
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
export async function handleFetchBulkTxPayloads({ msbInstance, respond, req }) {
|
|
@@ -5,7 +5,7 @@ import ResponseHandler from "../handlers/ResponseHandler.js";
|
|
|
5
5
|
import RoleOperationHandler from "../handlers/RoleOperationHandler.js";
|
|
6
6
|
import SubnetworkOperationHandler from "../handlers/SubnetworkOperationHandler.js";
|
|
7
7
|
import TransferOperationHandler from "../handlers/TransferOperationHandler.js";
|
|
8
|
-
import {NETWORK_MESSAGE_TYPES
|
|
8
|
+
import {NETWORK_MESSAGE_TYPES} from '../../../../utils/constants.js';
|
|
9
9
|
import * as operation from '../../../../utils/operations.js';
|
|
10
10
|
import TransactionRateLimiterService from "../../services/TransactionRateLimiterService.js";
|
|
11
11
|
|
|
@@ -17,8 +17,7 @@ class AdminResponse extends BaseResponse {
|
|
|
17
17
|
!await this.validateSignature(message) ||
|
|
18
18
|
!this.validateChannel(message, channelString)
|
|
19
19
|
) {
|
|
20
|
-
|
|
21
|
-
return false;
|
|
20
|
+
throw new Error("Admin response validation failed");
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
return true;
|
|
@@ -33,8 +32,7 @@ class AdminResponse extends BaseResponse {
|
|
|
33
32
|
!message.channel ||
|
|
34
33
|
!message.issuer ||
|
|
35
34
|
!message.timestamp) {
|
|
36
|
-
|
|
37
|
-
return false;
|
|
35
|
+
throw new Error("Admin response is missing required fields.");
|
|
38
36
|
}
|
|
39
37
|
return true;
|
|
40
38
|
}
|
|
@@ -42,8 +40,7 @@ class AdminResponse extends BaseResponse {
|
|
|
42
40
|
async validateAdminData(message) {
|
|
43
41
|
const adminEntry = await this.state.getAdminEntry();
|
|
44
42
|
if (!adminEntry) {
|
|
45
|
-
|
|
46
|
-
return false;
|
|
43
|
+
throw new Error("Admin entry is null");
|
|
47
44
|
}
|
|
48
45
|
|
|
49
46
|
const adminPublicKey = PeerWallet.decodeBech32m(adminEntry.address);
|
|
@@ -51,8 +48,7 @@ class AdminResponse extends BaseResponse {
|
|
|
51
48
|
const adminWritingKey = b4a.from(message.wk, 'hex');
|
|
52
49
|
|
|
53
50
|
if (!b4a.equals(adminPublicKey, receivedAdminPublicKey) || !b4a.equals(adminEntry.wk, adminWritingKey)) {
|
|
54
|
-
|
|
55
|
-
return false;
|
|
51
|
+
throw new Error("Admin public key or writing key mismatch in response.");
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
return true;
|
|
@@ -15,8 +15,7 @@ class CustomNodeResponse extends BaseResponse {
|
|
|
15
15
|
!await this.validateSignature(message) ||
|
|
16
16
|
!this.validateChannel(message, channelString)
|
|
17
17
|
) {
|
|
18
|
-
|
|
19
|
-
return false;
|
|
18
|
+
throw new Error("Custom node response validation failed");
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
return true;
|
|
@@ -30,8 +29,7 @@ class CustomNodeResponse extends BaseResponse {
|
|
|
30
29
|
!message.channel ||
|
|
31
30
|
!message.issuer ||
|
|
32
31
|
!message.timestamp) {
|
|
33
|
-
|
|
34
|
-
return false;
|
|
32
|
+
throw new Error("Custom node response is missing required fields.");
|
|
35
33
|
}
|
|
36
34
|
return true;
|
|
37
35
|
}
|
|
@@ -40,8 +38,7 @@ class CustomNodeResponse extends BaseResponse {
|
|
|
40
38
|
const customNodeAddressString = message.address;
|
|
41
39
|
const customNodeEntry = await this.state.getNodeEntry(customNodeAddressString);
|
|
42
40
|
if (customNodeEntry === null) {
|
|
43
|
-
|
|
44
|
-
return false;
|
|
41
|
+
throw new Error("Custom node entry is null - entry is not initialized.");
|
|
45
42
|
}
|
|
46
43
|
return true;
|
|
47
44
|
}
|
|
@@ -23,15 +23,12 @@ class PartialBootstrapDeployment extends PartialOperation {
|
|
|
23
23
|
return true;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
async validateBootstrapRegistration(payload) {
|
|
28
27
|
const bootstrapString = payload.bdo.bs.toString('hex');
|
|
29
|
-
if (null !== await this.state.
|
|
28
|
+
if (null !== await this.state.getRegisteredBootstrapEntryUnsigned(bootstrapString)) {
|
|
30
29
|
throw new Error(`Bootstrap with hash ${bootstrapString} already exists in the state. Bootstrap must be unique.`);
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
32
|
}
|
|
36
33
|
|
|
37
34
|
export default PartialBootstrapDeployment;
|
|
@@ -3,6 +3,7 @@ import b4a from 'b4a';
|
|
|
3
3
|
import { OperationType } from "../../../../utils/constants.js";
|
|
4
4
|
import { bufferToAddress } from "../../../state/utils/address.js";
|
|
5
5
|
import PartialOperation from './base/PartialOperation.js';
|
|
6
|
+
import { bufferToBigInt } from "../../../../utils/amountSerialization.js";
|
|
6
7
|
|
|
7
8
|
class PartialRoleAccess extends PartialOperation {
|
|
8
9
|
constructor(state) {
|
|
@@ -16,12 +17,15 @@ class PartialRoleAccess extends PartialOperation {
|
|
|
16
17
|
await this.validateSignature(payload);
|
|
17
18
|
await this.validateTransactionValidity(payload);
|
|
18
19
|
this.isOperationNotCompleted(payload);
|
|
19
|
-
await this.validateRequesterBalance(payload);
|
|
20
|
-
await this.validateRequesterBalance(payload, true);
|
|
21
20
|
|
|
22
21
|
// non common validations below
|
|
23
22
|
if (payload.type === OperationType.ADD_WRITER) {
|
|
23
|
+
await this.validateRequesterBalanceForAddWriterOperation(payload);
|
|
24
|
+
await this.validateRequesterBalanceForAddWriterOperation(payload, true);
|
|
24
25
|
await this.validateWriterKey(payload)
|
|
26
|
+
} else {
|
|
27
|
+
await this.validateRequesterBalance(payload, true);
|
|
28
|
+
await this.validateRequesterBalance(payload)
|
|
25
29
|
}
|
|
26
30
|
await this.isRequesterAllowedToChangeRole(payload);
|
|
27
31
|
|
|
@@ -64,9 +68,9 @@ class PartialRoleAccess extends PartialOperation {
|
|
|
64
68
|
const isAlreadyIndexer = nodeEntry.isIndexer;
|
|
65
69
|
if (isAlreadyIndexer) {
|
|
66
70
|
throw new Error(`Node with address ${nodeAddress} is an indexer.`);
|
|
67
|
-
}
|
|
71
|
+
}
|
|
68
72
|
return;
|
|
69
|
-
|
|
73
|
+
|
|
70
74
|
} else if (type === OperationType.ADMIN_RECOVERY) {
|
|
71
75
|
const adminEntry = await this.state.getAdminEntry();
|
|
72
76
|
if (!adminEntry) {
|
|
@@ -108,6 +112,26 @@ class PartialRoleAccess extends PartialOperation {
|
|
|
108
112
|
}
|
|
109
113
|
}
|
|
110
114
|
}
|
|
115
|
+
|
|
116
|
+
async validateRequesterBalanceForAddWriterOperation(payload, signed = false) {
|
|
117
|
+
const requesterAddress = bufferToAddress(payload.address);
|
|
118
|
+
let requesterEntry;
|
|
119
|
+
if (signed) {
|
|
120
|
+
requesterEntry = await this.state.getNodeEntry(requesterAddress);
|
|
121
|
+
} else {
|
|
122
|
+
requesterEntry = await this.state.getNodeEntryUnsigned(requesterAddress);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!requesterEntry) {
|
|
126
|
+
throw new Error('Requester address not found in state');
|
|
127
|
+
}
|
|
128
|
+
const requesterBalance = bufferToBigInt(requesterEntry.balance);
|
|
129
|
+
|
|
130
|
+
const requiredBalance = this.fee * 11n;
|
|
131
|
+
if (requesterBalance < requiredBalance) {
|
|
132
|
+
throw new Error('Insufficient requester balance to cover role access operation FEE.');
|
|
133
|
+
}
|
|
134
|
+
}
|
|
111
135
|
}
|
|
112
136
|
|
|
113
137
|
export default PartialRoleAccess;
|
|
@@ -16,8 +16,7 @@ class ValidatorResponse extends BaseResponse {
|
|
|
16
16
|
!await this.validateSignature(message) ||
|
|
17
17
|
!this.validateChannel(message, channelString)
|
|
18
18
|
) {
|
|
19
|
-
|
|
20
|
-
return false;
|
|
19
|
+
throw new Error("Validator response validation failed");
|
|
21
20
|
}
|
|
22
21
|
return true;
|
|
23
22
|
}
|
|
@@ -31,8 +30,7 @@ class ValidatorResponse extends BaseResponse {
|
|
|
31
30
|
!message.channel ||
|
|
32
31
|
!message.issuer ||
|
|
33
32
|
!message.timestamp) {
|
|
34
|
-
|
|
35
|
-
return false;
|
|
33
|
+
throw new Error("Validator response is missing required fields.");
|
|
36
34
|
}
|
|
37
35
|
return true;
|
|
38
36
|
}
|
|
@@ -42,8 +40,7 @@ class ValidatorResponse extends BaseResponse {
|
|
|
42
40
|
const adminEntry = await this.state.getAdminEntry();
|
|
43
41
|
|
|
44
42
|
if (validatorEntry === null || !validatorEntry.isWriter) {
|
|
45
|
-
|
|
46
|
-
return false;
|
|
43
|
+
throw new Error("Validator entry not found in state or is not a writer.");
|
|
47
44
|
}
|
|
48
45
|
|
|
49
46
|
if (adminEntry && message.address === adminEntry.address) {
|
|
@@ -51,8 +48,7 @@ class ValidatorResponse extends BaseResponse {
|
|
|
51
48
|
}
|
|
52
49
|
|
|
53
50
|
if (validatorEntry.isIndexer) {
|
|
54
|
-
|
|
55
|
-
return false;
|
|
51
|
+
throw new Error("Validator is an indexer.");
|
|
56
52
|
}
|
|
57
53
|
return true;
|
|
58
54
|
}
|
|
@@ -62,8 +58,7 @@ class ValidatorResponse extends BaseResponse {
|
|
|
62
58
|
const validatorEntry = await this.state.getNodeEntry(message.address);
|
|
63
59
|
|
|
64
60
|
if (!validatorEntry || !b4a.equals(validatorEntry.wk, writingKey)) {
|
|
65
|
-
|
|
66
|
-
return false;
|
|
61
|
+
throw new Error("Writing key does not match validator entry.");
|
|
67
62
|
}
|
|
68
63
|
return true;
|
|
69
64
|
}
|
|
@@ -23,8 +23,7 @@ class BaseResponse {
|
|
|
23
23
|
validateIssuerPublicKey(message) {
|
|
24
24
|
const issuerPublicKey = b4a.from(message.issuer, 'hex');
|
|
25
25
|
if (!b4a.equals(issuerPublicKey, this.#wallet.publicKey)) {
|
|
26
|
-
|
|
27
|
-
return false;
|
|
26
|
+
throw new Error("Issuer public key does not match the wallet's public key.");
|
|
28
27
|
}
|
|
29
28
|
return true;
|
|
30
29
|
}
|
|
@@ -35,16 +34,14 @@ class BaseResponse {
|
|
|
35
34
|
const fiveSeconds = 5000;
|
|
36
35
|
|
|
37
36
|
if (now - timestamp > fiveSeconds) {
|
|
38
|
-
|
|
39
|
-
return false;
|
|
37
|
+
throw new Error("Validator response is too old, ignoring.");
|
|
40
38
|
}
|
|
41
39
|
return true;
|
|
42
40
|
}
|
|
43
41
|
|
|
44
42
|
validateChannel(message, channelString) {
|
|
45
43
|
if (message.channel !== channelString) {
|
|
46
|
-
|
|
47
|
-
return false;
|
|
44
|
+
throw new Error("Channel mismatch in validator response.");
|
|
48
45
|
}
|
|
49
46
|
return true;
|
|
50
47
|
}
|
|
@@ -71,8 +68,7 @@ class BaseResponse {
|
|
|
71
68
|
}
|
|
72
69
|
|
|
73
70
|
if (!publicKey) {
|
|
74
|
-
|
|
75
|
-
return false;
|
|
71
|
+
throw new Error("Failed to derive public key from message.");
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
const messageWithoutSig = { ...message };
|
|
@@ -82,8 +78,7 @@ class BaseResponse {
|
|
|
82
78
|
const verified = this.#wallet.verify(signature, hash, publicKey);
|
|
83
79
|
|
|
84
80
|
if (!verified) {
|
|
85
|
-
|
|
86
|
-
return false;
|
|
81
|
+
throw new Error("Signature in the response verification failed.");
|
|
87
82
|
}
|
|
88
83
|
return true;
|
|
89
84
|
}
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
// PoolService.js
|
|
2
2
|
import { BATCH_SIZE, PROCESS_INTERVAL_MS } from '../../../utils/constants.js';
|
|
3
|
-
import
|
|
3
|
+
import Scheduler from '../../../utils/Scheduler.js';
|
|
4
|
+
|
|
4
5
|
class PoolService {
|
|
5
|
-
#shouldStopPool = false;
|
|
6
6
|
#state;
|
|
7
7
|
#address;
|
|
8
8
|
#options;
|
|
9
|
-
#tx_pool = [];
|
|
9
|
+
#tx_pool = [];
|
|
10
|
+
#scheduler = null;
|
|
10
11
|
|
|
11
12
|
constructor(state, address, options = {}) {
|
|
12
13
|
this.#state = state;
|
|
13
14
|
this.#address = address;
|
|
14
15
|
this.#options = options;
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
+
|
|
17
18
|
get tx_pool() {
|
|
18
19
|
return this.#tx_pool;
|
|
19
20
|
}
|
|
@@ -31,32 +32,68 @@ class PoolService {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
async start() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
35
|
+
if (!this.options.enable_wallet) {
|
|
36
|
+
console.info('PoolService can not start. Wallet is not enabled');
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (this.scheduler && this.scheduler.isRunning) {
|
|
40
|
+
console.info('PoolService is already started');
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.#scheduler = this.#createScheduler();
|
|
45
|
+
this.#scheduler.start();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async #worker(next) {
|
|
49
|
+
try {
|
|
50
|
+
await this.#processTransactions();
|
|
51
|
+
if (this.#tx_pool.length > 0) {
|
|
52
|
+
next(0);
|
|
53
|
+
} else {
|
|
54
|
+
next(PROCESS_INTERVAL_MS);
|
|
47
55
|
}
|
|
48
|
-
|
|
56
|
+
} catch (error) {
|
|
57
|
+
throw new Error(`PoolService worker error: ${error.message}`);
|
|
49
58
|
}
|
|
50
59
|
}
|
|
51
60
|
|
|
61
|
+
#createScheduler() {
|
|
62
|
+
return new Scheduler((next) => this.#worker(next), PROCESS_INTERVAL_MS);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async #processTransactions() {
|
|
66
|
+
const canValidate = await this.#checkValidationPermissions();
|
|
67
|
+
|
|
68
|
+
if (canValidate && this.#tx_pool.length > 0) {
|
|
69
|
+
const batch = this.#prepareBatch();
|
|
70
|
+
await this.#state.append(batch);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async #checkValidationPermissions() {
|
|
75
|
+
const isAdminAllowedToValidate = await this.state.isAdminAllowedToValidate();
|
|
76
|
+
const isNodeAllowedToValidate = await this.state.allowedToValidate(this.address);
|
|
77
|
+
return isNodeAllowedToValidate || isAdminAllowedToValidate;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
#prepareBatch() {
|
|
81
|
+
const length = Math.min(this.tx_pool.length, BATCH_SIZE);
|
|
82
|
+
const batch = this.tx_pool.slice(0, length);
|
|
83
|
+
this.tx_pool.splice(0, length);
|
|
84
|
+
return batch;
|
|
85
|
+
}
|
|
86
|
+
|
|
52
87
|
addTransaction(tx) {
|
|
53
88
|
this.tx_pool.push(tx);
|
|
54
89
|
}
|
|
55
90
|
|
|
56
|
-
stopPool() {
|
|
57
|
-
this.#
|
|
91
|
+
async stopPool(waitForCurrent = true) {
|
|
92
|
+
if (!this.#scheduler) return;
|
|
93
|
+
await this.#scheduler.stop(waitForCurrent);
|
|
94
|
+
this.#scheduler = null;
|
|
95
|
+
console.info('PoolService: closing gracefully...');
|
|
58
96
|
}
|
|
59
|
-
|
|
60
97
|
}
|
|
61
98
|
|
|
62
99
|
export default PoolService;
|
|
@@ -23,7 +23,15 @@ class TransactionRateLimiterService {
|
|
|
23
23
|
*/
|
|
24
24
|
#hasExceededRateLimit(peer) {
|
|
25
25
|
const peerData = this.#connectionsStatistics.get(peer);
|
|
26
|
-
|
|
26
|
+
const currentSecond = Math.floor((peerData.lastActivityTime - peerData.sessionStartTime) / 1000);
|
|
27
|
+
|
|
28
|
+
if (currentSecond > Math.floor((peerData.lastCounterReset - peerData.sessionStartTime) / 1000)) {
|
|
29
|
+
peerData.transactionCount = 0;
|
|
30
|
+
peerData.lastCounterReset = peerData.lastActivityTime;
|
|
31
|
+
this.#connectionsStatistics.set(peer, peerData);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return peerData.transactionCount >= MAX_TRANSACTIONS_PER_SECOND;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
/*
|
|
@@ -93,6 +101,7 @@ class TransactionRateLimiterService {
|
|
|
93
101
|
this.#connectionsStatistics.set(peer, {
|
|
94
102
|
sessionStartTime: timestamp,
|
|
95
103
|
lastActivityTime: 0,
|
|
104
|
+
lastCounterReset: timestamp,
|
|
96
105
|
transactionCount: 0
|
|
97
106
|
});
|
|
98
107
|
}
|