Skip to content
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

Calculate and set max_burned_mana field #1610

Merged
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
80a2eda
add workscore calc
Alex6323 Sep 15, 2023
f7cffb6
Merge branch '2.0' into feat/workscore
Alex6323 Sep 15, 2023
29843e5
fix merge
Alex6323 Sep 15, 2023
5da94e4
finish calculations
Alex6323 Sep 19, 2023
9d8d0cd
nits
Alex6323 Sep 20, 2023
5cd1d54
merge import
Alex6323 Sep 20, 2023
8ade41a
Merge branch '2.0' into feat/workscore; Add WorkScore trait;
Alex6323 Sep 20, 2023
00ddccf
nits
Alex6323 Sep 20, 2023
cca95f3
Merge branch '2.0' into feat/workscore
Alex6323 Sep 20, 2023
ab3b9fe
rename trait fn
Alex6323 Oct 8, 2023
25f183c
Merge branch '2.0' into feat/workscore
Alex6323 Oct 9, 2023
830ff47
update work score computations
Alex6323 Oct 9, 2023
6306da5
nit
Alex6323 Oct 9, 2023
7e8a204
move work score types into own module
Alex6323 Oct 9, 2023
f917fd9
add and impl mana cost fn
Alex6323 Oct 9, 2023
1ee7870
PR suggestions 1
Alex6323 Oct 10, 2023
9b64653
Merge branch '2.0' into feat/workscore
Alex6323 Nov 6, 2023
7fe6244
Merge branch '2.0' into feat/workscore
Alex6323 Nov 6, 2023
8789e80
is_signature
Alex6323 Nov 9, 2023
0797c70
remove todo
Alex6323 Nov 9, 2023
1766d74
Merge branch '2.0' into feat/workscore
Alex6323 Nov 9, 2023
db8266f
first impl
Alex6323 Nov 13, 2023
363a153
Merge branch 'upstream/2.0' into feat/workscore
Alex6323 Nov 20, 2023
fac3fd2
review 1
Alex6323 Nov 20, 2023
4665f7f
Merge branch '2.0' into feat/workscore
Alex6323 Nov 20, 2023
7aaae7d
review 2
Alex6323 Nov 20, 2023
d7929c6
review 3
Alex6323 Nov 20, 2023
ecf08e3
fix copyright year
Alex6323 Nov 20, 2023
7098d84
Merge branch '2.0' into feat/workscore
Alex6323 Nov 20, 2023
05edd44
Merge branch '2.0' into feat/workscore
Alex6323 Nov 20, 2023
15bfd4f
Merge branch '2.0' into feat/workscore
thibault-martinez Nov 21, 2023
711c35b
StorageScore :heart: WorkScore
Alex6323 Nov 21, 2023
8d6b744
Fix def_is_as_opt panic message (#1659)
thibault-martinez Nov 21, 2023
5f8e14f
Remove native tokens from AnchorOutput (#1660)
thibault-martinez Nov 21, 2023
21217b9
Python: add multi address (#1658)
Alex6323 Nov 21, 2023
e449156
fmt
Alex6323 Nov 21, 2023
f48a8cf
align + more impls
Alex6323 Nov 21, 2023
dc921e8
Merge branch '2.0' into feat/workscore
Alex6323 Nov 21, 2023
913a5b6
nit
Alex6323 Nov 21, 2023
87d56af
default impl for all features
Alex6323 Nov 22, 2023
9861038
default impl for all unlocks
Alex6323 Nov 22, 2023
d778512
self
Alex6323 Nov 22, 2023
751935c
Merge branch '2.0' into feat/workscore
Alex6323 Nov 22, 2023
620598b
Merge branch '2.0' into feat/workscore
thibault-martinez Nov 22, 2023
5d3d1e0
rm WorkScore impl for NativeTokens type
Alex6323 Nov 23, 2023
d23fb61
review suggestions
Alex6323 Nov 23, 2023
2fad332
align and improve
Alex6323 Nov 24, 2023
52dc638
cleanup
Alex6323 Nov 24, 2023
dfe23e9
Merge branch '2.0' into feat/workscore
Alex6323 Nov 24, 2023
01906c0
cleanup 2
Alex6323 Nov 24, 2023
3a303e0
forward work score in NativeTokenFeature
Alex6323 Nov 24, 2023
3fa452a
cleanup 3
Alex6323 Nov 24, 2023
d002ed1
forward 2
Alex6323 Nov 24, 2023
f5ee525
let's see if we're faster than thibault
Alex6323 Nov 24, 2023
f19d7dc
unnecessary import
Alex6323 Nov 24, 2023
80d2f38
underscore
Alex6323 Nov 24, 2023
34a0b99
Merge branch '2.0' into feat/workscore
Alex6323 Nov 24, 2023
787067c
final touches
Alex6323 Nov 24, 2023
ab6501e
remove todo
Alex6323 Nov 24, 2023
9f751e0
rm mana_cost fn from work score trait
Alex6323 Nov 27, 2023
80d982c
Merge branch '2.0' into feat/workscore
Alex6323 Nov 27, 2023
1f5e1af
Merge branch 'feat/workscore' into feat/types/calc-max-burned-mana
Alex6323 Nov 27, 2023
3e074ca
set max burned mana to minimum
Alex6323 Nov 27, 2023
bf6711e
Merge branch '2.0' into feat/types/calc-max-burned-mana
Alex6323 Nov 27, 2023
146db38
use builder
Alex6323 Nov 27, 2023
0fe0539
remnant
Alex6323 Nov 27, 2023
f390b58
use with_ instead of ctro to set mana
Alex6323 Nov 27, 2023
d6693b9
nit
Alex6323 Nov 27, 2023
0d6842c
another remnant
Alex6323 Nov 27, 2023
fa69705
ctor enum approach
Alex6323 Nov 28, 2023
24329d7
Merge branch '2.0' into feat/types/calc-max-burned-mana
Alex6323 Nov 28, 2023
7cdc9ac
rand
Alex6323 Nov 28, 2023
18f4885
remove builder method
Alex6323 Nov 28, 2023
fe883ff
Merge branch '2.0' into feat/types/calc-max-burned-mana
thibault-martinez Nov 29, 2023
09c8305
pr comments
Alex6323 Nov 29, 2023
e048bf1
improve convenience
Alex6323 Nov 29, 2023
da050fc
simplify
Alex6323 Nov 29, 2023
4c501d9
Merge branch '2.0' into feat/types/calc-max-burned-mana
Alex6323 Nov 29, 2023
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
21 changes: 14 additions & 7 deletions sdk/src/client/api/block_builder/mod.rs
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ pub use self::transaction::verify_semantic;
use crate::{
client::{ClientInner, Result},
types::block::{
core::{BlockHeader, UnsignedBlock},
core::{basic::MaxBurnedManaAmount, BasicBlockBody, BlockHeader, UnsignedBlock},
output::AccountId,
payload::Payload,
protocol::{WorkScore, WorkScoreParameters},
BlockBody,
},
};
Expand Down Expand Up @@ -43,12 +44,18 @@ impl ClientInner {
issuance.latest_finalized_slot,
issuer_id,
),
// TODO: burned mana calculation
BlockBody::build_basic(issuance.strong_parents()?, 0)
.with_weak_parents(issuance.weak_parents()?)
.with_shallow_like_parents(issuance.shallow_like_parents()?)
.with_payload(payload)
.finish_block_body()?,
BlockBody::build_basic(
issuance.strong_parents()?,
MaxBurnedManaAmount::MinimumAmount {
params: protocol_params.work_score_parameters,
reference_mana_cost: issuance.commitment.reference_mana_cost(),
},
)
.with_weak_parents(issuance.weak_parents()?)
.with_shallow_like_parents(issuance.shallow_like_parents()?)
.with_payload(payload)
.finish()?
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
.into(),
))
}
}
85 changes: 63 additions & 22 deletions sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,54 @@ pub type StrongParents = Parents<1, 8>;
pub type WeakParents = Parents<0, 8>;
pub type ShallowLikeParents = Parents<0, 8>;

