trac-msb 0.2.8 → 0.2.9
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 +3 -3
- package/package.json +8 -3
- package/proto/network.proto +74 -0
- package/rpc/create_server.js +2 -2
- package/rpc/handlers.js +2 -2
- package/rpc/rpc_server.js +2 -2
- package/rpc/rpc_services.js +44 -3
- package/rpc/utils/helpers.js +1 -1
- package/src/config/env.js +2 -0
- package/src/core/network/Network.js +29 -61
- 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 +1 -2
- 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 +16 -15
- package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +7 -11
- 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 +4 -4
- package/src/core/network/services/MessageOrchestrator.js +1 -1
- package/src/core/network/services/TransactionPoolService.js +1 -2
- package/src/core/network/services/TransactionRateLimiterService.js +5 -3
- package/src/core/state/State.js +1 -2
- package/src/index.js +153 -180
- 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/cli.js +0 -8
- package/src/utils/constants.js +34 -14
- package/src/utils/normalizers.js +84 -2
- package/src/utils/protobuf/network.cjs +840 -0
- package/src/utils/protobuf/operationHelpers.js +10 -0
- package/tests/acceptance/v1/rpc.test.mjs +1 -1
- package/tests/fixtures/networkV1.fixtures.js +84 -0
- package/tests/fixtures/protobuf.fixtures.js +83 -0
- package/tests/helpers/config.js +1 -1
- package/tests/helpers/setupApplyTests.js +53 -46
- 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 +203 -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/normalizers/normalizers.test.js +469 -0
- package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
- 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,12 +1,12 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
2
|
import PeerWallet from 'trac-wallet';
|
|
3
|
-
import Check from '
|
|
4
|
-
import {bufferToAddress} from "
|
|
5
|
-
import {createMessage} from "
|
|
6
|
-
import {OperationType} from "
|
|
7
|
-
import {bufferToBigInt} from "
|
|
8
|
-
import {FEE} from "
|
|
9
|
-
import * as operationsUtils from '
|
|
3
|
+
import Check from '../../../../../../utils/check.js';
|
|
4
|
+
import {bufferToAddress} from "../../../../../state/utils/address.js";
|
|
5
|
+
import {createMessage} from "../../../../../../utils/buffer.js";
|
|
6
|
+
import {OperationType} from "../../../../../../utils/constants.js";
|
|
7
|
+
import {bufferToBigInt} from "../../../../../../utils/amountSerialization.js";
|
|
8
|
+
import {FEE} from "../../../../../state/utils/transaction.js";
|
|
9
|
+
import * as operationsUtils from '../../../../../../utils/applyOperations.js';
|
|
10
10
|
|
|
11
11
|
const MAX_AMOUNT = BigInt('0xffffffffffffffffffffffffffffffff');
|
|
12
12
|
const FEE_BIGINT = bufferToBigInt(FEE);
|
|
@@ -16,15 +16,15 @@ class PartialOperation {
|
|
|
16
16
|
#state;
|
|
17
17
|
#check;
|
|
18
18
|
#config
|
|
19
|
-
#
|
|
19
|
+
#selfAddress
|
|
20
20
|
|
|
21
|
-
constructor(state,
|
|
21
|
+
constructor(state, selfAddress, config) {
|
|
22
22
|
this.#state = state;
|
|
23
23
|
this.#config = config;
|
|
24
24
|
this.#check = new Check(this.#config);
|
|
25
25
|
this.max_amount = MAX_AMOUNT;
|
|
26
26
|
this.fee = FEE_BIGINT;
|
|
27
|
-
this.#
|
|
27
|
+
this.#selfAddress = selfAddress;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
get state() {
|
|
@@ -176,7 +176,7 @@ class PartialOperation {
|
|
|
176
176
|
isOperationNotCompleted(payload) {
|
|
177
177
|
const operationKey = operationsUtils.operationToPayload(payload.type);
|
|
178
178
|
const operation = payload[operationKey];
|
|
179
|
-
const {va, vn, vs} = operation;
|
|
179
|
+
const { va, vn, vs } = operation;
|
|
180
180
|
|
|
181
181
|
const condition = va === undefined && vn === undefined && vs === undefined
|
|
182
182
|
if (!condition) {
|
|
@@ -219,8 +219,10 @@ class PartialOperation {
|
|
|
219
219
|
* Flow: Validator -> submits tx with tap-wallet -> RPC-> Validator -validates tx-> REJECT (self-validation)
|
|
220
220
|
*/
|
|
221
221
|
validateNoSelfValidation(payload) {
|
|
222
|
+
if (!this.#selfAddress) return;
|
|
223
|
+
|
|
222
224
|
const requesterAddress = bufferToAddress(payload.address, this.#config.addressPrefix);
|
|
223
|
-
if (this.#
|
|
225
|
+
if (this.#selfAddress === requesterAddress) {
|
|
224
226
|
throw new Error('Requester address cannot be the same as the validator wallet address.');
|
|
225
227
|
}
|
|
226
228
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { MessageHeader } from '../../../../utils/protobuf/network.cjs';
|
|
2
|
+
|
|
3
|
+
class NetworkMessageRouterV1 {
|
|
4
|
+
#config;
|
|
5
|
+
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.#config = config;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async route(incomingMessage) {
|
|
11
|
+
MessageHeader.decode(incomingMessage);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default NetworkMessageRouterV1;
|
|
@@ -44,10 +44,10 @@ class ConnectionManager {
|
|
|
44
44
|
|
|
45
45
|
const target = this.pickRandomValidator(connectedValidators);
|
|
46
46
|
const entry = this.#validators.get(target);
|
|
47
|
-
if (!entry || !entry.connection || !entry.connection.
|
|
47
|
+
if (!entry || !entry.connection || !entry.connection.protocolSession?.has('legacy')) return null;
|
|
48
48
|
|
|
49
49
|
try {
|
|
50
|
-
entry.connection.
|
|
50
|
+
entry.connection.protocolSession.send(message);
|
|
51
51
|
entry.sent = (entry.sent || 0) + 1;
|
|
52
52
|
} catch (e) {
|
|
53
53
|
// Swallow individual send errors.
|
|
@@ -67,9 +67,9 @@ class ConnectionManager {
|
|
|
67
67
|
if (!this.exists(publicKeyHex) || !this.connected(publicKeyHex)) return false; // Fail silently
|
|
68
68
|
|
|
69
69
|
const validator = this.#validators.get(publicKeyHex);
|
|
70
|
-
if (!validator || !validator.connection || !validator.connection.
|
|
70
|
+
if (!validator || !validator.connection || !validator.connection.protocolSession) return false;
|
|
71
71
|
try {
|
|
72
|
-
validator.connection.
|
|
72
|
+
validator.connection.protocolSession.send(message);
|
|
73
73
|
} catch (e) {
|
|
74
74
|
// Swallow individual send errors.
|
|
75
75
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { sleep } from '../../../utils/helpers.js';
|
|
2
|
-
import { operationToPayload } from '../../../utils/
|
|
2
|
+
import { operationToPayload } from '../../../utils/applyOperations.js';
|
|
3
3
|
/**
|
|
4
4
|
* MessageOrchestrator coordinates message submission, retry, and validator management.
|
|
5
5
|
* It works with ConnectionManager and ledger state to ensure reliable message delivery.
|
|
@@ -37,7 +37,7 @@ class TransactionPoolService {
|
|
|
37
37
|
console.info('TransactionPoolService can not start. Wallet is not enabled');
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
|
-
if (this
|
|
40
|
+
if (this.#scheduler && this.#scheduler.isRunning) {
|
|
41
41
|
console.info('TransactionPoolService is already started');
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
@@ -65,7 +65,6 @@ class TransactionPoolService {
|
|
|
65
65
|
|
|
66
66
|
async #processTransactions() {
|
|
67
67
|
const canValidate = await this.#checkValidationPermissions();
|
|
68
|
-
|
|
69
68
|
if (canValidate && this.#tx_pool.length > 0) {
|
|
70
69
|
const batch = this.#prepareBatch();
|
|
71
70
|
await this.#state.append(batch);
|
|
@@ -8,10 +8,12 @@ import {
|
|
|
8
8
|
class TransactionRateLimiterService {
|
|
9
9
|
#lastCleanup;
|
|
10
10
|
#connectionsStatistics;
|
|
11
|
+
#swarm;
|
|
11
12
|
|
|
12
|
-
constructor() {
|
|
13
|
+
constructor(swarm) {
|
|
13
14
|
this.#lastCleanup = Date.now();
|
|
14
15
|
this.#connectionsStatistics = new Map();
|
|
16
|
+
this.#swarm = swarm
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
/*
|
|
@@ -39,7 +41,7 @@ class TransactionRateLimiterService {
|
|
|
39
41
|
If the peer has exceeded the rate limit, it disconnects the peer.
|
|
40
42
|
Otherwise, it updates the connection info with the current timestamp.
|
|
41
43
|
*/
|
|
42
|
-
handleRateLimit(connection
|
|
44
|
+
handleRateLimit(connection) {
|
|
43
45
|
const peer = b4a.toString(connection.remotePublicKey, 'hex');
|
|
44
46
|
const currentTime = Date.now();
|
|
45
47
|
|
|
@@ -53,7 +55,7 @@ class TransactionRateLimiterService {
|
|
|
53
55
|
|
|
54
56
|
if (this.#hasExceededRateLimit(peer)) {
|
|
55
57
|
console.warn(`Rate limit exceeded for peer ${peer}. Disconnecting...`);
|
|
56
|
-
|
|
58
|
+
this.#swarm.leavePeer(connection.remotePublicKey);
|
|
57
59
|
connection.end();
|
|
58
60
|
return true;
|
|
59
61
|
}
|
package/src/core/state/State.js
CHANGED
|
@@ -201,8 +201,7 @@ class State extends ReadyResource {
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
async getIndexersEntry() {
|
|
204
|
-
|
|
205
|
-
return indexersEntry
|
|
204
|
+
return Object.values(this.#base.system.indexers);
|
|
206
205
|
}
|
|
207
206
|
|
|
208
207
|
async isWkInIndexersEntry(wk) {
|