trac-msb 0.2.26 → 0.3.0-beta.2

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.
Files changed (136) hide show
  1. package/SECURITY.md +2 -2
  2. package/cli/commandHandler.js +317 -20
  3. package/cli/handlers.js +9 -1
  4. package/cli/interactive.js +4 -3
  5. package/cli/tokenSupplyAudit.js +90 -0
  6. package/msb.mjs +1 -0
  7. package/package.json +15 -6
  8. package/rpc/create_server.js +12 -3
  9. package/rpc/handlers.js +121 -0
  10. package/rpc/routes/v1.js +12 -0
  11. package/src/codecs/apply/applyOperationCodec.js +334 -0
  12. package/src/codecs/apply/applyOperations.generated.cjs +6380 -0
  13. package/src/codecs/consensus/v1/consensusV1.generated.cjs +3508 -0
  14. package/src/codecs/consensus/v1/consensusV1OperationCodec.js +161 -0
  15. package/src/codecs/consensus/v1/vdfConfigCodec.js +83 -0
  16. package/src/{utils/protobuf → codecs/network/v1}/networkV1.generated.cjs +664 -393
  17. package/src/codecs/network/v1/networkV1OperationCodec.js +25 -0
  18. package/src/codecs/protobufRuntime.cjs +95 -0
  19. package/src/codecs/state/escrowEntryCodec.js +45 -0
  20. package/src/codecs/state/state.generated.cjs +659 -0
  21. package/src/config/args.js +0 -1
  22. package/src/config/config.js +41 -7
  23. package/src/config/env.js +33 -9
  24. package/src/core/consensus/epochAppenderSelection/EpochAppenderSelectionError.js +55 -0
  25. package/src/core/consensus/epochAppenderSelection/EpochAppenderSelectionOperations.js +118 -0
  26. package/src/core/consensus/epochAppenderSelection/EpochAppenderSelector.js +1042 -0
  27. package/src/core/consensus/epochAppenderSelection/createEpochAppenderSelector.js +23 -0
  28. package/src/core/consensus/epochAppenderSelection/epochAppenderSelectionLedgerReader.js +274 -0
  29. package/src/core/consensus/epochAppenderSelection/epochAppenderSelectionState.js +488 -0
  30. package/src/core/consensus/epochAppenderSelection/epochAppenderSelectionStorage.js +93 -0
  31. package/src/core/consensus/quorum.js +11 -0
  32. package/src/core/consensus/services/EpochCoordinationRound.js +839 -0
  33. package/src/core/consensus/services/EpochCoordinatorOperations.js +211 -0
  34. package/src/core/consensus/services/EpochCoordinatorService.js +335 -0
  35. package/src/core/consensus/services/EpochRoundListeners.js +116 -0
  36. package/src/core/consensus/services/EpochStateMachine.js +141 -0
  37. package/src/core/consensus/services/VDFBare.js +291 -0
  38. package/src/core/consensus/services/VDFNode.js +111 -0
  39. package/src/core/consensus/services/VDFProgressWatchdog.js +79 -0
  40. package/src/core/consensus/services/VDFService.js +87 -0
  41. package/src/core/consensus/services/VDFServiceManager.js +147 -0
  42. package/src/core/consensus/services/createVDFService.js +16 -0
  43. package/src/core/consensus/services/vdf-guardian.js +32 -0
  44. package/src/core/consensus/services/vdf-worker.js +90 -0
  45. package/src/core/network/Network.js +327 -238
  46. package/src/core/network/protocols/consensus/IndexerMessages.js +72 -0
  47. package/src/core/network/protocols/consensus/v1/ConsensusV1ConnectionPolicy.js +57 -0
  48. package/src/core/network/protocols/consensus/v1/ConsensusV1MessageRouter.js +147 -0
  49. package/src/core/network/protocols/consensus/v1/ConsensusV1Protocol.js +72 -0
  50. package/src/core/network/protocols/consensus/v1/IndexerPendingRequestService.js +108 -0
  51. package/src/core/network/protocols/consensus/v1/V1ConsensusProtocolError.js +29 -0
  52. package/src/core/network/protocols/consensus/v1/handlers/V1EpochAppendOperationHandler.js +68 -0
  53. package/src/core/network/protocols/consensus/v1/handlers/V1EpochProofProposalOperationHandler.js +226 -0
  54. package/src/core/network/protocols/consensus/v1/validators/ConsensusValidationSchema.js +192 -0
  55. package/src/core/network/protocols/consensus/v1/validators/V1BaseConsensusOperation.js +428 -0
  56. package/src/core/network/protocols/consensus/v1/validators/V1EpochAppendReply.js +47 -0
  57. package/src/core/network/protocols/consensus/v1/validators/V1EpochAppendRequest.js +17 -0
  58. package/src/core/network/protocols/consensus/v1/validators/V1EpochProofProposalApproval.js +116 -0
  59. package/src/core/network/protocols/consensus/v1/validators/V1EpochProofProposalRequest.js +152 -0
  60. package/src/core/network/protocols/consensus/v1/validators/consensusValidationSchemaHelpers.js +145 -0
  61. package/src/core/network/protocols/shared/ConnectionOperationHandler.js +27 -0
  62. package/src/core/network/protocols/{NetworkMessages.js → validators/ValidatorMessages.js} +24 -14
  63. package/src/core/network/protocols/{ProtocolSession.js → validators/ValidatorProtocolSession.js} +22 -25
  64. package/src/core/network/protocols/{legacy/NetworkMessageRouter.js → validators/legacy/ValidatorLegacyMessageRouter.js} +6 -6
  65. package/src/core/network/protocols/{LegacyProtocol.js → validators/legacy/ValidatorLegacyProtocol.js} +9 -7
  66. package/src/core/network/protocols/{legacy → validators/legacy}/handlers/BaseStateOperationHandler.js +1 -1
  67. package/src/core/network/protocols/{legacy → validators/legacy}/handlers/LegacyGetRequestHandler.js +5 -5
  68. package/src/core/network/protocols/{legacy → validators/legacy}/handlers/LegacyRoleOperationHandler.js +5 -5
  69. package/src/core/network/protocols/{legacy → validators/legacy}/handlers/LegacySubnetworkOperationHandler.js +5 -5
  70. package/src/core/network/protocols/{legacy → validators/legacy}/handlers/LegacyTransferOperationHandler.js +5 -5
  71. package/src/core/network/protocols/{ProtocolInterface.js → validators/shared/ValidatorProtocolInterface.js} +5 -5
  72. package/src/core/network/protocols/{shared → validators/shared}/validators/PartialBootstrapDeploymentValidator.js +1 -1
  73. package/src/core/network/protocols/validators/shared/validators/PartialHtlcValidator.js +325 -0
  74. package/src/core/network/protocols/{shared → validators/shared}/validators/PartialOperationValidator.js +44 -21
  75. package/src/core/network/protocols/{shared → validators/shared}/validators/PartialRoleAccessValidator.js +3 -3
  76. package/src/core/network/protocols/{shared → validators/shared}/validators/PartialTransactionValidator.js +3 -3
  77. package/src/core/network/protocols/{shared → validators/shared}/validators/PartialTransferValidator.js +4 -4
  78. package/src/core/network/protocols/{v1 → validators/v1}/V1ProtocolError.js +1 -1
  79. package/src/core/network/{services/PendingRequestService.js → protocols/validators/v1/ValidatorPendingRequestService.js} +19 -82
  80. package/src/core/network/protocols/{v1/NetworkMessageRouter.js → validators/v1/ValidatorV1MessageRouter.js} +9 -9
  81. package/src/core/network/protocols/{V1Protocol.js → validators/v1/ValidatorV1Protocol.js} +10 -8
  82. package/src/core/network/protocols/{connectionPolicies.js → validators/v1/connectionPolicies.js} +1 -1
  83. package/src/core/network/protocols/{v1 → validators/v1}/handlers/V1BaseOperationHandler.js +6 -23
  84. package/src/core/network/protocols/{v1 → validators/v1}/handlers/V1BroadcastTransactionOperationHandler.js +14 -9
  85. package/src/core/network/protocols/{v1 → validators/v1}/handlers/V1LivenessOperationHandler.js +5 -4
  86. package/src/core/network/protocols/{v1 → validators/v1}/handlers/broadcastTransaction/BroadcastTransactionErrorMapper.js +3 -3
  87. package/src/core/network/protocols/{v1 → validators/v1}/handlers/broadcastTransaction/BroadcastTransactionOperationStrategies.js +44 -1
  88. package/src/core/network/protocols/{v1 → validators/v1}/handlers/broadcastTransaction/BroadcastTransactionPayloadUtils.js +1 -1
  89. package/src/core/network/protocols/{v1 → validators/v1}/validators/V1BaseOperation.js +19 -15
  90. package/src/core/network/protocols/{v1 → validators/v1}/validators/V1BroadcastTransactionRequest.js +2 -2
  91. package/src/core/network/protocols/{v1 → validators/v1}/validators/V1BroadcastTransactionResponse.js +34 -18
  92. package/src/core/network/protocols/{v1 → validators/v1}/validators/V1ValidationSchema.js +3 -3
  93. package/src/core/network/services/IndexerConnectionManager.js +88 -0
  94. package/src/core/network/services/TransactionPoolService.js +11 -18
  95. package/src/core/network/services/TransactionRateLimiterService.js +1 -1
  96. package/src/core/network/services/ValidatorConnectionManager.js +163 -0
  97. package/src/core/network/services/{MessageOrchestrator.js → ValidatorMessageOrchestrator.js} +34 -42
  98. package/src/core/network/services/ValidatorObserverService.js +21 -36
  99. package/src/core/network/shared/BasePendingRequestService.js +67 -0
  100. package/src/core/network/shared/PeerConnectionManager.js +185 -0
  101. package/src/core/state/State.js +1803 -159
  102. package/src/core/state/StateEventQueue.js +69 -0
  103. package/src/core/state/utils/address.js +10 -6
  104. package/src/core/state/utils/epochProof.js +109 -0
  105. package/src/core/state/utils/escrowEntry.js +178 -0
  106. package/src/core/state/validators/StateValidationSchema.js +757 -0
  107. package/src/core/state/validators/stateValidationRules.js +312 -0
  108. package/src/index.js +280 -67
  109. package/src/messages/consensus/v1/ConsensusMessageBuilder.js +368 -0
  110. package/src/messages/consensus/v1/ConsensusMessageDirector.js +142 -0
  111. package/src/messages/consensus/v1/consensusMessageFactory.js +12 -0
  112. package/src/messages/consensus/v1/epochAppendMessage.js +58 -0
  113. package/src/messages/network/v1/NetworkMessageBuilder.js +15 -7
  114. package/src/messages/state/ApplyStateMessageBuilder.js +499 -13
  115. package/src/messages/state/ApplyStateMessageDirector.js +261 -2
  116. package/src/utils/StateMachine.js +125 -0
  117. package/src/utils/applyOperations.js +45 -2
  118. package/src/utils/buffer.js +239 -59
  119. package/src/utils/consensusConfig.js +76 -0
  120. package/src/utils/constants.js +174 -73
  121. package/src/utils/helpers.js +0 -25
  122. package/src/utils/htlcLock.js +122 -0
  123. package/src/utils/logger.js +10 -4
  124. package/src/utils/migrationUtils.js +2 -1
  125. package/src/utils/normalizers.js +113 -0
  126. package/src/utils/scheduler/SchedulableService.js +107 -0
  127. package/src/utils/{Scheduler.js → scheduler/Scheduler.js} +56 -17
  128. package/src/core/network/services/ConnectionManager.js +0 -406
  129. package/src/utils/check.js +0 -525
  130. package/src/utils/protobuf/applyOperations.cjs +0 -1373
  131. package/src/utils/protobuf/operationHelpers.js +0 -81
  132. /package/src/core/network/protocols/{legacy → validators/legacy}/handlers/LegacyResponseHandler.js +0 -0
  133. /package/src/core/network/protocols/{legacy → validators/legacy}/validators/ValidatorResponse.js +0 -0
  134. /package/src/core/network/protocols/{legacy → validators/legacy}/validators/base/BaseResponse.js +0 -0
  135. /package/src/core/network/protocols/{v1 → validators/v1}/validators/V1LivenessRequest.js +0 -0
  136. /package/src/core/network/protocols/{v1 → validators/v1}/validators/V1LivenessResponse.js +0 -0
