Skip to content

Commit 4186a49

Browse files
committed
fix comments
1 parent 9babfb2 commit 4186a49

File tree

10 files changed

+195
-157
lines changed

10 files changed

+195
-157
lines changed

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

+53-53
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub mod transactional;
1717

1818
use crate::storage::storage_api::{
1919
block_aux_data::{BlockAuxData, BlockWithExtraData},
20-
ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData,
21-
LockedUtxo, NftWithOwner, Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, Utxo,
22-
UtxoLock, UtxoWithExtraInfo,
20+
AmountWithDecimals, ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation,
21+
FungibleTokenData, LockedUtxo, NftWithOwner, Order, PoolBlockStats, PoolDataWithExtraInfo,
22+
TransactionInfo, Utxo, UtxoLock, UtxoWithExtraInfo,
2323
};
2424
use common::{
2525
address::Address,
@@ -55,7 +55,7 @@ struct ApiServerInMemoryStorage {
5555
address_utxos: BTreeMap<String, BTreeSet<UtxoOutPoint>>,
5656
locked_utxo_table: BTreeMap<UtxoOutPoint, BTreeMap<BlockHeight, LockedUtxo>>,
5757
address_locked_utxos: BTreeMap<String, BTreeSet<UtxoOutPoint>>,
58-
fungible_token_issuances: BTreeMap<TokenId, BTreeMap<BlockHeight, FungibleTokenData>>,
58+
fungible_token_data: BTreeMap<TokenId, BTreeMap<BlockHeight, FungibleTokenData>>,
5959
nft_token_issuances: BTreeMap<TokenId, BTreeMap<BlockHeight, NftWithOwner>>,
6060
statistics:
6161
BTreeMap<CoinOrTokenStatistic, BTreeMap<CoinOrTokenId, BTreeMap<BlockHeight, Amount>>>,
@@ -82,7 +82,7 @@ impl ApiServerInMemoryStorage {
8282
address_utxos: BTreeMap::new(),
8383
locked_utxo_table: BTreeMap::new(),
8484
address_locked_utxos: BTreeMap::new(),
85-
fungible_token_issuances: BTreeMap::new(),
85+
fungible_token_data: BTreeMap::new(),
8686
nft_token_issuances: BTreeMap::new(),
8787
statistics: BTreeMap::new(),
8888
orders_table: BTreeMap::new(),
@@ -122,36 +122,33 @@ impl ApiServerInMemoryStorage {
122122
fn get_address_balances(
123123
&self,
124124
address: &str,
125-
) -> Result<Vec<(CoinOrTokenId, Amount, u8)>, ApiServerStorageError> {
126-
let res =
127-
self.address_balance_table
128-
.get(address)
129-
.map_or_else(Vec::new, |by_coin_or_token| {
130-
by_coin_or_token
131-
.iter()
132-
.map(|(coin_or_token_id, by_height)| {
133-
let number_of_decimals = match coin_or_token_id {
134-
CoinOrTokenId::Coin => self.number_of_coin_decimals,
135-
CoinOrTokenId::TokenId(token_id) => self
136-
.fungible_token_issuances
137-
.get(token_id)
138-
.map_or(0, |by_height| {
139-
by_height
140-
.values()
141-
.last()
142-
.expect("not empty")
143-
.number_of_decimals
144-
}),
145-
};
146-
147-
(
148-
*coin_or_token_id,
149-
*by_height.values().last().expect("not empty"),
150-
number_of_decimals,
151-
)
152-
})
153-
.collect()
154-
});
125+
) -> Result<BTreeMap<CoinOrTokenId, AmountWithDecimals>, ApiServerStorageError> {
126+
let res = self.address_balance_table.get(address).map_or_else(
127+
BTreeMap::new,
128+
|by_coin_or_token| {
129+
by_coin_or_token
130+
.iter()
131+
.map(|(coin_or_token_id, by_height)| {
132+
let number_of_decimals = match coin_or_token_id {
133+
CoinOrTokenId::Coin => self.number_of_coin_decimals,
134+
CoinOrTokenId::TokenId(token_id) => {
135+
self.fungible_token_data.get(token_id).map_or(0, |by_height| {
136+
by_height.values().last().expect("not empty").number_of_decimals
137+
})
138+
}
139+
};
140+
141+
(
142+
*coin_or_token_id,
143+
AmountWithDecimals {
144+
amount: *by_height.values().last().expect("not empty"),
145+
decimals: number_of_decimals,
146+
},
147+
)
148+
})
149+
.collect()
150+
},
151+
);
155152
Ok(res)
156153
}
157154

@@ -631,7 +628,7 @@ impl ApiServerInMemoryStorage {
631628
token_id: TokenId,
632629
) -> Result<Option<FungibleTokenData>, ApiServerStorageError> {
633630
Ok(self
634-
.fungible_token_issuances
631+
.fungible_token_data
635632
.get(&token_id)
636633
.map(|by_height| by_height.values().last().cloned().expect("not empty")))
637634
}
@@ -651,15 +648,15 @@ impl ApiServerInMemoryStorage {
651648
token_id: TokenId,
652649
) -> Result<Option<u8>, ApiServerStorageError> {
653650
Ok(self
654-
.fungible_token_issuances
651+
.fungible_token_data
655652
.get(&token_id)
656653
.map(|data| data.values().last().expect("not empty").number_of_decimals)
657654
.or_else(|| self.nft_token_issuances.get(&token_id).map(|_| 0)))
658655
}
659656

660657
fn get_token_ids(&self, len: u32, offset: u32) -> Result<Vec<TokenId>, ApiServerStorageError> {
661658
Ok(self
662-
.fungible_token_issuances
659+
.fungible_token_data
663660
.keys()
664661
.chain(self.nft_token_issuances.keys())
665662
.skip(offset as usize)
@@ -675,7 +672,7 @@ impl ApiServerInMemoryStorage {
675672
ticker: &[u8],
676673
) -> Result<Vec<TokenId>, ApiServerStorageError> {
677674
Ok(self
678-
.fungible_token_issuances
675+
.fungible_token_data
679676
.iter()
680677
.filter_map(|(key, value)| {
681678
(value.values().last().expect("not empty").token_ticker == ticker).then_some(key)
@@ -772,7 +769,7 @@ impl ApiServerInMemoryStorage {
772769
self.transaction_table.clear();
773770
self.utxo_table.clear();
774771
self.address_utxos.clear();
775-
self.fungible_token_issuances.clear();
772+
self.fungible_token_data.clear();
776773
self.nft_token_issuances.clear();
777774
self.orders_table.clear();
778775

@@ -1108,16 +1105,13 @@ impl ApiServerInMemoryStorage {
11081105
Ok(())
11091106
}
11101107

1111-
fn set_fungible_token_issuance(
1108+
fn set_fungible_token_data(
11121109
&mut self,
11131110
token_id: TokenId,
11141111
block_height: BlockHeight,
1115-
issuance: FungibleTokenData,
1112+
data: FungibleTokenData,
11161113
) -> Result<(), ApiServerStorageError> {
1117-
self.fungible_token_issuances
1118-
.entry(token_id)
1119-
.or_default()
1120-
.insert(block_height, issuance);
1114+
self.fungible_token_data.entry(token_id).or_default().insert(block_height, data);
11211115
Ok(())
11221116
}
11231117

@@ -1128,21 +1122,27 @@ impl ApiServerInMemoryStorage {
11281122
issuance: NftIssuance,
11291123
owner: &Destination,
11301124
) -> Result<(), ApiServerStorageError> {
1131-
self.nft_token_issuances.entry(token_id).or_default().insert(
1132-
block_height,
1133-
NftWithOwner {
1134-
nft: issuance,
1135-
owner: Some(owner.clone()),
1136-
},
1125+
let res = self.nft_token_issuances.insert(
1126+
token_id,
1127+
BTreeMap::from([(
1128+
block_height,
1129+
NftWithOwner {
1130+
nft: issuance,
1131+
owner: Some(owner.clone()),
1132+
},
1133+
)]),
11371134
);
1135+
1136+
assert!(res.is_none(), "multiple nft issuances with same token_id");
1137+
11381138
Ok(())
11391139
}
11401140

11411141
fn del_token_issuance_above_height(
11421142
&mut self,
11431143
block_height: BlockHeight,
11441144
) -> Result<(), ApiServerStorageError> {
1145-
self.fungible_token_issuances.retain(|_, v| {
1145+
self.fungible_token_data.retain(|_, v| {
11461146
v.retain(|k, _| k <= &block_height);
11471147
!v.is_empty()
11481148
});

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use common::{
2424
};
2525

2626
use crate::storage::storage_api::{
27-
block_aux_data::BlockAuxData, ApiServerStorageError, ApiServerStorageRead, BlockInfo,
28-
CoinOrTokenStatistic, Delegation, FungibleTokenData, NftWithOwner, Order, PoolBlockStats,
29-
PoolDataWithExtraInfo, TransactionInfo, Utxo, UtxoWithExtraInfo,
27+
block_aux_data::BlockAuxData, AmountWithDecimals, ApiServerStorageError, ApiServerStorageRead,
28+
BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, NftWithOwner, Order,
29+
PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, Utxo, UtxoWithExtraInfo,
3030
};
3131

3232
use super::ApiServerInMemoryStorageTransactionalRo;
@@ -48,7 +48,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
4848
async fn get_address_balances(
4949
&self,
5050
address: &str,
51-
) -> Result<Vec<(CoinOrTokenId, Amount, u8)>, ApiServerStorageError> {
51+
) -> Result<BTreeMap<CoinOrTokenId, AmountWithDecimals>, ApiServerStorageError> {
5252
self.transaction.get_address_balances(address)
5353
}
5454

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use std::collections::{BTreeMap, BTreeSet};
1717

1818
use crate::storage::storage_api::{
1919
block_aux_data::{BlockAuxData, BlockWithExtraData},
20-
ApiServerStorageError, ApiServerStorageRead, ApiServerStorageWrite, BlockInfo,
21-
CoinOrTokenStatistic, Delegation, FungibleTokenData, LockedUtxo, NftWithOwner, Order,
22-
PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, Utxo, UtxoWithExtraInfo,
20+
AmountWithDecimals, ApiServerStorageError, ApiServerStorageRead, ApiServerStorageWrite,
21+
BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, LockedUtxo, NftWithOwner,
22+
Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo, Utxo, UtxoWithExtraInfo,
2323
};
2424
use common::{
2525
address::Address,
@@ -210,7 +210,16 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
210210
block_height: BlockHeight,
211211
issuance: FungibleTokenData,
212212
) -> Result<(), ApiServerStorageError> {
213-
self.transaction.set_fungible_token_issuance(token_id, block_height, issuance)
213+
self.transaction.set_fungible_token_data(token_id, block_height, issuance)
214+
}
215+
216+
async fn set_fungible_token_data(
217+
&mut self,
218+
token_id: TokenId,
219+
block_height: BlockHeight,
220+
data: FungibleTokenData,
221+
) -> Result<(), ApiServerStorageError> {
222+
self.transaction.set_fungible_token_data(token_id, block_height, data)
214223
}
215224

216225
async fn set_nft_token_issuance(
@@ -300,7 +309,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
300309
async fn get_address_balances(
301310
&self,
302311
address: &str,
303-
) -> Result<Vec<(CoinOrTokenId, Amount, u8)>, ApiServerStorageError> {
312+
) -> Result<BTreeMap<CoinOrTokenId, AmountWithDecimals>, ApiServerStorageError> {
304313
self.transaction.get_address_balances(address)
305314
}
306315

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

+52-13
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ use crate::storage::{
3737
impls::CURRENT_STORAGE_VERSION,
3838
storage_api::{
3939
block_aux_data::{BlockAuxData, BlockWithExtraData},
40-
ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData,
41-
LockedUtxo, NftWithOwner, Order, PoolBlockStats, PoolDataWithExtraInfo, TransactionInfo,
42-
Utxo, UtxoWithExtraInfo,
40+
AmountWithDecimals, ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation,
41+
FungibleTokenData, LockedUtxo, NftWithOwner, Order, PoolBlockStats, PoolDataWithExtraInfo,
42+
TransactionInfo, Utxo, UtxoWithExtraInfo,
4343
},
4444
};
4545

@@ -172,7 +172,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
172172
pub async fn get_address_balances(
173173
&self,
174174
address: &str,
175-
) -> Result<Vec<(CoinOrTokenId, Amount, u8)>, ApiServerStorageError> {
175+
) -> Result<BTreeMap<CoinOrTokenId, AmountWithDecimals>, ApiServerStorageError> {
176176
let rows = self
177177
.tx
178178
.query(
@@ -207,7 +207,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
207207
))
208208
})?;
209209

210-
Ok((coin_or_token_id, amount, number_of_decimals as u8))
210+
Ok((
211+
coin_or_token_id,
212+
AmountWithDecimals {
213+
amount,
214+
decimals: number_of_decimals as u8,
215+
},
216+
))
211217
})
212218
.collect()
213219
}
@@ -339,7 +345,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
339345
LIMIT 1)
340346
)
341347
ON CONFLICT (address, coin_or_token_id) DO UPDATE
342-
SET block_height = $2, amount = $4;
348+
SET block_height = EXCLUDED.block_height, amount = EXCLUDED.amount
349+
WHERE ml.latest_address_balance_cache.block_height < EXCLUDED.block_height;
343350
"#,
344351
&[&address.to_string(), &height, &coin_or_token_id.encode(), &amount.encode()],
345352
)
@@ -352,7 +359,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
352359
INSERT INTO ml.address_balance (address, block_height, coin_or_token_id, amount)
353360
VALUES ($1, $2, $3, $4)
354361
ON CONFLICT (address, block_height, coin_or_token_id)
355-
DO UPDATE SET amount = $4;
362+
DO UPDATE SET amount = EXCLUDED.amount;
356363
"#,
357364
&[&address.to_string(), &height, &coin_or_token_id.encode(), &amount.encode()],
358365
)
@@ -1277,7 +1284,12 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
12771284
INSERT INTO ml.latest_delegations_cache (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height)
12781285
VALUES($1, $2, $3, $4, $5, $6, $7)
12791286
ON CONFLICT (pool_id, delegation_id) DO UPDATE
1280-
SET block_height = $2, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7;
1287+
SET block_height = EXCLUDED.block_height,
1288+
balance = EXCLUDED.balance,
1289+
spend_destination = EXCLUDED.spend_destination,
1290+
next_nonce = EXCLUDED.next_nonce,
1291+
creation_block_height = EXCLUDED.creation_block_height
1292+
WHERE ml.latest_delegations_cache.block_height < EXCLUDED.block_height;
12811293
"#,
12821294
&[
12831295
&delegation_id.as_str(),
@@ -1298,7 +1310,11 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
12981310
INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height)
12991311
VALUES($1, $2, $3, $4, $5, $6, $7)
13001312
ON CONFLICT (delegation_id, block_height) DO UPDATE
1301-
SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7;
1313+
SET pool_id = EXCLUDED.pool_id,
1314+
balance = EXCLUDED.balance,
1315+
spend_destination = EXCLUDED.spend_destination,
1316+
next_nonce = EXCLUDED.next_nonce,
1317+
creation_block_height = EXCLUDED.creation_block_height;
13021318
"#,
13031319
&[
13041320
&delegation_id.as_str(),
@@ -2078,9 +2094,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
20782094

20792095
self.tx
20802096
.execute(
2081-
"INSERT INTO ml.fungible_token (token_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4)
2082-
ON CONFLICT (token_id, block_height) DO UPDATE
2083-
SET issuance = $3, ticker = $4;",
2097+
"INSERT INTO ml.fungible_token (token_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4);",
20842098
&[
20852099
&token_id.encode(),
20862100
&height,
@@ -2094,7 +2108,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
20942108
let coin_or_token_id = CoinOrTokenId::TokenId(token_id);
20952109
self.tx
20962110
.execute(
2097-
"INSERT INTO ml.coin_or_token_decimals (coin_or_token_id, block_height, number_of_decimals) VALUES ($1, $2, $3) ON CONFLICT (coin_or_token_id) DO NOTHING;",
2111+
"INSERT INTO ml.coin_or_token_decimals (coin_or_token_id, block_height, number_of_decimals) VALUES ($1, $2, $3);",
20982112
&[&coin_or_token_id.encode(), &height, &(issuance.number_of_decimals as i16)],
20992113
)
21002114
.await
@@ -2103,6 +2117,31 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
21032117
Ok(())
21042118
}
21052119

2120+
pub async fn set_fungible_token_data(
2121+
&mut self,
2122+
token_id: TokenId,
2123+
block_height: BlockHeight,
2124+
issuance: FungibleTokenData,
2125+
) -> Result<(), ApiServerStorageError> {
2126+
let height = Self::block_height_to_postgres_friendly(block_height);
2127+
2128+
self.tx
2129+
.execute(
2130+
"INSERT INTO ml.fungible_token (token_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4)
2131+
ON CONFLICT (token_id, block_height) DO UPDATE
2132+
SET issuance = $3, ticker = $4;",
2133+
&[
2134+
&token_id.encode(),
2135+
&height,
2136+
&issuance.encode(),
2137+
&issuance.token_ticker,
2138+
],
2139+
)
2140+
.await
2141+
.map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?;
2142+
Ok(())
2143+
}
2144+
21062145
pub async fn get_fungible_token_issuance(
21072146
&self,
21082147
token_id: TokenId,

0 commit comments

Comments
 (0)