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,161 @@
1
+ import b4a from 'b4a';
2
+ import consensusV1Generated from './consensusV1.generated.cjs';
3
+
4
+ const {
5
+ ConsensusMessageHeader,
6
+ ProofProposalApproval,
7
+ ProofProposal,
8
+ EpochAppendIntent,
9
+ } = consensusV1Generated.consensus.v1;
10
+
11
+ // Options for converting protobuf messages to plain objects, ensuring that bytes are returned as Buffers and enums as numbers.
12
+ const CONSENSUS_TO_OBJECT_OPTIONS = Object.freeze({
13
+ enums: Number,
14
+ longs: Number,
15
+ defaults: false,
16
+ arrays: true,
17
+ oneofs: false
18
+ });
19
+
20
+ const encodeMessage = (Message, payload) => {
21
+ const error = Message.verify(payload);
22
+ if (error) throw new Error(error);
23
+ return b4a.from(Message.encode(payload).finish());
24
+ }
25
+
26
+ const decodeMessage = (Message, payload) => {
27
+ return Message.toObject(
28
+ Message.decode(payload),
29
+ CONSENSUS_TO_OBJECT_OPTIONS
30
+ );
31
+ }
32
+
33
+ /**
34
+ * Encodes a consensus v1 message header for regular consensus network messages.
35
+ *
36
+ * @param {Object} payload - ConsensusMessageHeader payload.
37
+ * @returns {Buffer} Encoded consensus message.
38
+ */
39
+ export const encodeConsensusMessage = (payload) => {
40
+ return encodeMessage(ConsensusMessageHeader, payload);
41
+ }
42
+
43
+ /**
44
+ * Decodes a consensus v1 message header from regular consensus network messages.
45
+ *
46
+ * @param {Buffer} payload - Encoded ConsensusMessageHeader buffer.
47
+ * @returns {Object} Decoded consensus message.
48
+ */
49
+ export const decodeConsensusMessage = (payload) => {
50
+ return decodeMessage(ConsensusMessageHeader, payload);
51
+ }
52
+
53
+ /**
54
+ * Encodes a ProofProposalApproval for regular consensus usage.
55
+ *
56
+ * @param {Object} payload - ProofProposalApproval payload.
57
+ * @returns {Buffer} Encoded approval.
58
+ */
59
+ export const encodeProofProposalApproval = (payload) => {
60
+ return encodeMessage(ProofProposalApproval, payload);
61
+ }
62
+
63
+ /**
64
+ * Decodes a ProofProposalApproval for regular consensus usage.
65
+ *
66
+ * @param {Buffer} payload - Encoded ProofProposalApproval buffer.
67
+ * @returns {Object} Decoded approval.
68
+ */
69
+ export const decodeProofProposalApproval = (payload) => {
70
+ return decodeMessage(ProofProposalApproval, payload);
71
+ }
72
+
73
+ /**
74
+ * Encodes a ProofProposal for regular consensus usage.
75
+ *
76
+ * @param {Object} payload - ProofProposalApproval payload.
77
+ * @returns {Buffer} Encoded approval.
78
+ */
79
+ export const encodeProofProposal = (payload) => {
80
+ return encodeMessage(ProofProposal, payload);
81
+ }
82
+
83
+ /**
84
+ * Decodes a ProofProposal for regular consensus usage.
85
+ *
86
+ * @param {Buffer} payload - Encoded ProofProposal buffer.
87
+ * @returns {Object} Decoded .
88
+ */
89
+ export const decodeProofProposal = (payload) => {
90
+ return decodeMessage(ProofProposal, payload);
91
+ }
92
+
93
+ /**
94
+ * Safely encodes a ProofProposalApproval for apply-function validation paths.
95
+ * Returns an empty buffer when the approval payload is invalid.
96
+ *
97
+ * @param {*} payload - Input expected to match ProofProposalApproval.
98
+ * @returns {Buffer} Encoded approval or an empty buffer.
99
+ */
100
+ export const safeEncodeProofProposalApproval = (payload) => {
101
+ try {
102
+ return encodeProofProposalApproval(payload);
103
+ } catch (error) {
104
+ console.log("safeEncodeProofProposalApproval error:", error.message);
105
+ }
106
+
107
+ return b4a.alloc(0);
108
+ }
109
+
110
+ /**
111
+ * Safely decodes a ProofProposalApproval for apply-function validation paths.
112
+ * Returns null when the input is not a buffer or cannot be decoded.
113
+ *
114
+ * @param {*} payload - Encoded ProofProposalApproval buffer.
115
+ * @returns {Object|null} Decoded approval or null.
116
+ */
117
+ export const safeDecodeProofProposalApproval = (payload) => {
118
+ try {
119
+ if (!b4a.isBuffer(payload)) return null;
120
+ return decodeProofProposalApproval(payload);
121
+ } catch (error) {
122
+ console.log("safeDecodeProofProposalApproval error:", error.message);
123
+ }
124
+ return null;
125
+ }
126
+
127
+ /**
128
+ * Safely encodes a ProofProposal for apply-function validation paths.
129
+ * Returns an empty buffer when the payload is invalid.
130
+ *
131
+ * @param {*} payload - Input expected to match ProofProposal.
132
+ * @returns {Buffer} Encoded or an empty buffer.
133
+ */
134
+ export const safeEncodeProofProposal = (payload) => {
135
+ try {
136
+ return encodeProofProposal(payload);
137
+ } catch (error) {
138
+ console.log("safeEncodeProofProposal error:", error.message);
139
+ }
140
+
141
+ return b4a.alloc(0);
142
+ }
143
+
144
+ /**
145
+ * Safely decodes a ProofProposal for apply-function validation paths.
146
+ * Returns null when the input is not a buffer or cannot be decoded.
147
+ *
148
+ * @param {*} payload - Encoded ProofProposal buffer.
149
+ * @returns {Object|null} Decoded or null.
150
+ */
151
+ export const safeDecodeProofProposal = (payload) => {
152
+ try {
153
+ if (!b4a.isBuffer(payload)) return null;
154
+ return decodeProofProposal(payload);
155
+ } catch (error) {
156
+ console.log("safeDecodeProofProposal error:", error.message);
157
+ }
158
+ return null;
159
+ }
160
+
161
+ export const encodeEpochAppendIntent = payload => encodeMessage(EpochAppendIntent, payload);
@@ -0,0 +1,83 @@
1
+ import b4a from 'b4a';
2
+ import _ from 'lodash';
3
+ import {
4
+ VDF_DIFFICULTY_SIZE,
5
+ VDF_DISCRIMINANT_SIZE
6
+ } from '../../../utils/constants.js';
7
+
8
+ const VDF_CONFIG_SIZE = VDF_DIFFICULTY_SIZE + VDF_DISCRIMINANT_SIZE;
9
+
10
+ /**
11
+ * Canonically encodes a VDF configuration.
12
+ *
13
+ * @param {{difficulty: Buffer, discriminantBitSize: Buffer}} config - VDF configuration.
14
+ * @returns {Buffer} The canonical encoded VDF configuration.
15
+ * @throws {Error} When the configuration is structurally invalid.
16
+ */
17
+ export const encodeVdfConfig = (config) => {
18
+ if (!_.isPlainObject(config)) {
19
+ throw new Error('VDF config must be an object.');
20
+ }
21
+
22
+ if (!b4a.isBuffer(config.difficulty) || config.difficulty.length !== VDF_DIFFICULTY_SIZE) {
23
+ throw new Error(`VDF config difficulty must be a ${VDF_DIFFICULTY_SIZE}-byte buffer.`);
24
+ }
25
+
26
+ if (
27
+ !b4a.isBuffer(config.discriminantBitSize) ||
28
+ config.discriminantBitSize.length !== VDF_DISCRIMINANT_SIZE
29
+ ) {
30
+ throw new Error(
31
+ `VDF config discriminant bit size must be a ${VDF_DISCRIMINANT_SIZE}-byte buffer.`
32
+ );
33
+ }
34
+
35
+ return b4a.concat([config.difficulty, config.discriminantBitSize]);
36
+ }
37
+
38
+ /**
39
+ * Decodes a canonical VDF configuration.
40
+ *
41
+ * @param {Buffer} encoded - Canonical encoded VDF configuration.
42
+ * @returns {{difficulty: Buffer, discriminantBitSize: Buffer}} The decoded configuration.
43
+ * @throws {Error} When the encoded configuration is structurally invalid.
44
+ */
45
+ export const decodeVdfConfig = (encoded) => {
46
+ if (!b4a.isBuffer(encoded) ||
47
+ encoded.length !== VDF_CONFIG_SIZE) {
48
+ throw new Error(`Encoded VDF config must be a buffer of exactly ${VDF_CONFIG_SIZE} bytes.`);
49
+ }
50
+
51
+ return {
52
+ difficulty: b4a.toBuffer(encoded.subarray(0, VDF_DIFFICULTY_SIZE)),
53
+ discriminantBitSize: b4a.toBuffer(encoded.subarray(VDF_DIFFICULTY_SIZE))
54
+ };
55
+ }
56
+
57
+ /**
58
+ * Safely encodes a VDF configuration.
59
+ *
60
+ * @param {*} config - Value expected to contain a VDF configuration.
61
+ * @returns {Buffer} The canonical encoded configuration, or an empty buffer on failure.
62
+ */
63
+ export const safeEncodeVdfConfig = (config) => {
64
+ try {
65
+ return encodeVdfConfig(config);
66
+ } catch {
67
+ return b4a.alloc(0);
68
+ }
69
+ }
70
+
71
+ /**
72
+ * Safely decodes a canonical VDF configuration.
73
+ *
74
+ * @param {*} encoded - Value expected to contain an encoded VDF configuration.
75
+ * @returns {{difficulty: Buffer, discriminantBitSize: Buffer}|null} The decoded configuration, or null on failure.
76
+ */
77
+ export const safeDecodeVdfConfig = (encoded) => {
78
+ try {
79
+ return decodeVdfConfig(encoded);
80
+ } catch {
81
+ return null;
82
+ }
83
+ }