12 Napi::ThreadSafeFunction classCallback,
13 Napi::ThreadSafeFunction addContractsCallback,
14 Napi::ThreadSafeFunction bytecodeCommitmentCallback,
15 Napi::ThreadSafeFunction debugNameCallback,
16 Napi::ThreadSafeFunction createCheckpointCallback,
17 Napi::ThreadSafeFunction commitCheckpointCallback,
18 Napi::ThreadSafeFunction revertCheckpointCallback)
19 : contract_instance_callback_(
std::move(instanceCallback))
20 , contract_class_callback_(
std::move(classCallback))
21 , add_contracts_callback_(
std::move(addContractsCallback))
22 , bytecode_commitment_callback_(
std::move(bytecodeCommitmentCallback))
23 , debug_name_callback_(
std::move(debugNameCallback))
24 , create_checkpoint_callback_(
std::move(createCheckpointCallback))
25 , commit_checkpoint_callback_(
std::move(commitCheckpointCallback))
26 , revert_checkpoint_callback_(
std::move(revertCheckpointCallback))
33 throw std::runtime_error(
"Cannot call get_contract_instance after releasing callbacks");
36 debug(
"TsCallbackContractDB: Fetching contract instance for address ",
address);
42 if (!result_data.has_value()) {
47 auto instance = deserialize_from_msgpack<bb::avm2::ContractInstance>(*result_data,
"contract instance");
49 }
catch (
const std::exception& e) {
50 throw std::runtime_error(std::string(
"Failed to get contract instance for address ") +
format(
address) +
": " +
59 throw std::runtime_error(
"Cannot call get_contract_class after releasing callbacks");
62 debug(
"TsCallbackContractDB: Fetching contract class for class_id ",
class_id);
67 if (!result_data.has_value()) {
72 auto contract_class = deserialize_from_msgpack<bb::avm2::ContractClass>(*result_data,
"contract class");
74 }
catch (
const std::exception& e) {
75 throw std::runtime_error(std::string(
"Failed to get contract class for class_id ") +
format(
class_id) +
": " +
83 throw std::runtime_error(
"Cannot call add_contracts after releasing callbacks");
86 debug(
"TsCallbackContractDB: Adding contracts");
91 }
catch (
const std::exception& e) {
92 throw std::runtime_error(std::string(
"Failed to add contracts: ") + e.what());
100 throw std::runtime_error(
"Cannot call get_bytecode_commitment after releasing callbacks");
103 debug(
"TsCallbackContractDB: Fetching bytecode commitment for class_id ",
class_id);
109 if (!result_data.has_value()) {
114 auto commitment = deserialize_from_msgpack<bb::avm2::FF>(*result_data,
"bytecode commitment");
116 }
catch (
const std::exception& e) {
117 throw std::runtime_error(std::string(
"Failed to get bytecode commitment for class_id ") +
format(
class_id) +
126 throw std::runtime_error(
"Cannot call get_debug_function_name after releasing callbacks");
129 debug(
"TsCallbackContractDB: Fetching debug function name for address ",
address,
" selector ", selector);
135 if (!result_data.has_value()) {
136 debug(
"Debug function name not found for address ",
address,
" selector ", selector);
141 std::string name(result_data->begin(), result_data->end());
143 }
catch (
const std::exception& e) {
144 throw std::runtime_error(std::string(
"Failed to get debug function name for address ") +
format(
address) +
145 " selector " +
format(selector) +
": " + e.what());
152 throw std::runtime_error(
"Cannot call create_checkpoint after releasing callbacks");
155 debug(
"TsCallbackContractDB: Creating checkpoint");
163 auto js_result = js_callback.Call({});
165 if (!js_result.IsPromise()) {
166 data->error_message =
"TypeScript callback did not return a Promise";
167 data->result_promise.set_value(std::nullopt);
171 auto promise = js_result.As<Napi::Promise>();
176 }
catch (
const std::exception& e) {
177 throw std::runtime_error(std::string(
"Failed to create checkpoint: ") + e.what());
184 throw std::runtime_error(
"Cannot call commit_checkpoint after releasing callbacks");
187 debug(
"TsCallbackContractDB: Committing checkpoint");
195 auto js_result = js_callback.Call({});
197 if (!js_result.IsPromise()) {
198 data->error_message =
"TypeScript callback did not return a Promise";
199 data->result_promise.set_value(std::nullopt);
203 auto promise = js_result.As<Napi::Promise>();
208 }
catch (
const std::exception& e) {
209 throw std::runtime_error(std::string(
"Failed to commit checkpoint: ") + e.what());
216 throw std::runtime_error(
"Cannot call revert_checkpoint after releasing callbacks");
219 debug(
"TsCallbackContractDB: Reverting checkpoint");
227 auto js_result = js_callback.Call({});
229 if (!js_result.IsPromise()) {
230 data->error_message =
"TypeScript callback did not return a Promise";
231 data->result_promise.set_value(std::nullopt);
235 auto promise = js_result.As<Napi::Promise>();
240 }
catch (
const std::exception& e) {
241 throw std::runtime_error(std::string(
"Failed to revert checkpoint: ") + e.what());
std::shared_ptr< Napi::ThreadSafeFunction > instance
std::optional< bb::avm2::ContractClass > get_contract_class(const bb::avm2::ContractClassId &class_id) const override
Fetches a contract class by class ID.
void revert_checkpoint() override
Reverts the current checkpoint.
std::optional< std::string > get_debug_function_name(const bb::avm2::AztecAddress &address, const bb::avm2::FF &selector) const override
Fetches debug function name for a contract function.
void add_contracts(const bb::avm2::ContractDeploymentData &contract_deployment_data) override
Adds contracts from deployment data.
void commit_checkpoint() override
Commits the current checkpoint.
std::optional< bb::avm2::ContractInstance > get_contract_instance(const bb::avm2::AztecAddress &address) const override
Fetches a contract instance by address.
TsCallbackContractDB(Napi::ThreadSafeFunction instanceCallback, Napi::ThreadSafeFunction classCallback, Napi::ThreadSafeFunction addContractsCallback, Napi::ThreadSafeFunction bytecodeCommitmentCallback, Napi::ThreadSafeFunction debugNameCallback, Napi::ThreadSafeFunction createCheckpointCallback, Napi::ThreadSafeFunction commitCheckpointCallback, Napi::ThreadSafeFunction revertCheckpointCallback)
Constructs a callback-based contracts database.
Napi::ThreadSafeFunction create_checkpoint_callback_
Napi::ThreadSafeFunction bytecode_commitment_callback_
std::optional< bb::avm2::FF > get_bytecode_commitment(const bb::avm2::ContractClassId &class_id) const override
Fetches bytecode commitment for a contract class.
void release()
Releases the thread-safe function handles.
Napi::ThreadSafeFunction contract_class_callback_
Napi::ThreadSafeFunction revert_checkpoint_callback_
void create_checkpoint() override
Creates a new checkpoint.
Napi::ThreadSafeFunction debug_name_callback_
Napi::ThreadSafeFunction add_contracts_callback_
Napi::ThreadSafeFunction commit_checkpoint_callback_
Napi::ThreadSafeFunction contract_instance_callback_
std::string format(Args... args)
const std::vector< MemoryValue > data
std::vector< uint8_t > serialize_to_msgpack(const T &data)
Serializes data to msgpack format.
void attach_promise_handlers(Napi::Promise promise, Napi::Function resolve_handler, Napi::Function reject_handler)
Attaches resolve and reject handlers to a promise.
Napi::Function create_reject_handler(Napi::Env env, std::shared_ptr< CallbackResults > cb_results)
Creates a reject handler for promises.
Napi::Function create_void_resolve_handler(Napi::Env env, std::shared_ptr< CallbackResults > cb_results)
Creates a resolve handler for promises that return void.
std::optional< std::vector< uint8_t > > invoke_single_string_callback(const Napi::ThreadSafeFunction &callback, const std::string &input_str, const std::string &operation_name)
Helper for callbacks that take a single string argument and return Buffer | undefined.
std::optional< std::vector< uint8_t > > invoke_ts_callback_with_promise(const Napi::ThreadSafeFunction &callback, const std::string &operation_name, std::function< void(Napi::Env, Napi::Function, std::shared_ptr< CallbackResults >)> call_js_function, std::chrono::seconds timeout)
Generic callback invoker that handles the full BlockingCall pattern.
std::optional< std::vector< uint8_t > > invoke_double_string_callback(const Napi::ThreadSafeFunction &callback, const std::string &input_str1, const std::string &input_str2, const std::string &operation_name)
Helper for callbacks that take two string arguments and return string | undefined.
void invoke_buffer_void_callback(const Napi::ThreadSafeFunction &callback, std::vector< uint8_t > buffer_data, const std::string &operation_name)
Helper for callbacks that take a buffer and return void.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept