Skip to content

Commit 4e7b63e

Browse files
committed
Fix api simulation test
1 parent de02614 commit 4e7b63e

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

api-server/scanner-lib/src/sync/tests/simulation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ async fn simulation(
236236
let mut block_builder = tf.make_pos_block_builder().with_random_staking_pool(&mut rng);
237237

238238
for _ in 0..rng.gen_range(10..max_tx_per_block) {
239-
block_builder = block_builder.add_test_transaction(&mut rng);
239+
block_builder = block_builder.add_test_transaction(&mut rng, false);
240240
}
241241

242242
let block = block_builder.build(&mut rng);
@@ -335,11 +335,11 @@ async fn simulation(
335335
.and_modify(|amount| *amount = (*amount + *to_stake).unwrap())
336336
.or_insert(*to_stake);
337337
}
338-
TxOutput::Htlc(_, _) => unimplemented!(),
339338
| TxOutput::CreateDelegationId(_, _)
340339
| TxOutput::Transfer(_, _)
341340
| TxOutput::LockThenTransfer(_, _, _)
342-
| TxOutput::ProduceBlockFromStake(_, _) => {}
341+
| TxOutput::ProduceBlockFromStake(_, _)
342+
| TxOutput::Htlc(_, _) => {}
343343
});
344344

345345
tx.inputs().iter().for_each(|inp| match inp {

chainstate/test-framework/src/block_builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ impl<'f> BlockBuilder<'f> {
120120
}
121121

122122
/// Adds a transaction that uses random utxos and accounts
123-
pub fn add_test_transaction(mut self, rng: &mut (impl Rng + CryptoRng)) -> Self {
123+
pub fn add_test_transaction(
124+
mut self,
125+
rng: &mut (impl Rng + CryptoRng),
126+
supper_htlc: bool,
127+
) -> Self {
124128
let utxo_set = self
125129
.framework
126130
.storage
@@ -148,6 +152,7 @@ impl<'f> BlockBuilder<'f> {
148152
&self.pos_accounting_store,
149153
None,
150154
account_nonce_getter,
155+
supper_htlc,
151156
)
152157
.make(
153158
rng,

chainstate/test-framework/src/pos_block_builder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ impl<'f> PoSBlockBuilder<'f> {
360360
}
361361

362362
/// Adds a transaction that uses random utxos and accounts
363-
pub fn add_test_transaction(mut self, rng: &mut (impl Rng + CryptoRng)) -> Self {
363+
pub fn add_test_transaction(
364+
mut self,
365+
rng: &mut (impl Rng + CryptoRng),
366+
support_htlc: bool,
367+
) -> Self {
364368
let utxo_set = self
365369
.framework
366370
.storage
@@ -388,6 +392,7 @@ impl<'f> PoSBlockBuilder<'f> {
388392
&self.pos_accounting_store,
389393
self.staking_pool,
390394
account_nonce_getter,
395+
support_htlc,
391396
)
392397
.make(
393398
rng,

chainstate/test-framework/src/random_tx_maker.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ pub struct RandomTxMaker<'a> {
144144

145145
account_command_used: bool,
146146

147+
support_htlc: bool,
148+
147149
// There can be only one Unmint operation per transaction.
148150
// But it's unknown in advance which token burn would be utilized by unmint operation
149151
// so we have to collect all burns for all tokens just in case.
@@ -161,6 +163,7 @@ impl<'a> RandomTxMaker<'a> {
161163
pos_accounting_store: &'a InMemoryPoSAccounting,
162164
staking_pool: Option<PoolId>,
163165
account_nonce_getter: Box<dyn Fn(AccountType) -> Option<AccountNonce> + 'a>,
166+
support_htlc: bool,
164167
) -> Self {
165168
Self {
166169
chainstate,
@@ -174,6 +177,7 @@ impl<'a> RandomTxMaker<'a> {
174177
stake_pool_can_be_created: true,
175178
delegation_can_be_created: true,
176179
account_command_used: false,
180+
support_htlc,
177181
unmint_for: None,
178182
total_tokens_burned: BTreeMap::new(),
179183
fee_input: None,
@@ -815,18 +819,25 @@ impl<'a> RandomTxMaker<'a> {
815819
destination,
816820
timelock,
817821
),
818-
1 => TxOutput::Htlc(
819-
OutputValue::Coin(new_value),
820-
Box::new(HashedTimelockContract {
821-
secret_hash: HtlcSecretHash::zero(),
822-
spend_key: destination,
823-
refund_timelock: timelock,
824-
refund_key: key_manager.new_2_of_2_multisig_destination(
825-
self.chainstate.get_chain_config(),
826-
rng,
827-
),
828-
}),
829-
),
822+
1 => {
823+
if self.support_htlc {
824+
TxOutput::Htlc(
825+
OutputValue::Coin(new_value),
826+
Box::new(HashedTimelockContract {
827+
secret_hash: HtlcSecretHash::zero(),
828+
spend_key: destination,
829+
refund_timelock: timelock,
830+
refund_key: key_manager
831+
.new_2_of_2_multisig_destination(
832+
self.chainstate.get_chain_config(),
833+
rng,
834+
),
835+
}),
836+
)
837+
} else {
838+
TxOutput::Transfer(OutputValue::Coin(new_value), destination)
839+
}
840+
}
830841
_ => TxOutput::Transfer(OutputValue::Coin(new_value), destination),
831842
}
832843
}

chainstate/test-suite/src/tests/tx_verification_simulation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn simulation(#[case] seed: Seed, #[case] max_blocks: usize, #[case] max_tx_per_
115115
let mut block_builder = tf.make_pos_block_builder().with_random_staking_pool(&mut rng);
116116

117117
for _ in 0..rng.gen_range(10..max_tx_per_block) {
118-
block_builder = block_builder.add_test_transaction(&mut rng);
118+
block_builder = block_builder.add_test_transaction(&mut rng, true);
119119
}
120120

121121
let block = block_builder.build(&mut rng);
@@ -151,7 +151,7 @@ fn simulation(#[case] seed: Seed, #[case] max_blocks: usize, #[case] max_tx_per_
151151
let mut block_builder = tf2.make_pos_block_builder().with_random_staking_pool(&mut rng);
152152

153153
for _ in 0..rng.gen_range(10..max_tx_per_block) {
154-
block_builder = block_builder.add_test_transaction(&mut rng);
154+
block_builder = block_builder.add_test_transaction(&mut rng, true);
155155
}
156156

157157
let block = block_builder.build(&mut rng);

0 commit comments

Comments
 (0)