trac-msb 0.2.8 → 0.2.10
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/.github/workflows/acceptance-tests.yml +7 -11
- package/.github/workflows/lint-pr-title.yml +26 -0
- package/.github/workflows/unit-tests.yml +2 -8
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +33 -18
- package/docker-compose.yml +1 -0
- package/docs/trac_network_http_api.openapi.yaml +889 -0
- package/msb.mjs +5 -22
- package/package.json +14 -10
- package/proto/network.proto +74 -0
- package/rpc/create_server.js +2 -2
- package/rpc/handlers.js +165 -92
- package/rpc/routes/v1.js +3 -1
- package/rpc/rpc_server.js +4 -4
- package/rpc/rpc_services.js +62 -25
- package/rpc/utils/helpers.js +83 -52
- package/src/config/args.js +46 -0
- package/src/config/config.js +78 -5
- package/src/config/env.js +70 -3
- package/src/core/network/Network.js +34 -70
- package/src/core/network/identity/NetworkWalletFactory.js +2 -2
- package/src/core/network/protocols/LegacyProtocol.js +67 -0
- package/src/core/network/protocols/NetworkMessages.js +48 -0
- package/src/core/network/protocols/ProtocolInterface.js +31 -0
- package/src/core/network/protocols/ProtocolSession.js +59 -0
- package/src/core/network/protocols/V1Protocol.js +64 -0
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +84 -0
- package/src/core/network/protocols/legacy/handlers/GetRequestHandler.js +53 -0
- package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
- package/src/core/network/{messaging → protocols/legacy}/validators/base/BaseResponse.js +2 -3
- package/src/core/network/protocols/shared/handlers/RoleOperationHandler.js +88 -0
- package/src/core/network/protocols/shared/handlers/SubnetworkOperationHandler.js +93 -0
- package/src/core/network/{messaging → protocols/shared}/handlers/TransferOperationHandler.js +17 -16
- package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +10 -15
- package/src/core/network/{messaging → protocols/shared}/validators/PartialBootstrapDeployment.js +2 -2
- package/src/core/network/{messaging → protocols/shared}/validators/PartialRoleAccess.js +5 -5
- package/src/core/network/{messaging → protocols/shared}/validators/PartialTransaction.js +4 -4
- package/src/core/network/{messaging → protocols/shared}/validators/PartialTransfer.js +4 -4
- package/src/core/network/{messaging → protocols/shared}/validators/base/PartialOperation.js +14 -12
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +15 -0
- package/src/core/network/services/ConnectionManager.js +5 -5
- package/src/core/network/services/MessageOrchestrator.js +2 -2
- package/src/core/network/services/TransactionPoolService.js +5 -6
- package/src/core/network/services/TransactionRateLimiterService.js +12 -13
- package/src/core/network/services/ValidatorObserverService.js +5 -6
- package/src/core/state/State.js +3 -5
- package/src/index.js +156 -181
- package/src/messages/network/v1/NetworkMessageBuilder.js +325 -0
- package/src/messages/network/v1/NetworkMessageDirector.js +137 -0
- package/src/messages/network/v1/networkMessageFactory.js +12 -0
- package/src/messages/state/ApplyStateMessageBuilder.js +661 -0
- package/src/messages/state/ApplyStateMessageDirector.js +516 -0
- package/src/messages/state/applyStateMessageFactory.js +12 -0
- package/src/utils/buffer.js +53 -1
- package/src/utils/check.js +1 -1
- package/src/utils/cli.js +0 -8
- package/src/utils/constants.js +33 -30
- package/src/utils/fileUtils.js +13 -0
- package/src/utils/normalizers.js +84 -2
- package/src/utils/protobuf/network.cjs +840 -0
- package/src/utils/protobuf/operationHelpers.js +10 -0
- package/src/utils/type.js +26 -0
- package/tests/acceptance/v1/balance/balance.test.mjs +1 -2
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +26 -30
- package/tests/acceptance/v1/health/health.test.mjs +33 -0
- package/tests/acceptance/v1/rpc.test.mjs +4 -3
- package/tests/acceptance/v1/tx/tx.test.mjs +27 -16
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +26 -12
- package/tests/fixtures/check.fixtures.js +33 -32
- package/tests/fixtures/networkV1.fixtures.js +85 -0
- package/tests/fixtures/protobuf.fixtures.js +109 -25
- package/tests/helpers/StateNetworkFactory.js +2 -2
- package/tests/helpers/address.js +6 -0
- package/tests/helpers/autobaseTestHelpers.js +2 -1
- package/tests/helpers/config.js +2 -1
- package/tests/helpers/setupApplyTests.js +59 -56
- package/tests/unit/messages/messages.test.js +12 -0
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +276 -0
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +201 -0
- package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +521 -0
- package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +233 -0
- package/tests/unit/network/ConnectionManager.test.js +6 -5
- package/tests/unit/network/networkModule.test.js +3 -2
- package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +10 -6
- package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +9 -6
- package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +10 -7
- package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +18 -21
- package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +53 -38
- package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +46 -35
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +13 -16
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +17 -11
- package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +11 -12
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +9 -7
- package/tests/unit/state/apply/common/commonScenarioHelper.js +15 -14
- package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +9 -4
- package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +17 -11
- package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +19 -14
- package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +37 -29
- package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +9 -7
- package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +11 -9
- package/tests/unit/unit.test.js +1 -1
- package/tests/unit/utils/buffer/buffer.test.js +62 -1
- package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +4 -3
- package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +3 -2
- package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +3 -2
- package/tests/unit/utils/normalizers/normalizers.test.js +469 -0
- package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
- package/tests/unit/utils/type/type.test.js +25 -0
- package/tests/unit/utils/utils.test.js +1 -0
- package/docs/networking-dualstack-plan.md +0 -75
- package/docs/networking-layer-redesign.md +0 -155
- package/src/core/network/messaging/NetworkMessages.js +0 -64
- package/src/core/network/messaging/handlers/GetRequestHandler.js +0 -113
- package/src/core/network/messaging/handlers/ResponseHandler.js +0 -107
- package/src/core/network/messaging/handlers/RoleOperationHandler.js +0 -114
- package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +0 -149
- package/src/core/network/messaging/routes/NetworkMessageRouter.js +0 -98
- package/src/core/network/messaging/validators/AdminResponse.js +0 -58
- package/src/core/network/messaging/validators/CustomNodeResponse.js +0 -46
- package/src/messages/base/StateBuilder.js +0 -25
- package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +0 -425
- package/src/messages/completeStateMessages/CompleteStateMessageDirector.js +0 -252
- package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +0 -296
- package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +0 -272
- package/src/messages/partialStateMessages/PartialStateMessageDirector.js +0 -137
- package/src/messages/partialStateMessages/PartialStateMessageOperations.js +0 -138
- package/tests/integration/apply/addAdmin/addAdminBasic.test.js +0 -69
- package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +0 -126
- package/tests/integration/apply/addIndexer.test.js +0 -239
- package/tests/integration/apply/addWhitelist.test.js +0 -53
- package/tests/integration/apply/addWriter.test.js +0 -245
- package/tests/integration/apply/apply.test.js +0 -19
- package/tests/integration/apply/banValidator.test.js +0 -116
- package/tests/integration/apply/postTx/invalidSubValues.test.js +0 -103
- package/tests/integration/apply/postTx/postTx.test.js +0 -196
- package/tests/integration/apply/removeIndexer.test.js +0 -132
- package/tests/integration/apply/removeWriter.test.js +0 -168
- package/tests/integration/apply/transfer.test.js +0 -83
- package/tests/integration/integration.test.js +0 -9
- package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +0 -21
- package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +0 -17
- package/tests/unit/messageOperations/assembleAdminMessage.test.js +0 -68
- package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +0 -17
- package/tests/unit/messageOperations/assemblePostTransaction.test.js +0 -424
- package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +0 -19
- package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +0 -17
- package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +0 -59
- package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +0 -278
- package/tests/unit/messageOperations/stateMessageOperations.test.js +0 -19
- /package/src/core/network/{messaging → protocols/legacy}/validators/ValidatorResponse.js +0 -0
- /package/src/utils/{operations.js → applyOperations.js} +0 -0
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import {
|
|
3
|
-
CLEANUP_INTERVAL_MS,
|
|
4
|
-
CONNECTION_TIMEOUT_MS,
|
|
5
|
-
MAX_TRANSACTIONS_PER_SECOND
|
|
6
|
-
} from '../../../utils/constants.js';
|
|
7
2
|
|
|
8
3
|
class TransactionRateLimiterService {
|
|
9
4
|
#lastCleanup;
|
|
10
5
|
#connectionsStatistics;
|
|
6
|
+
#swarm;
|
|
7
|
+
#config;
|
|
11
8
|
|
|
12
|
-
constructor() {
|
|
9
|
+
constructor(swarm, config) {
|
|
13
10
|
this.#lastCleanup = Date.now();
|
|
14
11
|
this.#connectionsStatistics = new Map();
|
|
12
|
+
this.#swarm = swarm
|
|
13
|
+
this.#config = config
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
/*
|
|
18
17
|
Checks if the peer has exceeded the rate limit.
|
|
19
18
|
A peer is considered to have exceeded the rate limit if:
|
|
20
19
|
- The time since the last activity is greater than or equal to 1000 ms (1 second)
|
|
21
|
-
- The number of transactions in the current session is greater than or equal to
|
|
20
|
+
- The number of transactions in the current session is greater than or equal to rateLimitMaxTransactionsPerSecond
|
|
22
21
|
If the rate limit is exceeded, the peer is disconnected.
|
|
23
22
|
*/
|
|
24
23
|
#hasExceededRateLimit(peer) {
|
|
@@ -31,7 +30,7 @@ class TransactionRateLimiterService {
|
|
|
31
30
|
this.#connectionsStatistics.set(peer, peerData);
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
return peerData.transactionCount >=
|
|
33
|
+
return peerData.transactionCount >= this.#config.rateLimitMaxTransactionsPerSecond;
|
|
35
34
|
}
|
|
36
35
|
|
|
37
36
|
/*
|
|
@@ -39,7 +38,7 @@ class TransactionRateLimiterService {
|
|
|
39
38
|
If the peer has exceeded the rate limit, it disconnects the peer.
|
|
40
39
|
Otherwise, it updates the connection info with the current timestamp.
|
|
41
40
|
*/
|
|
42
|
-
handleRateLimit(connection
|
|
41
|
+
handleRateLimit(connection) {
|
|
43
42
|
const peer = b4a.toString(connection.remotePublicKey, 'hex');
|
|
44
43
|
const currentTime = Date.now();
|
|
45
44
|
|
|
@@ -53,7 +52,7 @@ class TransactionRateLimiterService {
|
|
|
53
52
|
|
|
54
53
|
if (this.#hasExceededRateLimit(peer)) {
|
|
55
54
|
console.warn(`Rate limit exceeded for peer ${peer}. Disconnecting...`);
|
|
56
|
-
|
|
55
|
+
this.#swarm.leavePeer(connection.remotePublicKey);
|
|
57
56
|
connection.end();
|
|
58
57
|
return true;
|
|
59
58
|
}
|
|
@@ -63,13 +62,13 @@ class TransactionRateLimiterService {
|
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
#shouldCleanupConnections(currentTime) {
|
|
66
|
-
return currentTime - this.#lastCleanup >=
|
|
65
|
+
return currentTime - this.#lastCleanup >= this.#config.rateLimitCleanupIntervalMs;
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
/**
|
|
70
69
|
Cleans up old connections that have timed out.
|
|
71
70
|
Condition for cleanup based on #shouldCleanupConnections:
|
|
72
|
-
- If the last cleanup was more than
|
|
71
|
+
- If the last cleanup was more than rateLimitCleanupIntervalMs ago
|
|
73
72
|
*/
|
|
74
73
|
#cleanUpOldConnections(currentTime) {
|
|
75
74
|
if (!this.#shouldCleanupConnections(currentTime)) {
|
|
@@ -123,7 +122,7 @@ class TransactionRateLimiterService {
|
|
|
123
122
|
*/
|
|
124
123
|
#isConnectionExpired(peer) {
|
|
125
124
|
const peerData = this.#connectionsStatistics.get(peer);
|
|
126
|
-
return peerData.lastActivityTime - peerData.sessionStartTime >=
|
|
125
|
+
return peerData.lastActivityTime - peerData.sessionStartTime >= this.#config.rateLimitConnectionTimeoutMs;
|
|
127
126
|
}
|
|
128
127
|
}
|
|
129
128
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import PeerWallet from "trac-wallet";
|
|
2
2
|
import b4a from "b4a";
|
|
3
|
-
import { MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION } from '../../../utils/constants.js';
|
|
4
3
|
import { bufferToAddress } from '../../state/utils/address.js';
|
|
5
4
|
import { sleep } from '../../../utils/helpers.js';
|
|
6
5
|
import Scheduler from "../../../utils/Scheduler.js";
|
|
@@ -31,7 +30,7 @@ class ValidatorObserverService {
|
|
|
31
30
|
* @param {Network} network
|
|
32
31
|
* @param {State} state
|
|
33
32
|
* @param {string} address
|
|
34
|
-
* @param {
|
|
33
|
+
* @param {Config} config
|
|
35
34
|
**/
|
|
36
35
|
constructor(network, state, address, config) {
|
|
37
36
|
this.#config = config
|
|
@@ -132,7 +131,7 @@ class ValidatorObserverService {
|
|
|
132
131
|
const validatorPubKeyHex = validatorPubKeyBuffer.toString('hex');
|
|
133
132
|
const adminEntry = await this.state.getAdminEntry();
|
|
134
133
|
|
|
135
|
-
if (validatorAddress !== adminEntry?.address || validatorListLength <
|
|
134
|
+
if (validatorAddress !== adminEntry?.address || validatorListLength < this.#config.maxWritersForAdminIndexerConnection) {
|
|
136
135
|
this.#network.tryConnect(validatorPubKeyHex, 'validator');
|
|
137
136
|
}
|
|
138
137
|
};
|
|
@@ -151,7 +150,7 @@ class ValidatorObserverService {
|
|
|
151
150
|
return false;
|
|
152
151
|
}
|
|
153
152
|
|
|
154
|
-
if (validatorAddress === adminEntry?.address && validatorListLength >=
|
|
153
|
+
if (validatorAddress === adminEntry?.address && validatorListLength >= this.#config.maxWritersForAdminIndexerConnection) {
|
|
155
154
|
if (this.#network.validatorConnectionManager.exists(validatorPubKeyBuffer)) {
|
|
156
155
|
this.#network.validatorConnectionManager.remove(validatorPubKeyBuffer)
|
|
157
156
|
}
|
|
@@ -161,12 +160,12 @@ class ValidatorObserverService {
|
|
|
161
160
|
// - Cannot connect if already connected to a validator
|
|
162
161
|
// - Validator must exist and be a writer
|
|
163
162
|
// - Cannot connect to indexers, except for admin-indexer
|
|
164
|
-
// - Admin-indexer connection is allowed only when writers length
|
|
163
|
+
// - Admin-indexer connection is allowed only when writers length is below maxWritersForAdminIndexerConnection
|
|
165
164
|
if (this.#network.validatorConnectionManager.connected(validatorPubKeyBuffer) ||
|
|
166
165
|
this.#network.validatorConnectionManager.maxConnectionsReached() ||
|
|
167
166
|
validatorEntry === null ||
|
|
168
167
|
!validatorEntry.isWriter ||
|
|
169
|
-
(validatorEntry.isIndexer && (validatorAddress !== adminEntry?.address || validatorListLength >=
|
|
168
|
+
(validatorEntry.isIndexer && (validatorAddress !== adminEntry?.address || validatorListLength >= this.#config.maxWritersForAdminIndexerConnection))
|
|
170
169
|
) {
|
|
171
170
|
return false;
|
|
172
171
|
}
|
package/src/core/state/State.js
CHANGED
|
@@ -12,7 +12,6 @@ import {
|
|
|
12
12
|
HYPERBEE_VALUE_ENCODING,
|
|
13
13
|
BATCH_SIZE,
|
|
14
14
|
ADMIN_INITIAL_STAKED_BALANCE,
|
|
15
|
-
MAX_WRITERS_FOR_ADMIN_INDEXER_CONNECTION,
|
|
16
15
|
TRAC_NAMESPACE,
|
|
17
16
|
CustomEventType
|
|
18
17
|
} from '../../utils/constants.js';
|
|
@@ -58,7 +57,7 @@ class State extends ReadyResource {
|
|
|
58
57
|
/**
|
|
59
58
|
* @param {Corestore} store
|
|
60
59
|
* @param {PeerWallet} wallet
|
|
61
|
-
* @param {
|
|
60
|
+
* @param {Config} config
|
|
62
61
|
**/
|
|
63
62
|
constructor(store, wallet, config) {
|
|
64
63
|
super();
|
|
@@ -178,7 +177,7 @@ class State extends ReadyResource {
|
|
|
178
177
|
async isAdminAllowedToValidate() {
|
|
179
178
|
const isAdmin = this.writingKey.toString('hex') === this.#config.bootstrap.toString('hex');
|
|
180
179
|
const isIndexer = this.isIndexer();
|
|
181
|
-
const lengthCondition = await this.getWriterLength() <=
|
|
180
|
+
const lengthCondition = await this.getWriterLength() <= this.#config.maxWritersForAdminIndexerConnection;
|
|
182
181
|
return !!(isAdmin && isIndexer && lengthCondition);
|
|
183
182
|
}
|
|
184
183
|
|
|
@@ -201,8 +200,7 @@ class State extends ReadyResource {
|
|
|
201
200
|
}
|
|
202
201
|
|
|
203
202
|
async getIndexersEntry() {
|
|
204
|
-
|
|
205
|
-
return indexersEntry
|
|
203
|
+
return Object.values(this.#base.system.indexers);
|
|
206
204
|
}
|
|
207
205
|
|
|
208
206
|
async isWkInIndexersEntry(wk) {
|