Skip to content

Commit 36ed048

Browse files
authored
Merge pull request #1930 from mintlayer/feature/api-server-tx-order-num
Add tx order number and pagination
2 parents cd6fedd + e8d0b45 commit 36ed048

16 files changed

Lines changed: 614 additions & 156 deletions

File tree

api-server/api-server-common/src/storage/impls/in_memory/mod.rs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::storage::storage_api::{
1919
block_aux_data::{BlockAuxData, BlockWithExtraData},
2020
AmountWithDecimals, ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation,
2121
FungibleTokenData, LockedUtxo, NftWithOwner, Order, PoolBlockStats, PoolDataWithExtraInfo,
22-
TransactionInfo, Utxo, UtxoLock, UtxoWithExtraInfo,
22+
TransactionInfo, TransactionWithBlockInfo, Utxo, UtxoLock, UtxoWithExtraInfo,
2323
};
2424
use common::{
2525
address::Address,
@@ -29,7 +29,7 @@ use common::{
2929
Block, ChainConfig, DelegationId, Destination, Genesis, OrderId, PoolId, Transaction,
3030
UtxoOutPoint,
3131
},
32-
primitives::{id::WithId, Amount, BlockHeight, CoinOrTokenId, Id},
32+
primitives::{id::WithId, Amount, BlockHeight, CoinOrTokenId, Id, Idable},
3333
};
3434
use std::{
3535
cmp::Reverse,
@@ -51,6 +51,7 @@ struct ApiServerInMemoryStorage {
5151
main_chain_blocks_table: BTreeMap<BlockHeight, Id<Block>>,
5252
pool_data_table: BTreeMap<PoolId, BTreeMap<BlockHeight, PoolDataWithExtraInfo>>,
5353
transaction_table: BTreeMap<Id<Transaction>, (Id<Block>, TransactionInfo)>,
54+
ordered_transaction_table: BTreeMap<u64, Id<Transaction>>,
5455
utxo_table: BTreeMap<UtxoOutPoint, BTreeMap<BlockHeight, Utxo>>,
5556
address_utxos: BTreeMap<String, BTreeSet<UtxoOutPoint>>,
5657
locked_utxo_table: BTreeMap<UtxoOutPoint, BTreeMap<BlockHeight, LockedUtxo>>,
@@ -78,6 +79,7 @@ impl ApiServerInMemoryStorage {
7879
main_chain_blocks_table: BTreeMap::new(),
7980
pool_data_table: BTreeMap::new(),
8081
transaction_table: BTreeMap::new(),
82+
ordered_transaction_table: BTreeMap::new(),
8183
utxo_table: BTreeMap::new(),
8284
address_utxos: BTreeMap::new(),
8385
locked_utxo_table: BTreeMap::new(),
@@ -194,11 +196,11 @@ impl ApiServerInMemoryStorage {
194196
}))
195197
}
196198

