Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions api-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/

### Added
- New endpoint was added: `/v2/transaction/{id}/output/{idx}`.
- New endpoint was added: `/v2/token/{id}/transactions` will return all transactions related to a token

### Changed
- `/v2/token/ticker/{ticker}` will now return all tokens whose ticker has the specified `{ticker}`
Expand Down
77 changes: 74 additions & 3 deletions api-server/api-server-common/src/storage/impls/in_memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::storage::storage_api::{
block_aux_data::{BlockAuxData, BlockWithExtraData},
AmountWithDecimals, ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation,
FungibleTokenData, LockedUtxo, NftWithOwner, Order, PoolBlockStats, PoolDataWithExtraInfo,
TransactionInfo, TransactionWithBlockInfo, Utxo, UtxoLock, UtxoWithExtraInfo,
TokenTransaction, TransactionInfo, TransactionWithBlockInfo, Utxo, UtxoLock, UtxoWithExtraInfo,
};
use common::{
address::Address,
Expand Down Expand Up @@ -48,6 +48,7 @@ struct ApiServerInMemoryStorage {
address_balance_table: BTreeMap<String, BTreeMap<CoinOrTokenId, BTreeMap<BlockHeight, Amount>>>,
address_locked_balance_table: BTreeMap<String, BTreeMap<(CoinOrTokenId, BlockHeight), Amount>>,
address_transactions_table: BTreeMap<String, BTreeMap<BlockHeight, Vec<Id<Transaction>>>>,
token_transactions_table: BTreeMap<TokenId, BTreeMap<BlockHeight, BTreeSet<TokenTransaction>>>,
delegation_table: BTreeMap<DelegationId, BTreeMap<BlockHeight, Delegation>>,
main_chain_blocks_table: BTreeMap<BlockHeight, Id<Block>>,
pool_data_table: BTreeMap<PoolId, BTreeMap<BlockHeight, PoolDataWithExtraInfo>>,
Expand Down Expand Up @@ -75,6 +76,7 @@ impl ApiServerInMemoryStorage {
address_balance_table: BTreeMap::new(),
address_locked_balance_table: BTreeMap::new(),
address_transactions_table: BTreeMap::new(),
token_transactions_table: BTreeMap::new(),
delegation_table: BTreeMap::new(),
main_chain_blocks_table: BTreeMap::new(),
pool_data_table: BTreeMap::new(),
Expand Down Expand Up @@ -173,6 +175,27 @@ impl ApiServerInMemoryStorage {
}))
}

fn get_token_transactions(
&self,
token_id: TokenId,
len: u32,
tx_global_index: u64,
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
Ok(self
.token_transactions_table
.get(&token_id)
.map_or_else(Vec::new, |transactions| {
transactions
.iter()
.rev()
.flat_map(|(_, txs)| txs.iter())
.cloned()
.flat_map(|tx| (tx.tx_global_index < tx_global_index).then_some(tx))
.take(len as usize)
.collect()
}))
}

fn get_block(&self, block_id: Id<Block>) -> Result<Option<BlockInfo>, ApiServerStorageError> {
let block_result = self.block_table.get(&block_id);
let block = match block_result {
Expand Down Expand Up @@ -214,7 +237,7 @@ impl ApiServerInMemoryStorage {
additional_info: additinal_data.clone(),
},
block_aux: *block_aux,
global_tx_index: *tx_global_index,
tx_global_index: *tx_global_index,
}
},
)
Expand All @@ -240,7 +263,7 @@ impl ApiServerInMemoryStorage {
TransactionWithBlockInfo {
tx_info: tx_info.clone(),
block_aux: *block_aux,
global_tx_index: *tx_global_index,
tx_global_index: *tx_global_index,
}
})
.collect())
Expand Down Expand Up @@ -864,6 +887,20 @@ impl ApiServerInMemoryStorage {
Ok(())
}

fn del_token_transactions_above_height(
&mut self,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
// Inefficient, but acceptable for testing with InMemoryStorage

self.token_transactions_table.retain(|_, v| {
v.retain(|k, _| k <= &block_height);
!v.is_empty()
});

Ok(())
}

