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.
Files changed (114) hide show
  1. package/package.json +9 -4
  2. package/proto/network/v1/enums/message_type.proto +16 -0
  3. package/proto/network/v1/enums/result_code.proto +84 -0
  4. package/proto/network/v1/messages/broadcast_transaction_request.proto +9 -0
  5. package/proto/network/v1/messages/broadcast_transaction_response.proto +13 -0
  6. package/proto/network/v1/messages/liveness_request.proto +8 -0
  7. package/proto/network/v1/messages/liveness_response.proto +11 -0
  8. package/proto/network/v1/network_message.proto +22 -0
  9. package/rpc/rpc_services.js +22 -4
  10. package/scripts/generate-protobufs.js +37 -12
  11. package/src/config/config.js +26 -5
  12. package/src/config/env.js +25 -11
  13. package/src/core/network/Network.js +73 -36
  14. package/src/core/network/protocols/LegacyProtocol.js +21 -11
  15. package/src/core/network/protocols/NetworkMessages.js +38 -17
  16. package/src/core/network/protocols/ProtocolInterface.js +14 -2
  17. package/src/core/network/protocols/ProtocolSession.js +144 -17
  18. package/src/core/network/protocols/V1Protocol.js +37 -18
  19. package/src/core/network/protocols/connectionPolicies.js +88 -0
  20. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +25 -19
  21. package/src/core/network/protocols/{shared/handlers/base/BaseOperationHandler.js → legacy/handlers/BaseStateOperationHandler.js} +23 -12
  22. package/src/core/network/protocols/legacy/handlers/{GetRequestHandler.js → LegacyGetRequestHandler.js} +6 -6
  23. package/src/core/network/protocols/legacy/handlers/LegacyResponseHandler.js +23 -0
  24. package/src/core/network/protocols/{shared/handlers/RoleOperationHandler.js → legacy/handlers/LegacyRoleOperationHandler.js} +18 -11
  25. package/src/core/network/protocols/{shared/handlers/SubnetworkOperationHandler.js → legacy/handlers/LegacySubnetworkOperationHandler.js} +28 -17
  26. package/src/core/network/protocols/{shared/handlers/TransferOperationHandler.js → legacy/handlers/LegacyTransferOperationHandler.js} +17 -11
  27. package/src/core/network/protocols/shared/errors/SharedValidatorRejectionError.js +27 -0
  28. package/src/core/network/protocols/shared/validators/{PartialBootstrapDeployment.js → PartialBootstrapDeploymentValidator.js} +9 -4
  29. package/src/core/network/protocols/shared/validators/{base/PartialOperation.js → PartialOperationValidator.js} +47 -25
  30. package/src/core/network/protocols/shared/validators/{PartialRoleAccess.js → PartialRoleAccessValidator.js} +51 -17
  31. package/src/core/network/protocols/shared/validators/{PartialTransaction.js → PartialTransactionValidator.js} +21 -7
  32. package/src/core/network/protocols/shared/validators/{PartialTransfer.js → PartialTransferValidator.js} +26 -9
  33. package/src/core/network/protocols/v1/NetworkMessageRouter.js +91 -7
  34. package/src/core/network/protocols/v1/V1ProtocolError.js +91 -0
  35. package/src/core/network/protocols/v1/handlers/V1BaseOperationHandler.js +65 -0
  36. package/src/core/network/protocols/v1/handlers/V1BroadcastTransactionOperationHandler.js +389 -0
  37. package/src/core/network/protocols/v1/handlers/V1LivenessOperationHandler.js +87 -0
  38. package/src/core/network/protocols/v1/validators/V1BaseOperation.js +211 -0
  39. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionRequest.js +26 -0
  40. package/src/core/network/protocols/v1/validators/V1BroadcastTransactionResponse.js +276 -0
  41. package/src/core/network/protocols/v1/validators/V1LivenessRequest.js +15 -0
  42. package/src/core/network/protocols/v1/validators/V1LivenessResponse.js +17 -0
  43. package/src/core/network/protocols/v1/validators/V1ValidationSchema.js +210 -0
  44. package/src/core/network/services/ConnectionManager.js +146 -94
  45. package/src/core/network/services/MessageOrchestrator.js +151 -27
  46. package/src/core/network/services/PendingRequestService.js +172 -0
  47. package/src/core/network/services/TransactionCommitService.js +149 -0
  48. package/src/core/network/services/TransactionPoolService.js +129 -18
  49. package/src/core/network/services/TransactionRateLimiterService.js +52 -34
  50. package/src/core/network/services/ValidatorHealthCheckService.js +127 -0
  51. package/src/core/network/services/ValidatorObserverService.js +18 -26
  52. package/src/core/state/State.js +70 -19
  53. package/src/index.js +5 -4
  54. package/src/messages/network/v1/NetworkMessageBuilder.js +59 -79
  55. package/src/messages/network/v1/NetworkMessageDirector.js +16 -50
  56. package/src/utils/Scheduler.js +0 -8
  57. package/src/utils/constants.js +71 -5
  58. package/src/utils/deepEqualApplyPayload.js +40 -0
  59. package/src/utils/helpers.js +10 -1
  60. package/src/utils/logger.js +25 -0
  61. package/src/utils/normalizers.js +38 -0
  62. package/src/utils/protobuf/networkV1.generated.cjs +2460 -0
  63. package/src/utils/protobuf/operationHelpers.js +24 -3
  64. package/tests/acceptance/v1/account/account.test.mjs +8 -2
  65. package/tests/acceptance/v1/tx/tx.test.mjs +23 -1
  66. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +34 -6
  67. package/tests/fixtures/networkV1.fixtures.js +2 -28
  68. package/tests/helpers/transactionPayloads.mjs +2 -2
  69. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +239 -79
  70. package/tests/unit/messages/network/NetworkMessageDirector.test.js +223 -77
  71. package/tests/unit/network/LegacyNetworkMessageRouter.test.js +54 -0
  72. package/tests/unit/network/ProtocolSession.test.js +127 -0
  73. package/tests/unit/network/networkModule.test.js +4 -1
  74. package/tests/unit/network/services/ConnectionManager.test.js +450 -0
  75. package/tests/unit/network/services/MessageOrchestrator.test.js +445 -0
  76. package/tests/unit/network/services/PendingRequestService.test.js +431 -0
  77. package/tests/unit/network/services/TransactionCommitService.test.js +246 -0
  78. package/tests/unit/network/services/TransactionPoolService.test.js +489 -0
  79. package/tests/unit/network/services/TransactionRateLimiterService.test.js +139 -0
  80. package/tests/unit/network/services/ValidatorHealthCheckService.test.js +115 -0
  81. package/tests/unit/network/services/services.test.js +17 -0
  82. package/tests/unit/network/utils/v1TestUtils.js +153 -0
  83. package/tests/unit/network/v1/NetworkMessageRouterV1.test.js +151 -0
  84. package/tests/unit/network/v1/V1BaseOperation.test.js +356 -0
  85. package/tests/unit/network/v1/V1BroadcastTransactionOperationHandler.test.js +129 -0
  86. package/tests/unit/network/v1/V1BroadcastTransactionRequest.test.js +53 -0
  87. package/tests/unit/network/v1/V1BroadcastTransactionResponse.test.js +512 -0
  88. package/tests/unit/network/v1/V1LivenessRequest.test.js +32 -0
  89. package/tests/unit/network/v1/V1LivenessResponse.test.js +45 -0
  90. package/tests/unit/network/v1/V1ResultCode.test.js +84 -0
  91. package/tests/unit/network/v1/V1ValidationSchema.test.js +13 -0
  92. package/tests/unit/network/v1/connectionPolicies.test.js +49 -0
  93. package/tests/unit/network/v1/handlers/V1BaseOperationHandler.test.js +284 -0
  94. package/tests/unit/network/v1/handlers/V1BroadcastTransactionOperationHandler.test.js +794 -0
  95. package/tests/unit/network/v1/handlers/V1LivenessOperationHandler.test.js +193 -0
  96. package/tests/unit/network/v1/v1.handlers.test.js +15 -0
  97. package/tests/unit/network/v1/v1.test.js +19 -0
  98. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionRequest.test.js +119 -0
  99. package/tests/unit/network/v1/v1ValidationSchema/broadcastTransactionResponse.test.js +136 -0
  100. package/tests/unit/network/v1/v1ValidationSchema/common.test.js +308 -0
  101. package/tests/unit/network/v1/v1ValidationSchema/livenessRequest.test.js +90 -0
  102. package/tests/unit/network/v1/v1ValidationSchema/livenessResponse.test.js +133 -0
  103. package/tests/unit/unit.test.js +2 -2
  104. package/tests/unit/utils/deepEqualApplyPayload/deepEqualApplyPayload.test.js +102 -0
  105. package/tests/unit/utils/protobuf/operationHelpers.test.js +2 -4
  106. package/tests/unit/utils/utils.test.js +1 -0
  107. package/.github/workflows/acceptance-tests.yml +0 -38
  108. package/.github/workflows/lint-pr-title.yml +0 -26
  109. package/.github/workflows/publish.yml +0 -33
  110. package/.github/workflows/unit-tests.yml +0 -34
  111. package/proto/network.proto +0 -74
  112. package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +0 -37
  113. package/src/utils/protobuf/network.cjs +0 -840
  114. package/tests/unit/network/ConnectionManager.test.js +0 -191
