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

Add work score trait and impls #1235

Merged
merged 60 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
60 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
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
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
22 changes: 21 additions & 1 deletion sdk/src/types/block/core/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use packable::{
use crate::types::block::{
core::{parent::verify_parents_sets, Block, Parents},
payload::{OptionalPayload, Payload},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
Error,
};

Expand Down Expand Up @@ -156,6 +156,26 @@ impl BasicBlock {
}
}

impl WorkScore for BasicBlock {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let missing_parent_score = {
let min_strong_parents_threshold = work_score_params.min_strong_parents_threshold as usize;
if self.strong_parents.len() < min_strong_parents_threshold {
let missing_parents_count = min_strong_parents_threshold - self.strong_parents.len();
work_score_params.missing_parent * missing_parents_count as u32
} else {
0
}
};
let payload_score = self
.payload
.as_ref()
.map(|p| p.work_score(work_score_params))
.unwrap_or(0);
work_score_params.block + missing_parent_score + payload_score
}
}

impl Packable for BasicBlock {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
12 changes: 11 additions & 1 deletion sdk/src/types/block/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ pub use self::{
validation::{ValidationBlock, ValidationBlockBuilder},
wrapper::{BlockHeader, BlockWrapper, BlockWrapperBuilder},
};
use super::protocol::WorkScore;
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
use crate::types::block::{
protocol::{ProtocolParameters, ProtocolParametersHash},
protocol::{ProtocolParameters, ProtocolParametersHash, WorkScoreStructure},
Error,
};

Expand Down Expand Up @@ -130,6 +131,15 @@ impl Block {
}
}

impl WorkScore for Block {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
match self {
Self::Basic(basic) => basic.work_score(work_score_params),
Self::Validation(validation) => 0,
}
}
}

impl Packable for Block {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/core/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use packable::{

use crate::types::block::{
core::{parent::verify_parents_sets, Block, Parents},
protocol::{ProtocolParameters, ProtocolParametersHash},
protocol::{ProtocolParameters, ProtocolParametersHash, WorkScore, WorkScoreStructure},
Error,
};

Expand Down
10 changes: 9 additions & 1 deletion sdk/src/types/block/core/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use packable::{
use crate::types::block::{
block_id::{BlockHash, BlockId},
core::{BasicBlock, ValidationBlock},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
signature::Signature,
slot::{SlotCommitmentId, SlotIndex},
Block, Error, IssuerId,
Expand Down Expand Up @@ -290,6 +290,14 @@ impl BlockWrapper {
}
}

impl WorkScore for BlockWrapper {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let block_score = self.block.work_score(work_score_params);
kwek20 marked this conversation as resolved.
Show resolved Hide resolved
let signature_score = self.signature.work_score(work_score_params);
block_score + signature_score
}
}

impl Packable for BlockWrapper {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
11 changes: 10 additions & 1 deletion sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::types::{
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier,
},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
semantic::{TransactionFailureReason, ValidationContext},
unlock::Unlock,
Error,
Expand Down Expand Up @@ -651,6 +651,15 @@ impl StateTransitionVerifier for AccountOutput {
}
}

impl WorkScore for AccountOutput {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let native_tokens_score = self.native_tokens().work_score(work_score_params);
let features_score = self.features().work_score(work_score_params);
let immutable_features_score = self.immutable_features().work_score(work_score_params);
work_score_params.output + native_tokens_score + features_score + immutable_features_score
}
}

impl Packable for AccountOutput {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
10 changes: 9 additions & 1 deletion sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::types::{
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, NativeToken,
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure,
},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
semantic::{TransactionFailureReason, ValidationContext},
unlock::Unlock,
Error,
Expand Down Expand Up @@ -323,6 +323,14 @@ impl BasicOutput {
}
}

impl WorkScore for BasicOutput {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let native_tokens_score = self.native_tokens().work_score(work_score_params);
let features_score = self.features().work_score(work_score_params);
work_score_params.output + native_tokens_score + features_score
}
}

