-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize balance-related queries with a cache #2383
base: master
Are you sure you want to change the base?
Changes from 70 commits
4a70f52
7d472fb
4296fd4
77d0f16
525e6f9
8eba5d7
c083f45
6b76c37
f246cd0
7472db7
b4d2e0f
d38fdb3
292acab
651bcc8
8342c8f
9a9f120
6aa9325
b153db4
512a8a3
adf9e2a
344ca90
f73dbed
80da09b
ad5216d
c784e44
5762665
5595985
38dd8d6
530bcaf
5604f30
562c087
82f7a16
7d0b014
1fe3bba
fe60c98
adef78e
9409d52
13b6db9
55f9025
d215844
53131a3
1e24aaa
eeea199
e77f33b
c9b7e29
076cb42
f5e2a6d
d1c6fcc
8cc0280
ca32195
f082291
f598395
8ce4550
4e84ac3
9577f30
a16ef36
80fb852
c94a484
ddf0571
9078b05
3b6b8c6
5162936
f736ecb
d880e0a
6927f03
66e26d9
a87698a
ddadd80
ccadf41
91e8abc
ab7808f
90fa259
0b39319
4d6e17a
8f3e817
895d9da
de11071
71226d4
474e207
ca6057d
4d43038
e3d898d
9848f64
dae0af5
facbd2e
9b8e491
9ea9c02
54087fc
20081fa
8b9f4e8
e819e23
de16099
4f307e3
66d5948
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,6 +17,8 @@ use fuel_core_storage::{ | |||||||||||||||||||||||
StorageInspect, | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
use super::database_description::IndexationKind; | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/// The table that stores all metadata about the database. | ||||||||||||||||||||||||
pub struct MetadataTable<Description>(core::marker::PhantomData<Description>); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
@@ -74,4 +76,12 @@ where | |||||||||||||||||||||||
|
||||||||||||||||||||||||
Ok(metadata) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
pub fn indexation_available(&self, kind: IndexationKind) -> StorageResult<bool> { | ||||||||||||||||||||||||
let Some(metadata) = self.storage::<MetadataTable<Description>>().get(&())? | ||||||||||||||||||||||||
else { | ||||||||||||||||||||||||
return Ok(false) | ||||||||||||||||||||||||
}; | ||||||||||||||||||||||||
Ok(metadata.indexation_available(kind)) | ||||||||||||||||||||||||
Comment on lines
+81
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: Don't you think an
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No strong opinions here, but I'm quite fond of early returns, tbh :) |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ use fuel_core_types::{ | |
}; | ||
use statistic::StatisticTable; | ||
|
||
pub mod balances; | ||
pub mod blocks; | ||
pub mod coins; | ||
pub mod contracts; | ||
|
@@ -113,6 +114,10 @@ pub enum Column { | |
DaCompressionTemporalRegistryScriptCode = 21, | ||
/// See [`DaCompressionTemporalRegistryPredicateCode`](da_compression::DaCompressionTemporalRegistryPredicateCode) | ||
DaCompressionTemporalRegistryPredicateCode = 22, | ||
/// Coin balances per user and asset. | ||
CoinBalances = 23, | ||
/// Message balances per user. | ||
MessageBalances = 24, | ||
Comment on lines
+117
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think having 2 tables for balance it is too much=D I think w can have just one table with some: enum Balance {
V1 {
coins: Amount,
messages: Amount,
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion would require the "asset id" to be a part of the key, even if it is technically not needed for messages. That's why I decided to structure it in two separate columns. Also, current solution would allow us to add more types of "amounts" in the future, without requiring |
||
} | ||
|
||
impl Column { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use fuel_core_storage::{ | ||
blueprint::plain::Plain, | ||
codec::{ | ||
postcard::Postcard, | ||
raw::Raw, | ||
}, | ||
structured_storage::TableWithBlueprint, | ||
Mappable, | ||
}; | ||
use fuel_core_types::{ | ||
fuel_tx::{ | ||
Address, | ||
AssetId, | ||
}, | ||
fuel_vm::double_key, | ||
}; | ||
use rand::{ | ||
distributions::Standard, | ||
prelude::Distribution, | ||
Rng, | ||
}; | ||
|
||
pub type Amount = u64; | ||
|
||
double_key!(BalancesKey, Address, address, AssetId, asset_id); | ||
impl Distribution<BalancesKey> for Standard { | ||
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> BalancesKey { | ||
let mut bytes = [0u8; BalancesKey::LEN]; | ||
rng.fill_bytes(bytes.as_mut()); | ||
BalancesKey::from_array(bytes) | ||
} | ||
} | ||
|
||
impl core::fmt::Display for BalancesKey { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR, but is there any benefit in putting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was considering this, but after giving this a second thought I came to the conclusion that For example, if one part is the "private key" I may want to display |
||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { | ||
write!(f, "address={} asset_id={}", self.address(), self.asset_id()) | ||
} | ||
} | ||
|
||
/// This table stores the balances of coins per owner and asset id. | ||
pub struct CoinBalances; | ||
|
||
impl Mappable for CoinBalances { | ||
type Key = BalancesKey; | ||
type OwnedKey = Self::Key; | ||
type Value = Amount; | ||
type OwnedValue = Self::Value; | ||
} | ||
|
||
impl TableWithBlueprint for CoinBalances { | ||
type Blueprint = Plain<Raw, Postcard>; | ||
type Column = super::Column; | ||
|
||
fn column() -> Self::Column { | ||
Self::Column::CoinBalances | ||
} | ||
} | ||
|
||
/// This table stores the balances of messages per owner. | ||
pub struct MessageBalances; | ||
|
||
impl Mappable for MessageBalances { | ||
type Key = Address; | ||
type OwnedKey = Self::Key; | ||
type Value = Amount; | ||
type OwnedValue = Self::Value; | ||
} | ||
|
||
impl TableWithBlueprint for MessageBalances { | ||
type Blueprint = Plain<Raw, Postcard>; | ||
type Column = super::Column; | ||
|
||
fn column() -> Self::Column { | ||
Self::Column::MessageBalances | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it will be right to increase it, but then you need to handle it here:
But maybe we don't need
version
function at all, if we version enum of the metadata.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the comment and decided to keep the
version()
at0
, as it used to be, because I can't really feel the notion of this version number.I'm good to update it, remove it or refactor if needed, but preferably in a follow-up PR.