Skip to content

Add tx order number and pagination #1930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
68 changes: 53 additions & 15 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, Utxo, UtxoLock, UtxoWithExtraInfo,
TransactionInfo, TransactionWithBlockInfo, Utxo, UtxoLock, UtxoWithExtraInfo,
};
use common::{
address::Address,
Expand All @@ -29,7 +29,7 @@ use common::{
Block, ChainConfig, DelegationId, Destination, Genesis, OrderId, PoolId, Transaction,
UtxoOutPoint,
},
primitives::{id::WithId, Amount, BlockHeight, CoinOrTokenId, Id},
primitives::{id::WithId, Amount, BlockHeight, CoinOrTokenId, Id, Idable},
};
use std::{
cmp::Reverse,
Expand All @@ -51,6 +51,7 @@ struct ApiServerInMemoryStorage {
main_chain_blocks_table: BTreeMap<BlockHeight, Id<Block>>,
pool_data_table: BTreeMap<PoolId, BTreeMap<BlockHeight, PoolDataWithExtraInfo>>,
transaction_table: BTreeMap<Id<Transaction>, (Id<Block>, TransactionInfo)>,
ordered_transaction_table: BTreeMap<u64, Id<Transaction>>,
utxo_table: BTreeMap<UtxoOutPoint, BTreeMap<BlockHeight, Utxo>>,
address_utxos: BTreeMap<String, BTreeSet<UtxoOutPoint>>,
locked_utxo_table: BTreeMap<UtxoOutPoint, BTreeMap<BlockHeight, LockedUtxo>>,
Expand Down Expand Up @@ -78,6 +79,7 @@ impl ApiServerInMemoryStorage {
main_chain_blocks_table: BTreeMap::new(),
pool_data_table: BTreeMap::new(),
transaction_table: BTreeMap::new(),
ordered_transaction_table: BTreeMap::new(),
utxo_table: BTreeMap::new(),
address_utxos: BTreeMap::new(),
locked_utxo_table: BTreeMap::new(),
Expand Down Expand Up @@ -194,11 +196,11 @@ impl ApiServerInMemoryStorage {
}))
}