#[derive(Copy, Clone)]
pub enum MaxBurnedManaAmount {
Amount(u64),
MinimumAmount {
params: WorkScoreParameters,
reference_mana_cost: u64,
},
}

/// A builder for a [`BasicBlockBody`].
pub struct BasicBlockBodyBuilder {
strong_parents: StrongParents,
weak_parents: WeakParents,
shallow_like_parents: ShallowLikeParents,
payload: OptionalPayload,
max_burned_mana: u64,
max_burned_mana: MaxBurnedManaAmount,
}

impl BasicBlockBodyBuilder {
/// Creates a new [`BasicBlockBodyBuilder`] with `max burned mana` set to the provided value.
#[inline(always)]
pub fn new_with_mana_amount(strong_parents: StrongParents, max_burned_mana: u64) -> Self {
Self::new(strong_parents, MaxBurnedManaAmount::Amount(max_burned_mana))
}

/// Creates a new [`BasicBlockBodyBuilder`] with `max burned mana` set to the minimum value according to the
/// provided reference mana cost.
#[inline(always)]
pub fn new_with_minimum_mana_amount(
strong_parents: StrongParents,
params: WorkScoreParameters,
reference_mana_cost: u64,
) -> Self {
Self {
strong_parents,
weak_parents: WeakParents::default(),
shallow_like_parents: ShallowLikeParents::default(),
payload: OptionalPayload::default(),
max_burned_mana: MaxBurnedManaAmount::MinimumAmount {
params,
reference_mana_cost,
},
}
}

/// Creates a new [`BasicBlockBodyBuilder`].
#[inline(always)]
pub fn new(strong_parents: StrongParents, max_burned_mana: u64) -> Self {
pub fn new(strong_parents: StrongParents, max_burned_mana: MaxBurnedManaAmount) -> Self {
Self {
strong_parents,
weak_parents: WeakParents::default(),
Expand Down Expand Up @@ -64,24 +99,27 @@ impl BasicBlockBodyBuilder {
self
}

/// Adds max burned mana to a [`BasicBlockBodyBuilder`].
#[inline(always)]
pub fn with_max_burned_mana(mut self, max_burned_mana: u64) -> Self {
self.max_burned_mana = max_burned_mana;
self
}

/// Finishes the builder into a [`BasicBlockBody`].
pub fn finish(self) -> Result<BasicBlockBody, Error> {
verify_parents_sets(&self.strong_parents, &self.weak_parents, &self.shallow_like_parents)?;

Ok(BasicBlockBody {
let mut body = BasicBlockBody {
strong_parents: self.strong_parents,
weak_parents: self.weak_parents,
shallow_like_parents: self.shallow_like_parents,
payload: self.payload,
max_burned_mana: self.max_burned_mana,
})
max_burned_mana: 0,
};

body.max_burned_mana = match self.max_burned_mana {
MaxBurnedManaAmount::Amount(amount) => amount,
MaxBurnedManaAmount::MinimumAmount {
params,
reference_mana_cost,
} => body.work_score(params) as u64 * reference_mana_cost,
};

Ok(body)
}

/// Finishes the builder into a [`BlockBody`].
Expand All @@ -97,7 +135,7 @@ impl From<BasicBlockBody> for BasicBlockBodyBuilder {
weak_parents: value.weak_parents,
shallow_like_parents: value.shallow_like_parents,
payload: value.payload,
max_burned_mana: value.max_burned_mana,
max_burned_mana: MaxBurnedManaAmount::Amount(value.max_burned_mana),
}
}
}
Expand Down Expand Up @@ -228,15 +266,18 @@ pub(crate) mod dto {
dto: BasicBlockBodyDto,
params: Option<&ProtocolParameters>,
) -> Result<Self, Self::Error> {
BasicBlockBodyBuilder::new(StrongParents::from_set(dto.strong_parents)?, dto.max_burned_mana)
.with_weak_parents(WeakParents::from_set(dto.weak_parents)?)
.with_shallow_like_parents(ShallowLikeParents::from_set(dto.shallow_like_parents)?)
.with_payload(
dto.payload
.map(|payload| Payload::try_from_dto_with_params_inner(payload, params))
.transpose()?,
)
.finish()
BasicBlockBodyBuilder::new_with_mana_amount(
StrongParents::from_set(dto.strong_parents)?,
dto.max_burned_mana,
)
.with_weak_parents(WeakParents::from_set(dto.weak_parents)?)
.with_shallow_like_parents(ShallowLikeParents::from_set(dto.shallow_like_parents)?)
.with_payload(
dto.payload
.map(|payload| Payload::try_from_dto_with_params_inner(payload, params))
.transpose()?,
)
.finish()
}
}
}
6 changes: 5 additions & 1 deletion sdk/src/types/block/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::{
validation::{ValidationBlockBody, ValidationBlockBodyBuilder},
};
use crate::types::block::{
core::basic::MaxBurnedManaAmount,
protocol::{ProtocolParameters, ProtocolParametersHash, WorkScore, WorkScoreParameters},
Error,
};
Expand Down Expand Up @@ -81,7 +82,10 @@ impl BlockBody {

/// Creates a new [`BasicBlockBodyBuilder`].
#[inline(always)]
pub fn build_basic(strong_parents: self::basic::StrongParents, max_burned_mana: u64) -> BasicBlockBodyBuilder {
pub fn build_basic(
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
strong_parents: self::basic::StrongParents,
max_burned_mana: MaxBurnedManaAmount,
) -> BasicBlockBodyBuilder {
BasicBlockBodyBuilder::new(strong_parents, max_burned_mana)
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/rand/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn rand_basic_block_body_with_strong_parents(strong_parents: basic::StrongPa
pub fn rand_basic_block_body_builder_with_strong_parents(
strong_parents: basic::StrongParents,
) -> BasicBlockBodyBuilder {
BlockBody::build_basic(strong_parents, rand_number())
BlockBody::build_basic(strong_parents, basic::MaxBurnedManaAmount::Amount(rand_number()))
}

/// Generates a random block with given block body.
Expand Down
Loading