Skip to content

Commit

Permalink
fix functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Aug 31, 2024
1 parent c24ff7b commit f7f72f3
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 15 deletions.
28 changes: 27 additions & 1 deletion test/functional/test_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ def init_mintlayer_types():
],
},

"TokenAdditionalInfo": {
"type": "struct",
"type_mapping": [
["num_decimals", "u8"],
["ticker", "Vec<u8>"],
]
},

"UtxoAdditionalInfo": {
"type": "enum",
"type_mapping": [
["TokenInfo", "TokenAdditionalInfo"],
["PoolInfo", "(Amount)"],
["AnyoneCanTake", ""], # TODO
],
},

"UtxoWithAdditionalInfo": {
"type": "struct",
"type_mapping": [
["utxo", "TxOutput"],
["additional_info", "Option<UtxoAdditionalInfo>"],
]
},

"StandardInputSignature": {
"type": "struct",
"type_mapping": [
Expand All @@ -219,9 +244,10 @@ def init_mintlayer_types():
"type_mapping": [
["tx", "TransactionV1"],
["witnesses", "Vec<Option<InputWitness>>"],
["input_utxos", "Vec<Option<TxOutput>>"],
["input_utxos", "Vec<Option<UtxoWithAdditionalInfo>>"],
["destinations", "Vec<Option<Destination>>"],
["htlc_secrets", "Vec<Option<[u8; 32]>>"],
["output_additional_infos", "Vec<Option<UtxoAdditionalInfo>>"],
]
},

Expand Down
14 changes: 9 additions & 5 deletions test/functional/wallet_htlc_refund.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ async def async_test(self):
assert_not_in("Tokens", balance)

# issue a valid token
token_id, _ = (await wallet.issue_new_token("XXXX", 2, "http://uri", alice_address))
token_ticker = "XXXX"
token_number_of_decimals = 2
token_id, _ = (await wallet.issue_new_token(token_ticker, token_number_of_decimals, "http://uri", alice_address))
assert token_id is not None
self.log.info(f"new token id: {token_id}")
token_id_hex = node.test_functions_reveal_token_id(token_id)
Expand Down Expand Up @@ -182,9 +184,10 @@ async def async_test(self):
alice_refund_ptx = {
'tx': tx['transaction'],
'witnesses': [None, None],
'input_utxos': alice_htlc_outputs,
'input_utxos': [{'utxo': out, 'additional_info': None} for out in alice_htlc_outputs],
'destinations': [refund_dest_obj, alice_htlc_change_dest],
'htlc_secrets': [None, None]
'htlc_secrets': [None, None],
'output_additional_infos': [None]
}
alice_refund_tx_hex = scalecodec.base.RuntimeConfiguration().create_scale_object('PartiallySignedTransaction').encode(alice_refund_ptx).to_hex()[2:]

Expand All @@ -210,9 +213,10 @@ async def async_test(self):
bob_refund_ptx = {
'tx': tx['transaction'],
'witnesses': [None, None],
'input_utxos': bob_htlc_outputs,
'input_utxos': [{'utxo': out, 'additional_info': None} for out in bob_htlc_outputs],
'destinations': [refund_dest_obj, bob_htlc_change_dest],
'htlc_secrets': [None, None]
'htlc_secrets': [None, None],
'output_additional_infos': [None]
}
bob_refund_tx_hex = scalecodec.base.RuntimeConfiguration().create_scale_object('PartiallySignedTransaction').encode(bob_refund_ptx).to_hex()[2:]

Expand Down
8 changes: 5 additions & 3 deletions wallet/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use utils::ensure;
pub use utxo_selector::UtxoSelectorError;
use wallet_types::account_id::AccountPrefixedId;
use wallet_types::account_info::{StandaloneAddressDetails, StandaloneAddresses};
use wallet_types::partially_signed_transaction::PartiallySignedTransaction;
use wallet_types::partially_signed_transaction::{PartiallySignedTransaction, UtxoAdditionalInfo};
use wallet_types::with_locked::WithLocked;

use crate::account::utxo_selector::{select_coins, OutputGroup};
Expand All @@ -46,7 +46,8 @@ use crate::key_chain::{AccountKeyChains, KeyChainError, VRFAccountKeyChains};
use crate::send_request::{
make_address_output, make_address_output_from_delegation, make_address_output_token,
make_decommission_stake_pool_output, make_mint_token_outputs, make_stake_output,
make_unmint_token_outputs, IssueNftArguments, SelectedInputs, StakePoolDataArguments,
make_unmint_token_outputs, IssueNftArguments, PoolOrTokenId, SelectedInputs,
StakePoolDataArguments,
};
use crate::wallet::WalletPoolsFilter;
use crate::wallet_events::{WalletEvents, WalletEventsNoOp};
Expand Down Expand Up @@ -611,6 +612,7 @@ impl<K: AccountKeyChains> Account<K> {
change_addresses: BTreeMap<Currency, Address<Destination>>,
median_time: BlockTimestamp,
fee_rate: CurrentFeeRate,
additional_utxo_infos: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> WalletResult<(PartiallySignedTransaction, BTreeMap<Currency, Amount>)> {
let mut request = self.select_inputs_for_send_request(
request,
Expand All @@ -623,7 +625,7 @@ impl<K: AccountKeyChains> Account<K> {
)?;

let fees = request.get_fees();
let ptx = request.into_partially_signed_tx(&BTreeMap::new())?;
let ptx = request.into_partially_signed_tx(additional_utxo_infos)?;

Ok((ptx, fees))
}
Expand Down
5 changes: 4 additions & 1 deletion wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ where
change_addresses: BTreeMap<Currency, Address<Destination>>,
current_fee_rate: FeeRate,
consolidate_fee_rate: FeeRate,
additional_utxo_infos: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> WalletResult<(PartiallySignedTransaction, BTreeMap<Currency, Amount>)> {
let request = SendRequest::new().with_outputs(outputs);
let latest_median_time = self.latest_median_time;
Expand All @@ -1452,6 +1453,7 @@ where
current_fee_rate,
consolidate_fee_rate,
},
additional_utxo_infos,
)
})
}
Expand Down Expand Up @@ -1849,11 +1851,12 @@ where
htlc: HashedTimelockContract,
current_fee_rate: FeeRate,
consolidate_fee_rate: FeeRate,
additional_utxo_infos: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
self.for_account_rw_unlocked_and_check_tx(
account_index,
&BTreeMap::new(),
additional_utxo_infos,
|account, db_tx| {
account.create_htlc_tx(
db_tx,
Expand Down
3 changes: 3 additions & 0 deletions wallet/src/wallet/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4504,6 +4504,7 @@ fn sign_send_request_cold_wallet(#[case] seed: Seed) {
[(Currency::Coin, cold_wallet_address.clone())].into(),
FeeRate::from_amount_per_kb(Amount::ZERO),
FeeRate::from_amount_per_kb(Amount::ZERO),
&BTreeMap::new(),
)
.unwrap();

Expand Down Expand Up @@ -4841,6 +4842,7 @@ fn create_htlc_and_spend(#[case] seed: Seed) {
htlc.clone(),
FeeRate::from_amount_per_kb(Amount::ZERO),
FeeRate::from_amount_per_kb(Amount::ZERO),
&BTreeMap::new(),
)
.unwrap();
let create_htlc_tx_id = create_htlc_tx.transaction().get_id();
Expand Down Expand Up @@ -4986,6 +4988,7 @@ fn create_htlc_and_refund(#[case] seed: Seed) {
htlc.clone(),
FeeRate::from_amount_per_kb(Amount::ZERO),
FeeRate::from_amount_per_kb(Amount::ZERO),
&BTreeMap::new(),
)
.unwrap();
let create_htlc_tx_id = create_htlc_tx.transaction().get_id();
Expand Down
6 changes: 6 additions & 0 deletions wallet/wallet-controller/src/runtime_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ impl<B: storage::Backend + 'static> RuntimeWallet<B> {
change_addresses: BTreeMap<Currency, Address<Destination>>,
current_fee_rate: FeeRate,
consolidate_fee_rate: FeeRate,
additional_utxo_infos: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> WalletResult<(PartiallySignedTransaction, BTreeMap<Currency, Amount>)> {
match self {
RuntimeWallet::Software(w) => w.create_unsigned_transaction_to_addresses(
Expand All @@ -860,6 +861,7 @@ impl<B: storage::Backend + 'static> RuntimeWallet<B> {
change_addresses,
current_fee_rate,
consolidate_fee_rate,
additional_utxo_infos,
),
#[cfg(feature = "trezor")]
RuntimeWallet::Trezor(w) => w.create_unsigned_transaction_to_addresses(
Expand All @@ -870,6 +872,7 @@ impl<B: storage::Backend + 'static> RuntimeWallet<B> {
change_addresses,
current_fee_rate,
consolidate_fee_rate,
additional_utxo_infos,
),
}
}
Expand Down Expand Up @@ -1008,6 +1011,7 @@ impl<B: storage::Backend + 'static> RuntimeWallet<B> {
htlc: HashedTimelockContract,
current_fee_rate: FeeRate,
consolidate_fee_rate: FeeRate,
additional_utxo_infos: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> WalletResult<SignedTransaction> {
match self {
RuntimeWallet::Software(w) => w.create_htlc_tx(
Expand All @@ -1016,6 +1020,7 @@ impl<B: storage::Backend + 'static> RuntimeWallet<B> {
htlc,
current_fee_rate,
consolidate_fee_rate,
additional_utxo_infos,
),
#[cfg(feature = "trezor")]
RuntimeWallet::Trezor(w) => w.create_htlc_tx(
Expand All @@ -1024,6 +1029,7 @@ impl<B: storage::Backend + 'static> RuntimeWallet<B> {
htlc,
current_fee_rate,
consolidate_fee_rate,
additional_utxo_infos,
),
}
}
Expand Down
16 changes: 14 additions & 2 deletions wallet/wallet-controller/src/synced_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ where
[(Currency::Coin, change_address)].into(),
current_fee_rate,
consolidate_fee_rate,
&BTreeMap::new(),
)
.map_err(ControllerError::WalletError)?;

Expand Down Expand Up @@ -713,11 +714,19 @@ where
ControllerError::<T>::ExpectingNonEmptyOutputs
);

let outputs = {
let (outputs, additional_utxo_infos) = {
let mut result = Vec::new();
let mut additional_utxo_infos = BTreeMap::new();

for (token_id, outputs_vec) in outputs {
let token_info = fetch_token_info(&self.rpc_client, token_id).await?;
additional_utxo_infos.insert(
PoolOrTokenId::TokenId(token_id),
UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals: token_info.token_number_of_decimals(),
ticker: token_info.token_ticker().to_vec(),
}),
);

match &token_info {
RPCTokenInfo::FungibleToken(token_info) => {
Expand All @@ -735,7 +744,7 @@ where
.map_err(ControllerError::InvalidTxOutput)?;
}

result
(result, additional_utxo_infos)
};

let (inputs, change_addresses) = {
Expand Down Expand Up @@ -813,6 +822,7 @@ where
change_addresses,
current_fee_rate,
consolidate_fee_rate,
&additional_utxo_infos,
)?;

let fees = into_balances(&self.rpc_client, self.chain_config, fees).await?;
Expand Down Expand Up @@ -1040,6 +1050,7 @@ where
&mut self,
output_value: OutputValue,
htlc: HashedTimelockContract,
additional_utxo_infos: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> Result<SignedTransaction, ControllerError<T>> {
let (current_fee_rate, consolidate_fee_rate) =
self.get_current_and_consolidation_fee_rate().await?;
Expand All @@ -1050,6 +1061,7 @@ where
htlc,
current_fee_rate,
consolidate_fee_rate,
additional_utxo_infos,
)?;
Ok(result)
}
Expand Down
19 changes: 16 additions & 3 deletions wallet/wallet-rpc-lib/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use wallet::{
currency_grouper::Currency, transaction_list::TransactionList, PoolData, TransactionToSign,
TxInfo,
},
send_request::PoolOrTokenId,
WalletError,
};

