@adapt-toolkit/sdk 0.1.0
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/dist/common/index.d.ts +7 -0
- package/dist/common/index.d.ts.map +1 -0
- package/dist/common/index.js +7 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/interfaces/adapt_environment.d.ts +21 -0
- package/dist/common/interfaces/adapt_environment.d.ts.map +1 -0
- package/dist/common/interfaces/adapt_environment.js +2 -0
- package/dist/common/interfaces/adapt_environment.js.map +1 -0
- package/dist/common/interfaces/adapt_evaluation_unit.d.ts +12 -0
- package/dist/common/interfaces/adapt_evaluation_unit.d.ts.map +1 -0
- package/dist/common/interfaces/adapt_evaluation_unit.js +2 -0
- package/dist/common/interfaces/adapt_evaluation_unit.js.map +1 -0
- package/dist/common/interfaces/adapt_packet_context.d.ts +29 -0
- package/dist/common/interfaces/adapt_packet_context.d.ts.map +1 -0
- package/dist/common/interfaces/adapt_packet_context.js +2 -0
- package/dist/common/interfaces/adapt_packet_context.js.map +1 -0
- package/dist/common/interfaces/adapt_value.d.ts +23 -0
- package/dist/common/interfaces/adapt_value.d.ts.map +1 -0
- package/dist/common/interfaces/adapt_value.js +2 -0
- package/dist/common/interfaces/adapt_value.js.map +1 -0
- package/dist/common/interfaces/common.d.ts +18 -0
- package/dist/common/interfaces/common.d.ts.map +1 -0
- package/dist/common/interfaces/common.js +4 -0
- package/dist/common/interfaces/common.js.map +1 -0
- package/dist/common/memory_management/adapt_object_lifetime.d.ts +114 -0
- package/dist/common/memory_management/adapt_object_lifetime.d.ts.map +1 -0
- package/dist/common/memory_management/adapt_object_lifetime.js +159 -0
- package/dist/common/memory_management/adapt_object_lifetime.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/loaders/browser.d.ts +5 -0
- package/dist/loaders/browser.d.ts.map +1 -0
- package/dist/loaders/browser.js +6 -0
- package/dist/loaders/browser.js.map +1 -0
- package/dist/loaders/node.d.ts +13 -0
- package/dist/loaders/node.d.ts.map +1 -0
- package/dist/loaders/node.js +29 -0
- package/dist/loaders/node.js.map +1 -0
- package/dist/loaders/react-native.d.ts +11 -0
- package/dist/loaders/react-native.d.ts.map +1 -0
- package/dist/loaders/react-native.js +19 -0
- package/dist/loaders/react-native.js.map +1 -0
- package/dist/loaders/shared.d.ts +17 -0
- package/dist/loaders/shared.d.ts.map +1 -0
- package/dist/loaders/shared.js +2 -0
- package/dist/loaders/shared.js.map +1 -0
- package/dist/utilities/broker/broker.d.ts +21 -0
- package/dist/utilities/broker/broker.d.ts.map +1 -0
- package/dist/utilities/broker/broker.js +267 -0
- package/dist/utilities/broker/broker.js.map +1 -0
- package/dist/utilities/broker/broker_client.d.ts +40 -0
- package/dist/utilities/broker/broker_client.d.ts.map +1 -0
- package/dist/utilities/broker/broker_client.js +111 -0
- package/dist/utilities/broker/broker_client.js.map +1 -0
- package/dist/utilities/broker/broker_configurator.d.ts +6 -0
- package/dist/utilities/broker/broker_configurator.d.ts.map +1 -0
- package/dist/utilities/broker/broker_configurator.js +8 -0
- package/dist/utilities/broker/broker_configurator.js.map +1 -0
- package/dist/utilities/broker/index.d.ts +4 -0
- package/dist/utilities/broker/index.d.ts.map +1 -0
- package/dist/utilities/broker/index.js +4 -0
- package/dist/utilities/broker/index.js.map +1 -0
- package/dist/utilities/common/adapt_waitlist.d.ts +5 -0
- package/dist/utilities/common/adapt_waitlist.d.ts.map +1 -0
- package/dist/utilities/common/adapt_waitlist.js +4 -0
- package/dist/utilities/common/adapt_waitlist.js.map +1 -0
- package/dist/utilities/common/async_queue_adapter.d.ts +40 -0
- package/dist/utilities/common/async_queue_adapter.d.ts.map +1 -0
- package/dist/utilities/common/async_queue_adapter.js +88 -0
- package/dist/utilities/common/async_queue_adapter.js.map +1 -0
- package/dist/utilities/common/command_line_configurator.d.ts +88 -0
- package/dist/utilities/common/command_line_configurator.d.ts.map +1 -0
- package/dist/utilities/common/command_line_configurator.js +217 -0
- package/dist/utilities/common/command_line_configurator.js.map +1 -0
- package/dist/utilities/common/decorators.d.ts +17 -0
- package/dist/utilities/common/decorators.d.ts.map +1 -0
- package/dist/utilities/common/decorators.js +84 -0
- package/dist/utilities/common/decorators.js.map +1 -0
- package/dist/utilities/common/errors/adapt_initialization_error.d.ts +4 -0
- package/dist/utilities/common/errors/adapt_initialization_error.d.ts.map +1 -0
- package/dist/utilities/common/errors/adapt_initialization_error.js +6 -0
- package/dist/utilities/common/errors/adapt_initialization_error.js.map +1 -0
- package/dist/utilities/common/errors/adapt_network_error.d.ts +8 -0
- package/dist/utilities/common/errors/adapt_network_error.d.ts.map +1 -0
- package/dist/utilities/common/errors/adapt_network_error.js +12 -0
- package/dist/utilities/common/errors/adapt_network_error.js.map +1 -0
- package/dist/utilities/common/errors/adapt_protocol_error.d.ts +12 -0
- package/dist/utilities/common/errors/adapt_protocol_error.d.ts.map +1 -0
- package/dist/utilities/common/errors/adapt_protocol_error.js +16 -0
- package/dist/utilities/common/errors/adapt_protocol_error.js.map +1 -0
- package/dist/utilities/common/errors/adapt_test_mode_error.d.ts +11 -0
- package/dist/utilities/common/errors/adapt_test_mode_error.d.ts.map +1 -0
- package/dist/utilities/common/errors/adapt_test_mode_error.js +22 -0
- package/dist/utilities/common/errors/adapt_test_mode_error.js.map +1 -0
- package/dist/utilities/common/http_server.d.ts +18 -0
- package/dist/utilities/common/http_server.d.ts.map +1 -0
- package/dist/utilities/common/http_server.js +41 -0
- package/dist/utilities/common/http_server.js.map +1 -0
- package/dist/utilities/common/index.d.ts +25 -0
- package/dist/utilities/common/index.d.ts.map +1 -0
- package/dist/utilities/common/index.js +36 -0
- package/dist/utilities/common/index.js.map +1 -0
- package/dist/utilities/common/logger/logger.d.ts +83 -0
- package/dist/utilities/common/logger/logger.d.ts.map +1 -0
- package/dist/utilities/common/logger/logger.js +272 -0
- package/dist/utilities/common/logger/logger.js.map +1 -0
- package/dist/utilities/common/logger/logging_level.d.ts +13 -0
- package/dist/utilities/common/logger/logging_level.d.ts.map +1 -0
- package/dist/utilities/common/logger/logging_level.js +36 -0
- package/dist/utilities/common/logger/logging_level.js.map +1 -0
- package/dist/utilities/common/logger/message.d.ts +65 -0
- package/dist/utilities/common/logger/message.d.ts.map +1 -0
- package/dist/utilities/common/logger/message.js +169 -0
- package/dist/utilities/common/logger/message.js.map +1 -0
- package/dist/utilities/common/logger/network_component.d.ts +3 -0
- package/dist/utilities/common/logger/network_component.d.ts.map +1 -0
- package/dist/utilities/common/logger/network_component.js +4 -0
- package/dist/utilities/common/logger/network_component.js.map +1 -0
- package/dist/utilities/common/totp_qr_code_generator.d.ts +12 -0
- package/dist/utilities/common/totp_qr_code_generator.d.ts.map +1 -0
- package/dist/utilities/common/totp_qr_code_generator.js +19 -0
- package/dist/utilities/common/totp_qr_code_generator.js.map +1 -0
- package/dist/utilities/common/waitlist/waitlist.d.ts +36 -0
- package/dist/utilities/common/waitlist/waitlist.d.ts.map +1 -0
- package/dist/utilities/common/waitlist/waitlist.js +116 -0
- package/dist/utilities/common/waitlist/waitlist.js.map +1 -0
- package/dist/utilities/common/web_socket_client.d.ts +50 -0
- package/dist/utilities/common/web_socket_client.d.ts.map +1 -0
- package/dist/utilities/common/web_socket_client.js +80 -0
- package/dist/utilities/common/web_socket_client.js.map +1 -0
- package/dist/utilities/common/web_socket_connection.d.ts +29 -0
- package/dist/utilities/common/web_socket_connection.d.ts.map +1 -0
- package/dist/utilities/common/web_socket_connection.js +94 -0
- package/dist/utilities/common/web_socket_connection.js.map +1 -0
- package/dist/utilities/evaluator/common.d.ts +10 -0
- package/dist/utilities/evaluator/common.d.ts.map +1 -0
- package/dist/utilities/evaluator/common.js +128 -0
- package/dist/utilities/evaluator/common.js.map +1 -0
- package/dist/utilities/evaluator/evaluator.d.ts +35 -0
- package/dist/utilities/evaluator/evaluator.d.ts.map +1 -0
- package/dist/utilities/evaluator/evaluator.js +141 -0
- package/dist/utilities/evaluator/evaluator.js.map +1 -0
- package/dist/utilities/evaluator/index.d.ts +3 -0
- package/dist/utilities/evaluator/index.d.ts.map +1 -0
- package/dist/utilities/evaluator/index.js +3 -0
- package/dist/utilities/evaluator/index.js.map +1 -0
- package/dist/utilities/executables/adapt_wrapper.d.ts +3 -0
- package/dist/utilities/executables/adapt_wrapper.d.ts.map +1 -0
- package/dist/utilities/executables/adapt_wrapper.js +25 -0
- package/dist/utilities/executables/adapt_wrapper.js.map +1 -0
- package/dist/utilities/executables/adapt_wrapper_mobile.d.ts +3 -0
- package/dist/utilities/executables/adapt_wrapper_mobile.d.ts.map +1 -0
- package/dist/utilities/executables/adapt_wrapper_mobile.js +20 -0
- package/dist/utilities/executables/adapt_wrapper_mobile.js.map +1 -0
- package/dist/utilities/executables/adapt_wrapper_script.d.ts +2 -0
- package/dist/utilities/executables/adapt_wrapper_script.d.ts.map +1 -0
- package/dist/utilities/executables/adapt_wrapper_script.js +6 -0
- package/dist/utilities/executables/adapt_wrapper_script.js.map +1 -0
- package/dist/utilities/executables/broker.d.ts +2 -0
- package/dist/utilities/executables/broker.d.ts.map +1 -0
- package/dist/utilities/executables/broker.js +23 -0
- package/dist/utilities/executables/broker.js.map +1 -0
- package/dist/utilities/executables/index.d.ts +2 -0
- package/dist/utilities/executables/index.d.ts.map +1 -0
- package/dist/utilities/executables/index.js +2 -0
- package/dist/utilities/executables/index.js.map +1 -0
- package/dist/utilities/executables/local_data_storage.d.ts +2 -0
- package/dist/utilities/executables/local_data_storage.d.ts.map +1 -0
- package/dist/utilities/executables/local_data_storage.js +20 -0
- package/dist/utilities/executables/local_data_storage.js.map +1 -0
- package/dist/utilities/executables/local_data_storage_cmd_helper.d.ts +2 -0
- package/dist/utilities/executables/local_data_storage_cmd_helper.d.ts.map +1 -0
- package/dist/utilities/executables/local_data_storage_cmd_helper.js +52 -0
- package/dist/utilities/executables/local_data_storage_cmd_helper.js.map +1 -0
- package/dist/utilities/storage/data_storage.d.ts +97 -0
- package/dist/utilities/storage/data_storage.d.ts.map +1 -0
- package/dist/utilities/storage/data_storage.js +440 -0
- package/dist/utilities/storage/data_storage.js.map +1 -0
- package/dist/utilities/storage/index.d.ts +6 -0
- package/dist/utilities/storage/index.d.ts.map +1 -0
- package/dist/utilities/storage/index.js +6 -0
- package/dist/utilities/storage/index.js.map +1 -0
- package/dist/utilities/storage/local_data_storage.d.ts +7 -0
- package/dist/utilities/storage/local_data_storage.d.ts.map +1 -0
- package/dist/utilities/storage/local_data_storage.js +18 -0
- package/dist/utilities/storage/local_data_storage.js.map +1 -0
- package/dist/utilities/storage/local_data_storage_base.d.ts +74 -0
- package/dist/utilities/storage/local_data_storage_base.d.ts.map +1 -0
- package/dist/utilities/storage/local_data_storage_base.js +163 -0
- package/dist/utilities/storage/local_data_storage_base.js.map +1 -0
- package/dist/utilities/storage/local_data_storage_configurator.d.ts +7 -0
- package/dist/utilities/storage/local_data_storage_configurator.d.ts.map +1 -0
- package/dist/utilities/storage/local_data_storage_configurator.js +13 -0
- package/dist/utilities/storage/local_data_storage_configurator.js.map +1 -0
- package/dist/utilities/storage/storage_client.d.ts +28 -0
- package/dist/utilities/storage/storage_client.d.ts.map +1 -0
- package/dist/utilities/storage/storage_client.js +79 -0
- package/dist/utilities/storage/storage_client.js.map +1 -0
- package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.d.ts +14 -0
- package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.d.ts.map +1 -0
- package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.js +57 -0
- package/dist/utilities/wrappers/configurators/adapt_wrapper_configurator.js.map +1 -0
- package/dist/utilities/wrappers/configurators/control_packet_configurator.d.ts +12 -0
- package/dist/utilities/wrappers/configurators/control_packet_configurator.d.ts.map +1 -0
- package/dist/utilities/wrappers/configurators/control_packet_configurator.js +52 -0
- package/dist/utilities/wrappers/configurators/control_packet_configurator.js.map +1 -0
- package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.d.ts +32 -0
- package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.d.ts.map +1 -0
- package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.js +123 -0
- package/dist/utilities/wrappers/configurators/packet_wrapper_configurator.js.map +1 -0
- package/dist/utilities/wrappers/enclave/control_packet.d.ts +18 -0
- package/dist/utilities/wrappers/enclave/control_packet.d.ts.map +1 -0
- package/dist/utilities/wrappers/enclave/control_packet.js +213 -0
- package/dist/utilities/wrappers/enclave/control_packet.js.map +1 -0
- package/dist/utilities/wrappers/index.d.ts +5 -0
- package/dist/utilities/wrappers/index.d.ts.map +1 -0
- package/dist/utilities/wrappers/index.js +5 -0
- package/dist/utilities/wrappers/index.js.map +1 -0
- package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.d.ts +19 -0
- package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.d.ts.map +1 -0
- package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.js +55 -0
- package/dist/utilities/wrappers/wrappers/adapt_packet_wrapper.js.map +1 -0
- package/dist/utilities/wrappers/wrappers/adapt_wrapper.d.ts +131 -0
- package/dist/utilities/wrappers/wrappers/adapt_wrapper.d.ts.map +1 -0
- package/dist/utilities/wrappers/wrappers/adapt_wrapper.js +716 -0
- package/dist/utilities/wrappers/wrappers/adapt_wrapper.js.map +1 -0
- package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.d.ts +75 -0
- package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.d.ts.map +1 -0
- package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.js +550 -0
- package/dist/utilities/wrappers/wrappers/impl/adapt_packet_wrapper_impl.js.map +1 -0
- package/dist/wasm/adapt.d.ts +79 -0
- package/dist/wasm/adapt.d.ts.map +1 -0
- package/dist/wasm/adapt.js +430 -0
- package/dist/wasm/adapt.js.map +1 -0
- package/dist/wasm/backend-node.d.ts +1 -0
- package/dist/wasm/backend-node.d.ts.map +1 -0
- package/dist/wasm/backend-node.js +1 -0
- package/dist/wasm/backend-node.js.map +1 -0
- package/dist/wasm/index.d.ts +12 -0
- package/dist/wasm/index.d.ts.map +1 -0
- package/dist/wasm/index.js +15 -0
- package/dist/wasm/index.js.map +1 -0
- package/dist/wasm/mufl-bindings.d.ts +3 -0
- package/dist/wasm/mufl-bindings.d.ts.map +1 -0
- package/dist/wasm/mufl-bindings.js +3833 -0
- package/dist/wasm/mufl-bindings.js.map +1 -0
- package/dist/wasm/mufl-bindings.wasm +0 -0
- package/dist/wasm/wasm_types.d.ts +96 -0
- package/dist/wasm/wasm_types.d.ts.map +1 -0
- package/dist/wasm/wasm_types.js +2 -0
- package/dist/wasm/wasm_types.js.map +1 -0
- package/dist/wrapper/conversion.d.ts +9 -0
- package/dist/wrapper/conversion.d.ts.map +1 -0
- package/dist/wrapper/conversion.js +30 -0
- package/dist/wrapper/conversion.js.map +1 -0
- package/dist/wrapper/index.d.ts +11 -0
- package/dist/wrapper/index.d.ts.map +1 -0
- package/dist/wrapper/index.js +25 -0
- package/dist/wrapper/index.js.map +1 -0
- package/dist/wrapper/memory_management/adapt_builder.d.ts +36 -0
- package/dist/wrapper/memory_management/adapt_builder.d.ts.map +1 -0
- package/dist/wrapper/memory_management/adapt_builder.js +98 -0
- package/dist/wrapper/memory_management/adapt_builder.js.map +1 -0
- package/dist/wrapper/memory_management/exit_handler_registrar.d.ts +10 -0
- package/dist/wrapper/memory_management/exit_handler_registrar.d.ts.map +1 -0
- package/dist/wrapper/memory_management/exit_handler_registrar.js +38 -0
- package/dist/wrapper/memory_management/exit_handler_registrar.js.map +1 -0
- package/dist/wrapper/protocol/message_handler_interfaces.d.ts +32 -0
- package/dist/wrapper/protocol/message_handler_interfaces.d.ts.map +1 -0
- package/dist/wrapper/protocol/message_handler_interfaces.js +2 -0
- package/dist/wrapper/protocol/message_handler_interfaces.js.map +1 -0
- package/dist/wrapper/protocol/protocol.d.ts +171 -0
- package/dist/wrapper/protocol/protocol.d.ts.map +1 -0
- package/dist/wrapper/protocol/protocol.js +387 -0
- package/dist/wrapper/protocol/protocol.js.map +1 -0
- package/package.json +127 -0
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
import { AsyncQueueAdapter, logging, AdaptInitializationError, LazyMessage } from '../../common/index.js';
|
|
2
|
+
import { AdaptObjectBuilder, object_to_adapt_value, Protocol, ReadFile } from '../../../wrapper/index.js';
|
|
3
|
+
import { AdaptEnvironment, AdaptObjectLifetime, AdaptPacketContext } from '@adapt-toolkit/sdk/backend';
|
|
4
|
+
import { AdaptPacketWrapper } from './adapt_packet_wrapper.js';
|
|
5
|
+
import { PacketWrapperConfigurator } from '../configurators/packet_wrapper_configurator.js';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import { BrokerClient } from '../../broker/index.js';
|
|
8
|
+
import { StorageClient } from '../../storage/index.js';
|
|
9
|
+
import { AdaptNetworkComponentConfigurator } from '../../common/index.js';
|
|
10
|
+
var AdaptWrapperServices;
|
|
11
|
+
(function (AdaptWrapperServices) {
|
|
12
|
+
class PacketManager {
|
|
13
|
+
/**
|
|
14
|
+
* @units is a map of locally available(i.e. previously loaded from the storage) units by its hash.
|
|
15
|
+
* Before requesting unit from the data storage, we have to first check, whether we have it locally
|
|
16
|
+
*
|
|
17
|
+
* @requested_units is a map of already requested, but not yet received units.
|
|
18
|
+
* It has to contain more than one callback, that should be called when the unit is received, because of the following situation:
|
|
19
|
+
* One adapt wrapper could manage several AdaptPacketWrappers with the same unit code underneath, but created with different seed phrases.
|
|
20
|
+
* Those packets could be requested simultanesously, in this case we don't want to request a unit from a storage multiple times.
|
|
21
|
+
* Instead, we can add a callback to an existing field in the map and call it as soon as we receive a unit from the storage.
|
|
22
|
+
*
|
|
23
|
+
* @packets is a map of AdaptPacketWrappers by packet's id. We add a packet there each time, when we create a new packet wrapper.
|
|
24
|
+
* It allows us to track packets managed by us and redirect messages addressed to packets.
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
#units = new Map(); // locally available units
|
|
28
|
+
#requested_units = new Map(); // these units have already been requested from data storage, but not yet received
|
|
29
|
+
#packets = new Map(); // created packets
|
|
30
|
+
#request_from_storage; // callback to request a unit from the data storage
|
|
31
|
+
#wrapper;
|
|
32
|
+
constructor(wrapper, request_from_storage) {
|
|
33
|
+
this.#wrapper = wrapper;
|
|
34
|
+
this.#request_from_storage = request_from_storage;
|
|
35
|
+
}
|
|
36
|
+
request_unit = (hash, cb) => {
|
|
37
|
+
logging.debug("Requesting unit: ", hash);
|
|
38
|
+
if (this.#units.has(hash)) {
|
|
39
|
+
logging.debug("Unit found: ", hash);
|
|
40
|
+
cb(this.#units.get(hash));
|
|
41
|
+
}
|
|
42
|
+
else if (this.#requested_units.has(hash)) {
|
|
43
|
+
logging.debug("Unit has already been requested. Waiting: ", hash);
|
|
44
|
+
this.#requested_units.set(hash, this.#requested_units.get(hash).concat(cb));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
logging.debug("Requesting unit from the data storage: ", hash);
|
|
48
|
+
this.#requested_units.set(hash, [cb]);
|
|
49
|
+
this.#request_from_storage(hash);
|
|
50
|
+
// request a unit data from the storage
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
request_unit_local = (hash, path, cb) => {
|
|
54
|
+
if (this.#units.has(hash)) {
|
|
55
|
+
cb(this.#units.get(hash));
|
|
56
|
+
}
|
|
57
|
+
else if (this.#requested_units.has(hash)) {
|
|
58
|
+
this.#requested_units.set(hash, this.#requested_units.get(hash).concat(cb));
|
|
59
|
+
}
|
|
60
|
+
this.#requested_units.set(hash, [cb]);
|
|
61
|
+
try {
|
|
62
|
+
ReadFile(path).then(data => {
|
|
63
|
+
this.add_unit(hash, data);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
throw new AdaptInitializationError("Failed to load a unit from file: " + e);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
add_unit = (hash, data) => {
|
|
71
|
+
if (!this.#requested_units.has(hash))
|
|
72
|
+
throw new Error("Internal error: returned unit that wasn't requested: " + hash);
|
|
73
|
+
this.#requested_units.get(hash).forEach(cb => {
|
|
74
|
+
cb(data);
|
|
75
|
+
});
|
|
76
|
+
this.#requested_units.delete(hash);
|
|
77
|
+
this.#units.set(hash, data);
|
|
78
|
+
};
|
|
79
|
+
add_unit_external = (hash, data) => {
|
|
80
|
+
this.#units.set(hash, data);
|
|
81
|
+
this.#requested_units.delete(hash);
|
|
82
|
+
};
|
|
83
|
+
add_packet = (id, wrapper) => {
|
|
84
|
+
if (this.#packets.has(id))
|
|
85
|
+
throw new Error("Packet with given id already exists: " + id);
|
|
86
|
+
this.#packets.set(id, wrapper);
|
|
87
|
+
};
|
|
88
|
+
remove_packet = (id) => {
|
|
89
|
+
if (!this.#packets.has(id)) {
|
|
90
|
+
throw new Error("Attempting to delete a packet with unknown id: " + id);
|
|
91
|
+
}
|
|
92
|
+
this.#packets.delete(id);
|
|
93
|
+
};
|
|
94
|
+
get packets() {
|
|
95
|
+
return this.#packets;
|
|
96
|
+
}
|
|
97
|
+
init = (packets) => {
|
|
98
|
+
packets.forEach(config => {
|
|
99
|
+
// each packet should either have a state hash(backup the packet from storage) or a unit hash(create a new packet from unit loaded from the storage)
|
|
100
|
+
const packet_id = config.packet_id;
|
|
101
|
+
const state_hash = config.state_hash;
|
|
102
|
+
const unit_hash = config.unit_hash;
|
|
103
|
+
if (config.load_from_storage) {
|
|
104
|
+
if (!this.#wrapper.storage_available)
|
|
105
|
+
throw new AdaptInitializationError("Failed to load packet from the storage. Storage is not available in the current wrapper configuration.");
|
|
106
|
+
if (state_hash || packet_id) {
|
|
107
|
+
logging.debug("Backing up a packet.", "State hash: ", state_hash, ", packet id: ", packet_id);
|
|
108
|
+
this.#wrapper.back_up_packet(config);
|
|
109
|
+
}
|
|
110
|
+
else if (unit_hash) {
|
|
111
|
+
logging.debug("Creating a packet.");
|
|
112
|
+
this.#wrapper.create_packet(config);
|
|
113
|
+
}
|
|
114
|
+
else
|
|
115
|
+
throw new Error("Neither --state_hash nor --unit_hash is specified.");
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
this.#wrapper.create_packet_local(config);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
add_packet_local = (packet_config, on_packet_created, unit, _packet) => {
|
|
123
|
+
AdaptObjectBuilder.execute_and_collect_garbage(async (builder) => {
|
|
124
|
+
const seed_phrase = packet_config.seed_phrase ? packet_config.seed_phrase : AdaptEnvironment.GetRandomBytes(builder.lifetime, 16).Visualize();
|
|
125
|
+
const entropy = builder.AdaptEnvironment.GetRandomBytes(256);
|
|
126
|
+
const init_arg = packet_config.init_arg ? builder.Attach(object_to_adapt_value(JSON.parse(packet_config.init_arg))) : undefined;
|
|
127
|
+
let packet = _packet
|
|
128
|
+
? builder.Attach(_packet)
|
|
129
|
+
: builder.AdaptEnvironment.CreatePacket(unit, "Seed phrase: " + seed_phrase, AdaptNetworkComponentConfigurator.test_mode ? "" : entropy.Visualize().substring(2), false, init_arg).Detach();
|
|
130
|
+
logging.info("Packet created with id:", packet.GetContainerID().Visualize());
|
|
131
|
+
let messages_adapt_values = [];
|
|
132
|
+
if (packet_config instanceof PacketWrapperConfigurator) {
|
|
133
|
+
packet_config.transactions.forEach(({ name, targ }) => {
|
|
134
|
+
const targ_mufl = packet.ParseValueFromJSON(targ);
|
|
135
|
+
const message = object_to_adapt_value({ name: name, targ: targ_mufl });
|
|
136
|
+
messages_adapt_values.push(message);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
let wrapper = new AdaptPacketWrapper(packet, packet_config, this.#wrapper, messages_adapt_values);
|
|
140
|
+
this.add_packet(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
141
|
+
if (packet_config instanceof PacketWrapperConfigurator)
|
|
142
|
+
this.#wrapper.on_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
143
|
+
else {
|
|
144
|
+
if (!this.#wrapper.control_packet) {
|
|
145
|
+
logging.error("Control packet is undefined.");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.#wrapper.control_packet.packet = wrapper;
|
|
149
|
+
this.#wrapper.on_control_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
150
|
+
}
|
|
151
|
+
on_packet_created(wrapper);
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
create_packet = (packet_config, on_packet_created, unit_contents, _packet) => {
|
|
155
|
+
const create_packet_cb = () => {
|
|
156
|
+
AdaptObjectBuilder.execute_and_collect_garbage(async (builder) => {
|
|
157
|
+
logging.debug("Loading unit from content");
|
|
158
|
+
const unit = builder.AdaptEvaluationUnit.LoadFromContents(unit_contents);
|
|
159
|
+
logging.debug("Building packet with seed phrase:", packet_config.seed_phrase);
|
|
160
|
+
const seed_phrase = packet_config.seed_phrase ? packet_config.seed_phrase : AdaptEnvironment.GetRandomBytes(builder.lifetime, 16).Visualize();
|
|
161
|
+
const entropy = builder.AdaptEnvironment.GetRandomBytes(256);
|
|
162
|
+
const init_arg = packet_config.init_arg ? builder.Attach(object_to_adapt_value(JSON.parse(packet_config.init_arg))) : undefined;
|
|
163
|
+
let packet = _packet ? builder.Attach(_packet) : builder.AdaptEnvironment.CreatePacket(unit, "Seed phrase: " + seed_phrase, AdaptNetworkComponentConfigurator.test_mode ? "" : entropy.Visualize().substring(2), false, init_arg).Detach();
|
|
164
|
+
logging.info("Packet created with id:", packet.GetContainerID().Visualize());
|
|
165
|
+
let messages_adapt_values = [];
|
|
166
|
+
if (packet_config instanceof PacketWrapperConfigurator) {
|
|
167
|
+
packet_config.transactions.forEach(({ name, targ }) => {
|
|
168
|
+
const targ_mufl = packet.ParseValueFromJSON(targ);
|
|
169
|
+
const message = object_to_adapt_value({ name: name, targ: targ_mufl });
|
|
170
|
+
messages_adapt_values.push(message);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
let wrapper = new AdaptPacketWrapper(packet, packet_config, this.#wrapper, messages_adapt_values);
|
|
174
|
+
this.add_packet(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
175
|
+
if (packet_config instanceof PacketWrapperConfigurator)
|
|
176
|
+
this.#wrapper.on_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
177
|
+
else {
|
|
178
|
+
if (!this.#wrapper.control_packet) {
|
|
179
|
+
logging.error("Control packet is undefined.");
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
this.#wrapper.control_packet.packet = wrapper;
|
|
183
|
+
this.#wrapper.on_control_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
184
|
+
}
|
|
185
|
+
on_packet_created(wrapper);
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
create_packet_cb();
|
|
189
|
+
if (!_packet && packet_config instanceof PacketWrapperConfigurator && packet_config.replicas > 1) {
|
|
190
|
+
logging.debug("Creating more replicas of the packet: ", packet_config.replicas);
|
|
191
|
+
packet_config.seed_phrase = undefined;
|
|
192
|
+
for (let i = 1; i < packet_config.replicas; i++) {
|
|
193
|
+
create_packet_cb();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
AdaptWrapperServices.PacketManager = PacketManager;
|
|
199
|
+
class BackupManager {
|
|
200
|
+
#wrapper;
|
|
201
|
+
#requested_backups = new Map();
|
|
202
|
+
#request_backup_data_from_storage;
|
|
203
|
+
constructor(wrapper, request_backup_data_from_storage) {
|
|
204
|
+
this.#wrapper = wrapper;
|
|
205
|
+
this.#request_backup_data_from_storage = request_backup_data_from_storage;
|
|
206
|
+
}
|
|
207
|
+
request_backup = (packet_info, on_backup) => {
|
|
208
|
+
const message_hash = this.#request_backup_data_from_storage(packet_info);
|
|
209
|
+
this.#requested_backups.set(message_hash, on_backup);
|
|
210
|
+
};
|
|
211
|
+
on_backup_data = (message_hash, data) => {
|
|
212
|
+
if (!this.#requested_backups.has(message_hash))
|
|
213
|
+
throw new Error("Internal error: unrecognized message hash");
|
|
214
|
+
this.#requested_backups.get(message_hash)(data);
|
|
215
|
+
this.#requested_backups.delete(message_hash);
|
|
216
|
+
};
|
|
217
|
+
back_up_packet = (packet_config, on_packet_backed_up, backup_data_message) => {
|
|
218
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
219
|
+
const message_body = builder.Attach(Protocol.GetBody(backup_data_message));
|
|
220
|
+
const packet_data = message_body.Reduce(Protocol.FieldName.packet_data);
|
|
221
|
+
const transaction_chain = message_body.Reduce(Protocol.FieldName.transaction_chain).Detach();
|
|
222
|
+
if (packet_data.GetBinary().length === 0) {
|
|
223
|
+
logging.info("Backup data for this packet not found.");
|
|
224
|
+
if (packet_config.attempt_backup) {
|
|
225
|
+
logging.info("Attempt backup option is set. Creating a new packet.");
|
|
226
|
+
this.#wrapper.create_packet(packet_config);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
logging.error("Failed to back up the packet.");
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
let packet = builder.AdaptPacketContext.LoadFromContents(packet_data.GetBinary()).Detach();
|
|
235
|
+
logging.info("Packet backup up: ", packet.GetContainerID().Visualize());
|
|
236
|
+
logging.info("Packet code id: ", packet.GetCodeID().Visualize());
|
|
237
|
+
const unit_hash = packet.GetCodeID();
|
|
238
|
+
this.#wrapper.packet_manager.request_unit(unit_hash.Visualize().substring(2), unit_contents => {
|
|
239
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
240
|
+
builder.Attach(transaction_chain);
|
|
241
|
+
builder.AdaptEvaluationUnit.LoadFromContents(unit_contents);
|
|
242
|
+
let idx = 0;
|
|
243
|
+
let transaction;
|
|
244
|
+
while (!(transaction = transaction_chain.Reduce(idx++)).IsNil()) {
|
|
245
|
+
const request_data = packet.ExecuteTransaction(builder.Attach(object_to_adapt_value({
|
|
246
|
+
name: "::key_storage_api::decrypt_message",
|
|
247
|
+
targ: transaction.Reduce(Protocol.FieldName.request_data_encrypted)
|
|
248
|
+
})));
|
|
249
|
+
const transient_data = packet.ExecuteTransaction(builder.Attach(object_to_adapt_value({
|
|
250
|
+
name: "::key_storage_api::decrypt_message",
|
|
251
|
+
targ: transaction.Reduce(Protocol.FieldName.transient_data_encrypted)
|
|
252
|
+
})));
|
|
253
|
+
const trn_result = packet.ExecuteTransaction(request_data, transient_data.Reduce(Protocol.FieldName.entropy).Visualize(), transient_data.Reduce(Protocol.FieldName.timestamp));
|
|
254
|
+
packet.Destroy();
|
|
255
|
+
packet = trn_result.GetPacket();
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
let messages_adapt_values = [];
|
|
259
|
+
packet_config.transactions.forEach(({ name, targ }) => {
|
|
260
|
+
const targ_mufl = packet.ParseValueFromJSON(targ);
|
|
261
|
+
const message = object_to_adapt_value({ name: name, args: targ_mufl });
|
|
262
|
+
messages_adapt_values.push(message);
|
|
263
|
+
});
|
|
264
|
+
let wrapper = new AdaptPacketWrapper(packet, packet_config, this.#wrapper, messages_adapt_values, true);
|
|
265
|
+
this.#wrapper.packet_manager.add_packet(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
266
|
+
logging.info("Packet backed up. ID: ", wrapper.packet.GetContainerID().Visualize());
|
|
267
|
+
logging.info("State id: ", wrapper.packet.GetHash().Visualize().substring(2));
|
|
268
|
+
this.#wrapper.on_packet_created(wrapper.packet.GetContainerID().Visualize(), wrapper);
|
|
269
|
+
on_packet_backed_up(wrapper);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
AdaptWrapperServices.BackupManager = BackupManager;
|
|
275
|
+
class BrokerAPI_impl {
|
|
276
|
+
#wrapper;
|
|
277
|
+
#cb;
|
|
278
|
+
#is_ready = false;
|
|
279
|
+
#pending_messages = [];
|
|
280
|
+
#broker_client;
|
|
281
|
+
// config: AdaptWrapperConfigurator
|
|
282
|
+
constructor(wrapper) {
|
|
283
|
+
logging.debug("Creating broker api service");
|
|
284
|
+
this.#wrapper = wrapper;
|
|
285
|
+
this.#broker_client = new BrokerClient(wrapper.config.broker_address, this);
|
|
286
|
+
}
|
|
287
|
+
start = () => {
|
|
288
|
+
this.#broker_client.start();
|
|
289
|
+
};
|
|
290
|
+
get is_ready() {
|
|
291
|
+
return this.#is_ready;
|
|
292
|
+
}
|
|
293
|
+
receive_from_broker = (message) => {
|
|
294
|
+
logging.message_received("broker_client", "adapt_wrapper", message);
|
|
295
|
+
this.#wrapper.receive_message(message);
|
|
296
|
+
};
|
|
297
|
+
register_callback_broker = (cb) => {
|
|
298
|
+
if (this.#cb) {
|
|
299
|
+
logging.error("Attempting to set the broker callback twice.");
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
this.#cb = cb;
|
|
303
|
+
};
|
|
304
|
+
broker_client_ready = () => {
|
|
305
|
+
this.#is_ready = true;
|
|
306
|
+
this.#pending_messages.forEach(msg => this.#wrapper.send_to_broker(msg));
|
|
307
|
+
};
|
|
308
|
+
send_to_broker = (message) => {
|
|
309
|
+
if (!this.#cb) {
|
|
310
|
+
logging.error("Callback to send data to broker is not registered.");
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (!this.#is_ready) {
|
|
314
|
+
this.#pending_messages.push(message);
|
|
315
|
+
logging.debug("Message added to the list of pending messages: ", new LazyMessage(() => message.Visualize()));
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
logging.message_sent("adapt_wrapper", 'broker_client', message);
|
|
319
|
+
this.#cb(message);
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
AdaptWrapperServices.BrokerAPI_impl = BrokerAPI_impl;
|
|
323
|
+
class StorageAPI_impl {
|
|
324
|
+
#wrapper;
|
|
325
|
+
#cb;
|
|
326
|
+
#is_ready = false;
|
|
327
|
+
#pending_messages = [];
|
|
328
|
+
#storage_client;
|
|
329
|
+
constructor(wrapper) {
|
|
330
|
+
this.#wrapper = wrapper;
|
|
331
|
+
if (wrapper.config.env === 'enclave') {
|
|
332
|
+
this.#storage_client = new StorageClient(wrapper.config.storage_address, this);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
start() {
|
|
336
|
+
if (this.#wrapper.config.env === 'enclave')
|
|
337
|
+
this.#storage_client?.start();
|
|
338
|
+
}
|
|
339
|
+
get is_ready() {
|
|
340
|
+
return this.#is_ready;
|
|
341
|
+
}
|
|
342
|
+
receive_from_data_storage = (message) => {
|
|
343
|
+
logging.message_received("storage_client", "adapt_wrapper", message);
|
|
344
|
+
this.#wrapper.receive_message(message);
|
|
345
|
+
};
|
|
346
|
+
register_callback_data_storage = (cb) => {
|
|
347
|
+
if (this.#cb) {
|
|
348
|
+
logging.error("Attempting to set the storage callback twice.");
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
this.#cb = cb;
|
|
352
|
+
};
|
|
353
|
+
storage_client_ready = () => {
|
|
354
|
+
this.#is_ready = true;
|
|
355
|
+
this.#pending_messages.forEach(msg => this.#wrapper.send_to_storage(msg));
|
|
356
|
+
};
|
|
357
|
+
send_to_storage = (message) => {
|
|
358
|
+
if (!this.#cb) {
|
|
359
|
+
logging.error("Callback to send data to storage is not regisered.");
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (!this.#is_ready) {
|
|
363
|
+
this.#pending_messages.push(message);
|
|
364
|
+
logging.debug("Message added to the list of pending messages: ", new LazyMessage(() => message.Visualize()));
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
logging.message_sent("adapt_wrapper", "storage_client", message);
|
|
368
|
+
this.#cb(message);
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
AdaptWrapperServices.StorageAPI_impl = StorageAPI_impl;
|
|
372
|
+
class MessageProcessor {
|
|
373
|
+
#wrapper;
|
|
374
|
+
#queue;
|
|
375
|
+
#send_to_broker;
|
|
376
|
+
#send_to_storage;
|
|
377
|
+
constructor(wrapper, send_to_broker, send_to_storage) {
|
|
378
|
+
this.#wrapper = wrapper;
|
|
379
|
+
this.#send_to_broker = send_to_broker;
|
|
380
|
+
this.#send_to_storage = send_to_storage;
|
|
381
|
+
this.#queue = new AsyncQueueAdapter(this.#worker.bind(this));
|
|
382
|
+
this.#queue.add_worker_functions([
|
|
383
|
+
{ type_id: Protocol.MessageName.b2w_init, func: this.b2w_init_handler.bind(this) },
|
|
384
|
+
{ type_id: Protocol.MessageName.b2w_exec_transaction, func: this.b2w_exec_transaction_handler.bind(this) },
|
|
385
|
+
{ type_id: Protocol.MessageName.b2w_reg_ok, func: this.b2w_reg_ok_handler.bind(this) },
|
|
386
|
+
{ type_id: Protocol.MessageName.d2w_backup_data, func: this.d2w_backup_data_handler.bind(this) },
|
|
387
|
+
{ type_id: Protocol.MessageName.d2w_create_packet_confirm, func: this.d2w_create_packet_confirm_handler.bind(this) },
|
|
388
|
+
{ type_id: Protocol.MessageName.d2w_init, func: this.d2w_init_handler.bind(this) },
|
|
389
|
+
{ type_id: Protocol.MessageName.d2w_code_object, func: this.d2w_code_object_handler.bind(this) },
|
|
390
|
+
{ type_id: Protocol.MessageName.d2w_last_snapshot_state, func: this.d2w_last_snapshot_state_handler.bind(this) },
|
|
391
|
+
{ type_id: Protocol.MessageName.d2w_last_state, func: this.d2w_last_state_handler.bind(this) },
|
|
392
|
+
{ type_id: Protocol.MessageName.d2w_snapshot, func: this.d2w_snapshot_handler.bind(this) },
|
|
393
|
+
{ type_id: Protocol.MessageName.d2w_transaction_chain, func: this.d2w_transaction_chain_handler.bind(this) },
|
|
394
|
+
{ type_id: Protocol.MessageName.d2w_write_snapshot_confirm, func: this.d2w_write_snapshot_confirm_handler.bind(this) },
|
|
395
|
+
{ type_id: Protocol.MessageName.d2w_write_transaction_confirm, func: this.d2w_write_transaction_confirm_handler.bind(this) },
|
|
396
|
+
]);
|
|
397
|
+
}
|
|
398
|
+
push(event) {
|
|
399
|
+
this.#queue.push(event);
|
|
400
|
+
}
|
|
401
|
+
#worker = (event, cb) => {
|
|
402
|
+
try {
|
|
403
|
+
if (event.kind === 'incoming') {
|
|
404
|
+
logging.debug("Message processed in queue (incoming): ", new LazyMessage(() => event.data.Visualize()));
|
|
405
|
+
this.#incoming_message_handler(event.data);
|
|
406
|
+
}
|
|
407
|
+
else if (event.kind === 'broker_out') {
|
|
408
|
+
logging.debug("Message processed in queue (broker_out): ", new LazyMessage(() => event.data.Visualize()));
|
|
409
|
+
this.#send_to_broker(event.data);
|
|
410
|
+
}
|
|
411
|
+
else if (event.kind === 'data_storage_out') {
|
|
412
|
+
logging.debug("Message processed in queue (storage_out): ", new LazyMessage(() => event.data.Visualize()));
|
|
413
|
+
this.#send_to_storage(event.data);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
catch (e) {
|
|
417
|
+
logging.error(e);
|
|
418
|
+
}
|
|
419
|
+
finally {
|
|
420
|
+
cb();
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
#incoming_message_handler = (message) => {
|
|
424
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
425
|
+
builder.Attach(message); // the message is attached here, so it does not need to be attached in the
|
|
426
|
+
try {
|
|
427
|
+
const type_id = Protocol.GetTypeId(message);
|
|
428
|
+
if (type_id[2] === 'd') { // message to data storage
|
|
429
|
+
this.#wrapper.send_to_storage(message.Detach());
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (type_id[2] === 'b') { // message to broker
|
|
433
|
+
this.#wrapper.send_to_broker(message.Detach());
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const func = this.#queue.get_function(type_id);
|
|
437
|
+
if (func)
|
|
438
|
+
func(message);
|
|
439
|
+
else
|
|
440
|
+
logging.error("Adapt wrapper received unrecognized message: ", type_id);
|
|
441
|
+
}
|
|
442
|
+
catch (e) {
|
|
443
|
+
logging.error(e);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
#pass_to_adapt_packet_wrapper = (message) => {
|
|
448
|
+
logging.debug("Passing the message to the packet wrapper: ", new LazyMessage(() => message.Visualize()));
|
|
449
|
+
this.#wrapper.send_to_packet_wrapper(message.Detach());
|
|
450
|
+
};
|
|
451
|
+
b2w_exec_transaction_handler = this.#pass_to_adapt_packet_wrapper;
|
|
452
|
+
b2w_init_handler = (message) => {
|
|
453
|
+
logging.error("Internal error: This message is not expected here: ", message.Visualize());
|
|
454
|
+
};
|
|
455
|
+
b2w_reg_ok_handler = this.#pass_to_adapt_packet_wrapper;
|
|
456
|
+
d2w_backup_data_handler = (message) => {
|
|
457
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
458
|
+
const message_hash = builder.Attach(Protocol.GetMessageHash(message));
|
|
459
|
+
this.#wrapper.backup_manager.on_backup_data(message_hash.Visualize(), message);
|
|
460
|
+
});
|
|
461
|
+
};
|
|
462
|
+
d2w_code_object_handler = (message) => {
|
|
463
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
464
|
+
const body = builder.Attach(Protocol.GetBody(message));
|
|
465
|
+
const unit_contents = body.Reduce(Protocol.FieldName.code_object).Reduce(Protocol.FieldName.code_object_data).GetBinary();
|
|
466
|
+
const unit_hash = body.Reduce(Protocol.FieldName.code_object).Reduce(Protocol.FieldName.code_object_hash).Visualize().substring(2);
|
|
467
|
+
this.#wrapper.packet_manager.add_unit(unit_hash, unit_contents);
|
|
468
|
+
});
|
|
469
|
+
};
|
|
470
|
+
d2w_create_packet_confirm_handler = this.#pass_to_adapt_packet_wrapper;
|
|
471
|
+
d2w_init_handler = (message) => {
|
|
472
|
+
logging.error("Internal error: This message is not expected here: ", message.Visualize());
|
|
473
|
+
};
|
|
474
|
+
d2w_last_snapshot_state_handler = this.#pass_to_adapt_packet_wrapper;
|
|
475
|
+
d2w_last_state_handler = this.#pass_to_adapt_packet_wrapper;
|
|
476
|
+
d2w_snapshot_handler = this.#pass_to_adapt_packet_wrapper;
|
|
477
|
+
d2w_transaction_chain_handler = this.#pass_to_adapt_packet_wrapper;
|
|
478
|
+
d2w_write_snapshot_confirm_handler = this.#pass_to_adapt_packet_wrapper;
|
|
479
|
+
d2w_write_transaction_confirm_handler = this.#pass_to_adapt_packet_wrapper;
|
|
480
|
+
}
|
|
481
|
+
AdaptWrapperServices.MessageProcessor = MessageProcessor;
|
|
482
|
+
class Helpers {
|
|
483
|
+
#wrapper;
|
|
484
|
+
constructor(wrapper) {
|
|
485
|
+
this.#wrapper = wrapper;
|
|
486
|
+
}
|
|
487
|
+
request_unit_from_storage = (unit_hash) => {
|
|
488
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
489
|
+
logging.debug('Unit hash is ', unit_hash);
|
|
490
|
+
const emptyPacket = AdaptEnvironment.EmptyPacket(undefined, false);
|
|
491
|
+
const unit_hash_adapt_value = builder.Attach(emptyPacket.ExecuteFunction("_binary_to_hash_code", [emptyPacket.ExecuteFunction('_hex_string_to_binary', [unit_hash])]));
|
|
492
|
+
// const unit_hash_adapt_value = builder.Attach(
|
|
493
|
+
// Protocol.GetProtocolContainer().ExecuteTransaction(
|
|
494
|
+
// builder.Attach(object_to_adapt_value({
|
|
495
|
+
// name: "::protocol_container::primitives_wrapper::binary_to_hash_code",
|
|
496
|
+
// targ: builder.Attach(Protocol.GetProtocolContainer().ExecuteTransaction(
|
|
497
|
+
// builder.Attach(object_to_adapt_value({
|
|
498
|
+
// name: "::protocol_container::primitives_wrapper::hex_string_to_binary",
|
|
499
|
+
// targ: unit_hash
|
|
500
|
+
// }))
|
|
501
|
+
// ))
|
|
502
|
+
//
|
|
503
|
+
// }))
|
|
504
|
+
// )
|
|
505
|
+
// );
|
|
506
|
+
const args = builder.Attach(Protocol.GetProtocolContainer().CreateDictionary().Mutate(Protocol.FieldName.code_object_hash, unit_hash_adapt_value));
|
|
507
|
+
const message = Protocol.CreateProtocolMessage(Protocol.MessageName.w2d_get_code_object, { body: args }).Detach();
|
|
508
|
+
logging.debug("Setting new packet with message hash:", new LazyMessage(() => message.GetHash().Visualize()));
|
|
509
|
+
this.#wrapper.send_to_storage(message);
|
|
510
|
+
});
|
|
511
|
+
};
|
|
512
|
+
request_backup_data_from_storage = ({ packet_id, state }) => {
|
|
513
|
+
if (!packet_id && !state)
|
|
514
|
+
throw new Error("Internal error: both packet id and state hash are undefined while trying to request packet backup data.");
|
|
515
|
+
if (state)
|
|
516
|
+
logging.debug("Attempting to back up a packet to a state: ", state);
|
|
517
|
+
else if (packet_id)
|
|
518
|
+
logging.debug("Attempting to back up a packet to the most recent state. Packet id: ", packet_id);
|
|
519
|
+
return AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
520
|
+
const packet = builder.AdaptPacketContext.EmptyPacket(false);
|
|
521
|
+
if (state) {
|
|
522
|
+
logging.debug("State is ", state);
|
|
523
|
+
}
|
|
524
|
+
const emptyPacket = AdaptEnvironment.EmptyPacket(undefined, false);
|
|
525
|
+
const stateAdaptValue = state ? builder.Attach(emptyPacket.ExecuteFunction('_binary_to_hash_code', [builder.Attach(emptyPacket.ExecuteFunction('_hex_string_to_binary', [state]))])) : undefined;
|
|
526
|
+
// const stateAdaptValue = state ? builder.Attach(
|
|
527
|
+
// Protocol.GetProtocolContainer().ExecuteTransaction(
|
|
528
|
+
// builder.Attach(object_to_adapt_value({
|
|
529
|
+
// name: "::protocol_container::primitives_wrapper::binary_to_hash_code",
|
|
530
|
+
// targ: builder.Attach(Protocol.GetProtocolContainer().ExecuteTransaction(
|
|
531
|
+
// builder.Attach(object_to_adapt_value({
|
|
532
|
+
// name: "::protocol_container::primitives_wrapper::hex_string_to_binary",
|
|
533
|
+
// targ: state
|
|
534
|
+
// }))
|
|
535
|
+
// ))
|
|
536
|
+
// }))
|
|
537
|
+
// )
|
|
538
|
+
// ) : undefined;
|
|
539
|
+
// create backup data request to the data storage
|
|
540
|
+
const message = Protocol.CreateProtocolMessage(Protocol.MessageName.w2d_request_backup_data, {
|
|
541
|
+
body: packet.CreateDictionary()
|
|
542
|
+
.Mutate(Protocol.FieldName.packet_id, packet_id ? packet_id : packet.Nil())
|
|
543
|
+
.Mutate(Protocol.FieldName.state, stateAdaptValue ? stateAdaptValue : packet.Nil())
|
|
544
|
+
});
|
|
545
|
+
const message_hash = message.GetHash().Visualize();
|
|
546
|
+
this.#wrapper.send_to_storage(message);
|
|
547
|
+
return message_hash;
|
|
548
|
+
});
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
AdaptWrapperServices.Helpers = Helpers;
|
|
552
|
+
class ControlPacket {
|
|
553
|
+
#wrapper;
|
|
554
|
+
packet;
|
|
555
|
+
constructor(wrapper, config) {
|
|
556
|
+
this.#wrapper = wrapper;
|
|
557
|
+
const on_unit_data = this.#wrapper.packet_manager.create_packet.bind(this, config, () => { });
|
|
558
|
+
if (config.load_from_storage) {
|
|
559
|
+
this.#wrapper.packet_manager.request_unit(config.unit_hash, on_unit_data);
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
const control_packet_unit_dir = config.control_packet_dir_path;
|
|
563
|
+
const control_packet_path = path.join(control_packet_unit_dir, config.unit_hash + ".muflo").normalize();
|
|
564
|
+
this.#wrapper.packet_manager.request_unit_local(config.unit_hash, control_packet_path, on_unit_data);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
AdaptWrapperServices.ControlPacket = ControlPacket;
|
|
569
|
+
})(AdaptWrapperServices || (AdaptWrapperServices = {}));
|
|
570
|
+
export class AdaptWrapper {
|
|
571
|
+
config;
|
|
572
|
+
#broker_service = new AdaptWrapperServices.BrokerAPI_impl(this);
|
|
573
|
+
// receive_from_broker = this.#broker_service.receive_from_broker.bind(this.#broker_service);
|
|
574
|
+
// register_callback_broker = this.#broker_service.register_callback_broker.bind(this.#broker_service);
|
|
575
|
+
// broker_client_ready = this.#broker_service.broker_client_ready.bind(this.#broker_service);
|
|
576
|
+
get is_broker_ready() {
|
|
577
|
+
return this.#broker_service.is_ready;
|
|
578
|
+
}
|
|
579
|
+
#send_to_broker = this.#broker_service.send_to_broker.bind(this.#broker_service);
|
|
580
|
+
#storage_service = new AdaptWrapperServices.StorageAPI_impl(this);
|
|
581
|
+
// receive_from_data_storage = this.#storage_service.receive_from_data_storage.bind(this.#storage_service);
|
|
582
|
+
// register_callback_data_storage = this.#storage_service.register_callback_data_storage.bind(this.#storage_service);
|
|
583
|
+
// storage_client_ready = this.#storage_service.storage_client_ready.bind(this.#storage_service);
|
|
584
|
+
get is_storage_ready() {
|
|
585
|
+
return this.#storage_service.is_ready;
|
|
586
|
+
}
|
|
587
|
+
get storage_available() {
|
|
588
|
+
return this.config.env === 'enclave';
|
|
589
|
+
}
|
|
590
|
+
#send_to_storage = this.#storage_service.send_to_storage.bind(this.#storage_service);
|
|
591
|
+
#helpers = new AdaptWrapperServices.Helpers(this);
|
|
592
|
+
#request_unit_from_storage = this.#helpers.request_unit_from_storage.bind(this.#helpers);
|
|
593
|
+
#request_backup_data_from_storage = this.#helpers.request_backup_data_from_storage.bind(this.#helpers);
|
|
594
|
+
packet_manager = new AdaptWrapperServices.PacketManager(this, this.#request_unit_from_storage.bind(this));
|
|
595
|
+
backup_manager = new AdaptWrapperServices.BackupManager(this, this.#request_backup_data_from_storage.bind(this));
|
|
596
|
+
#message_processor = new AdaptWrapperServices.MessageProcessor(this, this.#send_to_broker.bind(this), this.#send_to_storage.bind(this));
|
|
597
|
+
control_packet;
|
|
598
|
+
constructor(config) {
|
|
599
|
+
this.config = config;
|
|
600
|
+
logging.info("Calling constructor of adapt wrapper");
|
|
601
|
+
// this.config = config;
|
|
602
|
+
}
|
|
603
|
+
// =================================== public API ==============================================
|
|
604
|
+
receive_message(message) {
|
|
605
|
+
this.#message_processor.push({ kind: "incoming", data: message });
|
|
606
|
+
}
|
|
607
|
+
send_to_packet_wrapper(message) {
|
|
608
|
+
AdaptObjectBuilder.execute_and_collect_garbage(builder => {
|
|
609
|
+
const to = builder.Attach(Protocol.GetTo(message)).Visualize();
|
|
610
|
+
// if (this.control_packet.)
|
|
611
|
+
if (this.packet_manager.packets.has(to))
|
|
612
|
+
this.packet_manager.packets.get(to).on_message(message);
|
|
613
|
+
else {
|
|
614
|
+
logging.error("Packet with id ", to, " is not known.");
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
send_to_storage(message) {
|
|
619
|
+
this.#message_processor.push({ kind: "data_storage_out", data: message });
|
|
620
|
+
}
|
|
621
|
+
send_to_broker(message) {
|
|
622
|
+
this.#message_processor.push({ kind: "broker_out", data: message });
|
|
623
|
+
}
|
|
624
|
+
create_packet(config, on_packet_created = (() => { }), packet) {
|
|
625
|
+
this.packet_manager.request_unit(config.unit_hash, (contents) => this.packet_manager.create_packet(config, on_packet_created, contents, packet));
|
|
626
|
+
}
|
|
627
|
+
create_packet_ex(unit_hash, seed_phrase, on_packet_created = (() => { }), packet) {
|
|
628
|
+
let config = new PacketWrapperConfigurator();
|
|
629
|
+
config.unit_hash = unit_hash;
|
|
630
|
+
config.seed_phrase = seed_phrase;
|
|
631
|
+
this.create_packet(config, on_packet_created, packet);
|
|
632
|
+
}
|
|
633
|
+
create_packet_local(config, on_packet_created = (() => { })) {
|
|
634
|
+
let packet;
|
|
635
|
+
let unit_hash;
|
|
636
|
+
if (config.state_hash) {
|
|
637
|
+
const state_hash = config.state_hash;
|
|
638
|
+
const packet_path = config.packet_dir_path + "/" + state_hash + ".muflp";
|
|
639
|
+
try {
|
|
640
|
+
packet = AdaptPacketContext.LoadFromFile(new AdaptObjectLifetime(), packet_path).Detach();
|
|
641
|
+
}
|
|
642
|
+
catch (e) {
|
|
643
|
+
throw new AdaptInitializationError("Failed to load a packet from file: " + e);
|
|
644
|
+
}
|
|
645
|
+
unit_hash = packet.GetCodeID().Visualize();
|
|
646
|
+
if (config.unit_hash !== unit_hash)
|
|
647
|
+
throw new AdaptInitializationError("Provided packet and code do not match.");
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
if (!config.unit_hash)
|
|
651
|
+
throw new AdaptInitializationError("Unit hash is not provided.");
|
|
652
|
+
unit_hash = config.unit_hash;
|
|
653
|
+
}
|
|
654
|
+
this.packet_manager.request_unit_local(unit_hash, config.unit_dir_path + "/" + unit_hash + ".muflo", content => {
|
|
655
|
+
return this.packet_manager.create_packet(config, on_packet_created, content, packet);
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
back_up_packet(config, on_packet_backed_up = (() => { })) {
|
|
659
|
+
const state_hash = config.state_hash;
|
|
660
|
+
const packet_id = config.packet_id;
|
|
661
|
+
this.backup_manager.request_backup({ packet_id: packet_id, state: state_hash }, this.backup_manager.back_up_packet.bind(this, config, on_packet_backed_up));
|
|
662
|
+
}
|
|
663
|
+
back_up_packet_ex({ packet_id, state }, on_packet_backed_up = (() => { })) {
|
|
664
|
+
let config = new PacketWrapperConfigurator();
|
|
665
|
+
config.packet_id = packet_id;
|
|
666
|
+
config.state_hash = state;
|
|
667
|
+
this.back_up_packet(config, on_packet_backed_up);
|
|
668
|
+
}
|
|
669
|
+
remove_packet(packet_id) {
|
|
670
|
+
this.packet_manager.remove_packet(packet_id);
|
|
671
|
+
}
|
|
672
|
+
#on_container_created_cb;
|
|
673
|
+
#on_control_packet_created_cbs = [];
|
|
674
|
+
set on_packet_created_cb(cb) {
|
|
675
|
+
console.log("Packet created cb is set");
|
|
676
|
+
this.#on_container_created_cb = cb;
|
|
677
|
+
}
|
|
678
|
+
set on_control_packet_created_cb(cb) {
|
|
679
|
+
this.#on_control_packet_created_cbs.push(cb);
|
|
680
|
+
}
|
|
681
|
+
on_packet_created(cid, wrapper) {
|
|
682
|
+
if (this.#on_container_created_cb)
|
|
683
|
+
this.#on_container_created_cb(cid, wrapper);
|
|
684
|
+
}
|
|
685
|
+
on_control_packet_created(cid, wrapper) {
|
|
686
|
+
this.#on_control_packet_created_cbs.forEach(cb => cb(cid, wrapper));
|
|
687
|
+
}
|
|
688
|
+
add_packet(unit_hash, seed_phrase, on_packet_created = (() => { }), packet) {
|
|
689
|
+
logging.debug("Adding packet");
|
|
690
|
+
return this.create_packet_ex(unit_hash, seed_phrase, on_packet_created, packet);
|
|
691
|
+
}
|
|
692
|
+
add_packet_local(on_packet_created, unit, packet) {
|
|
693
|
+
logging.debug("add_packet_local");
|
|
694
|
+
this.packet_manager.add_packet_local(new PacketWrapperConfigurator(), on_packet_created, unit, packet);
|
|
695
|
+
}
|
|
696
|
+
add_unit(unit_hash, unit_data) {
|
|
697
|
+
logging.debug("Adding unit: ", unit_hash);
|
|
698
|
+
this.packet_manager.add_unit_external(unit_hash, unit_data);
|
|
699
|
+
}
|
|
700
|
+
start() {
|
|
701
|
+
this.#broker_service.start();
|
|
702
|
+
this.#storage_service.start();
|
|
703
|
+
if (this.config.env === 'enclave') {
|
|
704
|
+
this.on_control_packet_created_cb = () => {
|
|
705
|
+
this.packet_manager.init(this.config.packets);
|
|
706
|
+
};
|
|
707
|
+
if (!this.config.control_packet)
|
|
708
|
+
throw new AdaptInitializationError("Control packet configuration is not provided in nitro enclave environment.");
|
|
709
|
+
this.control_packet = new AdaptWrapperServices.ControlPacket(this, this.config.control_packet);
|
|
710
|
+
}
|
|
711
|
+
else {
|
|
712
|
+
this.packet_manager.init(this.config.packets);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
//# sourceMappingURL=adapt_wrapper.js.map
|