Skip to content

Commit

Permalink
Merge pull request #1753 from mintlayer:fix/port-api-server-changes-t…
Browse files Browse the repository at this point in the history
…o-release

port more changes from master into release 0.4.3
  • Loading branch information
TheQuantumPhysicist authored May 16, 2024
2 parents 227660f + 8e80a5e commit 690efc6
Show file tree
Hide file tree
Showing 10 changed files with 733 additions and 95 deletions.
34 changes: 17 additions & 17 deletions api-server/api-server-common/src/storage/impls/in_memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,23 +450,23 @@ impl ApiServerInMemoryStorage {
.get(address)
.unwrap_or(&BTreeSet::new())
.union(self.address_locked_utxos.get(address).unwrap_or(&BTreeSet::new()))
.map(|outpoint| {
(
outpoint.clone(),
self.get_utxo(outpoint.clone()).expect("no error").map_or_else(
|| {
self.locked_utxo_table
.get(outpoint)
.expect("must exit")
.values()
.last()
.expect("not empty")
.utxo_with_extra_info()
.clone()
},
|utxo| utxo.utxo_with_extra_info().clone(),
),
)
.filter_map(|outpoint| {
if let Some(utxo) = self.get_utxo(outpoint.clone()).expect("no error") {
(!utxo.spent())
.then_some((outpoint.clone(), utxo.utxo_with_extra_info().clone()))
} else {
Some((
outpoint.clone(),
self.locked_utxo_table
.get(outpoint)
.expect("must exit")
.values()
.last()
.expect("not empty")
.utxo_with_extra_info()
.clone(),
))
}
})
.collect();
Ok(result)
Expand Down
62 changes: 45 additions & 17 deletions api-server/api-server-common/src/storage/impls/postgres/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
"CREATE TABLE ml.delegations (
delegation_id TEXT NOT NULL,
block_height bigint NOT NULL,
creation_block_height bigint NOT NULL,
pool_id TEXT NOT NULL,
balance TEXT NOT NULL,
next_nonce bytea NOT NULL,
Expand Down Expand Up @@ -846,7 +847,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
let row = self
.tx
.query_opt(
r#"SELECT pool_id, balance, spend_destination, next_nonce
r#"SELECT pool_id, balance, spend_destination, next_nonce, creation_block_height
FROM ml.delegations
WHERE delegation_id = $1
AND block_height = (SELECT MAX(block_height) FROM ml.delegations WHERE delegation_id = $1);
Expand All @@ -868,6 +869,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
let balance: String = data.get(1);
let spend_destination: Vec<u8> = data.get(2);
let next_nonce: Vec<u8> = data.get(3);
let creation_block_height: i64 = data.get(4);

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

let delegation = Delegation::new(spend_destination, pool_id, balance, next_nonce);
let delegation = Delegation::new(
BlockHeight::new(creation_block_height as u64),
spend_destination,
pool_id,
balance,
next_nonce,
);
Ok(Some(delegation))
}

Expand All @@ -902,9 +910,9 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
let rows = self
.tx
.query(
r#"SELECT delegation_id, pool_id, balance, spend_destination, next_nonce
r#"SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, creation_block_height
FROM (
SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, ROW_NUMBER() OVER(PARTITION BY delegation_id ORDER BY block_height DESC) as newest
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
FROM ml.delegations
WHERE spend_destination = $1
) AS sub
Expand All @@ -929,6 +937,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
let balance: String = row.get(2);
let spend_destination: Vec<u8> = row.get(3);
let next_nonce: Vec<u8> = row.get(4);
let creation_block_height: i64 = row.get(5);

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

let delegation = Delegation::new(spend_destination, pool_id, balance, next_nonce);
let delegation = Delegation::new(
BlockHeight::new(creation_block_height as u64),
spend_destination,
pool_id,
balance,
next_nonce,
);
Ok((delegation_id, delegation))
})
.collect()
Expand All @@ -964,6 +979,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
chain_config: &ChainConfig,
) -> Result<(), ApiServerStorageError> {
let height = Self::block_height_to_postgres_friendly(block_height);
let creation_block_height =
Self::block_height_to_postgres_friendly(delegation.creation_block_height());
let pool_id = Address::new(chain_config, *delegation.pool_id())
.map_err(|_| ApiServerStorageError::AddressableError)?;
let delegation_id = Address::new(chain_config, delegation_id)
Expand All @@ -972,10 +989,10 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
self.tx
.execute(
r#"
INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce)
VALUES($1, $2, $3, $4, $5, $6)
INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height)
VALUES($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (delegation_id, block_height) DO UPDATE
SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6;
SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7;
"#,
&[
&delegation_id.as_str(),
Expand All @@ -984,6 +1001,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
&amount_to_str(*delegation.balance()),
&delegation.spend_destination().encode(),
&delegation.next_nonce().encode(),
&creation_block_height,
],
)
.await
Expand Down Expand Up @@ -1065,7 +1083,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
.map_err(|_| ApiServerStorageError::AddressableError)?;
self.tx
.query(
r#"SELECT delegation_id, balance, spend_destination, next_nonce
r#"SELECT delegation_id, balance, spend_destination, next_nonce, creation_block_height
FROM ml.delegations
WHERE pool_id = $1
AND (delegation_id, block_height) in (SELECT delegation_id, MAX(block_height)
Expand All @@ -1087,6 +1105,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
let balance: String = row.get(1);
let spend_destination: Vec<u8> = row.get(2);
let next_nonce: Vec<u8> = row.get(3);
let creation_block_height: i64 = row.get(4);

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

Ok((
delegation_id,
Delegation::new(spend_destination, pool_id, balance, next_nonce),
Delegation::new(
BlockHeight::new(creation_block_height as u64),
spend_destination,
pool_id,
balance,
next_nonce,
),
))
})
.collect()
Expand Down Expand Up @@ -1526,14 +1551,17 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
let rows = self
.tx
.query(
r#"
SELECT outpoint, utxo
FROM ml.utxo
WHERE address = $1
UNION
r#"SELECT outpoint, utxo
FROM (
SELECT outpoint, utxo, spent, ROW_NUMBER() OVER(PARTITION BY outpoint ORDER BY block_height DESC) as newest
FROM ml.utxo
WHERE address = $1
) AS sub
WHERE newest = 1 AND spent = false
UNION ALL
SELECT outpoint, utxo
FROM ml.locked_utxo
WHERE address = $1
FROM ml.locked_utxo AS locked
WHERE locked.address = $1 AND NOT EXISTS (SELECT 1 FROM ml.utxo WHERE outpoint = locked.outpoint)
;"#,
&[&address],
)
Expand Down
27 changes: 25 additions & 2 deletions api-server/api-server-common/src/storage/storage_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use common::{
block::timestamp::BlockTimestamp,
timelock::OutputTimeLock,
tokens::{
IsTokenFreezable, IsTokenFrozen, IsTokenUnfreezable, NftIssuance, TokenId,
TokenTotalSupply,
IsTokenFreezable, IsTokenFrozen, IsTokenUnfreezable, NftIssuance, RPCFungibleTokenInfo,
TokenId, TokenTotalSupply,
},
AccountNonce, Block, ChainConfig, DelegationId, Destination, PoolId, SignedTransaction,
Transaction, TxOutput, UtxoOutPoint,
Expand Down Expand Up @@ -66,6 +66,7 @@ pub enum ApiServerStorageError {

#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
pub struct Delegation {
creation_block_height: BlockHeight,
spend_destination: Destination,
pool_id: PoolId,
balance: Amount,
Expand All @@ -74,19 +75,25 @@ pub struct Delegation {

impl Delegation {
pub fn new(
creation_block_height: BlockHeight,
spend_destination: Destination,
pool_id: PoolId,
balance: Amount,
next_nonce: AccountNonce,
) -> Self {
Self {
creation_block_height,
spend_destination,
pool_id,
balance,
next_nonce,
}
}

pub fn creation_block_height(&self) -> BlockHeight {
self.creation_block_height
}

pub fn spend_destination(&self) -> &Destination {
&self.spend_destination
}
Expand All @@ -109,6 +116,7 @@ impl Delegation {
pool_id: self.pool_id,
balance: (self.balance + rewards).expect("no overflow"),
next_nonce: self.next_nonce,
creation_block_height: self.creation_block_height,
}
}

Expand All @@ -118,6 +126,7 @@ impl Delegation {
pool_id: self.pool_id,
balance: (self.balance - amount).expect("not underflow"),
next_nonce: nonce.increment().expect("no overflow"),
creation_block_height: self.creation_block_height,
}
}
}
Expand Down Expand Up @@ -287,6 +296,20 @@ impl FungibleTokenData {
self.authority = authority;
self
}

pub fn into_rpc_token_info(self, token_id: TokenId) -> RPCFungibleTokenInfo {
RPCFungibleTokenInfo {
token_id,
token_ticker: self.token_ticker.into(),
number_of_decimals: self.number_of_decimals,
metadata_uri: self.metadata_uri.into(),
circulating_supply: self.circulating_supply,
total_supply: self.total_supply.into(),
is_locked: self.is_locked,
frozen: common::chain::tokens::RPCIsTokenFrozen::new(self.frozen),
authority: self.authority,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
Expand Down
Loading

0 comments on commit 690efc6

Please sign in to comment.