Skip to content

Commit 0f1d451

Browse files
committed
fix comments
1 parent 50b1af5 commit 0f1d451

File tree

14 files changed

+771
-82
lines changed

14 files changed

+771
-82
lines changed

api-server/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/
88

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

1213
### Changed
1314
- `/v2/token/ticker/{ticker}` will now return all tokens whose ticker has the specified `{ticker}`

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct ApiServerInMemoryStorage {
4848
address_balance_table: BTreeMap<String, BTreeMap<CoinOrTokenId, BTreeMap<BlockHeight, Amount>>>,
4949
address_locked_balance_table: BTreeMap<String, BTreeMap<(CoinOrTokenId, BlockHeight), Amount>>,
5050
address_transactions_table: BTreeMap<String, BTreeMap<BlockHeight, Vec<Id<Transaction>>>>,
51-
token_transactions_table: BTreeMap<TokenId, BTreeMap<BlockHeight, Vec<TokenTransaction>>>,
51+
token_transactions_table: BTreeMap<TokenId, BTreeMap<BlockHeight, BTreeSet<TokenTransaction>>>,
5252
delegation_table: BTreeMap<DelegationId, BTreeMap<BlockHeight, Delegation>>,
5353
main_chain_blocks_table: BTreeMap<BlockHeight, Id<Block>>,
5454
pool_data_table: BTreeMap<PoolId, BTreeMap<BlockHeight, PoolDataWithExtraInfo>>,
@@ -179,7 +179,7 @@ impl ApiServerInMemoryStorage {
179179
&self,
180180
token_id: TokenId,
181181
len: u32,
182-
global_tx_index: i64,
182+
tx_global_index: u64,
183183
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
184184
Ok(self
185185
.token_transactions_table
@@ -190,7 +190,7 @@ impl ApiServerInMemoryStorage {
190190
.rev()
191191
.flat_map(|(_, txs)| txs.iter())
192192
.cloned()
193-
.flat_map(|tx| (tx.global_tx_index < global_tx_index).then_some(tx))
193+
.flat_map(|tx| (tx.tx_global_index < tx_global_index).then_some(tx))
194194
.take(len as usize)
195195
.collect()
196196
}))
@@ -237,7 +237,7 @@ impl ApiServerInMemoryStorage {
237237
additional_info: additinal_data.clone(),
238238
},
239239
block_aux: *block_aux,
240-
global_tx_index: *tx_global_index,
240+
tx_global_index: *tx_global_index,
241241
}
242242
},
243243
)
@@ -263,7 +263,7 @@ impl ApiServerInMemoryStorage {
263263
TransactionWithBlockInfo {
264264
tx_info: tx_info.clone(),
265265
block_aux: *block_aux,
266-
global_tx_index: *tx_global_index,
266+
tx_global_index: *tx_global_index,
267267
}
268268
})
269269
.collect())
@@ -985,26 +985,30 @@ impl ApiServerInMemoryStorage {
985985
transaction_ids: BTreeSet<Id<Transaction>>,
986986
block_height: BlockHeight,
987987
) -> Result<(), ApiServerStorageError> {
988-
let next_tx_idx = self.token_transactions_table.get(&token_id).map_or(1, |tx| {
989-
tx.values()
990-
.last()
991-
.expect("not empty")
992-
.last()
993-
.expect("not empty")
994-
.global_tx_index
995-
+ 1
996-
});
997-
self.token_transactions_table.entry(token_id).or_default().insert(
998-
block_height,
999-
transaction_ids
1000-
.into_iter()
1001-
.enumerate()
1002-
.map(|(idx, tx_id)| TokenTransaction {
1003-
global_tx_index: next_tx_idx + idx as i64,
988+
if transaction_ids.is_empty() {
989+
return Ok(());
990+
}
991+
992+
let next_tx_idx = self
993+
.token_transactions_table
994+
.values()
995+
.flat_map(|by_height| by_height.values())
996+
.flat_map(|tx_set| tx_set.iter())
997+
.map(|tx| tx.tx_global_index + 1)
998+
.max()
999+
.unwrap_or(0);
1000+
1001+
self.token_transactions_table
1002+
.entry(token_id)
1003+
.or_default()
1004+
.entry(block_height)
1005+
.or_default()
1006+
.extend(
1007+
transaction_ids.into_iter().enumerate().map(|(idx, tx_id)| TokenTransaction {
1008+
tx_global_index: next_tx_idx + idx as u64,
10041009
tx_id,
1005-
})
1006-
.collect(),
1007-
);
1010+
}),
1011+
);
10081012

10091013
Ok(())
10101014
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
7272
&self,
7373
token_id: TokenId,
7474
len: u32,
75-
global_tx_index: i64,
75+
tx_global_index: u64,
7676
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
77-
self.transaction.get_token_transactions(token_id, len, global_tx_index)
77+
self.transaction.get_token_transactions(token_id, len, tx_global_index)
7878
}
7979

8080
async fn get_block(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
352352
&self,
353353
token_id: TokenId,
354354
len: u32,
355-
global_tx_index: i64,
355+
tx_global_index: u64,
356356
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
357-
self.transaction.get_token_transactions(token_id, len, global_tx_index)
357+
self.transaction.get_token_transactions(token_id, len, tx_global_index)
358358
}
359359

360360
async fn get_latest_blocktimestamps(

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 = 23;
16+
pub const CURRENT_STORAGE_VERSION: u32 = 24;
1717

1818
pub mod in_memory;
1919
pub mod postgres;

api-server/api-server-common/src/storage/impls/postgres/queries.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ impl ApiServerStorageRead for ApiServerPostgresTransactionalRo<'_> {
9898
&self,
9999
token_id: TokenId,
100100
len: u32,
101-
global_tx_index: i64,
101+
tx_global_index: u64,
102102
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
103103
let conn = QueryFromConnection::new(self.connection.as_ref().expect(CONN_ERR));
104-
let res = conn.get_token_transactions(token_id, len, global_tx_index).await?;
104+
let res = conn.get_token_transactions(token_id, len, tx_global_index).await?;
105105

106106
Ok(res)
107107
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,10 @@ impl ApiServerStorageRead for ApiServerPostgresTransactionalRw<'_> {
461461
&self,
462462
token_id: TokenId,
463463
len: u32,
464-
global_tx_index: i64,
464+
tx_global_index: u64,
465465
) -> Result<Vec<TokenTransaction>, ApiServerStorageError> {
466466
let conn = QueryFromConnection::new(self.connection.as_ref().expect(CONN_ERR));
467-
let res = conn.get_token_transactions(token_id, len, global_tx_index).await?;
467+
let res = conn.get_token_transactions(token_id, len, tx_global_index).await?;
468468

469469
Ok(res)
470470
}

0 commit comments

Comments
 (0)