trac-msb 0.2.9 → 0.2.11
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/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 +4 -21
- package/package.json +16 -12
- package/proto/network/v1/enums/message_type.proto +16 -0
- package/proto/network/v1/enums/result_code.proto +84 -0
- package/proto/network/v1/messages/broadcast_transaction_request.proto +9 -0
- package/proto/network/v1/messages/broadcast_transaction_response.proto +13 -0
- package/proto/network/v1/messages/liveness_request.proto +8 -0
- package/proto/network/v1/messages/liveness_response.proto +11 -0
- package/proto/network/v1/network_message.proto +22 -0
- package/rpc/handlers.js +163 -90
- package/rpc/routes/v1.js +3 -1
- package/rpc/rpc_server.js +3 -3
- package/rpc/rpc_services.js +45 -31
- package/rpc/utils/helpers.js +82 -51
- package/scripts/generate-protobufs.js +37 -12
- package/src/config/args.js +46 -0
- package/src/config/config.js +99 -5
- package/src/config/env.js +86 -7
- package/src/core/network/Network.js +79 -46
- package/src/core/network/protocols/LegacyProtocol.js +21 -11
- package/src/core/network/protocols/NetworkMessages.js +38 -17
- package/src/core/network/protocols/ProtocolInterface.js +14 -2
- package/src/core/network/protocols/ProtocolSession.js +144 -17
- package/src/core/network/protocols/V1Protocol.js +37 -18
- package/src/core/network/protocols/connectionPolicies.js +88 -0
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +26 -20
- package/src/core/network/protocols/{shared/handlers/base/BaseOperationHandler.js → legacy/handlers/BaseStateOperationHandler.js} +25 -15
- package/src/core/network/protocols/legacy/handlers/{GetRequestHandler.js → LegacyGetRequestHandler.js} +6 -6
- package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +23 -0
- package/src/core/network/protocols/{shared/handlers/RoleOperationHandler.js → legacy/handlers/LegacyRoleOperationHandler.js} +20 -13
- package/src/core/network/protocols/{shared/handlers/SubnetworkOperationHandler.js → legacy/handlers/LegacySubnetworkOperationHandler.js} +29 -18
- package/src/core/network/protocols/{shared/handlers/TransferOperationHandler.js → legacy/handlers/LegacyTransferOperationHandler.js} +18 -12
- package/src/core/network/protocols/legacy/validators/base/BaseResponse.js +1 -1
- package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +27 -0
- package/src/core/network/protocols/shared/validators/{PartialBootstrapDeployment.js → PartialBootstrapDeploymentValidator.js} +9 -4
- package/src/core/network/protocols/shared/validators/{base/PartialOperation.js → PartialOperationValidator.js} +47 -25
- package/src/core/network/protocols/shared/validators/{PartialRoleAccess.js → PartialRoleAccessValidator.js} +51 -17
- package/src/core/network/protocols/shared/validators/{PartialTransaction.js → PartialTransactionValidator.js} +21 -7
- package/src/core/network/protocols/shared/validators/{PartialTransfer.js → PartialTransferValidator.js} +26 -9
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +91 -7
- package/src/core/network/protocols/v1/V1ProtocolError.js +91 -0
- package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +65 -0
- package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +389 -0
- package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +87 -0
- package/src/core/network/protocols/v1/validators/V1BaseOperation.js +211 -0
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +26 -0
- package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +276 -0
- package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +15 -0
- package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +17 -0
- package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +210 -0
- package/src/core/network/services/ConnectionManager.js +147 -95
- package/src/core/network/services/MessageOrchestrator.js +152 -28
- package/src/core/network/services/PendingRequestService.js +172 -0
- package/src/core/network/services/TransactionCommitService.js +149 -0
- package/src/core/network/services/TransactionPoolService.js +133 -22
- package/src/core/network/services/TransactionRateLimiterService.js +57 -42
- package/src/core/network/services/ValidatorHealthCheckService.js +127 -0
- package/src/core/network/services/ValidatorObserverService.js +23 -32
- package/src/core/state/State.js +72 -22
- package/src/index.js +8 -5
- package/src/messages/network/v1/NetworkMessageBuilder.js +61 -81
- package/src/messages/network/v1/NetworkMessageDirector.js +16 -50
- package/src/messages/state/ApplyStateMessageBuilder.js +1 -1
- package/src/utils/Scheduler.js +0 -8
- package/src/utils/check.js +1 -1
- package/src/utils/constants.js +68 -19
- package/src/utils/deepEqualApplyPayload.js +40 -0
- package/src/utils/fileUtils.js +13 -0
- package/src/utils/helpers.js +10 -1
- package/src/utils/logger.js +25 -0
- package/src/utils/normalizers.js +38 -0
- package/src/utils/protobuf/networkV1.generated.cjs +2460 -0
- package/src/utils/protobuf/operationHelpers.js +24 -3
- package/src/utils/type.js +26 -0
- package/tests/acceptance/v1/account/account.test.mjs +8 -2
- 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 +3 -2
- package/tests/acceptance/v1/tx/tx.test.mjs +50 -17
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +60 -18
- package/tests/fixtures/check.fixtures.js +33 -32
- package/tests/fixtures/networkV1.fixtures.js +2 -27
- package/tests/fixtures/protobuf.fixtures.js +33 -32
- 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 +6 -10
- package/tests/helpers/transactionPayloads.mjs +2 -2
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +241 -81
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +225 -81
- package/tests/unit/network/LegacyNetworkMessageRouter.test.js +54 -0
- package/tests/unit/network/ProtocolSession.test.js +127 -0
- package/tests/unit/network/networkModule.test.js +4 -1
- package/tests/unit/network/services/ConnectionManager.test.js +450 -0
- package/tests/unit/network/services/MessageOrchestrator.test.js +445 -0
- package/tests/unit/network/services/PendingRequestService.test.js +431 -0
- package/tests/unit/network/services/TransactionCommitService.test.js +246 -0
- package/tests/unit/network/services/TransactionPoolService.test.js +489 -0
- package/tests/unit/network/services/TransactionRateLimiterService.test.js +139 -0
- package/tests/unit/network/services/ValidatorHealthCheckService.test.js +115 -0
- package/tests/unit/network/services/services.test.js +17 -0
- package/tests/unit/network/utils/v1TestUtils.js +153 -0
- package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +151 -0
- package/tests/unit/network/v1/V1BaseOperation.test.js +356 -0
- package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +129 -0
- package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +53 -0
- package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +512 -0
- package/tests/unit/network/v1/V1LivenessRequest.test.js +32 -0
- package/tests/unit/network/v1/V1LivenessResponse.test.js +45 -0
- package/tests/unit/network/v1/V1ResultCode.test.js +84 -0
- package/tests/unit/network/v1/V1ValidationSchema.test.js +13 -0
- package/tests/unit/network/v1/connectionPolicies.test.js +49 -0
- package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +284 -0
- package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +794 -0
- package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +193 -0
- package/tests/unit/network/v1/v1.handlers.test.js +15 -0
- package/tests/unit/network/v1/v1.test.js +19 -0
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +119 -0
- package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +136 -0
- package/tests/unit/network/v1/v1ValidationSchema/common.test.js +308 -0
- package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +90 -0
- package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +133 -0
- package/tests/unit/unit.test.js +2 -2
- package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +102 -0
- 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/protobuf/operationHelpers.test.js +2 -4
- package/tests/unit/utils/type/type.test.js +25 -0
- package/tests/unit/utils/utils.test.js +2 -0
- package/.github/workflows/acceptance-tests.yml +0 -42
- package/.github/workflows/publish.yml +0 -33
- package/.github/workflows/unit-tests.yml +0 -40
- package/proto/network.proto +0 -74
- package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +0 -37
- package/src/utils/protobuf/network.cjs +0 -840
- package/tests/unit/network/ConnectionManager.test.js +0 -191
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
2
|
import {safeDecodeApplyOperation} from "../../../../../utils/protobuf/operationHelpers.js";
|
|
3
3
|
import deploymentEntryUtils from "../../../../state/utils/deploymentEntry.js";
|
|
4
|
-
import
|
|
4
|
+
import PartialOperationValidator from './PartialOperationValidator.js';
|
|
5
|
+
import {ResultCode} from "../../../../../utils/constants.js";
|
|
6
|
+
import SharedValidatorRejectionError from '../errors/SharedValidatorRejectionError.js';
|
|
5
7
|
|
|
6
|
-
class
|
|
8
|
+
class PartialTransactionValidator extends PartialOperationValidator {
|
|
7
9
|
#config
|
|
8
10
|
|
|
9
11
|
constructor(state, selfAddress, config) {
|
|
@@ -32,14 +34,20 @@ class PartialTransaction extends PartialOperation {
|
|
|
32
34
|
|
|
33
35
|
validateMsbBootstrap(payload) {
|
|
34
36
|
if (!b4a.equals(this.#config.bootstrap, payload.txo.mbs)) {
|
|
35
|
-
throw new
|
|
37
|
+
throw new SharedValidatorRejectionError(
|
|
38
|
+
ResultCode.MSB_BOOTSTRAP_MISMATCH,
|
|
39
|
+
`Declared MSB bootstrap is different than network bootstrap in transaction operation: ${payload.txo.mbs.toString('hex')}`
|
|
40
|
+
);
|
|
36
41
|
}
|
|
37
42
|
}
|
|
38
43
|
|
|
39
44
|
async validateIfExternalBootstrapHasBeenDeployed(payload) {
|
|
40
45
|
const externalBootstrapResult = await this.state.getRegisteredBootstrapEntry(payload.txo.bs.toString('hex'));
|
|
41
46
|
if (externalBootstrapResult === null) {
|
|
42
|
-
throw new
|
|
47
|
+
throw new SharedValidatorRejectionError(
|
|
48
|
+
ResultCode.EXTERNAL_BOOTSTRAP_NOT_DEPLOYED,
|
|
49
|
+
`External bootstrap with hash ${payload.txo.bs.toString('hex')} is not registered as deployment entry.`
|
|
50
|
+
);
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
const decodedPayload = deploymentEntryUtils.decode(externalBootstrapResult, this.#config.addressLength);
|
|
@@ -47,17 +55,23 @@ class PartialTransaction extends PartialOperation {
|
|
|
47
55
|
const getBootstrapTransactionTxPayload = await this.state.get(txHash.toString('hex'));
|
|
48
56
|
|
|
49
57
|
if (getBootstrapTransactionTxPayload === null) {
|
|
50
|
-
throw new
|
|
58
|
+
throw new SharedValidatorRejectionError(
|
|
59
|
+
ResultCode.EXTERNAL_BOOTSTRAP_TX_MISSING,
|
|
60
|
+
`External bootstrap is not registered as usual tx ${externalBootstrapResult.toString('hex')}: ${payload}`
|
|
61
|
+
);
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
const decodedBootstrapDeployment = safeDecodeApplyOperation(getBootstrapTransactionTxPayload)
|
|
54
65
|
|
|
55
66
|
// edge case
|
|
56
67
|
if (!b4a.equals(decodedBootstrapDeployment.bdo.bs, payload.txo.bs)) {
|
|
57
|
-
throw new
|
|
68
|
+
throw new SharedValidatorRejectionError(
|
|
69
|
+
ResultCode.EXTERNAL_BOOTSTRAP_MISMATCH,
|
|
70
|
+
`External bootstrap does not match the one in the transaction payload: ${decodedBootstrapDeployment.bdo.bs.toString('hex')} !== ${payload.txo.bs.toString('hex')}`
|
|
71
|
+
);
|
|
58
72
|
}
|
|
59
73
|
}
|
|
60
74
|
|
|
61
75
|
}
|
|
62
76
|
|
|
63
|
-
export default
|
|
77
|
+
export default PartialTransactionValidator;
|
|
@@ -2,9 +2,11 @@ import PeerWallet from 'trac-wallet';
|
|
|
2
2
|
|
|
3
3
|
import {bufferToAddress} from "../../../../state/utils/address.js";
|
|
4
4
|
import {bufferToBigInt} from "../../../../../utils/amountSerialization.js";
|
|
5
|
-
import
|
|
5
|
+
import {ResultCode} from "../../../../../utils/constants.js";
|
|
6
|
+
import PartialOperationValidator from './PartialOperationValidator.js';
|
|
7
|
+
import SharedValidatorRejectionError from '../errors/SharedValidatorRejectionError.js';
|
|
6
8
|
|
|
7
|
-
class
|
|
9
|
+
class PartialTransferValidator extends PartialOperationValidator {
|
|
8
10
|
#config
|
|
9
11
|
|
|
10
12
|
constructor(state, selfAddress, config) {
|
|
@@ -31,12 +33,18 @@ class PartialTransfer extends PartialOperation {
|
|
|
31
33
|
#validateRecipientAddress(payload) {
|
|
32
34
|
const incomingAddress = bufferToAddress(payload.tro.to, this.#config.addressPrefix);
|
|
33
35
|
if (!incomingAddress) {
|
|
34
|
-
throw new
|
|
36
|
+
throw new SharedValidatorRejectionError(
|
|
37
|
+
ResultCode.TRANSFER_RECIPIENT_ADDRESS_INVALID,
|
|
38
|
+
'Invalid recipient address in transfer payload.'
|
|
39
|
+
);
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
const incomingPublicKey = PeerWallet.decodeBech32mSafe(incomingAddress);
|
|
38
43
|
if (incomingPublicKey === null) {
|
|
39
|
-
throw new
|
|
44
|
+
throw new SharedValidatorRejectionError(
|
|
45
|
+
ResultCode.TRANSFER_RECIPIENT_PUBLIC_KEY_INVALID,
|
|
46
|
+
'Invalid recipient public key in transfer payload.'
|
|
47
|
+
);
|
|
40
48
|
}
|
|
41
49
|
|
|
42
50
|
}
|
|
@@ -47,7 +55,10 @@ class PartialTransfer extends PartialOperation {
|
|
|
47
55
|
|
|
48
56
|
const transferAmount = bufferToBigInt(payload.tro.am);
|
|
49
57
|
if (transferAmount > this.max_amount) {
|
|
50
|
-
throw new
|
|
58
|
+
throw new SharedValidatorRejectionError(
|
|
59
|
+
ResultCode.TRANSFER_AMOUNT_TOO_LARGE,
|
|
60
|
+
'Transfer amount exceeds maximum allowed value'
|
|
61
|
+
);
|
|
51
62
|
}
|
|
52
63
|
|
|
53
64
|
const isSelfTransfer = senderAddress === recipientAddress;
|
|
@@ -55,12 +66,15 @@ class PartialTransfer extends PartialOperation {
|
|
|
55
66
|
|
|
56
67
|
const senderEntry = await this.state.getNodeEntryUnsigned(senderAddress);
|
|
57
68
|
if (!senderEntry) {
|
|
58
|
-
throw new
|
|
69
|
+
throw new SharedValidatorRejectionError(ResultCode.TRANSFER_SENDER_NOT_FOUND, 'Sender account not found');
|
|
59
70
|
}
|
|
60
71
|
|
|
61
72
|
const senderBalance = bufferToBigInt(senderEntry.balance);
|
|
62
73
|
if (!(senderBalance >= totalDeductedAmount)) {
|
|
63
|
-
throw new
|
|
74
|
+
throw new SharedValidatorRejectionError(
|
|
75
|
+
ResultCode.TRANSFER_INSUFFICIENT_BALANCE,
|
|
76
|
+
'Insufficient balance for transfer' + (isSelfTransfer ? ' fee' : ' + fee')
|
|
77
|
+
);
|
|
64
78
|
}
|
|
65
79
|
|
|
66
80
|
if (!isSelfTransfer) {
|
|
@@ -69,11 +83,14 @@ class PartialTransfer extends PartialOperation {
|
|
|
69
83
|
const recipientBalance = bufferToBigInt(recipientEntry.balance);
|
|
70
84
|
const newRecipientBalance = recipientBalance + transferAmount;
|
|
71
85
|
if (newRecipientBalance > this.max_amount) {
|
|
72
|
-
throw new
|
|
86
|
+
throw new SharedValidatorRejectionError(
|
|
87
|
+
ResultCode.TRANSFER_RECIPIENT_BALANCE_OVERFLOW,
|
|
88
|
+
'Transfer would cause recipient balance to exceed maximum allowed value'
|
|
89
|
+
);
|
|
73
90
|
}
|
|
74
91
|
}
|
|
75
92
|
}
|
|
76
93
|
}
|
|
77
94
|
}
|
|
78
95
|
|
|
79
|
-
export default
|
|
96
|
+
export default PartialTransferValidator;
|
|
@@ -1,15 +1,99 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { decodeV1networkOperation } from '../../../../utils/protobuf/operationHelpers.js'
|
|
2
|
+
import b4a from 'b4a'
|
|
3
|
+
import { NetworkOperationType, V1_PROTOCOL_PAYLOAD_MAX_SIZE } from '../../../../utils/constants.js'
|
|
4
|
+
import { publicKeyToAddress } from '../../../../utils/helpers.js'
|
|
5
|
+
import V1LivenessOperationHandler from './handlers/V1LivenessOperationHandler.js'
|
|
6
|
+
import V1BroadcastTransactionOperationHandler from './handlers/V1BroadcastTransactionOperationHandler.js'
|
|
2
7
|
|
|
3
8
|
class NetworkMessageRouterV1 {
|
|
4
|
-
#config
|
|
9
|
+
#config
|
|
10
|
+
#livenessRequestHandler
|
|
11
|
+
#broadcastTransactionHandler
|
|
5
12
|
|
|
6
|
-
constructor(
|
|
7
|
-
|
|
13
|
+
constructor(
|
|
14
|
+
state,
|
|
15
|
+
wallet,
|
|
16
|
+
rateLimiterService,
|
|
17
|
+
txPoolService,
|
|
18
|
+
pendingRequestsService,
|
|
19
|
+
transactionCommitService,
|
|
20
|
+
config
|
|
21
|
+
) {
|
|
22
|
+
this.#config = config
|
|
23
|
+
this.#livenessRequestHandler = new V1LivenessOperationHandler(
|
|
24
|
+
wallet,
|
|
25
|
+
rateLimiterService,
|
|
26
|
+
pendingRequestsService,
|
|
27
|
+
config
|
|
28
|
+
);
|
|
29
|
+
this.#broadcastTransactionHandler = new V1BroadcastTransactionOperationHandler(
|
|
30
|
+
state,
|
|
31
|
+
wallet,
|
|
32
|
+
rateLimiterService,
|
|
33
|
+
txPoolService,
|
|
34
|
+
pendingRequestsService,
|
|
35
|
+
transactionCommitService,
|
|
36
|
+
config
|
|
37
|
+
);
|
|
8
38
|
}
|
|
9
39
|
|
|
10
|
-
async route(incomingMessage) {
|
|
11
|
-
|
|
40
|
+
async route(incomingMessage, connection) {
|
|
41
|
+
if (!this.#preValidate(incomingMessage)) {
|
|
42
|
+
this.#disconnect(connection, 'Pre-validation failed for incoming V1 message')
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
let decodedMessage;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
decodedMessage = decodeV1networkOperation(incomingMessage)
|
|
49
|
+
} catch (error) {
|
|
50
|
+
this.#disconnect(connection, `Failed to decode incoming V1 message: ${error.message}`)
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// TODO: Decide if we really need to check decodedMessage.type here, since this is done
|
|
55
|
+
// again in the next switch statement
|
|
56
|
+
if (!decodedMessage || !Number.isInteger(decodedMessage.type) || decodedMessage.type <= 0) {
|
|
57
|
+
this.#disconnect(connection, `Invalid V1 message type: ${decodedMessage?.type}`)
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// We received a v1 message, so we set the connection protocol accordingly
|
|
62
|
+
connection.protocolSession.setV1AsPreferredProtocol()
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
switch (decodedMessage.type) {
|
|
66
|
+
case NetworkOperationType.LIVENESS_REQUEST:
|
|
67
|
+
await this.#livenessRequestHandler.handleRequest(decodedMessage, connection);
|
|
68
|
+
break;
|
|
69
|
+
case NetworkOperationType.LIVENESS_RESPONSE:
|
|
70
|
+
await this.#livenessRequestHandler.handleResponse(decodedMessage, connection);
|
|
71
|
+
break;
|
|
72
|
+
|
|
73
|
+
case NetworkOperationType.BROADCAST_TRANSACTION_REQUEST:
|
|
74
|
+
await this.#broadcastTransactionHandler.handleRequest(decodedMessage, connection);
|
|
75
|
+
break;
|
|
76
|
+
|
|
77
|
+
case NetworkOperationType.BROADCAST_TRANSACTION_RESPONSE:
|
|
78
|
+
await this.#broadcastTransactionHandler.handleResponse(decodedMessage, connection);
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
this.#disconnect(connection, `Unsupported V1 message type: ${decodedMessage.type}`)
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
this.#disconnect(connection, `Unhandled error while routing V1 message: ${error.message}`)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#preValidate(incomingMessage) {
|
|
89
|
+
return !(!incomingMessage || !b4a.isBuffer(incomingMessage) || incomingMessage.length === 0 || incomingMessage.length > V1_PROTOCOL_PAYLOAD_MAX_SIZE);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
#disconnect(connection, reason) {
|
|
93
|
+
const sender = publicKeyToAddress(connection.remotePublicKey, this.#config)
|
|
94
|
+
console.error(`NetworkMessageRouterV1: ${reason}, sender: ${sender}`)
|
|
95
|
+
connection.end();
|
|
12
96
|
}
|
|
13
97
|
}
|
|
14
98
|
|
|
15
|
-
export default NetworkMessageRouterV1
|
|
99
|
+
export default NetworkMessageRouterV1
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {ResultCode} from '../../../../utils/constants.js';
|
|
2
|
+
|
|
3
|
+
export function getResultCode(err) {
|
|
4
|
+
return err instanceof V1ProtocolError ? err.resultCode : ResultCode.UNEXPECTED_ERROR;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function shouldEndConnection(err) {
|
|
8
|
+
return err instanceof V1ProtocolError ? Boolean(err.endConnection) : false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* V1 protocol error type.
|
|
13
|
+
*
|
|
14
|
+
* `V1ProtocolError` is the v1 base class used by handlers/validators to attach:
|
|
15
|
+
* - `resultCode`: a stable `ResultCode` enum value for programmatic handling
|
|
16
|
+
* - `endConnection`: a transport hint (close peer connection after responding)
|
|
17
|
+
*/
|
|
18
|
+
export class V1ProtocolError extends Error {
|
|
19
|
+
/**
|
|
20
|
+
* @param {number} resultCode Stable rejection reason (a `ResultCode` enum value).
|
|
21
|
+
* @param {string} message Human-readable error message.
|
|
22
|
+
* @param {boolean} [endConnection=false] Whether the transport should end the connection after responding.
|
|
23
|
+
*/
|
|
24
|
+
constructor(resultCode, message, endConnection = false) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = this.constructor.name;
|
|
27
|
+
this.resultCode = resultCode;
|
|
28
|
+
this.endConnection = Boolean(endConnection);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class V1InvalidPayloadError extends V1ProtocolError {
|
|
33
|
+
constructor(message = 'Invalid payload', endConnection = false) {
|
|
34
|
+
super(ResultCode.INVALID_PAYLOAD, message, endConnection);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class V1TxInvalidPayloadError extends V1ProtocolError {
|
|
39
|
+
constructor(message = 'Invalid tx payload', endConnection = false) {
|
|
40
|
+
super(ResultCode.TX_INVALID_PAYLOAD, message, endConnection);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class V1SignatureInvalidError extends V1ProtocolError {
|
|
45
|
+
constructor(message = 'Signature invalid', endConnection = true) {
|
|
46
|
+
super(ResultCode.SIGNATURE_INVALID, message, endConnection);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class V1RateLimitedError extends V1ProtocolError {
|
|
51
|
+
constructor(message = 'Rate limited', endConnection = true) {
|
|
52
|
+
super(ResultCode.RATE_LIMITED, message, endConnection);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class V1UnexpectedError extends V1ProtocolError {
|
|
57
|
+
constructor(message = 'Unexpected error', endConnection = true) {
|
|
58
|
+
super(ResultCode.UNEXPECTED_ERROR, message, endConnection);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class V1TimeoutError extends V1ProtocolError {
|
|
63
|
+
constructor(message = 'Request timed out', endConnection = false) {
|
|
64
|
+
super(ResultCode.TIMEOUT, message, endConnection);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class V1NodeHasNoWriteAccess extends V1ProtocolError {
|
|
69
|
+
constructor(message = 'Node has no write access', endConnection = true) {
|
|
70
|
+
super(ResultCode.NODE_HAS_NO_WRITE_ACCESS, message, endConnection);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class V1TxAcceptedProofUnavailable extends V1ProtocolError {
|
|
75
|
+
constructor(message = 'Transaction accepted but proof is unavailable', endConnection = false, timestamp = 0) {
|
|
76
|
+
super(ResultCode.TX_ACCEPTED_PROOF_UNAVAILABLE, message, endConnection);
|
|
77
|
+
this.timestamp = Number.isSafeInteger(timestamp) && timestamp > 0 ? timestamp : 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class V1NodeOverloadedError extends V1ProtocolError {
|
|
82
|
+
constructor(message = 'Commit queue is full', endConnection = true) {
|
|
83
|
+
super(ResultCode.NODE_OVERLOADED, message, endConnection);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class V1TxAlreadyPendingError extends V1ProtocolError {
|
|
88
|
+
constructor(message = 'Transaction is already pending', endConnection = false) {
|
|
89
|
+
super(ResultCode.TX_ALREADY_PENDING, message, endConnection);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {publicKeyToAddress} from "../../../../../utils/helpers.js";
|
|
2
|
+
import {V1ProtocolError, V1UnexpectedError} from "../V1ProtocolError.js";
|
|
3
|
+
|
|
4
|
+
class V1BaseOperationHandler {
|
|
5
|
+
#rateLimiterService;
|
|
6
|
+
#pendingRequestService;
|
|
7
|
+
#config;
|
|
8
|
+
|
|
9
|
+
constructor(rateLimiterService, pendingRequestService, config) {
|
|
10
|
+
this.#rateLimiterService = rateLimiterService;
|
|
11
|
+
this.#pendingRequestService = pendingRequestService;
|
|
12
|
+
this.#config = config;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get config() {
|
|
16
|
+
return this.#config;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
applyRateLimit(connection) {
|
|
20
|
+
if (!this.#config.disableRateLimit) {
|
|
21
|
+
this.#rateLimiterService.v1HandleRateLimit(connection);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async resolvePendingResponse(message, connection, validator, extractResultCode) {
|
|
26
|
+
const pendingRequestServiceEntry = this.#pendingRequestService.getPendingRequest(message.id);
|
|
27
|
+
if (!pendingRequestServiceEntry) return false;
|
|
28
|
+
|
|
29
|
+
this.#pendingRequestService.stopPendingRequestTimeout(message.id);
|
|
30
|
+
await validator.validate(message, connection, pendingRequestServiceEntry);
|
|
31
|
+
|
|
32
|
+
const resultCode = extractResultCode(message);
|
|
33
|
+
this.#pendingRequestService.resolvePendingRequest(message.id, resultCode);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
handlePendingResponseError(messageId, connection, error, step) {
|
|
38
|
+
const protocolError = this.#toProtocolError(error);
|
|
39
|
+
const rejected = this.#pendingRequestService.rejectPendingRequest(messageId, protocolError);
|
|
40
|
+
if (!rejected) return;
|
|
41
|
+
this.displayError(step, connection.remotePublicKey, protocolError);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async sendResponseAndMaybeClose(connection, response, endConnection) {
|
|
45
|
+
connection.protocolSession.sendAndForget(response);
|
|
46
|
+
if (!endConnection) return;
|
|
47
|
+
|
|
48
|
+
await connection.flush();
|
|
49
|
+
connection.end();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
displayError(step = "undefined step", senderPublicKey, error) {
|
|
53
|
+
const errorMessage = error?.message ?? 'Unexpected error';
|
|
54
|
+
console.error(`${this.constructor.name}: ${step} ${publicKeyToAddress(senderPublicKey, this.#config)}: ${errorMessage}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#toProtocolError(error) {
|
|
58
|
+
if (error instanceof V1ProtocolError) {
|
|
59
|
+
return error;
|
|
60
|
+
}
|
|
61
|
+
return new V1UnexpectedError(error?.message ?? 'Unexpected error');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default V1BaseOperationHandler;
|