Skip to content

Commit

Permalink
Fix some TODOs (#2174)
Browse files Browse the repository at this point in the history
* some TODOs

* review

* fix
  • Loading branch information
DaughterOfMars authored Mar 13, 2024
1 parent 9c4c593 commit 8a3ec9e
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 42 deletions.
3 changes: 0 additions & 3 deletions bindings/core/src/method/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub enum ClientMethod {
// If not provided, minimum amount will be used
#[serde(default, with = "option_string")]
amount: Option<u64>,
// TODO: Determine if `default` is wanted here
#[serde(default, with = "string")]
mana: u64,
account_id: AccountId,
Expand All @@ -58,7 +57,6 @@ pub enum ClientMethod {
// If not provided, minimum amount will be used
#[serde(default, with = "option_string")]
amount: Option<u64>,
// TODO: Determine if `default` is wanted here
#[serde(default, with = "string")]
mana: u64,
unlock_conditions: Vec<UnlockCondition>,
Expand Down Expand Up @@ -86,7 +84,6 @@ pub enum ClientMethod {
// If not provided, minimum amount will be used
#[serde(default, with = "option_string")]
amount: Option<u64>,
// TODO: Determine if `default` is wanted here
#[serde(default, with = "string")]
mana: u64,
nft_id: NftId,
Expand Down
39 changes: 32 additions & 7 deletions sdk/examples/client/block/02_block_custom_parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ use iota_sdk::{
secret::{SecretManager, SignBlock},
Client,
},
types::block::output::AccountId,
types::block::{
core::{basic::MaxBurnedManaAmount, BasicBlockBodyBuilder, BlockHeader},
output::AccountId,
UnsignedBlock,
},
};

#[tokio::main]
Expand All @@ -39,16 +43,37 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let issuance = client.get_issuance().await?;
println!("Issuance:\n{issuance:#?}");

let protocol_params = client.get_protocol_parameters().await?;

// Create and send the block with custom parents.
// TODO build block with custom parents, but without `build_basic_block()`
let block = client
.build_basic_block(issuer_id, None)
.await?
.sign_ed25519(&secret_manager, Bip44::new(IOTA_COIN_TYPE))
.await?;
let block = UnsignedBlock::new(
BlockHeader::new(
protocol_params.version(),
protocol_params.network_id(),
time::OffsetDateTime::now_utc().unix_timestamp_nanos() as _,
issuance.latest_commitment.id(),
issuance.latest_finalized_slot,
issuer_id,
),
BasicBlockBodyBuilder::new(
issuance.strong_parents()?,
MaxBurnedManaAmount::MinimumAmount {
params: protocol_params.work_score_parameters(),
reference_mana_cost: client
.get_account_congestion(&issuer_id, None)
.await?
.reference_mana_cost,
},
)
.finish_block_body()?,
)
.sign_ed25519(&secret_manager, Bip44::new(IOTA_COIN_TYPE))
.await?;

println!("{block:#?}");

client.post_block(&block).await?;

println!(
"Block with custom parents sent: {}/block/{}",
std::env::var("EXPLORER_URL").unwrap(),
Expand Down
9 changes: 2 additions & 7 deletions sdk/src/client/api/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ impl Client {
let issuance = self.get_issuance().await?;

let issuing_time = {
#[cfg(feature = "std")]
let issuing_time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
let issuing_time = instant::SystemTime::now()
.duration_since(instant::SystemTime::UNIX_EPOCH)
.expect("Time went backwards")
.as_nanos() as u64;
// TODO no_std way to have a nanosecond timestamp
// https://github.com/iotaledger/iota-sdk/issues/647
#[cfg(not(feature = "std"))]
let issuing_time = 0;

// Check that the issuing_time is in the range of +-5 minutes of the node to prevent potential issues
if !(issuance.latest_parent_block_issuing_time - FIVE_MINUTES_IN_NANOSECONDS
Expand Down
1 change: 0 additions & 1 deletion sdk/src/client/api/block_builder/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
},
};

// TODO this is wrong because of https://github.com/iotaledger/iota-sdk/issues/1208
const MAX_TX_LENGTH_FOR_BLOCK_WITH_8_PARENTS: usize = Block::LENGTH_MAX - Block::LENGTH_MIN - (7 * BlockId::LENGTH);
// Length for unlocks with a single signature unlock (unlocks length + unlock type + signature type + public key +
// signature)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ pub enum TransactionBuilderError {
UnfulfillableRequirement(Requirement),
/// Unsupported address type.
#[error("unsupported address type {0}")]
// TODO replace with string when 2.0 has Address::kind_str
UnsupportedAddressType(u8),
UnsupportedAddressType(String),
/// Block error.
#[error("{0}")]
Block(#[from] BlockError),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ impl TransactionBuilder {

self.fulfill_sender_requirement(restricted_address.address())
}
_ => Err(TransactionBuilderError::UnsupportedAddressType(address.kind())),
_ => Err(TransactionBuilderError::UnsupportedAddressType(
address.kind_str().to_owned(),
)),
}
}
}
15 changes: 6 additions & 9 deletions sdk/tests/client/transaction_builder/foundry_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ fn minted_native_tokens_in_new_remainder() {
// Account next state + foundry + basic output with native tokens
assert_eq!(selected.transaction.outputs().len(), 3);
selected.transaction.outputs().iter().for_each(|output| {
if let Output::Basic(_basic_output) = &output {
if let Output::Basic(basic_output) = &output {
// Basic output remainder has the minted native tokens
// TODO reenable when ISA supports NTs again
// assert_eq!(basic_output.native_token().unwrap().amount().as_u32(), 10);
assert_eq!(basic_output.native_token().unwrap().amount().as_u32(), 10);
}
});
}
Expand Down Expand Up @@ -321,10 +320,9 @@ fn melt_native_tokens() {
// Account next state + foundry + basic output with native tokens
assert_eq!(selected.transaction.outputs().len(), 3);
selected.transaction.outputs().iter().for_each(|output| {
if let Output::Basic(_basic_output) = &output {
if let Output::Basic(basic_output) = &output {
// Basic output remainder has the remaining native tokens
// TODO reenable when ISA supports NTs again
// assert_eq!(basic_output.native_token().unwrap().amount().as_u32(), 5);
assert_eq!(basic_output.native_token().unwrap().amount().as_u32(), 5);
}
});
}
Expand Down Expand Up @@ -1264,10 +1262,9 @@ fn melt_and_burn_native_tokens() {
assert_eq!(selected.transaction.outputs().len(), 3);
// Account state index is increased
selected.transaction.outputs().iter().for_each(|output| {
if let Output::Basic(_basic_output) = &output {
if let Output::Basic(basic_output) = &output {
// Basic output remainder has the remaining native tokens
// TODO reenable when ISA supports NTs again
// assert_eq!(basic_output.native_token().unwrap().amount().as_u32(), 421);
assert_eq!(basic_output.native_token().unwrap().amount().as_u32(), 421);
}
});
}
Expand Down
28 changes: 18 additions & 10 deletions sdk/tests/types/tagged_data_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use iota_sdk::types::block::{
payload::{tagged_data::TaggedDataPayload, PayloadError},
rand::bytes::{rand_bytes, rand_bytes_array},
Block,
};
use packable::{
bounded::{TryIntoBoundedU32Error, TryIntoBoundedU8Error},
Expand Down Expand Up @@ -57,19 +56,28 @@ fn new_valid_tag_length_min() {

#[test]
fn new_invalid_tag_length_more_than_max() {
assert!(matches!(
TaggedDataPayload::new(rand_bytes(65), [0x42, 0xff, 0x84, 0xa2, 0x42, 0xff, 0x84, 0xa2]),
Err(PayloadError::TagLength(TryIntoBoundedU8Error::Invalid(65)))
));
assert_eq!(
TaggedDataPayload::new(
[0u8; *TaggedDataPayload::TAG_LENGTH_RANGE.end() as usize + 1],
[0x42, 0xff, 0x84, 0xa2, 0x42, 0xff, 0x84, 0xa2],
),
Err(PayloadError::TagLength(TryIntoBoundedU8Error::Invalid(
TaggedDataPayload::TAG_LENGTH_RANGE.end() + 1
)))
);
}

#[test]
fn new_invalid_data_length_more_than_max() {
assert!(matches!(
// TODO https://github.com/iotaledger/iota-sdk/issues/1226
TaggedDataPayload::new(rand_bytes(32), [0u8; Block::LENGTH_MAX + 42]),
Err(PayloadError::TaggedDataLength(TryIntoBoundedU32Error::Invalid(l))) if l == Block::LENGTH_MAX as u32 + 42
));
assert_eq!(
TaggedDataPayload::new(
rand_bytes(32),
[0u8; *TaggedDataPayload::DATA_LENGTH_RANGE.end() as usize + 1]
),
Err(PayloadError::TaggedDataLength(TryIntoBoundedU32Error::Invalid(
TaggedDataPayload::DATA_LENGTH_RANGE.end() + 1
)))
);
}

#[test]
Expand Down
11 changes: 9 additions & 2 deletions sdk/tests/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,15 @@ async fn balance_expiration() -> Result<(), Box<dyn std::error::Error>> {
assert_eq!(balance.base_coin().available(), 0);

// Wait until expired
// TODO wait for slots, not seconds
tokio::time::sleep(std::time::Duration::from_secs(slots_until_expired as u64)).await;
let seconds_per_slot = wallet_0
.client()
.get_protocol_parameters()
.await?
.slot_duration_in_seconds();
tokio::time::sleep(std::time::Duration::from_secs(
seconds_per_slot as u64 * slots_until_expired as u64,
))
.await;

// Wallet 1 balance after expiration
let balance = wallet_1.sync(None).await?;
Expand Down

0 comments on commit 8a3ec9e

Please sign in to comment.