@@ -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} ;
2424use 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} ;
3434use 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
0 commit comments