@@ -83,11 +83,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
8383 . map_err ( |_| ApiServerStorageError :: TimestampTooHigh ( block_timestamp) )
8484 }
8585
86- fn tx_global_index_to_postgres_friendly ( tx_global_index : u64 ) -> i64 {
86+ fn tx_global_index_to_postgres_friendly (
87+ tx_global_index : u64 ,
88+ ) -> Result < i64 , ApiServerStorageError > {
8789 // Postgres doesn't like u64, so we have to convert it to i64
8890 tx_global_index
8991 . try_into ( )
90- . unwrap_or_else ( |e| panic ! ( "Invalid tx global index: {e}" ) )
92+ . map_err ( |_| ApiServerStorageError :: TxGlobalIndexTooHigh ( tx_global_index ) )
9193 }
9294
9395 pub async fn is_initialized ( & mut self ) -> Result < bool , ApiServerStorageError > {
@@ -499,28 +501,29 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
499501 & self ,
500502 token_id : TokenId ,
501503 len : u32 ,
502- global_tx_index : i64 ,
504+ tx_global_index : u64 ,
503505 ) -> Result < Vec < TokenTransaction > , ApiServerStorageError > {
506+ let tx_global_index = Self :: tx_global_index_to_postgres_friendly ( tx_global_index) ?;
504507 let len = len as i64 ;
505508 let rows = self
506509 . tx
507510 . query (
508511 r#"
509- SELECT global_tx_index , transaction_id
512+ SELECT tx_global_index , transaction_id
510513 FROM ml.token_transactions
511- WHERE token_id = $1 AND global_tx_index < $2
512- ORDER BY global_tx_index DESC
514+ WHERE token_id = $1 AND tx_global_index < $2
515+ ORDER BY tx_global_index DESC
513516 LIMIT $3;
514517 "# ,
515- & [ & token_id. encode ( ) , & global_tx_index , & len] ,
518+ & [ & token_id. encode ( ) , & tx_global_index , & len] ,
516519 )
517520 . await
518521 . map_err ( |e| ApiServerStorageError :: LowLevelStorageError ( e. to_string ( ) ) ) ?;
519522
520523 let mut transactions = Vec :: with_capacity ( rows. len ( ) ) ;
521524
522525 for row in & rows {
523- let global_tx_index : i64 = row. get ( 0 ) ;
526+ let tx_global_index : i64 = row. get ( 0 ) ;
524527 let tx_id: Vec < u8 > = row. get ( 1 ) ;
525528 let tx_id = Id :: < Transaction > :: decode_all ( & mut tx_id. as_slice ( ) ) . map_err ( |e| {
526529 ApiServerStorageError :: DeserializationError ( format ! (
@@ -530,7 +533,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
530533 } ) ?;
531534
532535 transactions. push ( TokenTransaction {
533- global_tx_index ,
536+ tx_global_index : tx_global_index as u64 ,
534537 tx_id,
535538 } ) ;
536539 }
@@ -763,7 +766,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
763766 self . just_execute (
764767 "CREATE TABLE ml.transactions (
765768 transaction_id bytea PRIMARY KEY,
766- global_tx_index bigint NOT NULL,
769+ tx_global_index bigint NOT NULL,
767770 owning_block_id bytea NOT NULL REFERENCES ml.blocks(block_id),
768771 transaction_data bytea NOT NULL
769772 );" , // block_id can be null if the transaction is not in the main chain
@@ -820,7 +823,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
820823
821824 self . just_execute (
822825 "CREATE TABLE ml.token_transactions (
823- global_tx_index bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
826+ tx_global_index bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 0 MINVALUE 0) ,
824827 token_id bytea NOT NULL,
825828 block_height bigint NOT NULL,
826829 transaction_id bytea NOT NULL,
@@ -829,7 +832,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
829832 )
830833 . await ?;
831834
832- self . just_execute ( "CREATE INDEX token_transactions_global_tx_index ON ml.token_transactions (token_id, global_tx_index DESC);" )
835+ self . just_execute ( "CREATE INDEX token_transactions_global_tx_index ON ml.token_transactions (token_id, tx_global_index DESC);" )
833836 . await ?;
834837
835838 self . just_execute (
@@ -1903,12 +1906,12 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
19031906 SELECT
19041907 t.transaction_data,
19051908 b.aux_data,
1906- t.global_tx_index
1909+ t.tx_global_index
19071910 FROM
19081911 ml.transactions t
19091912 INNER JOIN
19101913 ml.block_aux_data b ON t.owning_block_id = b.block_id
1911- ORDER BY t.global_tx_index DESC
1914+ ORDER BY t.tx_global_index DESC
19121915 OFFSET $1
19131916 LIMIT $2;
19141917 "# ,
@@ -1921,7 +1924,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
19211924 . map ( |data| {
19221925 let transaction_data: Vec < u8 > = data. get ( 0 ) ;
19231926 let block_data: Vec < u8 > = data. get ( 1 ) ;
1924- let global_tx_index : i64 = data. get ( 2 ) ;
1927+ let tx_global_index : i64 = data. get ( 2 ) ;
19251928
19261929 let block_aux =
19271930 BlockAuxData :: decode_all ( & mut block_data. as_slice ( ) ) . map_err ( |e| {
@@ -1941,7 +1944,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
19411944 Ok ( TransactionWithBlockInfo {
19421945 tx_info,
19431946 block_aux,
1944- global_tx_index : global_tx_index as u64 ,
1947+ tx_global_index : tx_global_index as u64 ,
19451948 } )
19461949 } )
19471950 . collect ( )
@@ -1950,24 +1953,24 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
19501953 pub async fn get_transactions_with_block_before_tx_global_index (
19511954 & self ,
19521955 len : u32 ,
1953- global_tx_index : u64 ,
1956+ tx_global_index : u64 ,
19541957 ) -> Result < Vec < TransactionWithBlockInfo > , ApiServerStorageError > {
19551958 let len = len as i64 ;
1956- let tx_global_index = Self :: tx_global_index_to_postgres_friendly ( global_tx_index ) ;
1959+ let tx_global_index = Self :: tx_global_index_to_postgres_friendly ( tx_global_index ) ? ;
19571960 let rows = self
19581961 . tx
19591962 . query (
19601963 r#"
19611964 SELECT
19621965 t.transaction_data,
19631966 b.aux_data,
1964- t.global_tx_index
1967+ t.tx_global_index
19651968 FROM
19661969 ml.transactions t
19671970 INNER JOIN
19681971 ml.block_aux_data b ON t.owning_block_id = b.block_id
1969- WHERE t.global_tx_index < $1
1970- ORDER BY t.global_tx_index DESC
1972+ WHERE t.tx_global_index < $1
1973+ ORDER BY t.tx_global_index DESC
19711974 LIMIT $2;
19721975 "# ,
19731976 & [ & tx_global_index, & len] ,
@@ -1979,7 +1982,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
19791982 . map ( |data| {
19801983 let transaction_data: Vec < u8 > = data. get ( 0 ) ;
19811984 let block_data: Vec < u8 > = data. get ( 1 ) ;
1982- let global_tx_index : i64 = data. get ( 2 ) ;
1985+ let tx_global_index : i64 = data. get ( 2 ) ;
19831986
19841987 let block_aux =
19851988 BlockAuxData :: decode_all ( & mut block_data. as_slice ( ) ) . map_err ( |e| {
@@ -1999,7 +2002,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
19992002 Ok ( TransactionWithBlockInfo {
20002003 tx_info,
20012004 block_aux,
2002- global_tx_index : global_tx_index as u64 ,
2005+ tx_global_index : tx_global_index as u64 ,
20032006 } )
20042007 } )
20052008 . collect ( )
@@ -2013,7 +2016,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
20132016 . query_one (
20142017 r#"
20152018 SELECT
2016- MAX(t.global_tx_index )
2019+ MAX(t.tx_global_index )
20172020 FROM
20182021 ml.transactions t;
20192022 "# ,
@@ -2030,7 +2033,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
20302033 pub async fn set_transaction (
20312034 & mut self ,
20322035 transaction_id : Id < Transaction > ,
2033- global_tx_index : u64 ,
2036+ tx_global_index : u64 ,
20342037 owning_block : Id < Block > ,
20352038 transaction : & TransactionInfo ,
20362039 ) -> Result < ( ) , ApiServerStorageError > {
@@ -2039,13 +2042,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
20392042 transaction_id,
20402043 owning_block
20412044 ) ;
2042- let global_tx_index = Self :: tx_global_index_to_postgres_friendly ( global_tx_index ) ;
2045+ let tx_global_index = Self :: tx_global_index_to_postgres_friendly ( tx_global_index ) ? ;
20432046
20442047 self . tx . execute (
2045- "INSERT INTO ml.transactions (transaction_id, owning_block_id, transaction_data, global_tx_index ) VALUES ($1, $2, $3, $4)
2048+ "INSERT INTO ml.transactions (transaction_id, owning_block_id, transaction_data, tx_global_index ) VALUES ($1, $2, $3, $4)
20462049 ON CONFLICT (transaction_id) DO UPDATE
2047- SET owning_block_id = $2, transaction_data = $3, global_tx_index = $4;" ,
2048- & [ & transaction_id. encode ( ) , & owning_block. encode ( ) , & transaction. encode ( ) , & global_tx_index ]
2050+ SET owning_block_id = $2, transaction_data = $3, tx_global_index = $4;" ,
2051+ & [ & transaction_id. encode ( ) , & owning_block. encode ( ) , & transaction. encode ( ) , & tx_global_index ]
20492052 ) . await
20502053 . map_err ( |e| ApiServerStorageError :: LowLevelStorageError ( e. to_string ( ) ) ) ?;
20512054
0 commit comments