Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ dyn-clone = "1.0"
# Note: enum iteration is also provided by strum. However, with strum it's a bit more limited,
# e.g. there is no way to to go from one enum value to the next one, like enum_iterator::Sequence::next()
# does.
num-bigint = "0.4.6"
enum-iterator = "2.0"
enumflags2 = "0.7"
expect-test = "1.3"
Expand Down
1 change: 1 addition & 0 deletions api-server/api-server-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bb8-postgres = "0.8"
clap = { workspace = true, features = ["derive"] }
futures = { workspace = true, default-features = false }
itertools.workspace = true
num-bigint.workspace = true
parity-scale-codec.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["full"] }
Expand Down
36 changes: 19 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 @@ -22,8 +22,6 @@ use std::{
sync::Arc,
};

use itertools::Itertools as _;

use common::{
address::Address,
chain::{
Expand All @@ -44,6 +42,9 @@ use crate::storage::storage_api::{

use super::CURRENT_STORAGE_VERSION;

use itertools::Itertools as _;
use num_bigint::BigUint;

#[derive(Debug, Clone, PartialEq, Eq)]
struct TokenTransactionOrderedByTxId(TokenTransaction);

Expand All @@ -63,8 +64,9 @@ impl Ord for TokenTransactionOrderedByTxId {
struct ApiServerInMemoryStorage {
block_table: BTreeMap<Id<Block>, BlockWithExtraData>,
block_aux_data_table: BTreeMap<Id<Block>, BlockAuxData>,
address_balance_table: BTreeMap<String, BTreeMap<CoinOrTokenId, BTreeMap<BlockHeight, Amount>>>,
address_locked_balance_table: BTreeMap<String, BTreeMap<(CoinOrTokenId, BlockHeight), Amount>>,
address_balance_table:
BTreeMap<String, BTreeMap<CoinOrTokenId, BTreeMap<BlockHeight, BigUint>>>,
address_locked_balance_table: BTreeMap<String, BTreeMap<(CoinOrTokenId, BlockHeight), BigUint>>,
address_transactions_table: BTreeMap<String, BTreeMap<BlockHeight, Vec<Id<Transaction>>>>,
token_transactions_table:
BTreeMap<TokenId, BTreeMap<BlockHeight, BTreeSet<TokenTransactionOrderedByTxId>>>,
Expand Down Expand Up @@ -123,13 +125,13 @@ impl ApiServerInMemoryStorage {
&self,
address: &str,
coin_or_token_id: CoinOrTokenId,
) -> Result<Option<Amount>, ApiServerStorageError> {
) -> Result<Option<BigUint>, ApiServerStorageError> {
self.address_balance_table
.get(address)
.and_then(|by_coin_or_token| by_coin_or_token.get(&coin_or_token_id))
.map_or_else(
|| Ok(None),
|by_height| Ok(by_height.values().last().copied()),
|by_height| Ok(by_height.values().last().cloned()),
)
}

Expand All @@ -155,7 +157,7 @@ impl ApiServerInMemoryStorage {
(
*coin_or_token_id,
AmountWithDecimals {
amount: *by_height.values().last().expect("not empty"),
amount: by_height.values().last().expect("not empty").clone(),
decimals: number_of_decimals,
},
)
Expand All @@ -170,14 +172,14 @@ impl ApiServerInMemoryStorage {
&self,
address: &str,
coin_or_token_id: CoinOrTokenId,
) -> Result<Option<Amount>, ApiServerStorageError> {
) -> Result<Option<BigUint>, ApiServerStorageError> {
self.address_locked_balance_table.get(address).map_or_else(
|| Ok(None),
|balance| {
let range_begin = (coin_or_token_id, BlockHeight::zero());
let range_end = (coin_or_token_id, BlockHeight::max());
let range = balance.range(range_begin..=range_end);
Ok(range.last().map(|(_, v)| *v))
Ok(range.last().map(|(_, v)| v.clone()))
},
)
}
Expand Down Expand Up @@ -927,7 +929,7 @@ impl ApiServerInMemoryStorage {
fn set_address_balance_at_height(
&mut self,
address: &Address<Destination>,
amount: Amount,
amount: BigUint,
coin_or_token_id: CoinOrTokenId,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
Expand All @@ -937,8 +939,8 @@ impl ApiServerInMemoryStorage {
.entry(coin_or_token_id)
.or_default()
.entry(block_height)
.and_modify(|e| *e = amount)
.or_insert(amount);
.and_modify(|e| *e = amount.clone())
.or_insert(amount.clone());

self.update_nft_owner(coin_or_token_id, amount, address, block_height);

Expand All @@ -950,7 +952,7 @@ impl ApiServerInMemoryStorage {
fn update_nft_owner(
&mut self,
coin_or_token_id: CoinOrTokenId,
amount: Amount,
amount: BigUint,
address: &Address<Destination>,
block_height: BlockHeight,
) {
Expand All @@ -960,7 +962,7 @@ impl ApiServerInMemoryStorage {

if let Some(by_height) = self.nft_token_issuances.get_mut(&token_id) {
let last = by_height.values().last().expect("not empty");
let owner = (amount > Amount::ZERO).then_some(address.as_object().clone());
let owner = (amount > BigUint::ZERO).then_some(address.as_object().clone());
let new = NftWithOwner {
nft: last.nft.clone(),
owner,
Expand All @@ -972,16 +974,16 @@ impl ApiServerInMemoryStorage {
fn set_address_locked_balance_at_height(
&mut self,
address: &Address<Destination>,
amount: Amount,
amount: BigUint,
coin_or_token_id: CoinOrTokenId,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
self.address_locked_balance_table
.entry(address.to_string())
.or_default()
.entry((coin_or_token_id, block_height))
.and_modify(|e| *e = amount)
.or_insert(amount);
.and_modify(|e| *e = amount.clone())
.or_insert(amount.clone());

self.update_nft_owner(coin_or_token_id, amount, address, block_height);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use crate::storage::storage_api::{

use super::ApiServerInMemoryStorageTransactionalRo;

use num_bigint::BigUint;

#[async_trait::async_trait]
impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
async fn is_initialized(&self) -> Result<bool, ApiServerStorageError> {
Expand All @@ -42,7 +44,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
&self,
address: &str,
coin_or_token_id: CoinOrTokenId,
) -> Result<Option<Amount>, ApiServerStorageError> {
) -> Result<Option<BigUint>, ApiServerStorageError> {
self.transaction.get_address_balance(address, coin_or_token_id)
}

Expand All @@ -57,7 +59,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRo<'_> {
&self,
address: &str,
coin_or_token_id: CoinOrTokenId,
) -> Result<Option<Amount>, ApiServerStorageError> {
) -> Result<Option<BigUint>, ApiServerStorageError> {
self.transaction.get_address_locked_balance(address, coin_or_token_id)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use common::{

use super::ApiServerInMemoryStorageTransactionalRw;

use num_bigint::BigUint;

#[async_trait::async_trait]
impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn reinitialize_storage(
Expand Down Expand Up @@ -74,7 +76,7 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn set_address_balance_at_height(
&mut self,
address: &Address<Destination>,
amount: Amount,
amount: BigUint,
coin_or_token_id: CoinOrTokenId,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
Expand All @@ -89,7 +91,7 @@ impl ApiServerStorageWrite for ApiServerInMemoryStorageTransactionalRw<'_> {
async fn set_address_locked_balance_at_height(
&mut self,
address: &Address<Destination>,
amount: Amount,
amount: BigUint,
coin_or_token_id: CoinOrTokenId,
block_height: BlockHeight,
) -> Result<(), ApiServerStorageError> {
Expand Down Expand Up @@ -327,7 +329,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
&self,
address: &str,
coin_or_token_id: CoinOrTokenId,
) -> Result<Option<Amount>, ApiServerStorageError> {
) -> Result<Option<BigUint>, ApiServerStorageError> {
self.transaction.get_address_balance(address, coin_or_token_id)
}

Expand All @@ -342,7 +344,7 @@ impl ApiServerStorageRead for ApiServerInMemoryStorageTransactionalRw<'_> {
&self,
address: &str,
coin_or_token_id: CoinOrTokenId,
) -> Result<Option<Amount>, ApiServerStorageError> {
) -> Result<Option<BigUint>, ApiServerStorageError> {
self.transaction.get_address_locked_balance(address, coin_or_token_id)
}

Expand Down
2 changes: 1 addition & 1 deletion api-server/api-server-common/src/storage/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub const CURRENT_STORAGE_VERSION: u32 = 24;
pub const CURRENT_STORAGE_VERSION: u32 = 25;

pub mod in_memory;
pub mod postgres;
Loading
Loading