Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up wallet syncing code #2108

Merged
merged 46 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1badfa3
const fn
Alex6323 Mar 1, 2024
bb673e2
sync options
Alex6323 Mar 1, 2024
9b77ea1
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 13, 2024
8134473
move instead of clone
Alex6323 Mar 14, 2024
264b2d9
combinators
Alex6323 Mar 14, 2024
010e276
rm unnecessary cloning 1
Alex6323 Mar 14, 2024
abe5f40
rm unnecessary cloning 2
Alex6323 Mar 14, 2024
c5e38fb
slices; impl Into<Option>
Alex6323 Mar 14, 2024
55b5a2f
refactor
Alex6323 Mar 14, 2024
8591c86
get rid of output_data
Alex6323 Mar 14, 2024
7a158a6
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 14, 2024
38afbb8
nits
Alex6323 Mar 14, 2024
1cb8c67
clippy
Alex6323 Mar 14, 2024
52e0afb
nit
Alex6323 Mar 14, 2024
99561d7
nest
Alex6323 Mar 15, 2024
afe5ada
nit
Alex6323 Mar 15, 2024
e7e592f
update python binding
Alex6323 Mar 15, 2024
0cc5e29
update nodejs binding
Alex6323 Mar 15, 2024
2f1c6e9
update core binding
Alex6323 Mar 15, 2024
dea3c64
rm output_data.py
Alex6323 Mar 15, 2024
863c7d1
nit
Alex6323 Mar 15, 2024
d035c55
suggestion
Alex6323 Mar 15, 2024
be7dcbb
rename local var
Alex6323 Mar 15, 2024
8dcb842
fix tests and clippy
Alex6323 Mar 15, 2024
62a9541
ci-doc
Alex6323 Mar 15, 2024
119fad0
rm HashSet piping of foundry output ids
Alex6323 Mar 15, 2024
ca13a6c
rm TODO
Alex6323 Mar 15, 2024
fec6cfd
fix nodejs how-to
Alex6323 Mar 15, 2024
b923a00
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 19, 2024
d12be48
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 22, 2024
0159dc9
remove todo
Alex6323 Mar 22, 2024
94de389
Merge branch '2.0' into cleanup-wallet-syncing
Mar 22, 2024
48b6661
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 27, 2024
e91b2af
undo rename
Alex6323 Apr 15, 2024
ea08631
Merge branch 'upstream/2.0' into cleanup-wallet-syncing
Alex6323 Apr 15, 2024
c3f0ae6
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Apr 17, 2024
94419c4
Merge branch 'upstream/2.0' into cleanup-wallet-syncing
Alex6323 Apr 26, 2024
d7f4443
rust: cleanup
Alex6323 Apr 29, 2024
9e0e4e3
python: cleanup
Alex6323 Apr 29, 2024
f69965a
nodejs: cleanup
Alex6323 Apr 29, 2024
346ab5f
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Apr 29, 2024
38b7bfc
rust: more cleanup
Alex6323 Apr 29, 2024
5de120c
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Apr 30, 2024
a9100c3
...
Alex6323 Apr 30, 2024
b745c52
....
Alex6323 Apr 30, 2024
6412d7c
.....
Alex6323 Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::OmittedDebug;
#[non_exhaustive]
pub enum WalletMethod {
/// Returns the accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithData`](crate::Response::OutputsWithData)
Accounts,
/// Backup storage. Password must be the current one, when Stronghold is used as SecretManager.
/// Expected response: [`Ok`](crate::Response::Ok)
Expand Down Expand Up @@ -139,8 +139,8 @@ pub enum WalletMethod {
/// Expected response: [`Transaction`](crate::Response::Transaction)
#[serde(rename_all = "camelCase")]
GetIncomingTransaction { transaction_id: TransactionId },
/// Get the [`OutputData`](iota_sdk::wallet::types::OutputData) of an output stored in the wallet.
/// Expected response: [`OutputData`](crate::Response::OutputData)
/// Get the [`OutputData`](iota_sdk::wallet::types::OutputData) of an output stored
/// in the wallet. Expected response: [`OutputData`](crate::Response::OutputData)
#[serde(rename_all = "camelCase")]
GetOutput { output_id: OutputId },
// /// Expected response: [`ParticipationEvent`](crate::Response::ParticipationEvent)
Expand Down Expand Up @@ -199,13 +199,13 @@ pub enum WalletMethod {
bip_path: Option<Bip44>,
},
/// Returns the implicit accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithData`](crate::Response::OutputsWithData)
ImplicitAccounts,
/// Returns all incoming transactions of the wallet.
/// Expected response: [`Transactions`](crate::Response::Transactions)
IncomingTransactions,
/// Returns all outputs of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithData`](crate::Response::OutputsWithData)
#[serde(rename_all = "camelCase")]
Outputs { filter_options: Option<FilterOptions> },
/// Returns all pending transactions of the wallet.
Expand Down Expand Up @@ -452,7 +452,7 @@ pub enum WalletMethod {
/// Expected response: [`Transactions`](crate::Response::Transactions)
Transactions,
/// Returns all unspent outputs of the wallet
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithData`](crate::Response::OutputsWithData)
#[serde(rename_all = "camelCase")]
UnspentOutputs { filter_options: Option<FilterOptions> },
/// Emits an event for testing if the event system is working
Expand Down
8 changes: 4 additions & 4 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) async fn call_wallet_method_internal(
method: WalletMethod,
) -> Result<Response, crate::Error> {
let response = match method {
WalletMethod::Accounts => Response::OutputsData(wallet.ledger().await.accounts().cloned().collect()),
WalletMethod::Accounts => Response::OutputsWithData(wallet.ledger().await.accounts().cloned().collect()),
#[cfg(feature = "stronghold")]
WalletMethod::BackupToStrongholdSnapshot { destination, password } => {
wallet.backup_to_stronghold_snapshot(destination, password).await?;
Expand Down Expand Up @@ -203,7 +203,7 @@ pub(crate) async fn call_wallet_method_internal(
Response::PreparedTransaction(data)
}
WalletMethod::ImplicitAccounts => {
Response::OutputsData(wallet.ledger().await.implicit_accounts().cloned().collect())
Response::OutputsWithData(wallet.ledger().await.implicit_accounts().cloned().collect())
}
WalletMethod::IncomingTransactions => Response::Transactions(
wallet
Expand All @@ -216,7 +216,7 @@ pub(crate) async fn call_wallet_method_internal(
),
WalletMethod::Outputs { filter_options } => {
let wallet_ledger = wallet.ledger().await;
Response::OutputsData(if let Some(filter) = filter_options {
Response::OutputsWithData(if let Some(filter) = filter_options {
wallet_ledger.filtered_outputs(filter).cloned().collect()
} else {
wallet_ledger.outputs().values().cloned().collect()
Expand Down Expand Up @@ -420,7 +420,7 @@ pub(crate) async fn call_wallet_method_internal(
),
WalletMethod::UnspentOutputs { filter_options } => {
let wallet_ledger = wallet.ledger().await;
Response::OutputsData(if let Some(filter) = filter_options {
Response::OutputsWithData(if let Some(filter) = filter_options {
wallet_ledger.filtered_unspent_outputs(filter).cloned().collect()
} else {
wallet_ledger.unspent_outputs().values().cloned().collect()
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub enum Response {
/// Response for:
/// - [`Outputs`](crate::method::WalletMethod::Outputs),
/// - [`UnspentOutputs`](crate::method::WalletMethod::UnspentOutputs)
OutputsData(Vec<OutputData>),
OutputsWithData(Vec<OutputData>),
/// Response for:
/// - [`PrepareBurn`](crate::method::WalletMethod::PrepareBurn),
/// - [`PrepareClaimOutputs`](crate::method::WalletMethod::PrepareClaimOutputs)
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn create_wallet() -> Result<(), Error> {
.await;

match response {
Response::OutputsData(_) => {}
Response::OutputsWithData(_) => {}
_ => panic!("unexpected response {response:?}"),
}

Expand Down
12 changes: 5 additions & 7 deletions bindings/nodejs/examples/how_tos/wallet/consolidate-outputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { CommonOutput, Utils, Wallet, initLogger } from '@iota/sdk';
import { CommonOutput, Wallet, initLogger } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });
Expand Down Expand Up @@ -46,11 +46,10 @@ async function run() {
const outputs = await wallet.unspentOutputs();
console.log('Outputs BEFORE consolidation:');

outputs.forEach(({ output, address }, i) => {
outputs.forEach(({ output }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- address: %s\n- amount: %d\n- native token: %s',
Utils.addressToBech32(address, 'rms'),
'- amount: %d\n- native token: %s',
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeToken() ?? []
Expand Down Expand Up @@ -80,11 +79,10 @@ async function run() {

// Outputs after consolidation
console.log('Outputs AFTER consolidation:');
outputs.forEach(({ output, address }, i) => {
outputs.forEach(({ output }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- address: %s\n- amount: %d\n- native tokens: %s',
Utils.addressToBech32(address, 'rms'),
'- amount: %d\n- native tokens: %s',
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeToken()
Expand Down
28 changes: 11 additions & 17 deletions bindings/nodejs/lib/types/wallet/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import { Type } from 'class-transformer';
import { Address, AddressDiscriminator } from '../block/address';
import { Output, OutputDiscriminator, OutputId } from '../block/output';
import { OutputMetadataResponse } from '../models/api';
import { OutputIdProof, OutputMetadataResponse } from '../models/api';

/** Output to claim */
export enum OutputsToClaim {
Expand All @@ -15,28 +14,23 @@ export enum OutputsToClaim {
All = 'All',
}

/** An output with metadata */
/** An output with extended metadata */
export class OutputData {
/** The identifier of an Output */
outputId!: OutputId;
/** The metadata of the output */
metadata!: OutputMetadataResponse;
/** The actual Output */
/** The output itself */
@Type(() => Output, {
discriminator: OutputDiscriminator,
})
output!: Output;
/** Associated account address */
@Type(() => Address, {
discriminator: AddressDiscriminator,
})
address!: Address;
/** Network ID */
/** The metadata of the output */
metadata!: OutputMetadataResponse;
/** The output ID proof */
OutputIdProof!: OutputIdProof;
/** The corresponding output ID */
outputId!: OutputId;
/** The network ID the output belongs to */
networkId!: string;
/** Remainder */
/** Whether the output represents a remainder amount */
remainder!: boolean;
/** BIP32 path */
chain?: Segment[];
}

/** A Segment of the BIP32 path*/
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/iota_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
from .types.native_token import *
from .types.node_info import *
from .types.output import *
from .types.output_data import *
from .types.output_id import *
from .types.output_id_proof import *
from .types.output_metadata import *
from .types.output_params import *
from .types.output_with_extended_metadata import *
from .types.payload import *
from .types.send_params import *
from .types.slot import *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
@json
@dataclass
class OutputData:
"""Output data.
"""Output with extended metadata.

Attributes:
output_id: With the output data corresponding output ID.
metadata: With the output corresponding metadata.
output: The output object itself.
output: The output itself.
metadata: The metadata of the output.
output_id_proof: The output ID proof.
output_id: The corresponding output ID.
network_id: The network ID the output belongs to.
remainder: Whether the output represents a remainder amount.
"""
output_id: OutputId
metadata: OutputMetadata
output: Output
metadata: OutputMetadata
output_id_proof: OutputIdProof
output_id: OutputId
Alex6323 marked this conversation as resolved.
Show resolved Hide resolved
network_id: str
remainder: bool
4 changes: 2 additions & 2 deletions bindings/python/iota_sdk/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from iota_sdk.types.client_options import ClientOptions
from iota_sdk.types.filter_options import FilterOptions
from iota_sdk.types.native_token import NativeToken
from iota_sdk.types.output_data import OutputData
from iota_sdk.types.output_id import OutputId
from iota_sdk.types.output import BasicOutput, NftOutput, Output, deserialize_output
from iota_sdk.types.output_id import OutputId
from iota_sdk.types.output_params import OutputParams
from iota_sdk.types.output_with_extended_metadata import OutputData
from iota_sdk.types.transaction_data import PreparedTransactionData, SignedTransactionData
from iota_sdk.types.transaction_id import TransactionId
from iota_sdk.types.send_params import BeginStakingParams, CreateAccountOutputParams, CreateDelegationParams, CreateNativeTokenParams, MintNftParams, SendManaParams, SendNativeTokenParams, SendNftParams, SendParams
Expand Down
34 changes: 19 additions & 15 deletions cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,38 +1389,38 @@ async fn print_wallet_address(wallet: &Wallet) -> Result<(), Error> {
let mut delegations = Vec::new();
let mut anchors = Vec::new();

for output_data in wallet.ledger().await.unspent_outputs().values() {
let output_id = output_data.output_id;
for output_with_ext_metadata in wallet.ledger().await.unspent_outputs().values() {
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
let output_id = output_with_ext_metadata.output_id;
output_ids.push(output_id);

// Output might be associated with the address, but can't be unlocked by it, so we check that here.
let required_address = &output_data
let required_address = &output_with_ext_metadata
.output
.required_address(slot_index, protocol_parameters.committable_age_range())?;

if required_address
.as_ref()
.is_some_and(|required_address| required_address == address.inner())
{
if let Some(nt) = output_data.output.native_token() {
if let Some(nt) = output_with_ext_metadata.output.native_token() {
native_tokens.add_native_token(*nt)?;
}
match &output_data.output {
match &output_with_ext_metadata.output {
Output::Basic(_) => {}
Output::Account(account) => accounts.push(account.account_id_non_null(&output_id)),
Output::Foundry(foundry) => foundries.push(foundry.id()),
Output::Nft(nft) => nfts.push(nft.nft_id_non_null(&output_id)),
Output::Delegation(delegation) => delegations.push(delegation.delegation_id_non_null(&output_id)),
Output::Anchor(anchor) => anchors.push(anchor.anchor_id_non_null(&output_id)),
}
let sdr_amount = output_data
let sdr_amount = output_with_ext_metadata
.output
.unlock_conditions()
.storage_deposit_return()
.map(|sdr| sdr.amount())
.unwrap_or(0);

amount += output_data.output.amount() - sdr_amount;
amount += output_with_ext_metadata.output.amount() - sdr_amount;
}
}

Expand Down Expand Up @@ -1765,26 +1765,30 @@ pub async fn prompt_internal(
Ok(PromptResponse::Reprompt)
}

fn print_outputs(mut outputs: Vec<OutputData>, title: &str) -> Result<(), Error> {
if outputs.is_empty() {
fn print_outputs(mut outputs_with_ext_metadata: Vec<OutputData>, title: &str) -> Result<(), Error> {
if outputs_with_ext_metadata.is_empty() {
println_log_info!("No outputs found");
} else {
println_log_info!("{title}");
outputs.sort_unstable_by_key(|o| o.output_id);
outputs_with_ext_metadata.sort_unstable_by_key(|o| o.output_id);

for (i, output_data) in outputs.into_iter().enumerate() {
let kind_str = if output_data.output.is_implicit_account() {
for (i, output_with_ext_metadata) in outputs_with_ext_metadata.into_iter().enumerate() {
let kind_str = if output_with_ext_metadata.output.is_implicit_account() {
"ImplicitAccount"
} else {
output_data.output.kind_str()
output_with_ext_metadata.output.kind_str()
};

println_log_info!(
"{:<5}{} {:<16}{}",
i,
&output_data.output_id,
&output_with_ext_metadata.output_id,
kind_str,
if output_data.is_spent() { "Spent" } else { "Unspent" },
if output_with_ext_metadata.is_spent() {
"Spent"
} else {
"Unspent"
},
);
}
}
Expand Down
12 changes: 6 additions & 6 deletions sdk/examples/how_tos/wallet/consolidate_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.unspent_outputs()
.values()
.enumerate()
.for_each(|(i, output_data)| {
.for_each(|(i, output_with_ext_metadata)| {
println!("OUTPUT #{i}");
println!(
"- amount: {:?}\n- native tokens: {:?}",
output_data.output.amount(),
output_data.output.native_token()
output_with_ext_metadata.output.amount(),
output_with_ext_metadata.output.native_token()
)
});

Expand Down Expand Up @@ -91,12 +91,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.unspent_outputs()
.values()
.enumerate()
.for_each(|(i, output_data)| {
.for_each(|(i, output_with_ext_metadata)| {
println!("OUTPUT #{i}");
println!(
"- amount: {:?}\n- native tokens: {:?}",
output_data.output.amount(),
output_data.output.native_token()
output_with_ext_metadata.output.amount(),
output_with_ext_metadata.output.native_token()
)
});

Expand Down
10 changes: 5 additions & 5 deletions sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,14 @@ impl WalletLedger {
pub fn implicit_accounts(&self) -> impl Iterator<Item = &OutputData> {
self.unspent_outputs
.values()
.filter(|output_data| output_data.output.is_implicit_account())
.filter(|output_with_ext_metadata| output_with_ext_metadata.output.is_implicit_account())
}

/// Returns accounts of the wallet.
pub fn accounts(&self) -> impl Iterator<Item = &OutputData> {
self.unspent_outputs
.values()
.filter(|output_data| output_data.output.is_account())
.filter(|output_with_ext_metadata| output_with_ext_metadata.output.is_account())
}

// Returns the first possible unexpired block issuer Account id, which can be an implicit account.
Expand Down Expand Up @@ -382,10 +382,10 @@ impl<S: 'static + SecretManage> Wallet<S> {
pub async fn get_foundry_output(&self, native_token_id: TokenId) -> Result<Output, WalletError> {
let foundry_id = FoundryId::from(native_token_id);

for output_data in self.ledger.read().await.outputs.values() {
if let Output::Foundry(foundry_output) = &output_data.output {
for output_with_ext_metadata in self.ledger.read().await.outputs.values() {
if let Output::Foundry(foundry_output) = &output_with_ext_metadata.output {
if foundry_output.id() == foundry_id {
return Ok(output_data.output.clone());
return Ok(output_with_ext_metadata.output.clone());
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/wallet/core/operations/background_syncing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ where
/// Start the background syncing process for the wallet, default interval is 7 seconds
pub async fn start_background_syncing(
&self,
options: Option<SyncOptions>,
options: impl Into<Option<SyncOptions>> + Send,
interval: Option<Duration>,
) -> Result<(), WalletError> {
log::debug!("[start_background_syncing]");

let options = options.into();
let (tx_background_sync, mut rx_background_sync) = self.background_syncing_status.clone();

// stop existing process if running
Expand Down
Loading
Loading