197-
fn get_transactions_with_block(
199+
fn get_transactions_with_block_info(
198200
&self,
199201
len: u32,
200-
offset: u32,
201-
) -> Result<Vec<(BlockAuxData, TransactionInfo)>, ApiServerStorageError> {
202+
offset: u64,
203+
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
202204
Ok(self
203205
.main_chain_blocks_table
204206
.values()
@@ -208,13 +210,21 @@ impl ApiServerInMemoryStorage {
208210
let block = self.block_table.get(block_id).expect("must exist");
209211
block.block.transactions().iter().zip(block.tx_additional_infos.iter()).map(
210212
|(tx, additinal_data)| {
211-
(
212-
*block_aux,
213-
TransactionInfo {
213+
let tx_global_index = self
214+
.ordered_transaction_table
215+
.iter()
216+
.find(|(_, tx_id)| **tx_id == tx.transaction().get_id())
217+
.expect("must exist")
218+
.0;
219+
220+
TransactionWithBlockInfo {
221+
tx_info: TransactionInfo {
214222
tx: tx.clone(),
215223
additional_info: additinal_data.clone(),
216224
},
217-
)
225+
block_aux: *block_aux,
226+
global_tx_index: *tx_global_index,
227+
}
218228
},
219229
)
220230
})
@@ -223,6 +233,32 @@ impl ApiServerInMemoryStorage {
223233
.collect())
224234
}
225235

236+
fn get_transactions_with_block_before_tx_global_index(
237+
&self,
238+
len: u32,
239+
tx_global_index: u64,
240+
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
241+
Ok(self
242+
.ordered_transaction_table
243+
.range(..tx_global_index)
244+
.rev()
245+
.take(len as usize)
246+
.map(|(tx_global_index, tx_id)| {
247+
let (block_id, tx_info) = self.transaction_table.get(tx_id).expect("must exist");
248+
let block_aux = self.block_aux_data_table.get(block_id).expect("must exist");
249+
TransactionWithBlockInfo {
250+
tx_info: tx_info.clone(),
251+
block_aux: *block_aux,
252+
global_tx_index: *tx_global_index,
253+
}
254+
})
255+
.collect())
256+
}
257+
258+
fn get_last_transaction_global_indeex(&self) -> Result<Option<u64>, ApiServerStorageError> {
259+
Ok(self.ordered_transaction_table.keys().last().copied())
260+
}
261+
226262
#[allow(clippy::type_complexity)]
227263
fn get_transaction_with_block(
228264
&self,
@@ -380,7 +416,7 @@ impl ApiServerInMemoryStorage {
380416
fn get_orders_by_height(
381417
&self,
382418
len: u32,
383-
offset: u32,
419+
offset: u64,
384420
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
385421
let len = len as usize;
386422
let offset = offset as usize;
@@ -413,7 +449,7 @@ impl ApiServerInMemoryStorage {
413449
&self,
414450
pair: (CoinOrTokenId, CoinOrTokenId),
415451
len: u32,
416-
offset: u32,
452+
offset: u64,
417453
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
418454
let len = len as usize;
419455
let offset = offset as usize;
@@ -447,7 +483,7 @@ impl ApiServerInMemoryStorage {
447483
fn get_latest_pool_ids(
448484
&self,
449485
len: u32,
450-
offset: u32,
486+
offset: u64,
451487
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
452488
let len = len as usize;
453489
let offset = offset as usize;
@@ -478,7 +514,7 @@ impl ApiServerInMemoryStorage {
478514
fn get_pool_data_with_largest_staker_balance(
479515
&self,
480516
len: u32,
481-
offset: u32,
517+
offset: u64,
482518
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
483519
let len = len as usize;
484520
let offset = offset as usize;
@@ -671,7 +707,7 @@ impl ApiServerInMemoryStorage {
671707
.or_else(|| self.nft_token_issuances.get(&token_id).map(|_| 0)))
672708
}
673709

674-
fn get_token_ids(&self, len: u32, offset: u32) -> Result<Vec<TokenId>, ApiServerStorageError> {
710+
fn get_token_ids(&self, len: u32, offset: u64) -> Result<Vec<TokenId>, ApiServerStorageError> {
675711
Ok(self
676712
.fungible_token_data
677713
.keys()
@@ -685,7 +721,7 @@ impl ApiServerInMemoryStorage {
685721
fn get_token_ids_by_ticker(
686722
&self,
687723
len: u32,
688-
offset: u32,
724+
offset: u64,
689725
ticker: &[u8],
690726
) -> Result<Vec<TokenId>, ApiServerStorageError> {
691727
Ok(self
@@ -971,6 +1007,7 @@ impl ApiServerInMemoryStorage {
9711007
fn set_transaction(
9721008
&mut self,
9731009
transaction_id: Id<Transaction>,
1010+
tx_global_index: u64,
9741011
owning_block: Id<Block>,
9751012
transaction: &TransactionInfo,
9761013
) -> Result<(), ApiServerStorageError> {
@@ -983,6 +1020,7 @@ impl ApiServerInMemoryStorage {
9831020

9841021
self.transaction_table
9851022
.insert(transaction_id, (owning_block, transaction.clone()));
1023+
self.ordered_transaction_table.insert(tx_global_index, transaction_id);
9861024
Ok(())
9871025
}
9881026

api-server/api-server-common/src/storage/impls/in_memory/transactional/read.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use common::{
2626
use crate::storage::storage_api::{
2727
block_aux_data::BlockAuxData, AmountWithDecimals, ApiServerStorageError, ApiServerStorageRead,
2828
BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, NftWithOwner, Order,
29-
PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, Utxo, UtxoWithExtraInfo,
29+
PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, TransactionWithBlockInfo, Utxo,
30+
UtxoWithExtraInfo,
3031
};
3132

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

91-
async fn get_transactions_with_block(
92+
async fn get_transactions_with_block_info(
9293
&self,
9394
len: u32,
94-
offset: u32,
95-
) -> Result<Vec<(BlockAuxData, TransactionInfo)>, ApiServerStorageError> {
96-
self.transaction.get_transactions_with_block(len, offset)
95+
offset: u64,
96+
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
97+
self.transaction.get_transactions_with_block_info(len, offset)
98+
}
99+
100+
async fn get_transactions_with_block_info_before_tx_global_index(
101+
&self,
102+
len: u32,
103+
tx_global_index: u64,
104+
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
105+
self.transaction
106+
.get_transactions_with_block_before_tx_global_index(len, tx_global_index)
107+
}
108+
109+
async fn get_last_transaction_global_index(
110+
&self,
111+
) -> Result<Option<u64>, ApiServerStorageError> {
112+
self.transaction.get_last_transaction_global_indeex()
97113
}
98114

99115
async fn get_delegation(
@@ -121,15 +137,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
121137
async fn get_latest_pool_data(
122138
&self,
123139
len: u32,
124-
offset: u32,
140+
offset: u64,
125141
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
126142
self.transaction.get_latest_pool_ids(len, offset)
127143
}
128144

129145
async fn get_pool_data_with_largest_staker_balance(
130146
&self,
131147
len: u32,
132-
offset: u32,
148+
offset: u64,
133149
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
134150
self.transaction.get_pool_data_with_largest_staker_balance(len, offset)
135151
}
@@ -243,15 +259,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
243259
async fn get_token_ids(
244260
&self,
245261
len: u32,
246-
offset: u32,
262+
offset: u64,
247263
) -> Result<Vec<TokenId>, ApiServerStorageError> {
248264
self.transaction.get_token_ids(len, offset)
249265
}
250266

251267
async fn get_token_ids_by_ticker(
252268
&self,
253269
len: u32,
254-
offset: u32,
270+
offset: u64,
255271
ticker: &[u8],
256272
) -> Result<Vec<TokenId>, ApiServerStorageError> {
257273
self.transaction.get_token_ids_by_ticker(len, offset, ticker)
@@ -279,7 +295,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
279295
async fn get_all_orders(
280296
&self,
281297
len: u32,
282-
offset: u32,
298+
offset: u64,
283299
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
284300
self.transaction.get_orders_by_height(len, offset)
285301
}
@@ -288,7 +304,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
288304
&self,
289305
pair: (CoinOrTokenId, CoinOrTokenId),
290306
len: u32,
291-
offset: u32,
307+
offset: u64,
292308
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
293309
self.transaction.get_orders_for_trading_pair(pair, len, offset)
294310
}

api-server/api-server-common/src/storage/impls/in_memory/transactional/write.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use crate::storage::storage_api::{
1919
block_aux_data::{BlockAuxData, BlockWithExtraData},
2020
AmountWithDecimals, ApiServerStorageError, ApiServerStorageRead, ApiServerStorageWrite,
2121
BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, LockedUtxo, NftWithOwner,
22-
Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, Utxo, UtxoWithExtraInfo,
22+
Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, TransactionWithBlockInfo, Utxo,
23+
UtxoWithExtraInfo,
2324
};
2425
use common::{
2526
address::Address,
@@ -125,10 +126,12 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
125126
async fn set_transaction(
126127
&mut self,
127128
transaction_id: Id<Transaction>,
129+
order_number: u64,
128130
owning_block: Id<Block>,
129131
transaction: &TransactionInfo,
130132
) -> Result<(), ApiServerStorageError> {
131-
self.transaction.set_transaction(transaction_id, owning_block, transaction)
133+
self.transaction
134+
.set_transaction(transaction_id, order_number, owning_block, transaction)
132135
}
133136

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

398-
async fn get_transactions_with_block(
401+
async fn get_transactions_with_block_info(
402+
&self,
403+
len: u32,
404+
offset: u64,
405+
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
406+
self.transaction.get_transactions_with_block_info(len, offset)
407+
}
408+
409+
async fn get_transactions_with_block_info_before_tx_global_index(
399410
&self,
400411
len: u32,
401-
offset: u32,
402-
) -> Result<Vec<(BlockAuxData, TransactionInfo)>, ApiServerStorageError> {
403-
self.transaction.get_transactions_with_block(len, offset)
412+
tx_global_index: u64,
413+
) -> Result<Vec<TransactionWithBlockInfo>, ApiServerStorageError> {
414+
self.transaction
415+
.get_transactions_with_block_before_tx_global_index(len, tx_global_index)
416+
}
417+
418+
async fn get_last_transaction_global_index(
419+
&self,
420+
) -> Result<Option<u64>, ApiServerStorageError> {
421+
self.transaction.get_last_transaction_global_indeex()
404422
}
405423

406424
async fn get_pool_data(
@@ -413,15 +431,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
413431
async fn get_latest_pool_data(
414432
&self,
415433
len: u32,
416-
offset: u32,
434+
offset: u64,
417435
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
418436
self.transaction.get_latest_pool_ids(len, offset)
419437
}
420438

421439
async fn get_pool_data_with_largest_staker_balance(
422440
&self,
423441
len: u32,
424-
offset: u32,
442+
offset: u64,
425443
) -> Result<Vec<(PoolId, PoolDataWithExtraInfo)>, ApiServerStorageError> {
426444
self.transaction.get_pool_data_with_largest_staker_balance(len, offset)
427445
}
@@ -500,15 +518,15 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
500518
async fn get_token_ids(
501519
&self,
502520
len: u32,
503-
offset: u32,
521+
offset: u64,
504522
) -> Result<Vec<TokenId>, ApiServerStorageError> {
505523
self.transaction.get_token_ids(len, offset)
506524
}
507525

508526
async fn get_token_ids_by_ticker(
509527
&self,
510528
len: u32,
511-
offset: u32,
529+
offset: u64,
512530
ticker: &[u8],
513531
) -> Result<Vec<TokenId>, ApiServerStorageError> {
514532
self.transaction.get_token_ids_by_ticker(len, offset, ticker)
@@ -536,7 +554,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
536554
async fn get_all_orders(
537555
&self,
538556
len: u32,
539-
offset: u32,
557+
offset: u64,
540558
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
541559
self.transaction.get_orders_by_height(len, offset)
542560
}
@@ -545,7 +563,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
545563
&self,
546564
pair: (CoinOrTokenId, CoinOrTokenId),
547565
len: u32,
548-
offset: u32,
566+
offset: u64,
549567
) -> Result<Vec<(OrderId, Order)>, ApiServerStorageError> {
550568
self.transaction.get_orders_for_trading_pair(pair, len, offset)
551569
}

api-server/api-server-common/src/storage/impls/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
pub const CURRENT_STORAGE_VERSION: u32 = 21;
16+
pub const CURRENT_STORAGE_VERSION: u32 = 22;
1717

1818
pub mod in_memory;
1919
pub mod postgres;

0 commit comments

Comments
 (0)