fn set_address_balance_at_height(
&mut self,
address: &Address<Destination>,
Expand Down Expand Up @@ -942,6 +979,40 @@ impl ApiServerInMemoryStorage {
Ok(())
}

fn set_token_transactions_at_height(
&mut self,
token_id: TokenId,
transaction_ids: BTreeSet<Id<Transaction>>,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
if transaction_ids.is_empty() {
return Ok(());
}

let next_tx_idx = self
.token_transactions_table
.values()
.flat_map(|by_height| by_height.values())
.flat_map(|tx_set| tx_set.iter())
.map(|tx| tx.tx_global_index + 1)
.max()
.unwrap_or(0);

self.token_transactions_table
.entry(token_id)
.or_default()
.entry(block_height)
.or_default()
.extend(
transaction_ids.into_iter().enumerate().map(|(idx, tx_id)| TokenTransaction {
tx_global_index: next_tx_idx + idx as u64,
tx_id,
}),
);

Ok(())
}

fn set_mainchain_block(
&mut self,
block_id: Id<Block>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use common::{
use crate::storage::storage_api::{
block_aux_data::BlockAuxData, AmountWithDecimals, ApiServerStorageError, ApiServerStorageRead,
BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, NftWithOwner, Order,
PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, TransactionWithBlockInfo, Utxo,
UtxoWithExtraInfo,
PoolBlockStats, PoolDataWithExtraInfo, TokenTransaction, TransactionInfo,
TransactionWithBlockInfo, Utxo, UtxoWithExtraInfo,
};

use super::ApiServerInMemoryStorageTransactionalRo;
Expand Down Expand Up @@ -68,6 +68,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
self.transaction.get_address_transactions(address)
}

async fn get_token_transactions(
&self,
token_id: TokenId,
len: u32,
tx_global_index: u64,
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
self.transaction.get_token_transactions(token_id, len, tx_global_index)
}

async fn get_block(
&self,
block_id: Id<Block>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::storage::storage_api::{
block_aux_data::{BlockAuxData, BlockWithExtraData},
AmountWithDecimals, ApiServerStorageError, ApiServerStorageRead, ApiServerStorageWrite,
BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, LockedUtxo, NftWithOwner,
Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, TransactionWithBlockInfo, Utxo,
UtxoWithExtraInfo,
Order, PoolBlockStats, PoolDataWithExtraInfo, TokenTransaction, TransactionInfo,
TransactionWithBlockInfo, Utxo, UtxoWithExtraInfo,
};
use common::{
address::Address,
Expand Down Expand Up @@ -64,6 +64,13 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
self.transaction.del_address_transactions_above_height(block_height)
}

async fn del_token_transactions_above_height(
&mut self,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
self.transaction.del_token_transactions_above_height(block_height)
}

async fn set_address_balance_at_height(
&mut self,
address: &Address<Destination>,
Expand Down Expand Up @@ -104,6 +111,16 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
.set_address_transactions_at_height(address, transactions, block_height)
}

async fn set_token_transactions_at_height(
&mut self,
token_id: TokenId,
transactions: BTreeSet<Id<Transaction>>,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
self.transaction
.set_token_transactions_at_height(token_id, transactions, block_height)
}

async fn set_mainchain_block(
&mut self,
block_id: Id<Block>,
Expand Down Expand Up @@ -331,6 +348,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
self.transaction.get_address_transactions(address)
}

async fn get_token_transactions(
&self,
token_id: TokenId,
len: u32,
tx_global_index: u64,
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
self.transaction.get_token_transactions(token_id, len, tx_global_index)
}

async fn get_latest_blocktimestamps(
&self,
) -> Result<Vec<BlockTimestamp>, ApiServerStorageError> {
Expand Down
2 changes: 1 addition & 1 deletion api-server/api-server-common/src/storage/impls/mod.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.

pub const CURRENT_STORAGE_VERSION: u32 = 23;
pub const CURRENT_STORAGE_VERSION: u32 = 24;

pub mod in_memory;
pub mod postgres;
Loading