trac-msb 0.2.12 → 0.2.13
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/package.json +9 -4
- 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/rpc_services.js +22 -4
- package/scripts/generate-protobufs.js +37 -12
- package/src/config/config.js +26 -5
- package/src/config/env.js +25 -11
- package/src/core/network/Network.js +73 -36
- 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 +25 -19
- package/src/core/network/protocols/{shared/handlers/base/BaseOperationHandler.js → legacy/handlers/BaseStateOperationHandler.js} +23 -12
- 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} +18 -11
- package/src/core/network/protocols/{shared/handlers/SubnetworkOperationHandler.js → legacy/handlers/LegacySubnetworkOperationHandler.js} +28 -17
- package/src/core/network/protocols/{shared/handlers/TransferOperationHandler.js → legacy/handlers/LegacyTransferOperationHandler.js} +17 -11
- 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 +146 -94
- package/src/core/network/services/MessageOrchestrator.js +151 -27
- 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 +129 -18
- package/src/core/network/services/TransactionRateLimiterService.js +52 -34
- package/src/core/network/services/ValidatorHealthCheckService.js +127 -0
- package/src/core/network/services/ValidatorObserverService.js +18 -26
- package/src/core/state/State.js +70 -19
- package/src/index.js +5 -4
- package/src/messages/network/v1/NetworkMessageBuilder.js +59 -79
- package/src/messages/network/v1/NetworkMessageDirector.js +16 -50
- package/src/utils/Scheduler.js +0 -8
- package/src/utils/constants.js +71 -5
- package/src/utils/deepEqualApplyPayload.js +40 -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/tests/acceptance/v1/account/account.test.mjs +8 -2
- package/tests/acceptance/v1/tx/tx.test.mjs +23 -1
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +34 -6
- package/tests/fixtures/networkV1.fixtures.js +2 -28
- package/tests/helpers/transactionPayloads.mjs +2 -2
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +239 -79
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +223 -77
- 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/protobuf/operationHelpers.test.js +2 -4
- package/tests/unit/utils/utils.test.js +1 -0
- package/.github/workflows/acceptance-tests.yml +0 -38
- package/.github/workflows/lint-pr-title.yml +0 -26
- package/.github/workflows/publish.yml +0 -33
- package/.github/workflows/unit-tests.yml +0 -34
- 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
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import {test} from 'brittle';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
import Autobase from 'autobase';
|
|
4
|
+
import Hypercore from 'hypercore';
|
|
5
|
+
|
|
6
|
+
import V1BroadcastTransactionResponse, {extractRequiredVaFromDecodedTx} from '../../../../src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js';
|
|
7
|
+
import {V1ProtocolError} from '../../../../src/core/network/protocols/v1/V1ProtocolError.js';
|
|
8
|
+
import Check from '../../../../src/utils/check.js';
|
|
9
|
+
import {
|
|
10
|
+
unsafeEncodeApplyOperation,
|
|
11
|
+
unsafeDecodeApplyOperation,
|
|
12
|
+
} from '../../../../src/utils/protobuf/operationHelpers.js';
|
|
13
|
+
import {addressToBuffer} from '../../../../src/core/state/utils/address.js';
|
|
14
|
+
import {publicKeyToAddress} from '../../../../src/utils/helpers.js';
|
|
15
|
+
import {OperationType, ResultCode} from '../../../../src/utils/constants.js';
|
|
16
|
+
import { config } from '../../../helpers/config.js';
|
|
17
|
+
import protobufFixtures from '../../../fixtures/protobuf.fixtures.js';
|
|
18
|
+
import { testKeyPair1 } from '../../../fixtures/apply.fixtures.js';
|
|
19
|
+
|
|
20
|
+
const remotePublicKey = b4a.from(testKeyPair1.publicKey, 'hex');
|
|
21
|
+
const remoteAddressBuffer = addressToBuffer(publicKeyToAddress(remotePublicKey, config), config.addressPrefix);
|
|
22
|
+
const writerKey = b4a.alloc(32, 2);
|
|
23
|
+
|
|
24
|
+
const createState = (overrides = {}) => ({
|
|
25
|
+
verifyProofOfPublication: async () => ({ ok: true }),
|
|
26
|
+
getRegisteredWriterKey: async () => remoteAddressBuffer,
|
|
27
|
+
getNodeEntry: async () => ({
|
|
28
|
+
isWriter: true,
|
|
29
|
+
wk: writerKey,
|
|
30
|
+
}),
|
|
31
|
+
...overrides
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const createValidator = (stateOverrides = {}) =>
|
|
35
|
+
new V1BroadcastTransactionResponse(createState(stateOverrides), config);
|
|
36
|
+
|
|
37
|
+
const overrideCheckMethods = (t, overrides) => {
|
|
38
|
+
const originals = {};
|
|
39
|
+
for (const [name, impl] of Object.entries(overrides)) {
|
|
40
|
+
originals[name] = Check.prototype[name];
|
|
41
|
+
Check.prototype[name] = impl;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
t.teardown(() => {
|
|
45
|
+
for (const [name, original] of Object.entries(originals)) {
|
|
46
|
+
Check.prototype[name] = original;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const overrideFunction = (t, target, key, replacement) => {
|
|
52
|
+
const original = target[key];
|
|
53
|
+
target[key] = replacement;
|
|
54
|
+
t.teardown(() => {
|
|
55
|
+
target[key] = original;
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
test('extractRequiredVaFromDecodedTx throws VALIDATOR_TX_OBJECT_INVALID for non-object', t => {
|
|
60
|
+
try {
|
|
61
|
+
extractRequiredVaFromDecodedTx(null);
|
|
62
|
+
t.fail('expected throw');
|
|
63
|
+
} catch (err) {
|
|
64
|
+
t.is(err.resultCode, ResultCode.VALIDATOR_TX_OBJECT_INVALID);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('extractRequiredVaFromDecodedTx throws VALIDATOR_VA_MISSING when va is missing', t => {
|
|
69
|
+
try {
|
|
70
|
+
extractRequiredVaFromDecodedTx({type: 1, txo: {tx: b4a.alloc(32)}});
|
|
71
|
+
t.fail('expected throw');
|
|
72
|
+
} catch (err) {
|
|
73
|
+
t.is(err.resultCode, ResultCode.VALIDATOR_VA_MISSING);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('extractRequiredVaFromDecodedTx returns va buffer when present', t => {
|
|
78
|
+
const va = b4a.alloc(39, 1);
|
|
79
|
+
const extracted = extractRequiredVaFromDecodedTx({
|
|
80
|
+
type: OperationType.TX,
|
|
81
|
+
txo: {va}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
t.ok(b4a.equals(extracted, va));
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('validate skips proof validation when result code is non-OK', async t => {
|
|
88
|
+
const validator = createValidator();
|
|
89
|
+
let proofCalled = false;
|
|
90
|
+
|
|
91
|
+
validator.isPayloadSchemaValid = () => true;
|
|
92
|
+
validator.validateResponseType = () => true;
|
|
93
|
+
validator.validatePeerCorrectness = () => true;
|
|
94
|
+
validator.validateSignature = async () => true;
|
|
95
|
+
validator.verifyProofOfPublication = async () => {
|
|
96
|
+
proofCalled = true;
|
|
97
|
+
return {};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const payload = {
|
|
101
|
+
broadcast_transaction_response: {
|
|
102
|
+
result: ResultCode.TIMEOUT,
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const result = await validator.validate(
|
|
107
|
+
payload,
|
|
108
|
+
{ remotePublicKey: b4a.alloc(32, 1) },
|
|
109
|
+
{},
|
|
110
|
+
null
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
t.is(result, true);
|
|
114
|
+
t.absent(proofCalled);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('validate runs proof validation pipeline when result code is OK', async t => {
|
|
118
|
+
const validator = createValidator();
|
|
119
|
+
const calls = [];
|
|
120
|
+
|
|
121
|
+
validator.isPayloadSchemaValid = () => calls.push('schema');
|
|
122
|
+
validator.validateResponseType = () => calls.push('type');
|
|
123
|
+
validator.validatePeerCorrectness = () => calls.push('peer');
|
|
124
|
+
validator.validateSignature = async () => calls.push('signature');
|
|
125
|
+
validator.verifyProofOfPublication = async () => {
|
|
126
|
+
calls.push('proof');
|
|
127
|
+
return { proof: {}, manifest: {} };
|
|
128
|
+
};
|
|
129
|
+
validator.assertProofPayloadMatchesRequestPayload = async () => {
|
|
130
|
+
calls.push('assert-proof-payload');
|
|
131
|
+
return {
|
|
132
|
+
validatorDecodedTx: { type: OperationType.TX, txo: { va: remoteAddressBuffer } },
|
|
133
|
+
manifest: {}
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
validator.validateDecodedCompletePayloadSchema = () => calls.push('schema-complete');
|
|
137
|
+
validator.validateWritingKey = async () => {
|
|
138
|
+
calls.push('writer-key');
|
|
139
|
+
return {
|
|
140
|
+
writerKeyFromManifest: writerKey,
|
|
141
|
+
validatorAddressCorrelatedWithManifest: remoteAddressBuffer
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
validator.validateValidatorCorrectness = async () => calls.push('validator-correctness');
|
|
145
|
+
|
|
146
|
+
const result = await validator.validate(
|
|
147
|
+
{ broadcast_transaction_response: { result: ResultCode.OK } },
|
|
148
|
+
{ remotePublicKey },
|
|
149
|
+
{ requestTxData: b4a.alloc(1), requestedTo: b4a.toString(remotePublicKey, 'hex') },
|
|
150
|
+
{}
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
t.is(result, true);
|
|
154
|
+
t.alike(calls, [
|
|
155
|
+
'schema',
|
|
156
|
+
'type',
|
|
157
|
+
'peer',
|
|
158
|
+
'signature',
|
|
159
|
+
'proof',
|
|
160
|
+
'assert-proof-payload',
|
|
161
|
+
'schema-complete',
|
|
162
|
+
'writer-key',
|
|
163
|
+
'validator-correctness'
|
|
164
|
+
]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('validate rejects TX_COMMITTED_RECEIPT_MISSING as validator internal error', async t => {
|
|
168
|
+
const validator = createValidator();
|
|
169
|
+
|
|
170
|
+
validator.isPayloadSchemaValid = () => true;
|
|
171
|
+
validator.validateResponseType = () => true;
|
|
172
|
+
validator.validatePeerCorrectness = () => true;
|
|
173
|
+
validator.validateSignature = async () => true;
|
|
174
|
+
|
|
175
|
+
const payload = {
|
|
176
|
+
broadcast_transaction_response: {
|
|
177
|
+
result: ResultCode.TX_COMMITTED_RECEIPT_MISSING,
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
await validator.validate(
|
|
183
|
+
payload,
|
|
184
|
+
{ remotePublicKey: b4a.alloc(32, 1) },
|
|
185
|
+
{},
|
|
186
|
+
null
|
|
187
|
+
);
|
|
188
|
+
t.fail('expected validate to throw');
|
|
189
|
+
} catch (error) {
|
|
190
|
+
t.ok(error instanceof V1ProtocolError);
|
|
191
|
+
t.is(error.resultCode, ResultCode.TX_COMMITTED_RECEIPT_MISSING);
|
|
192
|
+
t.is(error.endConnection, true);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('validateDecodedCompletePayloadSchema throws for missing, unknown and unsupported types', t => {
|
|
197
|
+
const validator = createValidator();
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
validator.validateDecodedCompletePayloadSchema({});
|
|
201
|
+
t.fail('expected missing type to throw');
|
|
202
|
+
} catch (error) {
|
|
203
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_RESPONSE_TX_TYPE_INVALID);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
validator.validateDecodedCompletePayloadSchema({ type: 9999 });
|
|
208
|
+
t.fail('expected unknown type to throw');
|
|
209
|
+
} catch (error) {
|
|
210
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_RESPONSE_TX_TYPE_UNKNOWN);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
validator.validateDecodedCompletePayloadSchema({ type: OperationType.ADD_ADMIN });
|
|
215
|
+
t.fail('expected unsupported type to throw');
|
|
216
|
+
} catch (error) {
|
|
217
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_RESPONSE_TX_TYPE_UNSUPPORTED);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('validateDecodedCompletePayloadSchema selects proper validators for supported operation types', t => {
|
|
222
|
+
const validator = createValidator();
|
|
223
|
+
const called = [];
|
|
224
|
+
|
|
225
|
+
overrideCheckMethods(t, {
|
|
226
|
+
validateRoleAccessOperation: () => {
|
|
227
|
+
called.push('role');
|
|
228
|
+
return true;
|
|
229
|
+
},
|
|
230
|
+
validateBootstrapDeploymentOperation: () => {
|
|
231
|
+
called.push('bootstrap');
|
|
232
|
+
return true;
|
|
233
|
+
},
|
|
234
|
+
validateTransactionOperation: () => {
|
|
235
|
+
called.push('tx');
|
|
236
|
+
return true;
|
|
237
|
+
},
|
|
238
|
+
validateTransferOperation: () => {
|
|
239
|
+
called.push('transfer');
|
|
240
|
+
return true;
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
validator.validateDecodedCompletePayloadSchema({ type: OperationType.ADD_WRITER });
|
|
245
|
+
validator.validateDecodedCompletePayloadSchema({ type: OperationType.BOOTSTRAP_DEPLOYMENT });
|
|
246
|
+
validator.validateDecodedCompletePayloadSchema({ type: OperationType.TX });
|
|
247
|
+
validator.validateDecodedCompletePayloadSchema({ type: OperationType.TRANSFER });
|
|
248
|
+
|
|
249
|
+
t.alike(called, ['role', 'bootstrap', 'tx', 'transfer']);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
test('validateDecodedCompletePayloadSchema throws VALIDATOR_RESPONSE_SCHEMA_INVALID when selected validator fails', t => {
|
|
253
|
+
const validator = createValidator();
|
|
254
|
+
|
|
255
|
+
overrideCheckMethods(t, {
|
|
256
|
+
validateTransactionOperation: () => false,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
validator.validateDecodedCompletePayloadSchema({ type: OperationType.TX });
|
|
261
|
+
t.fail('expected schema invalid');
|
|
262
|
+
} catch (error) {
|
|
263
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_RESPONSE_SCHEMA_INVALID);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
test('verifyProofOfPublication delegates verification to state instance', async t => {
|
|
268
|
+
const validator = createValidator({
|
|
269
|
+
verifyProofOfPublication: receivedProof => {
|
|
270
|
+
t.ok(b4a.equals(receivedProof, proof));
|
|
271
|
+
return { ok: true };
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
const proof = b4a.from('deadbeef', 'hex');
|
|
275
|
+
|
|
276
|
+
const result = await validator.verifyProofOfPublication({
|
|
277
|
+
broadcast_transaction_response: {
|
|
278
|
+
proof,
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
t.alike(result, { ok: true });
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('assertProofPayloadMatchesRequestPayload throws when pending request tx data is missing', async t => {
|
|
286
|
+
const validator = createValidator();
|
|
287
|
+
|
|
288
|
+
try {
|
|
289
|
+
await validator.assertProofPayloadMatchesRequestPayload(
|
|
290
|
+
{ proof: { block: { value: b4a.alloc(1) }, manifest: {} } },
|
|
291
|
+
{ requestTxData: null }
|
|
292
|
+
);
|
|
293
|
+
t.fail('expected missing tx data to throw');
|
|
294
|
+
} catch (error) {
|
|
295
|
+
t.is(error.resultCode, ResultCode.PENDING_REQUEST_MISSING_TX_DATA);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('assertProofPayloadMatchesRequestPayload throws PROOF_PAYLOAD_MISMATCH when decoded payloads differ', async t => {
|
|
300
|
+
const validator = createValidator();
|
|
301
|
+
|
|
302
|
+
const requestTxData = unsafeEncodeApplyOperation(protobufFixtures.validPartialTransactionOperation);
|
|
303
|
+
const responseTxData = unsafeEncodeApplyOperation(protobufFixtures.validPartialTransferOperation);
|
|
304
|
+
|
|
305
|
+
overrideFunction(t, Autobase, 'decodeValue', async () => responseTxData);
|
|
306
|
+
|
|
307
|
+
try {
|
|
308
|
+
await validator.assertProofPayloadMatchesRequestPayload(
|
|
309
|
+
{
|
|
310
|
+
proof: {
|
|
311
|
+
block: { value: b4a.alloc(1) },
|
|
312
|
+
manifest: {}
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
{ requestTxData }
|
|
316
|
+
);
|
|
317
|
+
t.fail('expected payload mismatch');
|
|
318
|
+
} catch (error) {
|
|
319
|
+
t.is(error.resultCode, ResultCode.PROOF_PAYLOAD_MISMATCH);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('assertProofPayloadMatchesRequestPayload strips validator metadata before comparison', async t => {
|
|
324
|
+
const validator = createValidator();
|
|
325
|
+
|
|
326
|
+
const requestPayload = structuredClone(protobufFixtures.validTransactionOperation);
|
|
327
|
+
requestPayload.txo.va = null;
|
|
328
|
+
requestPayload.txo.vn = null;
|
|
329
|
+
requestPayload.txo.vs = null;
|
|
330
|
+
const requestTxData = unsafeEncodeApplyOperation(requestPayload);
|
|
331
|
+
const responseTxData = unsafeEncodeApplyOperation(protobufFixtures.validTransactionOperation);
|
|
332
|
+
const manifest = { signers: [] };
|
|
333
|
+
|
|
334
|
+
overrideFunction(t, Autobase, 'decodeValue', async () => responseTxData);
|
|
335
|
+
|
|
336
|
+
const result = await validator.assertProofPayloadMatchesRequestPayload(
|
|
337
|
+
{
|
|
338
|
+
proof: {
|
|
339
|
+
block: { value: b4a.alloc(1) },
|
|
340
|
+
manifest,
|
|
341
|
+
}
|
|
342
|
+
},
|
|
343
|
+
{ requestTxData }
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
t.is(result.validatorDecodedTx.type, OperationType.TX);
|
|
347
|
+
t.alike(result.manifest, manifest);
|
|
348
|
+
|
|
349
|
+
const decodedResponse = unsafeDecodeApplyOperation(responseTxData);
|
|
350
|
+
t.alike(result.validatorDecodedTx, decodedResponse);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test('validateWritingKey throws when writer key is not registered', async t => {
|
|
354
|
+
const validator = createValidator({
|
|
355
|
+
getRegisteredWriterKey: async () => null
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
overrideFunction(t, Hypercore, 'key', () => writerKey);
|
|
359
|
+
|
|
360
|
+
try {
|
|
361
|
+
await validator.validateWritingKey({}, {});
|
|
362
|
+
t.fail('expected validateWritingKey to throw');
|
|
363
|
+
} catch (error) {
|
|
364
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_WRITER_KEY_NOT_REGISTERED);
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
test('validateWritingKey returns writer key and correlated validator address', async t => {
|
|
369
|
+
const registeredAddress = b4a.alloc(39, 7);
|
|
370
|
+
const validator = createValidator({
|
|
371
|
+
getRegisteredWriterKey: async writerKeyHex => {
|
|
372
|
+
t.is(writerKeyHex, b4a.toString(writerKey, 'hex'));
|
|
373
|
+
return registeredAddress;
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
overrideFunction(t, Hypercore, 'key', () => writerKey);
|
|
378
|
+
|
|
379
|
+
const result = await validator.validateWritingKey({}, {});
|
|
380
|
+
|
|
381
|
+
t.ok(b4a.equals(result.writerKeyFromManifest, writerKey));
|
|
382
|
+
t.ok(b4a.equals(result.validatorAddressCorrelatedWithManifest, registeredAddress));
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
test('validateValidatorCorrectness throws VALIDATOR_ADDRESS_MISMATCH when tx va differs from connection-derived address', async t => {
|
|
386
|
+
const validator = createValidator({
|
|
387
|
+
getNodeEntry: async () => null
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
try {
|
|
391
|
+
await validator.validateValidatorCorrectness(
|
|
392
|
+
{ txo: { va: b4a.alloc(remoteAddressBuffer.length, 9) } },
|
|
393
|
+
remotePublicKey,
|
|
394
|
+
writerKey,
|
|
395
|
+
remoteAddressBuffer
|
|
396
|
+
);
|
|
397
|
+
t.fail('expected address mismatch');
|
|
398
|
+
} catch (error) {
|
|
399
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_ADDRESS_MISMATCH);
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test('validateValidatorCorrectness throws VALIDATOR_ADDRESS_MISMATCH when tx va differs from manifest-correlated address', async t => {
|
|
404
|
+
const validator = createValidator({
|
|
405
|
+
getNodeEntry: async () => null
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
await validator.validateValidatorCorrectness(
|
|
410
|
+
{ txo: { va: remoteAddressBuffer } },
|
|
411
|
+
remotePublicKey,
|
|
412
|
+
writerKey,
|
|
413
|
+
b4a.alloc(remoteAddressBuffer.length, 8)
|
|
414
|
+
);
|
|
415
|
+
t.fail('expected address mismatch');
|
|
416
|
+
} catch (error) {
|
|
417
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_ADDRESS_MISMATCH);
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('validateValidatorCorrectness throws VALIDATOR_NODE_ENTRY_NOT_FOUND when state has no node entry', async t => {
|
|
422
|
+
const validator = createValidator({
|
|
423
|
+
getNodeEntry: async () => null
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
try {
|
|
427
|
+
await validator.validateValidatorCorrectness(
|
|
428
|
+
{ txo: { va: remoteAddressBuffer } },
|
|
429
|
+
remotePublicKey,
|
|
430
|
+
writerKey,
|
|
431
|
+
remoteAddressBuffer
|
|
432
|
+
);
|
|
433
|
+
t.fail('expected missing node entry');
|
|
434
|
+
} catch (error) {
|
|
435
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_NODE_ENTRY_NOT_FOUND);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
test('validateValidatorCorrectness throws VALIDATOR_NODE_NOT_WRITER when node is not a writer', async t => {
|
|
440
|
+
const validator = createValidator({
|
|
441
|
+
getNodeEntry: async () => ({
|
|
442
|
+
isWriter: false,
|
|
443
|
+
wk: writerKey,
|
|
444
|
+
})
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
try {
|
|
448
|
+
await validator.validateValidatorCorrectness(
|
|
449
|
+
{ txo: { va: remoteAddressBuffer } },
|
|
450
|
+
remotePublicKey,
|
|
451
|
+
writerKey,
|
|
452
|
+
remoteAddressBuffer
|
|
453
|
+
);
|
|
454
|
+
t.fail('expected node-not-writer');
|
|
455
|
+
} catch (error) {
|
|
456
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_NODE_NOT_WRITER);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
test('validateValidatorCorrectness throws VALIDATOR_WRITER_KEY_MISMATCH when state writer key differs', async t => {
|
|
461
|
+
const validator = createValidator({
|
|
462
|
+
getNodeEntry: async () => ({
|
|
463
|
+
isWriter: true,
|
|
464
|
+
wk: b4a.alloc(32, 99),
|
|
465
|
+
})
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
try {
|
|
469
|
+
await validator.validateValidatorCorrectness(
|
|
470
|
+
{ txo: { va: remoteAddressBuffer } },
|
|
471
|
+
remotePublicKey,
|
|
472
|
+
writerKey,
|
|
473
|
+
remoteAddressBuffer
|
|
474
|
+
);
|
|
475
|
+
t.fail('expected writer-key mismatch');
|
|
476
|
+
} catch (error) {
|
|
477
|
+
t.is(error.resultCode, ResultCode.VALIDATOR_WRITER_KEY_MISMATCH);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test('validateValidatorCorrectness passes when validator address and writer key are consistent', async t => {
|
|
482
|
+
const validator = createValidator({
|
|
483
|
+
getNodeEntry: async () => ({
|
|
484
|
+
isWriter: true,
|
|
485
|
+
wk: writerKey,
|
|
486
|
+
})
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
await validator.validateValidatorCorrectness(
|
|
490
|
+
{ txo: { va: remoteAddressBuffer } },
|
|
491
|
+
remotePublicKey,
|
|
492
|
+
writerKey,
|
|
493
|
+
remoteAddressBuffer
|
|
494
|
+
);
|
|
495
|
+
|
|
496
|
+
t.pass();
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
test('validateIfResultCodeIsValidatorInternalError throws only for TX_COMMITTED_RECEIPT_MISSING', t => {
|
|
500
|
+
const validator = createValidator();
|
|
501
|
+
|
|
502
|
+
try {
|
|
503
|
+
validator.validateIfResultCodeIsValidatorInternalError(ResultCode.TX_COMMITTED_RECEIPT_MISSING);
|
|
504
|
+
t.fail('expected internal error result code to throw');
|
|
505
|
+
} catch (error) {
|
|
506
|
+
t.is(error.resultCode, ResultCode.TX_COMMITTED_RECEIPT_MISSING);
|
|
507
|
+
t.is(error.endConnection, true);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
validator.validateIfResultCodeIsValidatorInternalError(ResultCode.OK);
|
|
511
|
+
t.pass();
|
|
512
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import test from 'brittle';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
|
|
4
|
+
import V1LivenessRequest from '../../../../src/core/network/protocols/v1/validators/V1LivenessRequest.js';
|
|
5
|
+
import { config } from '../../../helpers/config.js';
|
|
6
|
+
import { errorMessageIncludes } from '../../../helpers/regexHelper.js';
|
|
7
|
+
|
|
8
|
+
test('V1LivenessRequest.validate runs schema and signature validation', async t => {
|
|
9
|
+
const validator = new V1LivenessRequest(config);
|
|
10
|
+
const calls = [];
|
|
11
|
+
|
|
12
|
+
validator.isPayloadSchemaValid = () => calls.push('schema');
|
|
13
|
+
validator.validateSignature = async () => calls.push('signature');
|
|
14
|
+
|
|
15
|
+
const result = await validator.validate({}, b4a.alloc(32, 1));
|
|
16
|
+
|
|
17
|
+
t.is(result, true);
|
|
18
|
+
t.alike(calls, ['schema', 'signature']);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('V1LivenessRequest.validate propagates signature validation errors', async t => {
|
|
22
|
+
const validator = new V1LivenessRequest(config);
|
|
23
|
+
validator.isPayloadSchemaValid = () => true;
|
|
24
|
+
validator.validateSignature = async () => {
|
|
25
|
+
throw new Error('signature failed');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
await t.exception(
|
|
29
|
+
async () => validator.validate({}, b4a.alloc(32, 1)),
|
|
30
|
+
errorMessageIncludes('signature failed')
|
|
31
|
+
);
|
|
32
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import test from 'brittle';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
|
|
4
|
+
import V1LivenessResponse from '../../../../src/core/network/protocols/v1/validators/V1LivenessResponse.js';
|
|
5
|
+
import { config } from '../../../helpers/config.js';
|
|
6
|
+
import { errorMessageIncludes } from '../../../helpers/regexHelper.js';
|
|
7
|
+
|
|
8
|
+
test('V1LivenessResponse.validate runs all validation steps in order', async t => {
|
|
9
|
+
const validator = new V1LivenessResponse(config);
|
|
10
|
+
const calls = [];
|
|
11
|
+
|
|
12
|
+
validator.isPayloadSchemaValid = () => calls.push('schema');
|
|
13
|
+
validator.validateResponseType = () => calls.push('responseType');
|
|
14
|
+
validator.validatePeerCorrectness = () => calls.push('peer');
|
|
15
|
+
validator.validateSignature = async () => calls.push('signature');
|
|
16
|
+
|
|
17
|
+
const result = await validator.validate(
|
|
18
|
+
{},
|
|
19
|
+
{ remotePublicKey: b4a.alloc(32, 1) },
|
|
20
|
+
{ requestedTo: b4a.toString(b4a.alloc(32, 1), 'hex') }
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
t.is(result, true);
|
|
24
|
+
t.alike(calls, ['schema', 'responseType', 'peer', 'signature']);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('V1LivenessResponse.validate propagates response-type errors', async t => {
|
|
28
|
+
const validator = new V1LivenessResponse(config);
|
|
29
|
+
|
|
30
|
+
validator.isPayloadSchemaValid = () => true;
|
|
31
|
+
validator.validateResponseType = () => {
|
|
32
|
+
throw new Error('invalid response type');
|
|
33
|
+
};
|
|
34
|
+
validator.validatePeerCorrectness = () => true;
|
|
35
|
+
validator.validateSignature = async () => true;
|
|
36
|
+
|
|
37
|
+
await t.exception(
|
|
38
|
+
async () => validator.validate(
|
|
39
|
+
{},
|
|
40
|
+
{ remotePublicKey: b4a.alloc(32, 1) },
|
|
41
|
+
{ requestedTo: b4a.toString(b4a.alloc(32, 1), 'hex') }
|
|
42
|
+
),
|
|
43
|
+
errorMessageIncludes('invalid response type')
|
|
44
|
+
);
|
|
45
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {test} from 'brittle';
|
|
2
|
+
|
|
3
|
+
import {ResultCode} from '../../../../src/utils/constants.js';
|
|
4
|
+
|
|
5
|
+
const expectedResultCodeMap = Object.freeze({
|
|
6
|
+
UNSPECIFIED: 0,
|
|
7
|
+
OK: 1,
|
|
8
|
+
INVALID_PAYLOAD: 2,
|
|
9
|
+
RATE_LIMITED: 3,
|
|
10
|
+
SIGNATURE_INVALID: 4,
|
|
11
|
+
UNEXPECTED_ERROR: 5,
|
|
12
|
+
TIMEOUT: 6,
|
|
13
|
+
NODE_HAS_NO_WRITE_ACCESS: 7,
|
|
14
|
+
TX_ACCEPTED_PROOF_UNAVAILABLE: 8,
|
|
15
|
+
NODE_OVERLOADED: 9,
|
|
16
|
+
TX_ALREADY_PENDING: 10,
|
|
17
|
+
OPERATION_TYPE_UNKNOWN: 11,
|
|
18
|
+
SCHEMA_VALIDATION_FAILED: 12,
|
|
19
|
+
REQUESTER_ADDRESS_INVALID: 13,
|
|
20
|
+
REQUESTER_PUBLIC_KEY_INVALID: 14,
|
|
21
|
+
TX_HASH_MISMATCH: 15,
|
|
22
|
+
TX_SIGNATURE_INVALID: 16,
|
|
23
|
+
TX_EXPIRED: 17,
|
|
24
|
+
TX_ALREADY_EXISTS: 18,
|
|
25
|
+
OPERATION_ALREADY_COMPLETED: 19,
|
|
26
|
+
REQUESTER_NOT_FOUND: 20,
|
|
27
|
+
INSUFFICIENT_FEE_BALANCE: 21,
|
|
28
|
+
EXTERNAL_BOOTSTRAP_EQUALS_MSB_BOOTSTRAP: 22,
|
|
29
|
+
SELF_VALIDATION_FORBIDDEN: 23,
|
|
30
|
+
ROLE_NODE_ENTRY_NOT_FOUND: 24,
|
|
31
|
+
ROLE_NODE_ALREADY_WRITER: 25,
|
|
32
|
+
ROLE_NODE_NOT_WHITELISTED: 26,
|
|
33
|
+
ROLE_NODE_NOT_WRITER: 27,
|
|
34
|
+
ROLE_NODE_IS_INDEXER: 28,
|
|
35
|
+
ROLE_ADMIN_ENTRY_MISSING: 29,
|
|
36
|
+
ROLE_INVALID_RECOVERY_CASE: 30,
|
|
37
|
+
ROLE_UNKNOWN_OPERATION: 31,
|
|
38
|
+
ROLE_INVALID_WRITER_KEY: 32,
|
|
39
|
+
ROLE_INSUFFICIENT_FEE_BALANCE: 33,
|
|
40
|
+
MSB_BOOTSTRAP_MISMATCH: 34,
|
|
41
|
+
EXTERNAL_BOOTSTRAP_NOT_DEPLOYED: 35,
|
|
42
|
+
EXTERNAL_BOOTSTRAP_TX_MISSING: 36,
|
|
43
|
+
EXTERNAL_BOOTSTRAP_MISMATCH: 37,
|
|
44
|
+
BOOTSTRAP_ALREADY_EXISTS: 38,
|
|
45
|
+
TRANSFER_RECIPIENT_ADDRESS_INVALID: 39,
|
|
46
|
+
TRANSFER_RECIPIENT_PUBLIC_KEY_INVALID: 40,
|
|
47
|
+
TRANSFER_AMOUNT_TOO_LARGE: 41,
|
|
48
|
+
TRANSFER_SENDER_NOT_FOUND: 42,
|
|
49
|
+
TRANSFER_INSUFFICIENT_BALANCE: 43,
|
|
50
|
+
TRANSFER_RECIPIENT_BALANCE_OVERFLOW: 44,
|
|
51
|
+
TX_HASH_INVALID_FORMAT: 45,
|
|
52
|
+
INTERNAL_ENQUEUE_VALIDATION_FAILED: 46,
|
|
53
|
+
TX_COMMITTED_RECEIPT_MISSING: 47,
|
|
54
|
+
VALIDATOR_RESPONSE_TX_TYPE_INVALID: 48,
|
|
55
|
+
VALIDATOR_RESPONSE_TX_TYPE_UNKNOWN: 49,
|
|
56
|
+
VALIDATOR_RESPONSE_TX_TYPE_UNSUPPORTED: 50,
|
|
57
|
+
VALIDATOR_RESPONSE_SCHEMA_INVALID: 51,
|
|
58
|
+
PENDING_REQUEST_MISSING_TX_DATA: 52,
|
|
59
|
+
PROOF_PAYLOAD_MISMATCH: 53,
|
|
60
|
+
VALIDATOR_WRITER_KEY_NOT_REGISTERED: 54,
|
|
61
|
+
VALIDATOR_ADDRESS_MISMATCH: 55,
|
|
62
|
+
VALIDATOR_NODE_ENTRY_NOT_FOUND: 56,
|
|
63
|
+
VALIDATOR_NODE_NOT_WRITER: 57,
|
|
64
|
+
VALIDATOR_WRITER_KEY_MISMATCH: 58,
|
|
65
|
+
VALIDATOR_TX_OBJECT_INVALID: 59,
|
|
66
|
+
VALIDATOR_VA_MISSING: 60,
|
|
67
|
+
TX_INVALID_PAYLOAD: 61
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('ResultCode values are unique', t => {
|
|
71
|
+
const values = Object.values(ResultCode);
|
|
72
|
+
t.is(new Set(values).size, values.length);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('ResultCode preserves existing numeric values (append-only)', t => {
|
|
76
|
+
t.alike(ResultCode, expectedResultCodeMap);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('ResultCode does not expose deprecated alias', t => {
|
|
80
|
+
t.absent(
|
|
81
|
+
Object.prototype.hasOwnProperty.call(ResultCode, 'TX_ACCEPTED_RECEIPT_MISSING'),
|
|
82
|
+
'deprecated alias should not be present'
|
|
83
|
+
);
|
|
84
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { default as test } from 'brittle';
|
|
2
|
+
|
|
3
|
+
async function runV1ValidationSchemaTests() {
|
|
4
|
+
test.pause();
|
|
5
|
+
await import('./v1ValidationSchema/livenessRequest.test.js');
|
|
6
|
+
await import('./v1ValidationSchema/livenessResponse.test.js');
|
|
7
|
+
await import('./v1ValidationSchema/broadcastTransactionRequest.test.js');
|
|
8
|
+
await import('./v1ValidationSchema/broadcastTransactionResponse.test.js');
|
|
9
|
+
test.resume();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
await runV1ValidationSchemaTests();
|
|
13
|
+
|