Skip to content

Commit b5847db

Browse files
committed
Input commitments tests in chainstate
1 parent fca9037 commit b5847db

File tree

9 files changed

+1128
-47
lines changed

9 files changed

+1128
-47
lines changed

chainstate/test-framework/src/framework.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ use common::{
3636
};
3737
use crypto::{key::PrivateKey, vrf::VRFPrivateKey};
3838
use orders_accounting::OrdersAccountingDB;
39-
use pos_accounting::PoSAccountingDB;
39+
use pos_accounting::{PoSAccountingDB, PoSAccountingData};
4040
use randomness::{CryptoRng, Rng};
4141
use utils::atomics::SeqCstAtomicU64;
42-
use utxo::{Utxo, UtxosDB};
42+
use utxo::{Utxo, UtxosDB, UtxosStorageRead};
4343

4444
use crate::{
4545
framework_builder::TestFrameworkBuilderValue,
@@ -627,6 +627,45 @@ impl TestFramework {
627627
pub fn utxo(&self, outpoint: &UtxoOutPoint) -> Utxo {
628628
self.chainstate.utxo(&outpoint).unwrap().unwrap()
629629
}
630+
631+
pub fn pos_accounting_data_at_tip(&self) -> PoSAccountingData {
632+
self.storage.transaction_ro().unwrap().read_pos_accounting_data_tip().unwrap()
633+
}
634+
635+
pub fn find_kernel_outpoint_for_pool(&self, pool_id: &PoolId) -> Option<UtxoOutPoint> {
636+
let utxos = self.storage.transaction_ro().unwrap().read_utxo_set().unwrap();
637+
638+
let mut outpoints_iter =
639+
utxos.into_iter().filter_map(|(outpoint, utxo)| match utxo.output() {
640+
TxOutput::ProduceBlockFromStake(_, used_pool_id)
641+
| TxOutput::CreateStakePool(used_pool_id, _) => {
642+
(used_pool_id == pool_id).then_some(outpoint)
643+
}
644+
645+
TxOutput::Transfer(_, _)
646+
| TxOutput::LockThenTransfer(_, _, _)
647+
| TxOutput::Burn(_)
648+
| TxOutput::CreateDelegationId(_, _)
649+
| TxOutput::DelegateStaking(_, _)
650+
| TxOutput::IssueFungibleToken(_)
651+
| TxOutput::IssueNft(_, _, _)
652+
| TxOutput::DataDeposit(_)
653+
| TxOutput::Htlc(_, _)
654+
| TxOutput::CreateOrder(_) => None,
655+
});
656+
657+
let result = outpoints_iter.next();
658+
assert!(outpoints_iter.next().is_none());
659+
660+
result
661+
}
662+
663+
// FIXME unify with `utxo` above
664+
pub fn coin_amount_from_outpoint(&self, outpoint: &UtxoOutPoint) -> Amount {
665+
let storage_tx = self.storage.transaction_ro().unwrap();
666+
let utxo = storage_tx.get_utxo(outpoint).unwrap().unwrap();
667+
get_output_value(utxo.output()).unwrap().coin_amount().unwrap()
668+
}
630669
}
631670

632671
#[rstest]

chainstate/test-framework/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ pub type TestChainstate = Box<dyn chainstate::chainstate_interface::ChainstateIn
3838
pub use {
3939
crate::utils::{
4040
anyonecanspend_address, create_chain_config_with_default_staking_pool,
41-
create_chain_config_with_staking_pool, create_custom_genesis_with_stake_pool,
42-
create_stake_pool_data_with_all_reward_to_staker, empty_witness, get_output_value,
43-
output_value_amount, pos_mine, produce_kernel_signature,
41+
create_chain_config_with_staking_pool, create_chain_config_with_staking_pool_impl,
42+
create_custom_genesis_with_stake_pool, create_stake_pool_data_with_all_reward_to_staker,
43+
empty_witness, get_output_value, output_value_amount, pos_mine, produce_kernel_signature,
4444
},
4545
block_builder::BlockBuilder,
4646
framework::TestFramework,

chainstate/test-framework/src/utils.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,28 @@ pub fn create_chain_config_with_staking_pool(
215215
mint_amount: Amount,
216216
pool_id: PoolId,
217217
pool_data: StakePoolData,
218+
) -> ConfigBuilder {
219+
create_chain_config_with_staking_pool_impl(
220+
rng,
221+
mint_amount,
222+
pool_id,
223+
pool_data,
224+
BlockHeight::new(1),
225+
)
226+
}
227+
228+
// FIXME name
229+
pub fn create_chain_config_with_staking_pool_impl(
230+
rng: &mut impl Rng,
231+
mint_amount: Amount,
232+
pool_id: PoolId,
233+
pool_data: StakePoolData,
234+
switch_to_pos_height: BlockHeight,
218235
) -> ConfigBuilder {
219236
let upgrades = vec![
220237
(BlockHeight::new(0), ConsensusUpgrade::IgnoreConsensus),
221238
(
222-
BlockHeight::new(1),
239+
switch_to_pos_height,
223240
ConsensusUpgrade::PoS {
224241
initial_difficulty: Some(Uint256::MAX.into()),
225242
config: PoSChainConfigBuilder::new_for_unit_test().build(),

chainstate/test-suite/src/tests/helpers/mod.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ use common::{
2323
output_value::OutputValue,
2424
signature::inputsig::InputWitness,
2525
timelock::OutputTimeLock,
26-
tokens::{TokenId, TokenIssuance},
27-
AccountCommand, AccountNonce, AccountType, Block, Destination, GenBlock, Transaction,
28-
TxInput, TxOutput, UtxoOutPoint,
26+
tokens::{IsTokenFreezable, TokenId, TokenIssuance, TokenIssuanceV1, TokenTotalSupply},
27+
AccountCommand, AccountNonce, AccountType, Block, Destination, GenBlock, OrderId,
28+
OrdersVersion, Transaction, TxInput, TxOutput, UtxoOutPoint,
2929
},
3030
primitives::{Amount, BlockDistance, BlockHeight, Id, Idable},
3131
};
3232
use crypto::key::{KeyKind, PrivateKey};
33+
use orders_accounting::OrdersAccountingDB;
3334
use randomness::{CryptoRng, Rng};
35+
use test_utils::random_ascii_alphanumeric_string;
3436

3537
pub mod block_creation_helpers;
3638
pub mod block_index_handle_impl;
@@ -185,3 +187,73 @@ pub fn mint_tokens_in_block(
185187

186188
(block_id, tx_id)
187189
}
190+
191+
// Note: this function will create 2 blocks
192+
pub fn issue_and_mint_random_token_from_best_block(
193+
rng: &mut (impl Rng + CryptoRng),
194+
tf: &mut TestFramework,
195+
utxo_to_pay_fee: UtxoOutPoint,
196+
amount_to_mint: Amount,
197+
total_supply: TokenTotalSupply,
198+
is_freezable: IsTokenFreezable,
199+
) -> (
200+
TokenId,
201+
/*tokens*/ UtxoOutPoint,
202+
/*coins change*/ UtxoOutPoint,
203+
) {
204+
let best_block_id = tf.best_block_id();
205+
let issuance = {
206+
let max_ticker_len = tf.chain_config().token_max_ticker_len();
207+
let max_dec_count = tf.chain_config().token_max_dec_count();
208+
let max_uri_len = tf.chain_config().token_max_uri_len();
209+
210+
let issuance = TokenIssuanceV1 {
211+
token_ticker: random_ascii_alphanumeric_string(rng, 1..max_ticker_len)
212+
.as_bytes()
213+
.to_vec(),
214+
number_of_decimals: rng.gen_range(1..max_dec_count),
215+
metadata_uri: random_ascii_alphanumeric_string(rng, 1..max_uri_len).as_bytes().to_vec(),
216+
total_supply,
217+
is_freezable,
218+
authority: Destination::AnyoneCanSpend,
219+
};
220+
TokenIssuance::V1(issuance)
221+
};
222+
223+
let (token_id, _, utxo_with_change) =
224+
issue_token_from_block(rng, tf, best_block_id, utxo_to_pay_fee, issuance);
225+
226+
let best_block_id = tf.best_block_id();
227+
let (_, mint_tx_id) = mint_tokens_in_block(
228+
rng,
229+
tf,
230+
best_block_id,
231+
utxo_with_change,
232+
token_id,
233+
amount_to_mint,
234+
true,
235+
);
236+
237+
(
238+
token_id,
239+
UtxoOutPoint::new(mint_tx_id.into(), 0),
240+
UtxoOutPoint::new(mint_tx_id.into(), 1),
241+
)
242+
}
243+
244+
pub fn calculate_fill_order(
245+
tf: &TestFramework,
246+
order_id: &OrderId,
247+
fill_amount_in_ask_currency: Amount,
248+
orders_version: OrdersVersion,
249+
) -> Amount {
250+
let db_tx = tf.storage.transaction_ro().unwrap();
251+
let orders_db = OrdersAccountingDB::new(&db_tx);
252+
orders_accounting::calculate_fill_order(
253+
&orders_db,
254+
*order_id,
255+
fill_amount_in_ask_currency,
256+
orders_version,
257+
)
258+
.unwrap()
259+
}

0 commit comments

Comments
 (0)