fn get_transactions_with_block(
fn get_transactions_with_block_info(
&self,
len: u32,
offset: u32,
) -> Result<Vec<(BlockAuxData, TransactionInfo)>, ApiServerStorageError> {
offset: u64,
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
Ok(self
.main_chain_blocks_table
.values()
Expand All @@ -208,13 +210,21 @@ impl ApiServerInMemoryStorage {
let block = self.block_table.get(block_id).expect("must exist");
block.block.transactions().iter().zip(block.tx_additional_infos.iter()).map(
|(tx, additinal_data)| {
(
*block_aux,
TransactionInfo {
let tx_global_index = self
.ordered_transaction_table
.iter()
.find(|(_, tx_id)| **tx_id == tx.transaction().get_id())
.expect("must exist")
.0;

TransactionWithBlockInfo {
tx_info: TransactionInfo {
tx: tx.clone(),
additional_info: additinal_data.clone(),
},
)
block_aux: *block_aux,
global_tx_index: *tx_global_index,
}
},
)
})
Expand All @@ -223,6 +233,32 @@ impl ApiServerInMemoryStorage {
.collect())
}

fn get_transactions_with_block_before_tx_global_index(
&self,
len: u32,
tx_global_index: u64,
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
Ok(self
.ordered_transaction_table
.range(..tx_global_index)
.rev()
.take(len as usize)
.map(|(tx_global_index, tx_id)| {
let (block_id, tx_info) = self.transaction_table.get(tx_id).expect("must exist");
let block_aux = self.block_aux_data_table.get(block_id).expect("must exist");
TransactionWithBlockInfo {
tx_info: tx_info.clone(),
block_aux: *block_aux,
global_tx_index: *tx_global_index,
}
})
.collect())
}

fn get_last_transaction_global_indeex(&self) -> Result<Option<u64>, ApiServerStorageError> {
Ok(self.ordered_transaction_table.keys().last().copied())
}

#[allow(clippy::type_complexity)]
fn get_transaction_with_block(
&self,
Expand Down Expand Up @@ -380,7 +416,7 @@ impl ApiServerInMemoryStorage {
fn get_orders_by_height(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
let len = len as usize;
let offset = offset as usize;
Expand Down Expand Up @@ -413,7 +449,7 @@ impl ApiServerInMemoryStorage {
&self,
pair: (CoinOrTokenId, CoinOrTokenId),
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
let len = len as usize;
let offset = offset as usize;
Expand Down Expand Up @@ -447,7 +483,7 @@ impl ApiServerInMemoryStorage {
fn get_latest_pool_ids(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
let len = len as usize;
let offset = offset as usize;
Expand Down Expand Up @@ -478,7 +514,7 @@ impl ApiServerInMemoryStorage {
fn get_pool_data_with_largest_staker_balance(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
let len = len as usize;
let offset = offset as usize;
Expand Down Expand Up @@ -671,7 +707,7 @@ impl ApiServerInMemoryStorage {
.or_else(|| self.nft_token_issuances.get(&token_id).map(|_| 0)))
}

fn get_token_ids(&self, len: u32, offset: u32) -> Result<Vec<TokenId>, ApiServerStorageError> {
fn get_token_ids(&self, len: u32, offset: u64) -> Result<Vec<TokenId>, ApiServerStorageError> {
Ok(self
.fungible_token_data
.keys()
Expand All @@ -685,7 +721,7 @@ impl ApiServerInMemoryStorage {
fn get_token_ids_by_ticker(
&self,
len: u32,
offset: u32,
offset: u64,
ticker: &[u8],
) -> Result<Vec<TokenId>, ApiServerStorageError> {
Ok(self
Expand Down Expand Up @@ -971,6 +1007,7 @@ impl ApiServerInMemoryStorage {
fn set_transaction(
&mut self,
transaction_id: Id<Transaction>,
tx_global_index: u64,
owning_block: Id<Block>,
transaction: &TransactionInfo,
) -> Result<(), ApiServerStorageError> {
Expand All @@ -983,6 +1020,7 @@ impl ApiServerInMemoryStorage {

self.transaction_table
.insert(transaction_id, (owning_block, transaction.clone()));
self.ordered_transaction_table.insert(tx_global_index, transaction_id);
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +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, Utxo, UtxoWithExtraInfo,
PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, TransactionWithBlockInfo, Utxo,
UtxoWithExtraInfo,
};

use super::ApiServerInMemoryStorageTransactionalRo;
Expand Down Expand Up @@ -88,12 +89,27 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
self.transaction.get_transaction_with_block(transaction_id)
}

async fn get_transactions_with_block(
async fn get_transactions_with_block_info(
&self,
len: u32,
offset: u32,
) -> Result<Vec<(BlockAuxData, TransactionInfo)>, ApiServerStorageError> {
self.transaction.get_transactions_with_block(len, offset)
offset: u64,
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the function should now be called "get_transactions_with_block_info"?
Same for get_transactions_with_block_by_order_number.

self.transaction.get_transactions_with_block_info(len, offset)
}

async fn get_transactions_with_block_info_before_tx_global_index(
&self,
len: u32,
tx_global_index: u64,
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
self.transaction
.get_transactions_with_block_before_tx_global_index(len, tx_global_index)
}

async fn get_last_transaction_global_index(
&self,
) -> Result<Option<u64>, ApiServerStorageError> {
self.transaction.get_last_transaction_global_indeex()
}

async fn get_delegation(
Expand Down Expand Up @@ -121,15 +137,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
async fn get_latest_pool_data(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
self.transaction.get_latest_pool_ids(len, offset)
}

async fn get_pool_data_with_largest_staker_balance(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
self.transaction.get_pool_data_with_largest_staker_balance(len, offset)
}
Expand Down Expand Up @@ -243,15 +259,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
async fn get_token_ids(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<TokenId>, ApiServerStorageError> {
self.transaction.get_token_ids(len, offset)
}

async fn get_token_ids_by_ticker(
&self,
len: u32,
offset: u32,
offset: u64,
ticker: &[u8],
) -> Result<Vec<TokenId>, ApiServerStorageError> {
self.transaction.get_token_ids_by_ticker(len, offset, ticker)
Expand Down Expand Up @@ -279,7 +295,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
async fn get_all_orders(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
self.transaction.get_orders_by_height(len, offset)
}
Expand All @@ -288,7 +304,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
&self,
pair: (CoinOrTokenId, CoinOrTokenId),
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
self.transaction.get_orders_for_trading_pair(pair, len, offset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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, Utxo, UtxoWithExtraInfo,
Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, TransactionWithBlockInfo, Utxo,
UtxoWithExtraInfo,
};
use common::{
address::Address,
Expand Down Expand Up @@ -125,10 +126,12 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn set_transaction(
&mut self,
transaction_id: Id<Transaction>,
order_number: u64,
owning_block: Id<Block>,
transaction: &TransactionInfo,
) -> Result<(), ApiServerStorageError> {
self.transaction.set_transaction(transaction_id, owning_block, transaction)
self.transaction
.set_transaction(transaction_id, order_number, owning_block, transaction)
}

async fn set_block_aux_data(
Expand Down Expand Up @@ -395,12 +398,27 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
self.transaction.get_transaction_with_block(transaction_id)
}

async fn get_transactions_with_block(
async fn get_transactions_with_block_info(
&self,
len: u32,
offset: u64,
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
self.transaction.get_transactions_with_block_info(len, offset)
}

async fn get_transactions_with_block_info_before_tx_global_index(
&self,
len: u32,
offset: u32,
) -> Result<Vec<(BlockAuxData, TransactionInfo)>, ApiServerStorageError> {
self.transaction.get_transactions_with_block(len, offset)
tx_global_index: u64,
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
self.transaction
.get_transactions_with_block_before_tx_global_index(len, tx_global_index)
}

async fn get_last_transaction_global_index(
&self,
) -> Result<Option<u64>, ApiServerStorageError> {
self.transaction.get_last_transaction_global_indeex()
}

async fn get_pool_data(
Expand All @@ -413,15 +431,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn get_latest_pool_data(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
self.transaction.get_latest_pool_ids(len, offset)
}

async fn get_pool_data_with_largest_staker_balance(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
self.transaction.get_pool_data_with_largest_staker_balance(len, offset)
}
Expand Down Expand Up @@ -500,15 +518,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn get_token_ids(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<TokenId>, ApiServerStorageError> {
self.transaction.get_token_ids(len, offset)
}

async fn get_token_ids_by_ticker(
&self,
len: u32,
offset: u32,
offset: u64,
ticker: &[u8],
) -> Result<Vec<TokenId>, ApiServerStorageError> {
self.transaction.get_token_ids_by_ticker(len, offset, ticker)
Expand Down Expand Up @@ -536,7 +554,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn get_all_orders(
&self,
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
self.transaction.get_orders_by_height(len, offset)
}
Expand All @@ -545,7 +563,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
&self,
pair: (CoinOrTokenId, CoinOrTokenId),
len: u32,
offset: u32,
offset: u64,
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
self.transaction.get_orders_for_trading_pair(pair, len, offset)
}
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 = 21;
pub const CURRENT_STORAGE_VERSION: u32 = 22;

pub mod in_memory;
pub mod postgres;
Loading