@@ -1,191 +0,0 @@
1
- import sinon from "sinon";
2
- import { hook, test } from 'brittle'
3
- import { default as EventEmitter } from "bare-events"
4
- import { testKeyPair1, testKeyPair2, testKeyPair3, testKeyPair4, testKeyPair5, testKeyPair6, testKeyPair7, testKeyPair8, testKeyPair9 } from "../../fixtures/apply.fixtures.js";
5
- import ConnectionManager from "../../../src/core/network/services/ConnectionManager.js";
6
- import { tick } from "../../helpers/setupApplyTests.js";
7
- import b4a from 'b4a'
8
- import { createConfig, ENV } from "../../../src/config/env.js";
9
-
10
- const createConnection = (key) => {
11
- const emitter = new EventEmitter()
12
- emitter.protocolSession = {
13
- has: (name) => name === 'legacy',
14
- send: sinon.stub().resolves(),
15
- };
16
- emitter.connected = true
17
- emitter.remotePublicKey = b4a.from(key, 'hex')
18
-
19
- return { key: b4a.from(key, 'hex'), connection: emitter }
20
- }
21
-
22
- const makeManager = (maxValidators = 6, conns = connections) => {
23
- const merged = createConfig(ENV.DEVELOPMENT, { maxValidators })
24
- const connectionManager = new ConnectionManager(merged)
25
-
26
- conns.forEach(({ key, connection }) => {
27
- connectionManager.addValidator(key, connection)
28
- });
29
-
30
- return connectionManager
31
- }
32
-
33
- const reset = () => {
34
- sinon.restore()
35
- connections.forEach(connection => {
36
- connection.connection.protocolSession.send.resetHistory()
37
- })
38
- }
39
-
40
- let connections
41
- hook('Initialize state', async () => {
42
- connections = [
43
- createConnection(testKeyPair1.publicKey),
44
- createConnection(testKeyPair2.publicKey),
45
- createConnection(testKeyPair3.publicKey),
46
- createConnection(testKeyPair4.publicKey),
47
- ]
48
- });
49
-
50
- test('ConnectionManager', () => {
51
- test('addValidator', async t => {
52
- test('adds a validator', async t => {
53
- reset()
54
- const connectionManager = makeManager()
55
- t.is(connectionManager.connectionCount(), connections.length, 'should have the same length')
56
- const data = createConnection(testKeyPair5.publicKey)
57
- connectionManager.addValidator(data.key, data.connection)
58
- t.is(connectionManager.connectionCount(), connections.length + 1, 'should have the same length')
59
- })
60
-
61
- test('dont surpass maxConnections', async t => {
62
- reset()
63
- const maxConnections = 5
64
- const connectionManager = makeManager(maxConnections)
65
- t.is(connectionManager.connectionCount(), connections.length, 'should have the same length')
66
-
67
- const toAdd = createConnection(testKeyPair5.publicKey)
68
- connectionManager.addValidator(toAdd.key, toAdd.connection)
69
- t.is(connectionManager.connectionCount(), maxConnections, 'should match the max connections')
70
-
71
- const toNotAdd = createConnection(testKeyPair6.publicKey)
72
- connectionManager.addValidator(toNotAdd.key, toNotAdd.connection)
73
- t.is(connectionManager.connectionCount(), maxConnections, 'should not increase length')
74
- })
75
-
76
- test('does not add new validator when pool is full', async t => {
77
- reset()
78
- const maxConnections = 2
79
- const localConnections = [
80
- createConnection(testKeyPair1.publicKey),
81
- createConnection(testKeyPair2.publicKey),
82
- ]
83
-
84
- const connectionManager = makeManager(maxConnections)
85
- localConnections.forEach(({ key, connection }) => {
86
- connectionManager.addValidator(key, connection)
87
- })
88
-
89
- t.is(connectionManager.connectionCount(), maxConnections, 'pool should be full')
90
-
91
- const newConn = createConnection(testKeyPair3.publicKey)
92
- connectionManager.addValidator(newConn.key, newConn.connection)
93
-
94
- t.is(connectionManager.connectionCount(), maxConnections, 'should stay at max size')
95
- t.not(connectionManager.connected(newConn.key), 'new validator should not be in the pool')
96
-
97
- const remainingOld = localConnections.filter(c => connectionManager.connected(c.key)).length
98
- t.is(remainingOld, 2, 'all of the old validators should remain')
99
- })
100
- })
101
-
102
- test('connected', async t => {
103
- test('true', async t => {
104
- reset()
105
- const connectionManager = makeManager()
106
- connections.forEach(con => {
107
- t.ok(connectionManager.connected(con.key), 'should respond true')
108
- })
109
- })
110
-
111
- test('false', async t => {
112
- reset()
113
- const connectionManager = makeManager()
114
- t.ok(!connectionManager.connected(testKeyPair6.publicKey), 'should respond false')
115
- })
116
- })
117
-
118
- test('send', async t => {
119
- test('triggers send on messenger', async t => {
120
- reset()
121
- const connectionManager = makeManager()
122
-
123
- const target = connectionManager.send([1,2,3,4])
124
-
125
- const totalCalls = connections.reduce((sum, con) => sum + con.connection.protocolSession.send.callCount, 0)
126
- t.is(totalCalls, 1, 'should send to exactly one validator')
127
- t.ok(target, 'should return a target public key')
128
- })
129
-
130
- test('does not throw on individual send errors', async t => {
131
- reset()
132
- const errorConnections = [
133
- createConnection(testKeyPair7.publicKey),
134
- createConnection(testKeyPair8.publicKey),
135
- ]
136
-
137
- errorConnections.forEach(con => {
138
- con.connection.protocolSession.send = sinon.stub().throws(new Error())
139
- })
140
-
141
- const connectionManager = makeManager(5, errorConnections)
142
-
143
- t.is(errorConnections.length, 2, 'should have two connections')
144
- connectionManager.send([1,2,3,4])
145
- t.ok(true, 'send should not throw even if individual sends fail')
146
- })
147
- })
148
-
149
- test('on close', async t => {
150
- test('removes from list', async t => {
151
- reset()
152
- const connectionManager = makeManager()
153
-
154
- const connectionCount = connectionManager.connectionCount()
155
-
156
- connections[1].connection.connected = false
157
- connections[1].connection.emit('close')
158
- await tick()
159
- t.is(connectionCount, connectionManager.connectionCount() + 1, 'first on the list should have been called')
160
- })
161
- })
162
-
163
- test('remove', async t => {
164
- test('removes a validator by public key', async t => {
165
- reset()
166
- const connectionManager = makeManager()
167
- const previousCount = connectionManager.connectionCount()
168
- const lastValidator = connections.shift()
169
-
170
- t.ok(connectionManager.connected(lastValidator.key), 'should be connected')
171
- connectionManager.remove(lastValidator.key)
172
-
173
- t.is(connectionManager.connectionCount(), previousCount - 1, 'should reduce the connection count')
174
- t.ok(!connectionManager.connected(lastValidator.key), 'should be connected')
175
- })
176
- })
177
-
178
- test('on close', async t => {
179
- test('removes from list', async t => {
180
- reset()
181
- const connectionManager = makeManager()
182
-
183
- const connectionCount = connectionManager.connectionCount()
184
-
185
- connections[1].connection.connected = false
186
- connections[1].connection.emit('close')
187
- await tick()
188
- t.is(connectionCount, connectionManager.connectionCount() + 1, 'first on the list should have been called')
189
- })
190
- })
191
- })