Expand Down Expand Up @@ -76,8 +77,12 @@ use wallet_controller::{
};
use wallet_types::{
account_info::StandaloneAddressDetails,
partially_signed_transaction::PartiallySignedTransaction, signature_status::SignatureStatus,
wallet_tx::TxData, with_locked::WithLocked,
partially_signed_transaction::{
PartiallySignedTransaction, TokenAdditionalInfo, UtxoAdditionalInfo,
},
signature_status::SignatureStatus,
wallet_tx::TxData,
with_locked::WithLocked,
};

#[cfg(feature = "trezor")]
Expand Down Expand Up @@ -1451,12 +1456,20 @@ where
self.wallet
.call_async(move |controller| {
Box::pin(async move {
let mut additional_utxo_infos = BTreeMap::new();
let value = match token_id {
Some(token_id) => {
let token_info = controller.get_token_info(token_id).await?;
let amount = amount
.to_amount(token_info.token_number_of_decimals())
.ok_or(RpcError::InvalidCoinAmount)?;
additional_utxo_infos.insert(
PoolOrTokenId::TokenId(token_id),
UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals: token_info.token_number_of_decimals(),
ticker: token_info.token_ticker().to_vec(),
}),
);
OutputValue::TokenV1(token_id, amount)
}
None => {
Expand All @@ -1470,7 +1483,7 @@ where
controller
.synced_controller(account_index, config)
.await?
.create_htlc_tx(value, htlc)
.create_htlc_tx(value, htlc, &additional_utxo_infos)
.await
.map_err(RpcError::Controller)
})
Expand Down

0 comments on commit f7f72f3

Please sign in to comment.