Skip to content

Commit a9012b5

Browse files
committed
Refactor token tests in chainstate to check all available token data. Add separate test for get_tokens_info_for_rpc
1 parent 02f2bd4 commit a9012b5

File tree

16 files changed

+1749
-784
lines changed

16 files changed

+1749
-784
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chainstate/src/detail/chainstateref/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
361361
self.last_common_ancestor(block_index, &best_block_index)
362362
}
363363

364+
// Note: this function will return Some for NFTs only, because the data is only written
365+
// for NFTs. This is decided by `token_issuance_cache::has_tokens_issuance_to_cache`.
364366
#[log_error]
365367
pub fn get_token_aux_data(
366368
&self,
@@ -369,8 +371,9 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
369371
self.db_tx.get_token_aux_data(token_id).map_err(PropertyQueryError::from)
370372
}
371373

374+
// Note: same as get_token_aux_data, this only works for NFTs, for the same reason.
372375
#[log_error]
373-
pub fn get_token_id(
376+
pub fn get_token_id_from_issuance_tx(
374377
&self,
375378
tx_id: &Id<Transaction>,
376379
) -> Result<Option<TokenId>, PropertyQueryError> {

chainstate/src/detail/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
391391
&self,
392392
tx_id: &Id<Transaction>,
393393
) -> Result<Option<TokenId>, PropertyQueryError> {
394-
self.chainstate_ref.get_token_id(tx_id)
394+
self.chainstate_ref.get_token_id_from_issuance_tx(tx_id)
395395
}
396396

397397
pub fn get_mainchain_blocks_list(&self) -> Result<Vec<Id<Block>>, PropertyQueryError> {

chainstate/src/interface/chainstate_interface.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,17 @@ pub trait ChainstateInterface: Send + Sync {
217217
token_ids: &BTreeSet<TokenId>,
218218
) -> Result<Vec<RPCTokenInfo>, ChainstateError>;
219219

220+
/// Return token's auxiliary data; available for NFTs only.
220221
fn get_token_aux_data(
221222
&self,
222223
token_id: TokenId,
223224
) -> Result<Option<TokenAuxiliaryData>, ChainstateError>;
225+
/// Obtain token id given the id of the issuing tx; available for NFTs only.
224226
fn get_token_id_from_issuance_tx(
225227
&self,
226228
tx_id: &Id<Transaction>,
227229
) -> Result<Option<TokenId>, ChainstateError>;
230+
/// Obtain token data given its id; available for fungible tokens only.
228231
fn get_token_data(
229232
&self,
230233
id: &TokenId,

chainstate/test-framework/src/helpers.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn issue_and_mint_random_token_from_best_block(
6464
TokenIssuance::V1(issuance)
6565
};
6666

67-
let (token_id, _, utxo_with_change) =
67+
let (token_id, _, _, utxo_with_change) =
6868
issue_token_from_block(rng, tf, best_block_id, utxo_to_pay_fee, issuance);
6969

7070
let best_block_id = tf.best_block_id();
@@ -91,7 +91,12 @@ pub fn issue_token_from_block(
9191
parent_block_id: Id<GenBlock>,
9292
utxo_to_pay_fee: UtxoOutPoint,
9393
issuance: TokenIssuance,
94-
) -> (TokenId, Id<Block>, UtxoOutPoint) {
94+
) -> (
95+
TokenId,
96+
/*issuance_block_id*/ Id<Block>,
97+
/*issuance_tx*/ Transaction,
98+
/*change_outpoint*/ UtxoOutPoint,
99+
) {
95100
let token_issuance_fee = tf.chainstate.get_chain_config().fungible_token_issuance_fee();
96101

97102
let fee_utxo_coins =
@@ -118,13 +123,64 @@ pub fn issue_token_from_block(
118123
let tx_id = tx.transaction().get_id();
119124
let block = tf
120125
.make_block_builder()
121-
.add_transaction(tx)
126+
.add_transaction(tx.clone())
122127
.with_parent(parent_block_id)
123128
.build(rng);
124129
let block_id = block.get_id();
125130
tf.process_block(block, BlockSource::Local).unwrap();
126131

127-
(token_id, block_id, UtxoOutPoint::new(tx_id.into(), 0))
132+
(
133+
token_id,
134+
block_id,
135+
tx.take_transaction(),
136+
UtxoOutPoint::new(tx_id.into(), 0),
137+
)
138+
}
139+
140+
pub fn make_token_issuance(
141+
rng: &mut impl Rng,
142+
supply: TokenTotalSupply,
143+
freezable: IsTokenFreezable,
144+
) -> TokenIssuance {
145+
TokenIssuance::V1(TokenIssuanceV1 {
146+
token_ticker: random_ascii_alphanumeric_string(rng, 1..5).as_bytes().to_vec(),
147+
number_of_decimals: rng.gen_range(1..18),
148+
metadata_uri: random_ascii_alphanumeric_string(rng, 1..1024).as_bytes().to_vec(),
149+
total_supply: supply,
150+
authority: Destination::AnyoneCanSpend,
151+
is_freezable: freezable,
152+
})
153+
}
154+
155+
pub fn issue_token_from_genesis(
156+
rng: &mut (impl Rng + CryptoRng),
157+
tf: &mut TestFramework,
158+
supply: TokenTotalSupply,
159+
freezable: IsTokenFreezable,
160+
) -> (
161+
TokenId,
162+
/*issuance_block_id*/ Id<Block>,
163+
/*issuance_tx*/ Transaction,
164+
TokenIssuance,
165+
/*change_outpoint*/ UtxoOutPoint,
166+
) {
167+
let utxo_input_outpoint = UtxoOutPoint::new(tf.best_block_id().into(), 0);
168+
let issuance = make_token_issuance(rng, supply, freezable);
169+
let (token_id, issuance_block_id, issuance_tx, change_outpoint) = issue_token_from_block(
170+
rng,
171+
tf,
172+
tf.genesis().get_id().into(),
173+
utxo_input_outpoint,
174+
issuance.clone(),
175+
);
176+
177+
(
178+
token_id,
179+
issuance_block_id,
180+
issuance_tx,
181+
issuance,
182+
change_outpoint,
183+
)
128184
}
129185

130186
pub fn mint_tokens_in_block(

chainstate/test-suite/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ tokio = { workspace = true, features = ["rt", "time"] }
3737

3838
criterion.workspace = true
3939
expect-test.workspace = true
40+
rand.workspace = true
4041
rstest.workspace = true
4142

4243
[[bench]]

0 commit comments

Comments
 (0)