Skip to content

Commit 690efc6

Browse files
Merge pull request #1753 from mintlayer:fix/port-api-server-changes-to-release
port more changes from master into release 0.4.3
2 parents 227660f + 8e80a5e commit 690efc6

File tree

10 files changed

+733
-95
lines changed

10 files changed

+733
-95
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,23 +450,23 @@ impl ApiServerInMemoryStorage {
450450
.get(address)
451451
.unwrap_or(&BTreeSet::new())
452452
.union(self.address_locked_utxos.get(address).unwrap_or(&BTreeSet::new()))
453-
.map(|outpoint| {
454-
(
455-
outpoint.clone(),
456-
self.get_utxo(outpoint.clone()).expect("no error").map_or_else(
457-
|| {
458-
self.locked_utxo_table
459-
.get(outpoint)
460-
.expect("must exit")
461-
.values()
462-
.last()
463-
.expect("not empty")
464-
.utxo_with_extra_info()
465-
.clone()
466-
},
467-
|utxo| utxo.utxo_with_extra_info().clone(),
468-
),
469-
)
453+
.filter_map(|outpoint| {
454+
if let Some(utxo) = self.get_utxo(outpoint.clone()).expect("no error") {
455+
(!utxo.spent())
456+
.then_some((outpoint.clone(), utxo.utxo_with_extra_info().clone()))
457+
} else {
458+
Some((
459+
outpoint.clone(),
460+
self.locked_utxo_table
461+
.get(outpoint)
462+
.expect("must exit")
463+
.values()
464+
.last()
465+
.expect("not empty")
466+
.utxo_with_extra_info()
467+
.clone(),
468+
))
469+
}
470470
})
471471
.collect();
472472
Ok(result)

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
589589
"CREATE TABLE ml.delegations (
590590
delegation_id TEXT NOT NULL,
591591
block_height bigint NOT NULL,
592+
creation_block_height bigint NOT NULL,
592593
pool_id TEXT NOT NULL,
593594
balance TEXT NOT NULL,
594595
next_nonce bytea NOT NULL,
@@ -846,7 +847,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
846847
let row = self
847848
.tx
848849
.query_opt(
849-
r#"SELECT pool_id, balance, spend_destination, next_nonce
850+
r#"SELECT pool_id, balance, spend_destination, next_nonce, creation_block_height
850851
FROM ml.delegations
851852
WHERE delegation_id = $1
852853
AND block_height = (SELECT MAX(block_height) FROM ml.delegations WHERE delegation_id = $1);
@@ -868,6 +869,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
868869
let balance: String = data.get(1);
869870
let spend_destination: Vec<u8> = data.get(2);
870871
let next_nonce: Vec<u8> = data.get(3);
872+
let creation_block_height: i64 = data.get(4);
871873

872874
let balance = Amount::from_fixedpoint_str(&balance, 0).ok_or_else(|| {
873875
ApiServerStorageError::DeserializationError(format!(
@@ -890,7 +892,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
890892
))
891893
})?;
892894

893-
let delegation = Delegation::new(spend_destination, pool_id, balance, next_nonce);
895+
let delegation = Delegation::new(
896+
BlockHeight::new(creation_block_height as u64),
897+
spend_destination,
898+
pool_id,
899+
balance,
900+
next_nonce,
901+
);
894902
Ok(Some(delegation))
895903
}
896904

@@ -902,9 +910,9 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
902910
let rows = self
903911
.tx
904912
.query(
905-
r#"SELECT delegation_id, pool_id, balance, spend_destination, next_nonce
913+
r#"SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, creation_block_height
906914
FROM (
907-
SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, ROW_NUMBER() OVER(PARTITION BY delegation_id ORDER BY block_height DESC) as newest
915+
SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, creation_block_height, ROW_NUMBER() OVER(PARTITION BY delegation_id ORDER BY block_height DESC) as newest
908916
FROM ml.delegations
909917
WHERE spend_destination = $1
910918
) AS sub
@@ -929,6 +937,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
929937
let balance: String = row.get(2);
930938
let spend_destination: Vec<u8> = row.get(3);
931939
let next_nonce: Vec<u8> = row.get(4);
940+
let creation_block_height: i64 = row.get(5);
932941

933942
let balance = Amount::from_fixedpoint_str(&balance, 0).ok_or_else(|| {
934943
ApiServerStorageError::DeserializationError(format!(
@@ -950,7 +959,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
950959
))
951960
})?;
952961

953-
let delegation = Delegation::new(spend_destination, pool_id, balance, next_nonce);
962+
let delegation = Delegation::new(
963+
BlockHeight::new(creation_block_height as u64),
964+
spend_destination,
965+
pool_id,
966+
balance,
967+
next_nonce,
968+
);
954969
Ok((delegation_id, delegation))
955970
})
956971
.collect()
@@ -964,6 +979,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
964979
chain_config: &ChainConfig,
965980
) -> Result<(), ApiServerStorageError> {
966981
let height = Self::block_height_to_postgres_friendly(block_height);
982+
let creation_block_height =
983+
Self::block_height_to_postgres_friendly(delegation.creation_block_height());
967984
let pool_id = Address::new(chain_config, *delegation.pool_id())
968985
.map_err(|_| ApiServerStorageError::AddressableError)?;
969986
let delegation_id = Address::new(chain_config, delegation_id)
@@ -972,10 +989,10 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
972989
self.tx
973990
.execute(
974991
r#"
975-
INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce)
976-
VALUES($1, $2, $3, $4, $5, $6)
992+
INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height)
993+
VALUES($1, $2, $3, $4, $5, $6, $7)
977994
ON CONFLICT (delegation_id, block_height) DO UPDATE
978-
SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6;
995+
SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7;
979996
"#,
980997
&[
981998
&delegation_id.as_str(),
@@ -984,6 +1001,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
9841001
&amount_to_str(*delegation.balance()),
9851002
&delegation.spend_destination().encode(),
9861003
&delegation.next_nonce().encode(),
1004+
&creation_block_height,
9871005
],
9881006
)
9891007
.await
@@ -1065,7 +1083,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
10651083
.map_err(|_| ApiServerStorageError::AddressableError)?;
10661084
self.tx
10671085
.query(
1068-
r#"SELECT delegation_id, balance, spend_destination, next_nonce
1086+
r#"SELECT delegation_id, balance, spend_destination, next_nonce, creation_block_height
10691087
FROM ml.delegations
10701088
WHERE pool_id = $1
10711089
AND (delegation_id, block_height) in (SELECT delegation_id, MAX(block_height)
@@ -1087,6 +1105,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
10871105
let balance: String = row.get(1);
10881106
let spend_destination: Vec<u8> = row.get(2);
10891107
let next_nonce: Vec<u8> = row.get(3);
1108+
let creation_block_height: i64 = row.get(4);
10901109

10911110
let balance = Amount::from_fixedpoint_str(&balance, 0).ok_or_else(|| {
10921111
ApiServerStorageError::DeserializationError(format!(
@@ -1110,7 +1129,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
11101129

11111130
Ok((
11121131
delegation_id,
1113-
Delegation::new(spend_destination, pool_id, balance, next_nonce),
1132+
Delegation::new(
1133+
BlockHeight::new(creation_block_height as u64),
1134+
spend_destination,
1135+
pool_id,
1136+
balance,
1137+
next_nonce,
1138+
),
11141139
))
11151140
})
11161141
.collect()
@@ -1526,14 +1551,17 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
15261551
let rows = self
15271552
.tx
15281553
.query(
1529-
r#"
1530-
SELECT outpoint, utxo
1531-
FROM ml.utxo
1532-
WHERE address = $1
1533-
UNION
1554+
r#"SELECT outpoint, utxo
1555+
FROM (
1556+
SELECT outpoint, utxo, spent, ROW_NUMBER() OVER(PARTITION BY outpoint ORDER BY block_height DESC) as newest
1557+
FROM ml.utxo
1558+
WHERE address = $1
1559+
) AS sub
1560+
WHERE newest = 1 AND spent = false
1561+
UNION ALL
15341562
SELECT outpoint, utxo
1535-
FROM ml.locked_utxo
1536-
WHERE address = $1
1563+
FROM ml.locked_utxo AS locked
1564+
WHERE locked.address = $1 AND NOT EXISTS (SELECT 1 FROM ml.utxo WHERE outpoint = locked.outpoint)
15371565
;"#,
15381566
&[&address],
15391567
)

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use common::{
2020
block::timestamp::BlockTimestamp,
2121
timelock::OutputTimeLock,
2222
tokens::{
23-
IsTokenFreezable, IsTokenFrozen, IsTokenUnfreezable, NftIssuance, TokenId,
24-
TokenTotalSupply,
23+
IsTokenFreezable, IsTokenFrozen, IsTokenUnfreezable, NftIssuance, RPCFungibleTokenInfo,
24+
TokenId, TokenTotalSupply,
2525
},
2626
AccountNonce, Block, ChainConfig, DelegationId, Destination, PoolId, SignedTransaction,
2727
Transaction, TxOutput, UtxoOutPoint,
@@ -66,6 +66,7 @@ pub enum ApiServerStorageError {
6666

6767
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
6868
pub struct Delegation {
69+
creation_block_height: BlockHeight,
6970
spend_destination: Destination,
7071
pool_id: PoolId,
7172
balance: Amount,
@@ -74,19 +75,25 @@ pub struct Delegation {
7475

7576
impl Delegation {
7677
pub fn new(
78+
creation_block_height: BlockHeight,
7779
spend_destination: Destination,
7880
pool_id: PoolId,
7981
balance: Amount,
8082
next_nonce: AccountNonce,
8183
) -> Self {
8284
Self {
85+
creation_block_height,
8386
spend_destination,
8487
pool_id,
8588
balance,
8689
next_nonce,
8790
}
8891
}
8992

93+
pub fn creation_block_height(&self) -> BlockHeight {
94+
self.creation_block_height
95+
}
96+
9097
pub fn spend_destination(&self) -> &Destination {
9198
&self.spend_destination
9299
}
@@ -109,6 +116,7 @@ impl Delegation {
109116
pool_id: self.pool_id,
110117
balance: (self.balance + rewards).expect("no overflow"),
111118
next_nonce: self.next_nonce,
119+
creation_block_height: self.creation_block_height,
112120
}
113121
}
114122

@@ -118,6 +126,7 @@ impl Delegation {
118126
pool_id: self.pool_id,
119127
balance: (self.balance - amount).expect("not underflow"),
120128
next_nonce: nonce.increment().expect("no overflow"),
129+
creation_block_height: self.creation_block_height,
121130
}
122131
}
123132
}
@@ -287,6 +296,20 @@ impl FungibleTokenData {
287296
self.authority = authority;
288297
self
289298
}
299+
300+
pub fn into_rpc_token_info(self, token_id: TokenId) -> RPCFungibleTokenInfo {
301+
RPCFungibleTokenInfo {
302+
token_id,
303+
token_ticker: self.token_ticker.into(),
304+
number_of_decimals: self.number_of_decimals,
305+
metadata_uri: self.metadata_uri.into(),
306+
circulating_supply: self.circulating_supply,
307+
total_supply: self.total_supply.into(),
308+
is_locked: self.is_locked,
309+
frozen: common::chain::tokens::RPCIsTokenFrozen::new(self.frozen),
310+
authority: self.authority,
311+
}
312+
}
290313
}
291314

292315
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]

0 commit comments

Comments
 (0)