Skip to content

Commit 2456eb9

Browse files
committed
Rename lock tokens and add randomness to functional test
lock_tokens -> lock_token_supply specify a randomseed argument for the functional tests randomize token supply change test
1 parent b085273 commit 2456eb9

File tree

9 files changed

+45
-38
lines changed

9 files changed

+45
-38
lines changed

test/functional/test_framework/wallet_cli_controller.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,5 @@ async def stop_staking(self) -> str:
258258
async def get_addresses_usage(self) -> str:
259259
return await self._write_command("showreceiveaddresses\n")
260260

261-
async def get_balance(self, with_locked: str = 'unlocked') -> str:
262-
return await self._write_command(f"getbalance {with_locked}\n")
261+
async def get_balance(self, with_locked: str = 'unlocked', utxo_states: List[str] = ['confirmed']) -> str:
262+
return await self._write_command(f"getbalance {with_locked} {' '.join(utxo_states)}\n")

test/functional/test_runner.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import datetime
3131
import locale
3232
import os
33+
import random
3334
import time
3435
import shutil
3536
import signal
@@ -443,6 +444,7 @@ def __init__(self, *, num_tests_parallel, tests_dir, tmpdir, test_list, flags, u
443444
self.num_running = 0
444445
self.jobs = []
445446
self.use_term_control = use_term_control
447+
self.randomseed = random.randrange(sys.maxsize)
446448

447449
def get_next(self):
448450
while self.num_running < self.num_jobs and self.test_list:
@@ -451,14 +453,15 @@ def get_next(self):
451453
test = self.test_list.pop(0)
452454
portseed = len(self.test_list)
453455
portseed_arg = ["--portseed={}".format(portseed)]
456+
randomseed_arg = [f"--randomseed={self.randomseed}"]
454457
log_stdout = tempfile.SpooledTemporaryFile(max_size=2**16)
455458
log_stderr = tempfile.SpooledTemporaryFile(max_size=2**16)
456459
test_argv = test.split()
457460
testdir = "{}/{}_{}".format(self.tmpdir, re.sub(".py$", "", test_argv[0]), portseed)
458461
tmpdir_arg = ["--tmpdir={}".format(testdir)]
459462
self.jobs.append((test,
460463
time.time(),
461-
subprocess.Popen([sys.executable, self.tests_dir + test_argv[0]] + test_argv[1:] + self.flags + portseed_arg + tmpdir_arg,
464+
subprocess.Popen([sys.executable, self.tests_dir + test_argv[0]] + test_argv[1:] + self.flags + portseed_arg + tmpdir_arg + randomseed_arg,
462465
universal_newlines=True,
463466
stdout=log_stdout,
464467
stderr=log_stderr),

test/functional/wallet_tokens_change_supply.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import asyncio
3838
import sys
39+
import random
3940

4041
class WalletTokens(BitcoinTestFramework):
4142

@@ -145,36 +146,39 @@ async def async_test(self):
145146
self.generate_block()
146147
assert_in("Success", await wallet.sync())
147148

148-
tokens_to_mint = 10000
149+
tokens_to_mint = random.randrange(2, 10000)
149150
total_tokens_supply = tokens_to_mint
150151
assert_in("The transaction was submitted successfully", await wallet.mint_tokens(token_id, address, tokens_to_mint))
151152

152153
self.generate_block()
153154
assert_in("Success", await wallet.sync())
154155

155-
assert_in(f"{token_id} amount: {total_tokens_supply}", await wallet.get_balance())
156-
157-
# cannot unmint more than minted
158-
assert_in(f"Trying to unmint Amount {{ val: {tokens_to_mint+1}00 }} but the current supply is Amount {{ val: {tokens_to_mint}00 }}", await wallet.unmint_tokens(token_id, tokens_to_mint + 1))
159-
160-
tokens_to_unmint = 1000
161-
total_tokens_supply = total_tokens_supply - tokens_to_unmint
162-
assert_in("The transaction was submitted successfully", await wallet.unmint_tokens(token_id, tokens_to_unmint))
163-
164-
self.generate_block()
165-
assert_in("Success", await wallet.sync())
166-
assert_in(f"{token_id} amount: {total_tokens_supply}", await wallet.get_balance())
167-
168-
# mint some more tokens
169-
tokens_to_mint = 1000
170-
total_tokens_supply = total_tokens_supply + tokens_to_mint
171-
assert_in("The transaction was submitted successfully", await wallet.mint_tokens(token_id, address, tokens_to_mint))
172-
173-
self.generate_block()
174-
assert_in("Success", await wallet.sync())
175-
assert_in(f"{token_id} amount: {total_tokens_supply}", await wallet.get_balance())
176-
177-
# lock token suply
156+
# randomize minting and unminting
157+
for _ in range(10):
158+
if random.choice([True, False]):
159+
# mint some more tokens
160+
tokens_to_mint = random.randrange(1, 10000)
161+
total_tokens_supply = total_tokens_supply + tokens_to_mint
162+
assert_in("The transaction was submitted successfully", await wallet.mint_tokens(token_id, address, tokens_to_mint))
163+
else:
164+
# unmint some tokens
165+
tokens_to_unmint = random.randrange(1, 20000)
166+
if tokens_to_unmint <= total_tokens_supply:
167+
total_tokens_supply = total_tokens_supply - tokens_to_unmint
168+
assert_in("The transaction was submitted successfully", await wallet.unmint_tokens(token_id, tokens_to_unmint))
169+
else:
170+
assert_in(f"Trying to unmint Amount {{ val: {tokens_to_unmint}00 }} but the current supply is Amount {{ val: {total_tokens_supply}00 }}", await wallet.unmint_tokens(token_id, tokens_to_unmint))
171+
continue
172+
173+
# either generate a new block or leave the transaction as in-memory state
174+
if random.choice([True, False]):
175+
self.generate_block()
176+
assert_in("Success", await wallet.sync())
177+
178+
# check total supply is correct
179+
assert_in(f"{token_id} amount: {total_tokens_supply}", await wallet.get_balance(utxo_states=['confirmed', 'inactive']))
180+
181+
# lock token supply
178182
assert_in("The transaction was submitted successfully", await wallet.lock_tokens(token_id))
179183
self.generate_block()
180184
assert_in("Success", await wallet.sync())

wallet/src/account/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl Account {
775775
)
776776
}
777777

778-
pub fn lock_tokens(
778+
pub fn lock_token_supply(
779779
&mut self,
780780
db_tx: &mut impl WalletStorageWriteUnlocked,
781781
token_id: TokenId,

wallet/src/wallet/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl<B: storage::Backend> Wallet<B> {
851851
})
852852
}
853853

854-
pub fn lock_tokens(
854+
pub fn lock_token_supply(
855855
&mut self,
856856
account_index: U31,
857857
token_id: TokenId,
@@ -860,7 +860,7 @@ impl<B: storage::Backend> Wallet<B> {
860860
) -> WalletResult<SignedTransaction> {
861861
let latest_median_time = self.latest_median_time;
862862
self.for_account_rw_unlocked(account_index, |account, db_tx| {
863-
account.lock_tokens(
863+
account.lock_token_supply(
864864
db_tx,
865865
token_id,
866866
latest_median_time,

wallet/src/wallet/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2716,7 +2716,7 @@ fn change_token_supply_fixed(#[case] seed: Seed) {
27162716
);
27172717

27182718
let err = wallet
2719-
.lock_tokens(
2719+
.lock_token_supply(
27202720
DEFAULT_ACCOUNT_INDEX,
27212721
issued_token_id,
27222722
FeeRate::new(Amount::ZERO),
@@ -2952,7 +2952,7 @@ fn change_token_supply_unlimited(#[case] seed: Seed) {
29522952
);
29532953

29542954
let err = wallet
2955-
.lock_tokens(
2955+
.lock_token_supply(
29562956
DEFAULT_ACCOUNT_INDEX,
29572957
issued_token_id,
29582958
FeeRate::new(Amount::ZERO),
@@ -3183,7 +3183,7 @@ fn change_and_lock_token_supply_lockable(#[case] seed: Seed) {
31833183
assert!(token_issuance_data.total_supply.check_can_lock().is_ok());
31843184

31853185
let lock_transaction = wallet
3186-
.lock_tokens(
3186+
.lock_token_supply(
31873187
DEFAULT_ACCOUNT_INDEX,
31883188
issued_token_id,
31893189
FeeRate::new(Amount::ZERO),
@@ -3249,7 +3249,7 @@ fn change_and_lock_token_supply_lockable(#[case] seed: Seed) {
32493249
assert_eq!(err, WalletError::CannotChangeLockedTokenSupply);
32503250

32513251
let err = wallet
3252-
.lock_tokens(
3252+
.lock_token_supply(
32533253
DEFAULT_ACCOUNT_INDEX,
32543254
issued_token_id,
32553255
FeeRate::new(Amount::ZERO),

wallet/wallet-cli-lib/src/commands/helper_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ pub fn to_per_thousand(
238238
variable_name: &str,
239239
) -> Result<PerThousand, WalletCliError> {
240240
PerThousand::from_decimal_str(value_str).ok_or(WalletCliError::InvalidInput(format!(
241-
"Failed to parse {variable_name} the decimal that must be in the range [0.001,1.000] or [0.1%,100%]",
241+
"Failed to parse {variable_name}. The decimal must be in the range [0.001,1.000] or [0.1%,100%]",
242242
)))
243243
}
244244

wallet/wallet-cli-lib/src/commands/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ impl CommandHandler {
891891

892892
self.get_synced_controller()
893893
.await?
894-
.lock_tokens(token_id)
894+
.lock_token_supply(token_id)
895895
.await
896896
.map_err(WalletCliError::Controller)?;
897897

wallet/wallet-controller/src/synced_controller.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ impl<'a, T: NodeInterface, W: WalletEvents> SyncedController<'a, T, W> {
205205
self.broadcast_to_mempool(tx).await
206206
}
207207

208-
pub async fn lock_tokens(&mut self, token_id: TokenId) -> Result<(), ControllerError<T>> {
208+
pub async fn lock_token_supply(&mut self, token_id: TokenId) -> Result<(), ControllerError<T>> {
209209
let (current_fee_rate, consolidate_fee_rate) =
210210
self.get_current_and_consolidation_fee_rate().await?;
211211

212212
let tx = self
213213
.wallet
214-
.lock_tokens(
214+
.lock_token_supply(
215215
self.account_index,
216216
token_id,
217217
current_fee_rate,

0 commit comments

Comments
 (0)