@@ -0,0 +1,312 @@
1
+ import b4a from 'b4a';
2
+ import {
3
+ NETWORK_ID_BYTE_LENGTH,
4
+ EPOCH_BYTE_LENGTH,
5
+ HASH_BYTE_LENGTH,
6
+ VDF_DIFFICULTY_SIZE,
7
+ VDF_DISCRIMINANT_SIZE,
8
+ VDF_PROOF_BYTE_LENGTHS,
9
+ SIGNATURE_BYTE_LENGTH
10
+ } from '../../../utils/constants.js';
11
+ import {
12
+ decodeProofProposalApproval,
13
+ decodeProofProposal,
14
+ encodeProofProposalApproval,
15
+ encodeProofProposal
16
+ } from '../../../codecs/consensus/v1/consensusV1OperationCodec.js';
17
+
18
+ export function registerStrictTypeRules(validator) {
19
+ // Check types before the built-in rules read properties or format errors
20
+ // with an untrusted value (e.g. Symbol).
21
+ for (const [type, condition] of [
22
+ ['object', 'value !== null && typeof value === "object" && !Array.isArray(value)'],
23
+ ['number', 'typeof value === "number"'],
24
+ ['array', 'Array.isArray(value)']
25
+ ]) {
26
+ const compileRule = validator.rules[type];
27
+ validator.add(type, function (rule, path, context) {
28
+ const compiled = compileRule.call(this, rule, path, context);
29
+ const typeError = this.makeError({type, actual: 'typeof value', messages: rule.messages});
30
+ return {
31
+ ...compiled,
32
+ source: `
33
+ if (!(${condition})) {
34
+ ${typeError}
35
+ return value;
36
+ }
37
+ ${compiled.source}
38
+ `
39
+ };
40
+ });
41
+ }
42
+ }
43
+
44
+ export function bufferRule({schema, messages, index}, _path, context) {
45
+ context.customs[index] = {isBuffer: b4a.isBuffer};
46
+ const typeError = this.makeError({type: 'buffer', actual: 'typeof value', messages});
47
+ const lengthError = this.makeError({type: 'bufferLength', expected: schema.length, actual: 'value.length', messages});
48
+ const emptyError = this.makeError({type: 'emptyBuffer', actual: 'value', messages});
49
+
50
+ return {
51
+ source: `
52
+ if (!context.customs[${index}].isBuffer(value)) {
53
+ ${typeError}
54
+ return value;
55
+ }
56
+ if (value.length !== ${schema.length}) {
57
+ ${lengthError}
58
+ return value;
59
+ }
60
+ let isEmpty = true;
61
+ for (let i = 0; i < value.length; i++) {
62
+ if (value[i] !== 0) {
63
+ isEmpty = false;
64
+ break;
65
+ }
66
+ }
67
+ if (isEmpty) {
68
+ ${emptyError}
69
+ }
70
+ return value;
71
+ `
72
+ };
73
+ }
74
+
75
+ export function bufferMaxLengthRule({schema, messages, index}, _path, context) {
76
+ context.customs[index] = {isBuffer: b4a.isBuffer};
77
+ const typeError = this.makeError({type: 'buffer', actual: 'typeof value', messages});
78
+ const lengthError = this.makeError({type: 'bufferMaxLength', expected: schema.maxLength, actual: 'value.length', messages});
79
+
80
+ return {
81
+ source: `
82
+ if (!context.customs[${index}].isBuffer(value)) {
83
+ ${typeError}
84
+ return value;
85
+ }
86
+ if (value.length === 0 || value.length > ${schema.maxLength}) {
87
+ ${lengthError}
88
+ }
89
+ return value;
90
+ `
91
+ };
92
+ }
93
+
94
+ export function bufferAmountRule({schema, messages, index}, _path, context) {
95
+ context.customs[index] = {isBuffer: b4a.isBuffer};
96
+ const typeError = this.makeError({type: 'buffer', actual: 'typeof value', messages});
97
+ const lengthError = this.makeError({type: 'bufferLength', expected: schema.length, actual: 'value.length', messages});
98
+
99
+ return {
100
+ source: `
101
+ if (!context.customs[${index}].isBuffer(value)) {
102
+ ${typeError}
103
+ return value;
104
+ }
105
+ if (value.length !== ${schema.length}) {
106
+ ${lengthError}
107
+ }
108
+ return value;
109
+ `
110
+ };
111
+ }
112
+
113
+ export function boundedHtlcBufferArray(min, max, length) {
114
+ return {
115
+ type: 'custom',
116
+ required: true,
117
+ check(value, errors, _schema, field) {
118
+ if (!Array.isArray(value) || value.length < min || value.length > max) {
119
+ errors.push({type: 'array', field, message: `Expected ${min} to ${max} entries`});
120
+ return value;
121
+ }
122
+ for (let index = 0; index < value.length; index++) {
123
+ const item = value[index];
124
+ if (!b4a.isBuffer(item) || item.length !== length || item.every(byte => byte === 0)) {
125
+ errors.push({type: 'buffer', field: `${field}[${index}]`, message: `Expected a non-zero ${length}-byte buffer`});
126
+ }
127
+ }
128
+ return value;
129
+ }
130
+ };
131
+ }
132
+
133
+ export function createProofDataRule(addressLength) {
134
+ const fields = Object.freeze([
135
+ {name: 'network_id', length: NETWORK_ID_BYTE_LENGTH},
136
+ {name: 'epoch', length: EPOCH_BYTE_LENGTH},
137
+ {name: 'previous_epoch_record_hash', length: HASH_BYTE_LENGTH},
138
+ {name: 'proposer', length: addressLength},
139
+ {name: 'difficulty', length: VDF_DIFFICULTY_SIZE},
140
+ {name: 'discriminant_bit_size', length: VDF_DISCRIMINANT_SIZE},
141
+ {name: 'signature', length: SIGNATURE_BYTE_LENGTH}
142
+ ]);
143
+
144
+ return function proofDataRule({schema, messages, index}, _path, context) {
145
+ // Generated functions have no access to this closure. Store runtime
146
+ // dependencies under the rule index assigned by fastest-validator.
147
+ context.customs[index] = {
148
+ schema,
149
+ decodeProofData: decodeProofProposal,
150
+ encodeProofData: encodeProofProposal,
151
+ equals: b4a.equals,
152
+ isBuffer: b4a.isBuffer,
153
+ fields,
154
+ proofByteLengths: VDF_PROOF_BYTE_LENGTHS
155
+ };
156
+ const typeError = this.makeError({type: 'proofData', actual: 'typeof value', messages});
157
+ const proofError = this.makeError({type: 'proofData', actual: 'value', messages});
158
+
159
+ return {
160
+ source: `
161
+ const proofDataRule = context.customs[${index}];
162
+ if (!proofDataRule.isBuffer(value) || value.length === 0) {
163
+ ${typeError}
164
+ return value;
165
+ }
166
+
167
+ let proofData;
168
+ try {
169
+ proofData = proofDataRule.decodeProofData(value);
170
+ } catch {
171
+ ${proofError}
172
+ return value;
173
+ }
174
+
175
+ const reencodedProofDataInput = {};
176
+ for (const field of proofDataRule.fields) {
177
+ const fieldValue = proofData[field.name];
178
+ if (!proofDataRule.isBuffer(fieldValue) || fieldValue.length !== field.length) {
179
+ ${proofError}
180
+ return value;
181
+ }
182
+
183
+ let fieldIsZeroFilled = true;
184
+ for (let i = 0; i < fieldValue.length; i++) {
185
+ if (fieldValue[i] !== 0) {
186
+ fieldIsZeroFilled = false;
187
+ break;
188
+ }
189
+ }
190
+
191
+ if (fieldIsZeroFilled) {
192
+ ${proofError}
193
+ return value;
194
+ }
195
+
196
+ reencodedProofDataInput[field.name] = fieldValue;
197
+ }
198
+
199
+ const discriminantBitSize = proofData.discriminant_bit_size.readUInt16BE(0);
200
+ const expectedProofLength = proofDataRule.proofByteLengths[discriminantBitSize];
201
+ const proof = proofData.proof;
202
+ if (
203
+ !Number.isInteger(expectedProofLength) ||
204
+ !proofDataRule.isBuffer(proof) ||
205
+ proof.length !== expectedProofLength
206
+ ) {
207
+ ${proofError}
208
+ return value;
209
+ }
210
+
211
+ let proofIsZeroFilled = true;
212
+ for (let i = 0; i < proof.length; i++) {
213
+ if (proof[i] !== 0) {
214
+ proofIsZeroFilled = false;
215
+ break;
216
+ }
217
+ }
218
+ if (proofIsZeroFilled) {
219
+ ${proofError}
220
+ return value;
221
+ }
222
+ reencodedProofDataInput.proof = proof;
223
+
224
+ const reencodedProofData = proofDataRule.encodeProofData(reencodedProofDataInput);
225
+ if (!proofDataRule.equals(value, reencodedProofData)) {
226
+ ${proofError}
227
+ }
228
+
229
+ return value;
230
+ `
231
+ };
232
+ };
233
+ }
234
+
235
+ export function createProofProposalApprovalRule(addressLength) {
236
+ return function proofProposalApprovalRule({schema, messages, index}, _path, context) {
237
+ context.customs[index] = {
238
+ schema,
239
+ decodeApproval: decodeProofProposalApproval,
240
+ encodeApproval: encodeProofProposalApproval,
241
+ equals: b4a.equals,
242
+ isBuffer: b4a.isBuffer,
243
+ addressLength,
244
+ signatureByteLength: SIGNATURE_BYTE_LENGTH
245
+ };
246
+ const typeError = this.makeError({type: 'proofProposalApproval', actual: 'typeof value', messages});
247
+ const approvalError = this.makeError({type: 'proofProposalApproval', actual: 'value', messages});
248
+
249
+ return {
250
+ source: `
251
+ const proofProposalApprovalRule = context.customs[${index}];
252
+ if (!proofProposalApprovalRule.isBuffer(value) || value.length === 0) {
253
+ ${typeError}
254
+ return value;
255
+ }
256
+
257
+ let approval;
258
+ try {
259
+ approval = proofProposalApprovalRule.decodeApproval(value);
260
+ } catch {
261
+ ${approvalError}
262
+ return value;
263
+ }
264
+
265
+ const approver = approval.approver;
266
+ const approvalSignature = approval.approval_sig;
267
+
268
+ if (!proofProposalApprovalRule.isBuffer(approver) || approver.length !== proofProposalApprovalRule.addressLength) {
269
+ ${approvalError}
270
+ return value;
271
+ }
272
+
273
+ if (!proofProposalApprovalRule.isBuffer(approvalSignature) || approvalSignature.length !== proofProposalApprovalRule.signatureByteLength) {
274
+ ${approvalError}
275
+ return value;
276
+ }
277
+
278
+ let approverIsEmpty = true;
279
+ for (let i = 0; i < approver.length; i++) {
280
+ if (approver[i] !== 0) {
281
+ approverIsEmpty = false;
282
+ break;
283
+ }
284
+ }
285
+
286
+ let approvalSignatureIsEmpty = true;
287
+ for (let i = 0; i < approvalSignature.length; i++) {
288
+ if (approvalSignature[i] !== 0) {
289
+ approvalSignatureIsEmpty = false;
290
+ break;
291
+ }
292
+ }
293
+
294
+ if (approverIsEmpty || approvalSignatureIsEmpty) {
295
+ ${approvalError}
296
+ return value;
297
+ }
298
+
299
+ const reencodedApproval = proofProposalApprovalRule.encodeApproval({
300
+ approver,
301
+ approval_sig: approvalSignature
302
+ });
303
+
304
+ if (!proofProposalApprovalRule.equals(value, reencodedApproval)) {
305
+ ${approvalError}
306
+ }
307
+
308
+ return value;
309
+ `
310
+ };
311
+ };
312
+ }