pub struct MockChainBuilder { /* private fields */ }testing only.Expand description
A builder for a MockChain’s genesis block.
§Example
let mut builder = MockChain::builder();
let existing_wallet =
builder.add_existing_wallet_with_assets(Auth::IncrNonce, [FungibleAsset::mock(500)])?;
let new_wallet = builder.create_new_wallet(Auth::IncrNonce)?;
let existing_note = builder.add_p2id_note(
existing_wallet.id(),
new_wallet.id(),
&[FungibleAsset::mock(100)],
NoteType::Private,
)?;
let chain = builder.build()?;
// The existing wallet and note should be part of the chain state.
assert!(chain.committed_account(existing_wallet.id()).is_ok());
assert!(chain.committed_notes().get(&existing_note.id()).is_some());
// The new wallet should *not* be part of the chain state - it must be created in
// a transaction first.
assert!(chain.committed_account(new_wallet.id()).is_err());
Note the distinction between add_ and create_ APIs. Any add_ APIs will add something to
the genesis chain state while create_ APIs do not mutate the genesis state. The latter are
simply convenient for creating accounts or notes that will be created by transactions.
See also the MockChain docs for examples on using the mock chain.
Implementations§
Source§impl MockChainBuilder
impl MockChainBuilder
Sourcepub fn new() -> MockChainBuilder
pub fn new() -> MockChainBuilder
Initializes a new mock chain builder with an empty state.
By default, the native_asset_id is set to ACCOUNT_ID_NATIVE_ASSET_FAUCET and can be
overwritten using Self::native_asset_id.
The verification_base_fee is initialized to 0 which means no fees are required by default.
Sourcepub fn with_accounts(
accounts: impl IntoIterator<Item = Account>,
) -> Result<MockChainBuilder, Error>
pub fn with_accounts( accounts: impl IntoIterator<Item = Account>, ) -> Result<MockChainBuilder, Error>
Initializes a new mock chain builder with the provided accounts.
This method only adds the accounts and cannot not register any authenticators for them.
Calling MockChain::build_tx_context on accounts added in this way will not work if the
account needs an authenticator.
Due to these limitations, prefer using other methods to add accounts to the chain, e.g.
MockChainBuilder::add_account_from_builder.
Sourcepub fn native_asset_id(self, native_asset_id: AccountId) -> MockChainBuilder
pub fn native_asset_id(self, native_asset_id: AccountId) -> MockChainBuilder
Sets the native asset ID of the chain.
This must be a fungible faucet AccountId and is the asset in which fees will be accepted
by the transaction kernel.
Sourcepub fn verification_base_fee(
self,
verification_base_fee: u32,
) -> MockChainBuilder
pub fn verification_base_fee( self, verification_base_fee: u32, ) -> MockChainBuilder
Sets the verification_base_fee of the chain.
See FeeParameters for more details.
Sourcepub fn build(self) -> Result<MockChain, Error>
pub fn build(self) -> Result<MockChain, Error>
Consumes the builder, creates the genesis block of the chain and returns the MockChain.
Sourcepub fn create_new_wallet(&mut self, auth_method: Auth) -> Result<Account, Error>
pub fn create_new_wallet(&mut self, auth_method: Auth) -> Result<Account, Error>
Creates a new public BasicWallet account and registers the authenticator (if any) for
it.
This does not add the account to the chain state, but it can still be used to call
MockChain::build_tx_context to automatically add the authenticator.
Sourcepub fn add_existing_wallet(
&mut self,
auth_method: Auth,
) -> Result<Account, Error>
pub fn add_existing_wallet( &mut self, auth_method: Auth, ) -> Result<Account, Error>
Adds an existing public BasicWallet account to the initial chain state and registers the
authenticator (if any).
Sourcepub fn add_existing_wallet_with_assets(
&mut self,
auth_method: Auth,
assets: impl IntoIterator<Item = Asset>,
) -> Result<Account, Error>
pub fn add_existing_wallet_with_assets( &mut self, auth_method: Auth, assets: impl IntoIterator<Item = Asset>, ) -> Result<Account, Error>
Adds an existing public BasicWallet account to the initial chain state and registers the
authenticator (if any).
Sourcepub fn create_new_faucet(
&mut self,
auth_method: Auth,
token_symbol: &str,
max_supply: u64,
) -> Result<Account, Error>
pub fn create_new_faucet( &mut self, auth_method: Auth, token_symbol: &str, max_supply: u64, ) -> Result<Account, Error>
Creates a new public BasicFungibleFaucet account and registers the authenticator (if
any) for it.
This does not add the account to the chain state, but it can still be used to call
MockChain::build_tx_context to automatically add the authenticator.
Sourcepub fn add_existing_basic_faucet(
&mut self,
auth_method: Auth,
token_symbol: &str,
max_supply: u64,
total_issuance: Option<u64>,
) -> Result<Account, Error>
pub fn add_existing_basic_faucet( &mut self, auth_method: Auth, token_symbol: &str, max_supply: u64, total_issuance: Option<u64>, ) -> Result<Account, Error>
Adds an existing BasicFungibleFaucet account to the initial chain state and
registers the authenticator.
Basic fungible faucets always use AccountStorageMode::Public and require authentication.
Sourcepub fn add_existing_network_faucet(
&mut self,
token_symbol: &str,
max_supply: u64,
owner_account_id: AccountId,
total_issuance: Option<u64>,
) -> Result<Account, Error>
pub fn add_existing_network_faucet( &mut self, token_symbol: &str, max_supply: u64, owner_account_id: AccountId, total_issuance: Option<u64>, ) -> Result<Account, Error>
Adds an existing NetworkFungibleFaucet account to the initial chain state.
Network fungible faucets always use AccountStorageMode::Network and Auth::NoAuth.
Sourcepub fn create_new_mock_account(
&mut self,
auth_method: Auth,
) -> Result<Account, Error>
pub fn create_new_mock_account( &mut self, auth_method: Auth, ) -> Result<Account, Error>
Creates a new public account with an MockAccountComponent and registers the
authenticator (if any).
Sourcepub fn add_existing_mock_account(
&mut self,
auth_method: Auth,
) -> Result<Account, Error>
pub fn add_existing_mock_account( &mut self, auth_method: Auth, ) -> Result<Account, Error>
Adds an existing public account with an MockAccountComponent to the initial chain state
and registers the authenticator (if any).
Sourcepub fn add_existing_mock_account_with_storage(
&mut self,
auth_method: Auth,
slots: impl IntoIterator<Item = StorageSlot>,
) -> Result<Account, Error>
pub fn add_existing_mock_account_with_storage( &mut self, auth_method: Auth, slots: impl IntoIterator<Item = StorageSlot>, ) -> Result<Account, Error>
Adds an existing public account with an MockAccountComponent to the initial chain state
and registers the authenticator (if any).
Sourcepub fn add_existing_mock_account_with_assets(
&mut self,
auth_method: Auth,
assets: impl IntoIterator<Item = Asset>,
) -> Result<Account, Error>
pub fn add_existing_mock_account_with_assets( &mut self, auth_method: Auth, assets: impl IntoIterator<Item = Asset>, ) -> Result<Account, Error>
Adds an existing public account with an MockAccountComponent to the initial chain state
and registers the authenticator (if any).
Sourcepub fn add_existing_mock_account_with_storage_and_assets(
&mut self,
auth_method: Auth,
slots: impl IntoIterator<Item = StorageSlot>,
assets: impl IntoIterator<Item = Asset>,
) -> Result<Account, Error>
pub fn add_existing_mock_account_with_storage_and_assets( &mut self, auth_method: Auth, slots: impl IntoIterator<Item = StorageSlot>, assets: impl IntoIterator<Item = Asset>, ) -> Result<Account, Error>
Adds an existing public account with an MockAccountComponent to the initial chain state
and registers the authenticator (if any).
Sourcepub fn add_account_from_builder(
&mut self,
auth_method: Auth,
account_builder: AccountBuilder,
account_state: AccountState,
) -> Result<Account, Error>
pub fn add_account_from_builder( &mut self, auth_method: Auth, account_builder: AccountBuilder, account_state: AccountState, ) -> Result<Account, Error>
Builds the provided AccountBuilder with the provided auth method and registers the
authenticator (if any).
- If
AccountState::Existsis given the account is built as an existing account and added to the initial chain state. It can then be used in a transaction without having to validate its seed. - If
AccountState::Newis given the account is built as a new account and is not added to the chain. Its authenticator is registered (if present). Its first transaction will be its creation transaction.MockChain::build_tx_contextcan be called with the account to automatically add the authenticator.
Sourcepub fn add_account(&mut self, account: Account) -> Result<(), Error>
pub fn add_account(&mut self, account: Account) -> Result<(), Error>
Adds the provided account to the list of genesis accounts.
This method only adds the account and does not store its account authenticator for it.
Calling MockChain::build_tx_context on accounts added in this way will not work if
the account needs an authenticator.
Due to these limitations, prefer using other methods to add accounts to the chain, e.g.
MockChainBuilder::add_account_from_builder.
Sourcepub fn add_output_note(&mut self, note: impl Into<OutputNote>)
pub fn add_output_note(&mut self, note: impl Into<OutputNote>)
Adds the provided note to the initial chain state.
Sourcepub fn add_p2any_note(
&mut self,
sender_account_id: AccountId,
note_type: NoteType,
assets: impl IntoIterator<Item = Asset>,
) -> Result<Note, Error>
pub fn add_p2any_note( &mut self, sender_account_id: AccountId, note_type: NoteType, assets: impl IntoIterator<Item = Asset>, ) -> Result<Note, Error>
Creates a new P2ANY note from the provided parameters and adds it to the list of genesis notes.
This note is similar to a P2ID note but can be consumed by any account.
Sourcepub fn add_p2id_note(
&mut self,
sender_account_id: AccountId,
target_account_id: AccountId,
asset: &[Asset],
note_type: NoteType,
) -> Result<Note, NoteError>
pub fn add_p2id_note( &mut self, sender_account_id: AccountId, target_account_id: AccountId, asset: &[Asset], note_type: NoteType, ) -> Result<Note, NoteError>
Creates a new P2ID note from the provided parameters and adds it to the list of genesis notes.
In the created MockChain, the note will be immediately spendable by target_account_id
and carries no additional reclaim or timelock conditions.
Sourcepub fn add_p2ide_note(
&mut self,
sender_account_id: AccountId,
target_account_id: AccountId,
asset: &[Asset],
note_type: NoteType,
reclaim_height: Option<BlockNumber>,
timelock_height: Option<BlockNumber>,
) -> Result<Note, NoteError>
pub fn add_p2ide_note( &mut self, sender_account_id: AccountId, target_account_id: AccountId, asset: &[Asset], note_type: NoteType, reclaim_height: Option<BlockNumber>, timelock_height: Option<BlockNumber>, ) -> Result<Note, NoteError>
Adds a P2IDE OutputNote (pay‑to‑ID‑extended) to the list of genesis notes.
A P2IDE note can include an optional timelock_height and/or an optional
reclaim_height after which the sender_account_id may reclaim the
funds.
Sourcepub fn add_swap_note(
&mut self,
sender: AccountId,
offered_asset: Asset,
requested_asset: Asset,
payback_note_type: NoteType,
) -> Result<(Note, NoteDetails), Error>
pub fn add_swap_note( &mut self, sender: AccountId, offered_asset: Asset, requested_asset: Asset, payback_note_type: NoteType, ) -> Result<(Note, NoteDetails), Error>
Adds a public SWAP OutputNote to the list of genesis notes.
Sourcepub fn add_spawn_note<'note, I>(
&mut self,
output_notes: impl IntoIterator<Item = &'note Note, IntoIter = I>,
) -> Result<Note, Error>where
I: ExactSizeIterator<Item = &'note Note>,
pub fn add_spawn_note<'note, I>(
&mut self,
output_notes: impl IntoIterator<Item = &'note Note, IntoIter = I>,
) -> Result<Note, Error>where
I: ExactSizeIterator<Item = &'note Note>,
Adds a public SPAWN note to the list of genesis notes.
A SPAWN note contains a note script that creates all output_notes that get passed as a
parameter.
§Errors
Returns an error if:
- the sender account ID of the provided output notes is not consistent or does not match the transaction’s sender.
Sourcepub fn add_p2id_note_with_fee(
&mut self,
target_account_id: AccountId,
amount: u64,
) -> Result<Note, Error>
pub fn add_p2id_note_with_fee( &mut self, target_account_id: AccountId, amount: u64, ) -> Result<Note, Error>
Creates a new P2ID note with the provided amount of the native fee asset of the chain.
The native asset ID of the asset can be set using Self::native_asset_id. By default it
is ACCOUNT_ID_NATIVE_ASSET_FAUCET.
In the created MockChain, the note will be immediately spendable by target_account_id.
Sourcepub fn rng_mut(&mut self) -> &mut RpoRandomCoin
pub fn rng_mut(&mut self) -> &mut RpoRandomCoin
Returns a mutable reference to the builder’s RNG.
This can be used when creating accounts or notes and randomness is required.
Trait Implementations§
Source§impl Clone for MockChainBuilder
impl Clone for MockChainBuilder
Source§fn clone(&self) -> MockChainBuilder
fn clone(&self) -> MockChainBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MockChainBuilder
impl Debug for MockChainBuilder
Source§impl Default for MockChainBuilder
impl Default for MockChainBuilder
Source§fn default() -> MockChainBuilder
fn default() -> MockChainBuilder
Auto Trait Implementations§
impl Freeze for MockChainBuilder
impl RefUnwindSafe for MockChainBuilder
impl Send for MockChainBuilder
impl Sync for MockChainBuilder
impl Unpin for MockChainBuilder
impl UnwindSafe for MockChainBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more