Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/

## [Unreleased]

### Added
- Node RPC: new method added - `chainstate_tokens_info`.

### Changed
- Wallet RPC:
`wallet_info`: the structure of the returned field `extra_info` was changed.
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion chainstate/src/detail/chainstateref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
self.last_common_ancestor(block_index, &best_block_index)
}

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

// Note: same as get_token_aux_data, this only works for NFTs, for the same reason.
#[log_error]
pub fn get_token_id(
pub fn get_token_id_from_issuance_tx(
&self,
tx_id: &Id<Transaction>,
) -> Result<Option<TokenId>, PropertyQueryError> {
Expand Down
5 changes: 2 additions & 3 deletions chainstate/src/detail/error_classification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,8 @@ impl BlockProcessingErrorClassification for PropertyQueryError {
| PropertyQueryError::GenesisHeaderRequested
| PropertyQueryError::InvalidStartingBlockHeightForMainchainBlocks(_)
| PropertyQueryError::InvalidBlockHeightRange { .. }
| PropertyQueryError::UnsupportedTokenV0InOrder(_) => {
BlockProcessingErrorClass::General
}
| PropertyQueryError::UnsupportedTokenV0InOrder(_)
| PropertyQueryError::TokenInfoMissing(_) => BlockProcessingErrorClass::General,
// Note: these errors are strange - sometimes they don't look like General, judging
// by the code that uses them. But other times some of them seem to just wrap storage
// errors.
Expand Down
17 changes: 15 additions & 2 deletions chainstate/src/detail/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::num::NonZeroUsize;
use std::{collections::BTreeSet, num::NonZeroUsize};

use chainstate_storage::BlockchainStorageRead;
use chainstate_types::{BlockIndex, GenBlockIndex, Locator, PropertyQueryError};
Expand Down Expand Up @@ -367,6 +367,19 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
}
}

pub fn get_tokens_info_for_rpc(
&self,
token_ids: &BTreeSet<TokenId>,
) -> Result<Vec<RPCTokenInfo>, PropertyQueryError> {
token_ids
.iter()
.map(|id| -> Result<_, PropertyQueryError> {
self.get_token_info_for_rpc(*id)?
.ok_or(PropertyQueryError::TokenInfoMissing(*id))
})
.collect::<Result<_, _>>()
}

pub fn get_token_aux_data(
&self,
token_id: &TokenId,
Expand All @@ -378,7 +391,7 @@ impl<'a, S: BlockchainStorageRead, V: TransactionVerificationStrategy> Chainstat
&self,
tx_id: &Id<Transaction>,
) -> Result<Option<TokenId>, PropertyQueryError> {
self.chainstate_ref.get_token_id(tx_id)
self.chainstate_ref.get_token_id_from_issuance_tx(tx_id)
}

pub fn get_mainchain_blocks_list(&self) -> Result<Vec<Id<Block>>, PropertyQueryError> {
Expand Down
16 changes: 15 additions & 1 deletion chainstate/src/interface/chainstate_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{collections::BTreeMap, num::NonZeroUsize, sync::Arc};
use std::{
collections::{BTreeMap, BTreeSet},
num::NonZeroUsize,
sync::Arc,
};

use crate::{
detail::BlockSource, ChainInfo, ChainstateConfig, ChainstateError, ChainstateEvent,
Expand Down Expand Up @@ -206,14 +210,24 @@ pub trait ChainstateInterface: Send + Sync {
&self,
token_id: TokenId,
) -> Result<Option<RPCTokenInfo>, ChainstateError>;

/// Return infos for the specified token ids.
fn get_tokens_info_for_rpc(
&self,
token_ids: &BTreeSet<TokenId>,
) -> Result<Vec<RPCTokenInfo>, ChainstateError>;

/// Return token's auxiliary data; available for NFTs only.
fn get_token_aux_data(
&self,
token_id: TokenId,
) -> Result<Option<TokenAuxiliaryData>, ChainstateError>;
/// Obtain token id given the id of the issuing tx; available for NFTs only.
fn get_token_id_from_issuance_tx(
&self,
tx_id: &Id<Transaction>,
) -> Result<Option<TokenId>, ChainstateError>;
/// Obtain token data given its id; available for fungible tokens only.
fn get_token_data(
&self,
id: &TokenId,
Expand Down
18 changes: 17 additions & 1 deletion chainstate/src/interface/chainstate_interface_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{collections::BTreeMap, num::NonZeroUsize, sync::Arc};
use std::{
collections::{BTreeMap, BTreeSet},
num::NonZeroUsize,
sync::Arc,
};

use crate::{
detail::{
Expand Down Expand Up @@ -523,6 +527,18 @@ where
.map_err(ChainstateError::FailedToReadProperty)
}

#[tracing::instrument(skip(self))]
fn get_tokens_info_for_rpc(
&self,
token_ids: &BTreeSet<TokenId>,
) -> Result<Vec<RPCTokenInfo>, ChainstateError> {
self.chainstate
.query()
.map_err(ChainstateError::from)?
.get_tokens_info_for_rpc(token_ids)
.map_err(ChainstateError::FailedToReadProperty)
}

#[tracing::instrument(skip_all, fields(token_id = %token_id))]
fn get_token_aux_data(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.

use std::{
collections::BTreeMap,
collections::{BTreeMap, BTreeSet},
num::NonZeroUsize,
ops::{Deref, DerefMut},
sync::Arc,
Expand Down Expand Up @@ -276,6 +276,13 @@ where
self.deref().get_token_info_for_rpc(token_id)
}

fn get_tokens_info_for_rpc(
&self,
token_ids: &BTreeSet<TokenId>,
) -> Result<Vec<RPCTokenInfo>, ChainstateError> {
self.deref().get_tokens_info_for_rpc(token_ids)
}

fn get_token_aux_data(
&self,
token_id: TokenId,
Expand Down
82 changes: 46 additions & 36 deletions chainstate/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@ trait ChainstateRpc {
delegation_address: String,
) -> RpcResult<Option<Amount>>;

/// Get token information, given a token id, in address form.
/// Get token information, given a token id in address form.
#[method(name = "token_info")]
async fn token_info(&self, token_id: String) -> RpcResult<Option<RPCTokenInfo>>;

/// Get tokens information, given multiple token ids in address form.
#[method(name = "tokens_info")]
async fn tokens_info(&self, token_ids: Vec<String>) -> RpcResult<Vec<RPCTokenInfo>>;

/// Get order information, given an order id, in address form.
#[method(name = "order_info")]
async fn order_info(&self, order_id: String) -> RpcResult<Option<RpcOrderInfo>>;
Expand Down Expand Up @@ -243,19 +247,22 @@ impl ChainstateRpcServer for super::ChainstateHandle {

if let Some((block, block_index)) = both {
let token_ids = collect_token_v1_ids_from_output_values_holder(&block);
let mut token_decimals = BTreeMap::new();

// TODO replace this loop with a single ChainstateInterface function call obtaining
// all infos at once (when the function is implemented).
for token_id in token_ids {
let token_info: RPCTokenInfo = rpc::handle_result(
self.call(move |this| get_existing_token_info_for_rpc(this, token_id)).await,
)?;
token_decimals.insert(
token_id,
TokenDecimals(token_info.token_number_of_decimals()),
);
}
let token_decimals: BTreeMap<TokenId, TokenDecimals> = rpc::handle_result(
self.call(move |this| -> Result<_, ChainstateError> {
let infos = this.get_tokens_info_for_rpc(&token_ids)?;
let decimals = infos
.iter()
.map(|info| {
(
info.token_id(),
TokenDecimals(info.token_number_of_decimals()),
)
})
.collect::<BTreeMap<_, _>>();
Ok(decimals)
})
.await,
)?;

let rpc_block: RpcBlock = rpc::handle_result(RpcBlock::new(
&chain_config,
Expand Down Expand Up @@ -438,6 +445,27 @@ impl ChainstateRpcServer for super::ChainstateHandle {
)
}

async fn tokens_info(&self, token_ids: Vec<String>) -> RpcResult<Vec<RPCTokenInfo>> {
rpc::handle_result(
self.call(move |this| -> Result<_, DynamizedError> {
let chain_config = this.get_chain_config();

let token_ids = token_ids
.into_iter()
.map(|token_id| -> Result<_, DynamizedError> {
Ok(
dynamize_err(Address::<TokenId>::from_string(chain_config, token_id))?
.into_object(),
)
})
.collect::<Result<_, _>>()?;

dynamize_err(this.get_tokens_info_for_rpc(&token_ids))
})
.await,
)
}

async fn order_info(&self, order_id: String) -> RpcResult<Option<RpcOrderInfo>> {
rpc::handle_result(
self.call(move |this| {
Expand Down Expand Up @@ -488,33 +516,15 @@ impl ChainstateRpcServer for super::ChainstateHandle {
}
}

fn dynamize_err<T, E: std::error::Error + Send + Sync>(
o: Result<T, E>,
) -> Result<T, Box<dyn std::error::Error + Send + Sync>>
type DynamizedError = Box<dyn std::error::Error + Send + Sync>;

fn dynamize_err<T, E: std::error::Error + Send + Sync>(o: Result<T, E>) -> Result<T, DynamizedError>
where
Box<dyn std::error::Error + Send + Sync>: From<E>,
DynamizedError: From<E>,
{
o.map_err(Into::into)
}

fn get_existing_token_info_for_rpc(
chainstate: &(impl ChainstateInterface + ?Sized),
token_id: TokenId,
) -> Result<RPCTokenInfo, LocalRpcError> {
chainstate
.get_token_info_for_rpc(token_id)?
.ok_or(LocalRpcError::MissingTokenInfo(token_id))
}

#[derive(thiserror::Error, Debug, PartialEq, Eq)]
enum LocalRpcError {
#[error("Token info missing for token {0:x}")]
MissingTokenInfo(TokenId),

#[error(transparent)]
ChainstateError(#[from] ChainstateError),
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
64 changes: 60 additions & 4 deletions chainstate/test-framework/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn issue_and_mint_random_token_from_best_block(
TokenIssuance::V1(issuance)
};

let (token_id, _, utxo_with_change) =
let (token_id, _, _, utxo_with_change) =
issue_token_from_block(rng, tf, best_block_id, utxo_to_pay_fee, issuance);

let best_block_id = tf.best_block_id();
Expand All @@ -91,7 +91,12 @@ pub fn issue_token_from_block(
parent_block_id: Id<GenBlock>,
utxo_to_pay_fee: UtxoOutPoint,
issuance: TokenIssuance,
) -> (TokenId, Id<Block>, UtxoOutPoint) {
) -> (
TokenId,
/*issuance_block_id*/ Id<Block>,
/*issuance_tx*/ Transaction,
/*change_outpoint*/ UtxoOutPoint,
) {
let token_issuance_fee = tf.chainstate.get_chain_config().fungible_token_issuance_fee();

let fee_utxo_coins =
Expand All @@ -118,13 +123,64 @@ pub fn issue_token_from_block(
let tx_id = tx.transaction().get_id();
let block = tf
.make_block_builder()
.add_transaction(tx)
.add_transaction(tx.clone())
.with_parent(parent_block_id)
.build(rng);
let block_id = block.get_id();
tf.process_block(block, BlockSource::Local).unwrap();

(token_id, block_id, UtxoOutPoint::new(tx_id.into(), 0))
(
token_id,
block_id,
tx.take_transaction(),
UtxoOutPoint::new(tx_id.into(), 0),
)
}

pub fn make_token_issuance(
rng: &mut impl Rng,
supply: TokenTotalSupply,
freezable: IsTokenFreezable,
) -> TokenIssuance {
TokenIssuance::V1(TokenIssuanceV1 {
token_ticker: random_ascii_alphanumeric_string(rng, 1..5).as_bytes().to_vec(),
number_of_decimals: rng.gen_range(1..18),
metadata_uri: random_ascii_alphanumeric_string(rng, 1..1024).as_bytes().to_vec(),
total_supply: supply,
authority: Destination::AnyoneCanSpend,
is_freezable: freezable,
})
}

pub fn issue_token_from_genesis(
rng: &mut (impl Rng + CryptoRng),
tf: &mut TestFramework,
supply: TokenTotalSupply,
freezable: IsTokenFreezable,
) -> (
TokenId,
/*issuance_block_id*/ Id<Block>,
/*issuance_tx*/ Transaction,
TokenIssuance,
/*change_outpoint*/ UtxoOutPoint,
) {
let utxo_input_outpoint = UtxoOutPoint::new(tf.best_block_id().into(), 0);
let issuance = make_token_issuance(rng, supply, freezable);
let (token_id, issuance_block_id, issuance_tx, change_outpoint) = issue_token_from_block(
rng,
tf,
tf.genesis().get_id().into(),
utxo_input_outpoint,
issuance.clone(),
);

(
token_id,
issuance_block_id,
issuance_tx,
issuance,
change_outpoint,
)
}

pub fn mint_tokens_in_block(
Expand Down
1 change: 1 addition & 0 deletions chainstate/test-suite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tokio = { workspace = true, features = ["rt", "time"] }

criterion.workspace = true
expect-test.workspace = true
rand.workspace = true
rstest.workspace = true

[[bench]]
Expand Down
Loading