fn verify_unlock_conditions<const VERIFY: bool>(unlock_conditions: &UnlockConditions) -> Result<(), Error> {
if VERIFY {
if unlock_conditions.address().is_none() {
Expand Down
8 changes: 7 additions & 1 deletion sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::types::{
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, Output,
OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError, StateTransitionVerifier,
},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
semantic::{TransactionFailureReason, ValidationContext},
slot::EpochIndex,
unlock::Unlock,
Expand Down Expand Up @@ -405,6 +405,12 @@ impl StateTransitionVerifier for DelegationOutput {
}
}

impl WorkScore for DelegationOutput {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
work_score_params.output
}
}

impl Packable for DelegationOutput {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
19 changes: 18 additions & 1 deletion sdk/src/types/block/output/feature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ pub use self::{
staking::StakingFeature,
tag::TagFeature,
};
use crate::types::block::{create_bitflags, Error};
use crate::types::block::{
create_bitflags,
protocol::{WorkScore, WorkScoreStructure},
Error,
};

///
#[derive(Clone, Eq, PartialEq, Hash, From, Packable)]
Expand Down Expand Up @@ -313,6 +317,19 @@ impl Features {
}
}

impl WorkScore for Features {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
self
.iter()
.map(|f| match f {
Feature::BlockIssuer(_) => work_score_params.block_issuer,
Feature::Staking(_) => work_score_params.staking,
_ => 0,
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
})
.sum::<u32>()
}
}

#[inline]
fn verify_unique_sorted<const VERIFY: bool>(features: &[Feature], _: &()) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(features.iter().map(Feature::kind)) {
Expand Down
16 changes: 15 additions & 1 deletion sdk/src/types/block/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::types::{
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier, TokenId, TokenScheme,
},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
semantic::{TransactionFailureReason, ValidationContext},
unlock::Unlock,
Error,
Expand Down Expand Up @@ -598,6 +598,20 @@ impl StateTransitionVerifier for FoundryOutput {
}
}

impl WorkScore for FoundryOutput {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let native_tokens_score = self.native_tokens().work_score(work_score_params);
let features_score = self.features().work_score(work_score_params);
let immutable_features_score = self.immutable_features().work_score(work_score_params);
let token_scheme_score = self
.token_scheme()
.is_simple()
.then_some(work_score_params.native_token)
.unwrap_or(0);
work_score_params.output + native_tokens_score + features_score + immutable_features_score + token_scheme_score
}
}

impl Packable for FoundryOutput {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
14 changes: 13 additions & 1 deletion sdk/src/types/block/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub use self::{
token_scheme::{SimpleTokenScheme, TokenScheme},
unlock_condition::{UnlockCondition, UnlockConditions},
};
use super::protocol::ProtocolParameters;
use super::protocol::{ProtocolParameters, WorkScore, WorkScoreStructure};
use crate::types::block::{address::Address, semantic::ValidationContext, slot::SlotIndex, Error};

/// The maximum number of outputs of a transaction.
Expand Down Expand Up @@ -474,6 +474,18 @@ impl Rent for Output {
}
}

impl WorkScore for Output {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
match self {
Self::Basic(basic) => basic.work_score(work_score_params),
Self::Account(account) => account.work_score(work_score_params),
Self::Foundry(foundry) => foundry.work_score(work_score_params),
Self::Nft(nft) => nft.work_score(work_score_params),
Self::Delegation(delegation) => delegation.work_score(work_score_params),
}
}
}

