1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
use core::fmt;
use std::any::Any;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::Arc;
use std::{ffi, marker, ops};
use anyhow::bail;
use fedimint_core::api::{DynGlobalApi, InviteCode};
use fedimint_core::config::ClientConfig;
use fedimint_core::core::{
Decoder, DynInput, DynOutput, IntoDynInstance, ModuleInstanceId, ModuleKind, OperationId,
};
use fedimint_core::db::{AutocommitError, Database, DatabaseTransaction, PhantomBound};
use fedimint_core::module::registry::{ModuleDecoderRegistry, ModuleRegistry};
use fedimint_core::module::{CommonModuleInit, ModuleCommon, ModuleInit, TransactionItemAmount};
use fedimint_core::task::{MaybeSend, MaybeSync};
use fedimint_core::util::{BoxFuture, BoxStream};
use fedimint_core::{
apply, async_trait_maybe_send, dyn_newtype_define, maybe_add_send_sync, Amount, OutPoint,
TransactionId,
};
use secp256k1_zkp::PublicKey;
use self::init::ClientModuleInit;
use crate::module::recovery::{DynModuleBackup, ModuleBackup};
use crate::sm::{self, ActiveStateMeta, Context, DynContext, DynState, State};
use crate::transaction::{ClientInput, ClientOutput, TransactionBuilder};
use crate::{oplog, AddStateMachinesResult, ClientStrong, ClientWeak, TransactionUpdates};
pub mod init;
pub mod recovery;
pub type ClientModuleRegistry = ModuleRegistry<DynClientModule>;
/// A final, fully initialized [`crate::Client`]
///
/// Client modules need to be able to access a `Client` they are a part
/// of. To break the circular dependency, the final `Client` is passed
/// after `Client` was built via a shared state.
#[derive(Clone, Default)]
pub struct FinalClient(Arc<std::sync::OnceLock<ClientWeak>>);
impl FinalClient {
/// Get a temporary [`ClientStrong`]
///
/// Care must be taken to not let the user take ownership of this value,
/// and not store it elsewhere permanently either, as it could prevent
/// the cleanup of the Client.
pub(crate) fn get(&self) -> ClientStrong {
self.0
.get()
.expect("client must be already set")
.upgrade()
.expect("client module context must not be use past client shutdown")
}
pub(crate) fn set(&self, client: ClientWeak) {
self.0.set(client).expect("FinalLazyClient already set");
}
}
/// A Client context for a [`ClientModule`] `M`
///
/// Client modules can interact with the whole
/// client through this struct.
pub struct ClientContext<M> {
client: FinalClient,
module_instance_id: ModuleInstanceId,
module_db: Database,
_marker: marker::PhantomData<M>,
}
impl<M> Clone for ClientContext<M> {
fn clone(&self) -> Self {
Self {
client: self.client.clone(),
module_db: self.module_db.clone(),
module_instance_id: self.module_instance_id,
_marker: marker::PhantomData,
}
}
}
/// A reference back to itself that the module cacn get from the
/// [`ClientContext`]
pub struct ClientContextSelfRef<'s, M> {
// we are OK storing `ClientStrong` here, because of the `'s` preventing `Self` from being
// stored permanently somewhere
client: ClientStrong,
module_instance_id: ModuleInstanceId,
_marker: marker::PhantomData<&'s M>,
}
impl<M> ops::Deref for ClientContextSelfRef<'_, M>
where
M: ClientModule,
{
type Target = M;
fn deref(&self) -> &Self::Target {
self.client
.get_module(self.module_instance_id)
.as_any()
.downcast_ref::<M>()
.unwrap_or_else(|| panic!("Module is not of type {}", std::any::type_name::<M>()))
}
}
impl<M> fmt::Debug for ClientContext<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("ClientContext")
}
}
/// A context of a database transaction started with
/// `ClientContext::module_autocommit`
pub struct ClientDbTxContext<'r, 'o, M> {
dbtx: &'r mut DatabaseTransaction<'o>,
client: &'r ClientContext<M>,
}
impl<'r, 'o, M> ClientDbTxContext<'r, 'o, M>
where
M: ClientModule,
{
/// Get a reference to [`DatabaseTransaction`] isolated database transaction
pub fn module_dbtx(&mut self) -> DatabaseTransaction<'_> {
self.dbtx
.to_ref_with_prefix_module_id(self.client.module_instance_id)
}
pub fn client_ctx(&self) -> &ClientContext<M> {
self.client
}
pub async fn add_state_machines(
&mut self,
dyn_states: Vec<DynState>,
) -> AddStateMachinesResult {
self.client
.client
.get()
.add_state_machines(self.dbtx, dyn_states)
.await
}
pub fn decoders(&self) -> ModuleDecoderRegistry {
self.client.client.get().decoders().clone()
}
pub async fn add_operation_log_entry(
&mut self,
operation_id: OperationId,
operation_type: &str,
operation_meta: impl serde::Serialize,
) {
self.client
.client
.get()
.operation_log()
.add_operation_log_entry(self.dbtx, operation_id, operation_type, operation_meta)
.await
}
pub async fn add_state_machines_dbtx(
&mut self,
states: Vec<DynState>,
) -> AddStateMachinesResult {
self.client
.client
.get()
.executor
.add_state_machines_dbtx(self.dbtx, states)
.await
}
}
impl<M> ClientContext<M>
where
M: ClientModule,
{
/// Get a reference back to client module from the [`Self`]
///
/// It's often necessary for a client module to "move self"
/// by-value, especially due to async lifetimes issues.
/// Clients usually work with `&mut self`, which can't really
/// work in such context.
///
/// Fortunately [`ClientContext`] is `Clone` and `Send, and
/// can be used to recover the reference to the module at later
/// time.
#[allow(clippy::needless_lifetimes)] // just for explicitiness
pub fn self_ref<'s>(&'s self) -> ClientContextSelfRef<'s, M> {
ClientContextSelfRef {
client: self.client.get(),
module_instance_id: self.module_instance_id,
_marker: marker::PhantomData,
}
}
/// Get a reference to a global Api handle
pub fn global_api(&self) -> DynGlobalApi {
self.client.get().api_clone()
}
pub fn decoders(&self) -> ModuleDecoderRegistry {
self.client.get().decoders().clone()
}
pub fn input_from_dyn<'i>(
&self,
input: &'i DynInput,
) -> Option<&'i <M::Common as ModuleCommon>::Input> {
(input.module_instance_id() == self.module_instance_id).then(|| {
input
.as_any()
.downcast_ref::<<M::Common as ModuleCommon>::Input>()
.expect("instance_id just checked")
})
}
pub fn output_from_dyn<'o>(
&self,
output: &'o DynOutput,
) -> Option<&'o <M::Common as ModuleCommon>::Output> {
(output.module_instance_id() == self.module_instance_id).then(|| {
output
.as_any()
.downcast_ref::<<M::Common as ModuleCommon>::Output>()
.expect("instance_id just checked")
})
}
pub fn map_dyn<'s, 'i, 'o, I>(
&'s self,
typed: impl IntoIterator<Item = I> + 'i,
) -> impl Iterator<Item = <I as IntoDynInstance>::DynType> + 'o
where
I: IntoDynInstance,
'i: 'o,
's: 'o,
{
typed.into_iter().map(move |i| self.make_dyn(i))
}
/// Turn a typed output into a dyn version
pub fn make_dyn_output(&self, output: <M::Common as ModuleCommon>::Output) -> DynOutput {
self.make_dyn(output)
}
/// Turn a typed input into a dyn version
pub fn make_dyn_input(&self, input: <M::Common as ModuleCommon>::Input) -> DynInput {
self.make_dyn(input)
}
/// Turn a `typed` into a dyn version
pub fn make_dyn<I>(&self, typed: I) -> <I as IntoDynInstance>::DynType
where
I: IntoDynInstance,
{
typed.into_dyn(self.module_instance_id)
}
/// Turn a typed [`ClientOutput`] into a dyn version
pub fn make_client_output<O, S>(&self, output: ClientOutput<O, S>) -> ClientOutput
where
O: IntoDynInstance<DynType = DynOutput> + 'static,
S: IntoDynInstance<DynType = DynState> + 'static,
{
self.make_dyn(output)
}
/// Turn a typed [`ClientInput`] into a dyn version
pub fn make_client_input<O, S>(&self, input: ClientInput<O, S>) -> ClientInput
where
O: IntoDynInstance<DynType = DynInput> + 'static,
S: IntoDynInstance<DynType = DynState> + 'static,
{
self.make_dyn(input)
}
pub fn make_dyn_state<S>(&self, sm: S) -> DynState
where
S: sm::IState + 'static,
{
DynState::from_typed(self.module_instance_id, sm)
}
/// An [`Database::autocommit`] on module's own database partition
pub async fn module_autocommit<'s, 'dbtx, F, T, E>(
&'s self,
tx_fn: F,
max_attempts: Option<usize>,
) -> Result<T, AutocommitError<E>>
where
's: 'dbtx,
for<'r, 'o> F: Fn(
&'r mut ClientDbTxContext<'r, 'o, M>,
PhantomBound<'dbtx, 'o>,
) -> BoxFuture<'r, Result<T, E>>
+ MaybeSync,
{
let tx_fn = &tx_fn;
self.global_db()
.autocommit(
move |dbtx, _| {
Box::pin(async move {
tx_fn(&mut ClientDbTxContext { dbtx, client: self }, PhantomData).await
})
},
max_attempts,
)
.await
}
// TODO: rename `module_autocommit` to something else and make this a default
pub async fn module_autocommit_2<'s, 'dbtx, F, T>(
&'s self,
tx_fn: F,
max_attempts: Option<usize>,
) -> anyhow::Result<T>
where
's: 'dbtx,
for<'r, 'o> F: Fn(
&'r mut ClientDbTxContext<'r, 'o, M>,
PhantomBound<'dbtx, 'o>,
) -> BoxFuture<'r, anyhow::Result<T>>
+ MaybeSync,
{
self.module_autocommit(tx_fn, max_attempts)
.await
.map_err(|e| match e {
AutocommitError::ClosureError { error, .. } => error,
AutocommitError::CommitFailed { last_error, .. } => {
panic!("Commit to DB failed: {last_error}")
}
})
}
/// See [`crate::Client::finalize_and_submit_transaction`]
pub async fn finalize_and_submit_transaction<F, Meta>(
&self,
operation_id: OperationId,
operation_type: &str,
operation_meta: F,
tx_builder: TransactionBuilder,
) -> anyhow::Result<(TransactionId, Vec<OutPoint>)>
where
F: Fn(TransactionId, Vec<OutPoint>) -> Meta + Clone + MaybeSend + MaybeSync,
Meta: serde::Serialize + MaybeSend,
{
self.client
.get()
.finalize_and_submit_transaction(
operation_id,
operation_type,
operation_meta,
tx_builder,
)
.await
}
/// See [`crate::Client::transaction_updates`]
pub async fn transaction_updates(&self, operation_id: OperationId) -> TransactionUpdates {
self.client.get().transaction_updates(operation_id).await
}
/// See [`crate::Client::await_primary_module_outputs`]
pub async fn await_primary_module_outputs(
&self,
operation_id: OperationId,
outputs: Vec<OutPoint>,
) -> anyhow::Result<Amount> {
self.client
.get()
.await_primary_module_outputs(operation_id, outputs)
.await
}
// TODO: unify with `Self::get_operation`
pub async fn get_operation(
&self,
operation_id: OperationId,
) -> anyhow::Result<oplog::OperationLogEntry> {
let operation = self
.client
.get()
.operation_log()
.get_operation(operation_id)
.await
.ok_or(anyhow::anyhow!("Operation not found"))?;
if operation.operation_module_kind() != M::kind().as_str() {
bail!("Operation is not a lightning operation");
}
Ok(operation)
}
pub fn global_db(&self) -> fedimint_core::db::Database {
self.client.get().db().clone()
}
pub fn module_db(&self) -> &Database {
&self.module_db
}
pub async fn has_active_states(&self, op_id: OperationId) -> bool {
self.client.get().has_active_states(op_id).await
}
pub async fn get_own_active_states(&self) -> Vec<(M::States, ActiveStateMeta)> {
self.client
.get()
.executor
.get_active_states()
.await
.into_iter()
.filter(|s| s.0.module_instance_id() == self.module_instance_id)
.map(|s| {
(
s.0.as_any()
.downcast_ref::<M::States>()
.expect("incorrect output type passed to module plugin")
.clone(),
s.1,
)
})
.collect()
}
pub fn get_config(&self) -> ClientConfig {
self.client.get().get_config().clone()
}
/// Returns an invite code for the federation that points to an arbitrary
/// guardian server for fetching the config
pub fn get_invite_code(&self) -> InviteCode {
let cfg = self.get_config().global;
let (any_guardian_id, any_guardian_url) = cfg
.api_endpoints
.iter()
.next()
.expect("A federation always has at least one guardian");
InviteCode::new(
any_guardian_url.url.clone(),
*any_guardian_id,
cfg.calculate_federation_id(),
)
}
pub fn get_internal_payment_markers(&self) -> anyhow::Result<(PublicKey, u64)> {
self.client.get().get_internal_payment_markers()
}
}
/// Fedimint module client
#[apply(async_trait_maybe_send!)]
pub trait ClientModule: Debug + MaybeSend + MaybeSync + 'static {
type Init: ClientModuleInit;
/// Common module types shared between client and server
type Common: ModuleCommon;
/// Data stored in regular backups so that restoring doesn't have to start
/// from epoch 0
type Backup: ModuleBackup;
/// Data and API clients available to state machine transitions of this
/// module
type ModuleStateMachineContext: Context;
/// All possible states this client can submit to the executor
type States: State<ModuleContext = Self::ModuleStateMachineContext>
+ IntoDynInstance<DynType = DynState>;
fn decoder() -> Decoder {
let mut decoder_builder = Self::Common::decoder_builder();
decoder_builder.with_decodable_type::<Self::States>();
decoder_builder.with_decodable_type::<Self::Backup>();
decoder_builder.build()
}
fn kind() -> ModuleKind {
<<<Self as ClientModule>::Init as ModuleInit>::Common as CommonModuleInit>::KIND
}
fn context(&self) -> Self::ModuleStateMachineContext;
async fn handle_cli_command(
&self,
_args: &[ffi::OsString],
) -> anyhow::Result<serde_json::Value> {
Err(anyhow::format_err!(
"This module does not implement cli commands"
))
}
/// Returns the amount represented by the input and the fee its processing
/// requires.
///
/// If the semantics of a given input aren't known this function returns
/// `None`, this only happens if a future version of Fedimint introduces a
/// new input variant. For clients this should only be the case when
/// processing transactions created by other users, so the result of
/// this function can be `unwrap`ped whenever dealing with inputs
/// generated by ourselves.
fn input_amount(
&self,
input: &<Self::Common as ModuleCommon>::Input,
) -> Option<TransactionItemAmount>;
/// Returns the amount represented by the output and the fee its processing
/// requires
///
/// If the semantics of a given output aren't known this function returns
/// `None`, this only happens if a future version of Fedimint introduces a
/// new output variant. For clients this should only be the case when
/// processing transactions created by other users, so the result of
/// this function can be `unwrap`ped whenever dealing with inputs
/// generated by ourselves.
fn output_amount(
&self,
output: &<Self::Common as ModuleCommon>::Output,
) -> Option<TransactionItemAmount>;
fn supports_backup(&self) -> bool {
false
}
async fn backup(&self) -> anyhow::Result<Self::Backup> {
anyhow::bail!("Backup not supported");
}
/// Does this module support being a primary module
///
/// If it does it must implement:
///
/// * [`Self::create_sufficient_input`]
/// * [`Self::create_exact_output`]
/// * [`Self::await_primary_module_output`]
/// * [`Self::get_balance`]
/// * [`Self::subscribe_balance_changes`]
fn supports_being_primary(&self) -> bool {
false
}
/// Creates an input of **at least** a given `min_amount` from the holdings
/// managed by the module.
///
/// If successful it returns:
/// * A set of private keys belonging to the input for signing the
/// transaction
/// * The input of **at least** `min_amount`, the actual amount might be
/// larger, the caller has to handle this case and possibly generate
/// change using `create_change_output`.
/// * A closure that generates states belonging to the input. This closure
/// takes the transaction id of the transaction in which the input was
/// used and the input index as input since these cannot be known at time
/// of calling `create_funding_input` and have to be injected later.
///
/// The function returns an error if the client's funds are not sufficient
/// to create the requested input.
async fn create_sufficient_input(
&self,
_dbtx: &mut DatabaseTransaction<'_>,
_operation_id: OperationId,
_min_amount: Amount,
) -> anyhow::Result<Vec<ClientInput<<Self::Common as ModuleCommon>::Input, Self::States>>> {
unimplemented!()
}
/// Creates an output of **exactly** `amount` that will pay into the
/// holdings managed by the module.
///
/// It returns:
/// * The output of **exactly** `amount`.
/// * A closure that generates states belonging to the output. This closure
/// takes the transaction id of the transaction in which the output was
/// used and the output index as input since these cannot be known at time
/// of calling `create_change_output` and have to be injected later.
async fn create_exact_output(
&self,
_dbtx: &mut DatabaseTransaction<'_>,
_operation_id: OperationId,
_amount: Amount,
) -> Vec<ClientOutput<<Self::Common as ModuleCommon>::Output, Self::States>> {
unimplemented!()
}
/// Waits for the funds from an output created by
/// [`Self::create_exact_output`] to become available. This function
/// returning typically implies a change in the output of
/// [`Self::get_balance`].
async fn await_primary_module_output(
&self,
_operation_id: OperationId,
_out_point: OutPoint,
) -> anyhow::Result<Amount> {
unimplemented!()
}
/// Returns the balance held by this module and available for funding
/// transactions.
async fn get_balance(&self, _dbtx: &mut DatabaseTransaction<'_>) -> Amount {
unimplemented!()
}
/// Returns a stream that will output the updated module balance each time
/// it changes.
async fn subscribe_balance_changes(&self) -> BoxStream<'static, ()> {
unimplemented!()
}
/// Leave the federation
///
/// While technically there's nothing stopping the client from just
/// abandoning Federation at any point by deleting all the related
/// local data, it is useful to make sure it's safe beforehand.
///
/// This call indicates the desire of the caller client code
/// to orderly and safely leave the Federation by this module instance.
/// The goal of the implementations is to fulfil that wish,
/// giving prompt and informative feedback if it's not yet possible.
///
/// The client module implementation should handle the request
/// and return as fast as possible avoiding blocking for longer than
/// necessary. This would usually involve some combination of:
///
/// * recording the state of being in process of leaving the Federation to
/// prevent initiating new conditions that could delay its completion;
/// * performing any fast to complete cleanup/exit logic;
/// * initiating any time-consuming logic (e.g. canceling outstanding
/// contracts), as background jobs, tasks machines, etc.
/// * checking for any conditions indicating it might not be safe to leave
/// at the moment.
///
/// This function should return `Ok` only if from the perspective
/// of this module instance, it is safe to delete client data and
/// stop using it, with no further actions (like background jobs) required
/// to complete.
///
/// This function should return an error if it's not currently possible
/// to safely (e.g. without loosing funds) leave the Federation.
/// It should avoid running indefinitely trying to complete any cleanup
/// actions necessary to reach a clean state, preferring spawning new
/// state machines and returning an informative error about cleanup
/// still in progress.
///
/// If any internal task needs to complete, any user action is required,
/// or even external condition needs to be met this function
/// should return a `Err`.
///
/// Notably modules should not disable interaction that might be necessary
/// for the user (possibly through other modules) to leave the Federation.
/// In particular a Mint module should retain ability to create new notes,
/// and LN module should retain ability to send funds out.
///
/// Calling code must NOT assume that a module that once returned `Ok`,
/// will not return `Err` at later point. E.g. a Mint module might have
/// no outstanding balance at first, but other modules winding down
/// might "cash-out" to Ecash.
///
/// Before leaving the Federation and deleting any state the calling code
/// must collect a full round of `Ok` from all the modules.
///
/// Calling code should allow the user to override and ignore any
/// outstanding errors, after sufficient amount of warnings. Ideally,
/// this should be done on per-module basis, to avoid mistakes.
async fn leave(&self, _dbtx: &mut DatabaseTransaction<'_>) -> anyhow::Result<()> {
bail!("Unable to determine if safe to leave the federation: Not implemented")
}
}
/// Type-erased version of [`ClientModule`]
#[apply(async_trait_maybe_send!)]
pub trait IClientModule: Debug {
fn as_any(&self) -> &(maybe_add_send_sync!(dyn std::any::Any));
fn decoder(&self) -> Decoder;
fn context(&self, instance: ModuleInstanceId) -> DynContext;
async fn handle_cli_command(&self, args: &[ffi::OsString])
-> anyhow::Result<serde_json::Value>;
fn input_amount(&self, input: &DynInput) -> Option<TransactionItemAmount>;
fn output_amount(&self, output: &DynOutput) -> Option<TransactionItemAmount>;
fn supports_backup(&self) -> bool;
async fn backup(&self, module_instance_id: ModuleInstanceId)
-> anyhow::Result<DynModuleBackup>;
fn supports_being_primary(&self) -> bool;
async fn create_sufficient_input(
&self,
module_instance: ModuleInstanceId,
dbtx: &mut DatabaseTransaction<'_>,
operation_id: OperationId,
min_amount: Amount,
) -> anyhow::Result<Vec<ClientInput>>;
async fn create_exact_output(
&self,
module_instance: ModuleInstanceId,
dbtx: &mut DatabaseTransaction<'_>,
operation_id: OperationId,
amount: Amount,
) -> Vec<ClientOutput>;
async fn await_primary_module_output(
&self,
operation_id: OperationId,
out_point: OutPoint,
) -> anyhow::Result<Amount>;
async fn get_balance(
&self,
module_instance: ModuleInstanceId,
dbtx: &mut DatabaseTransaction<'_>,
) -> Amount;
async fn subscribe_balance_changes(&self) -> BoxStream<'static, ()>;
}
#[apply(async_trait_maybe_send!)]
impl<T> IClientModule for T
where
T: ClientModule,
{
fn as_any(&self) -> &(maybe_add_send_sync!(dyn Any)) {
self
}
fn decoder(&self) -> Decoder {
T::decoder()
}
fn context(&self, instance: ModuleInstanceId) -> DynContext {
DynContext::from_typed(instance, <T as ClientModule>::context(self))
}
async fn handle_cli_command(
&self,
args: &[ffi::OsString],
) -> anyhow::Result<serde_json::Value> {
<T as ClientModule>::handle_cli_command(self, args).await
}
fn input_amount(&self, input: &DynInput) -> Option<TransactionItemAmount> {
<T as ClientModule>::input_amount(
self,
input
.as_any()
.downcast_ref()
.expect("Dispatched to correct module"),
)
}
fn output_amount(&self, output: &DynOutput) -> Option<TransactionItemAmount> {
<T as ClientModule>::output_amount(
self,
output
.as_any()
.downcast_ref()
.expect("Dispatched to correct module"),
)
}
fn supports_backup(&self) -> bool {
<T as ClientModule>::supports_backup(self)
}
async fn backup(
&self,
module_instance_id: ModuleInstanceId,
) -> anyhow::Result<DynModuleBackup> {
Ok(DynModuleBackup::from_typed(
module_instance_id,
<T as ClientModule>::backup(self).await?,
))
}
fn supports_being_primary(&self) -> bool {
<T as ClientModule>::supports_being_primary(self)
}
async fn create_sufficient_input(
&self,
module_instance: ModuleInstanceId,
dbtx: &mut DatabaseTransaction<'_>,
operation_id: OperationId,
min_amount: Amount,
) -> anyhow::Result<Vec<ClientInput>> {
Ok(<T as ClientModule>::create_sufficient_input(
self,
&mut dbtx.to_ref_with_prefix_module_id(module_instance),
operation_id,
min_amount,
)
.await?
.into_iter()
.map(|input| input.into_dyn(module_instance))
.collect())
}
async fn create_exact_output(
&self,
module_instance: ModuleInstanceId,
dbtx: &mut DatabaseTransaction<'_>,
operation_id: OperationId,
amount: Amount,
) -> Vec<ClientOutput> {
<T as ClientModule>::create_exact_output(
self,
&mut dbtx.to_ref_with_prefix_module_id(module_instance),
operation_id,
amount,
)
.await
.into_iter()
.map(|output| output.into_dyn(module_instance))
.collect()
}
async fn await_primary_module_output(
&self,
operation_id: OperationId,
out_point: OutPoint,
) -> anyhow::Result<Amount> {
<T as ClientModule>::await_primary_module_output(self, operation_id, out_point).await
}
async fn get_balance(
&self,
module_instance: ModuleInstanceId,
dbtx: &mut DatabaseTransaction<'_>,
) -> Amount {
<T as ClientModule>::get_balance(
self,
&mut dbtx.to_ref_with_prefix_module_id(module_instance),
)
.await
}
async fn subscribe_balance_changes(&self) -> BoxStream<'static, ()> {
<T as ClientModule>::subscribe_balance_changes(self).await
}
}
dyn_newtype_define!(
#[derive(Clone)]
pub DynClientModule(Arc<IClientModule>)
);
impl AsRef<maybe_add_send_sync!(dyn IClientModule + 'static)> for DynClientModule {
fn as_ref(&self) -> &maybe_add_send_sync!(dyn IClientModule + 'static) {
self.inner.as_ref()
}
}
pub type StateGenerator<S> =
Arc<maybe_add_send_sync!(dyn Fn(TransactionId, u64) -> Vec<S> + 'static)>;