pub(crate) fn verify_output_amount_min(amount: u64) -> Result<(), Error> {
if amount < Output::AMOUNT_MIN {
Err(Error::InvalidOutputAmount(amount))
Expand Down
13 changes: 12 additions & 1 deletion sdk/src/types/block/output/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ use iterator_sorted::is_unique_sorted;
use packable::{bounded::BoundedU8, prefix::BoxedSlicePrefix, Packable};
use primitive_types::U256;

use crate::types::block::{output::foundry::FoundryId, Error};
use crate::types::block::{
output::foundry::FoundryId,
protocol::{WorkScore, WorkScoreStructure},
Error,
};

impl_id!(pub TokenId, 38, "Unique identifiers of native tokens. The TokenId of native tokens minted by a specific foundry is the same as the FoundryId.");

Expand Down Expand Up @@ -247,6 +251,13 @@ impl NativeTokens {
}
}

// TODO: should we also impl `WorkScore` for `NativeToken` for plain consistency?
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
impl WorkScore for NativeTokens {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
self.len() as u32 * work_score_params.native_token
kwek20 marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[inline]
fn verify_unique_sorted<const VERIFY: bool>(native_tokens: &[NativeToken], _: &()) -> Result<(), Error> {
if VERIFY && !is_unique_sorted(native_tokens.iter().map(NativeToken::token_id)) {
Expand Down
11 changes: 10 additions & 1 deletion sdk/src/types/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::types::{
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier,
},
protocol::ProtocolParameters,
protocol::{ProtocolParameters, WorkScore, WorkScoreStructure},
semantic::{TransactionFailureReason, ValidationContext},
unlock::Unlock,
Error,
Expand Down Expand Up @@ -461,6 +461,15 @@ impl StateTransitionVerifier for NftOutput {
}
}

impl WorkScore for NftOutput {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let native_tokens_score = self.native_tokens().work_score(work_score_params);
let features_score = self.features().work_score(work_score_params);
let immutable_features_score = self.immutable_features().work_score(work_score_params);
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
work_score_params.output + native_tokens_score + features_score + immutable_features_score
}
}

impl Packable for NftOutput {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
15 changes: 14 additions & 1 deletion sdk/src/types/block/payload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod tagged_data;
pub mod transaction;

use alloc::boxed::Box;
use core::ops::Deref;
use core::{mem::size_of, ops::Deref};
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved

use packable::{
error::{UnpackError, UnpackErrorExt},
Expand All @@ -21,6 +21,7 @@ pub(crate) use self::{
tagged_data::{TagLength, TaggedDataLength},
transaction::{ContextInputCount, InputCount, OutputCount},
};
use super::protocol::{WorkScore, WorkScoreStructure};
use crate::types::block::{protocol::ProtocolParameters, Error};

/// A generic payload that can represent different types defining block payloads.
Expand Down Expand Up @@ -63,6 +64,18 @@ impl Payload {
}
}

impl WorkScore for Payload {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
// 1 byte for the payload kind
let score = size_of::<u8>() as u32
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
+ match self {
Self::Transaction(transaction_payload) => transaction_payload.work_score(work_score_params),
Self::TaggedData(tagged_data_payload) => tagged_data_payload.work_score(work_score_params),
};
score
}
}

impl Packable for Payload {
type UnpackError = Error;
type UnpackVisitor = ProtocolParameters;
Expand Down
14 changes: 12 additions & 2 deletions sdk/src/types/block/payload/tagged_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ use core::ops::RangeInclusive;
use packable::{
bounded::{BoundedU32, BoundedU8},
prefix::BoxedSlicePrefix,
Packable,
Packable, PackableExt,
};

use crate::types::block::{BlockWrapper, Error};
use crate::types::block::{
protocol::{WorkScore, WorkScoreStructure},
BlockWrapper, Error,
};

pub(crate) type TagLength =
BoundedU8<{ *TaggedDataPayload::TAG_LENGTH_RANGE.start() }, { *TaggedDataPayload::TAG_LENGTH_RANGE.end() }>;
Expand Down Expand Up @@ -60,6 +63,13 @@ impl TaggedDataPayload {
}
}

impl WorkScore for TaggedDataPayload {
fn work_score(&self, work_score_params: WorkScoreStructure) -> u32 {
let size_score = self.packed_len() as u32 * work_score_params.data_byte;
size_score
}
}

impl core::fmt::Debug for TaggedDataPayload {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TaggedDataPayload")
Expand Down
Loading