Skip to content

Update AssetSuply and SupplyInfo (new)#133

Merged
dmidem merged 21 commits intozsa1from
update_asset_supply
Jan 8, 2025
Merged

Update AssetSuply and SupplyInfo (new)#133
dmidem merged 21 commits intozsa1from
update_asset_supply

Conversation

@dmidem
Copy link

@dmidem dmidem commented Jan 7, 2025

This PR is an updated copy of #128:

The amount in AssetSupply must be a NoteValue (u64) and not a ValueSum (i128).
We add reference_notes into SupplyInfo. It is a hashmap of asset bases to their respective reference note

The code was updated, get_reference_note method of IssueAction was added and used, get_reference_notes was removed. Also, instead of a separate new reference_notes HashMap in SupplyInfo a new field reference_note was added to AssetSupply struct. Unit tests were fixed accordingly.

ConstanceBeguier and others added 13 commits December 19, 2024 17:13
Co-authored-by: Dmitry Demin <dmidem@users.noreply.github.com>
…e type of the value was changed from ValueSum to NoteValue
…nfig Ubuntu package to be installed"

This reverts commit c30e108.
…ich is required by the yeslogic-fontconfig-sys crate (a transitive dev dependency). The CI error started occurring after GitHub updated ubuntu-latest from Ubuntu 22.04 to 24.04.
…s field from SupplyInfo, add and use get_reference_note method to IssueAction
@QED-it QED-it deleted a comment from what-the-diff bot Jan 7, 2025
if !asset_set.insert(asset) {
return Err(BurnError::DuplicateAsset);
}
for (asset, value) in burn.into_iter().cloned() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to clone the iterator?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .cloned() method clones the iterating value, not the iterator itself. However, since we changed burn to be a slice, we don't need it now, so I removed it.

…tion in burn_validation, supply_info, and issuance
Copy link
Collaborator

@PaulLaux PaulLaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comments, one is significant.

Comment on lines +172 to +176
pub fn get_reference_note(&self) -> Option<&Note> {
self.notes.iter().find(|note| {
(note.recipient() == ReferenceKeys::recipient()) && (note.value() == NoteValue::zero())
})
}
Copy link
Collaborator

@PaulLaux PaulLaux Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So according to the finalized spec, the reference note must be the first note.
The logic should be:

  • get first note
  • check if it is the ref note
  • if yes, return.

with .find() we might get a non deterministic behavior.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the existing verify_reference_note()

Copy link
Author

@dmidem dmidem Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if the first note doesn't match the reference note condition, it should return None even if, for example, there's the second note that does match the condition?

Now I changed it this way:

    /// Returns the reference note.
    pub fn get_reference_note(&self) -> Option<&Note> {
        self.notes.first().filter(|note| {
            (note.recipient() == ReferenceKeys::recipient()) && (note.value() == NoteValue::zero())
        })
    }

It considers the first note only, and Option::filter additionally checks the referenence note condition for the first note.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter is fine
but the logic to verify a referance note repeat itself many times: see verify_reference_note() x2 in tests.
please extract and use the extracted fn ->bool here and in both tests.

Also update doc for this function.

Copy link
Author

@dmidem dmidem Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I added new function:

fn is_reference_note(note: &Note) -> bool {
    note.value() == NoteValue::zero() && note.recipient() == ReferenceKeys::recipient()
}

and used it in get_reference_note and verify_reference_note, but verify_reference_note still has an additional check for asset matching, which does make sense for the tests, but looks duplicated for get_reference_note as we already checked this in IssueAction::verify_supply. So, I kept verify_reference_note as a separate function that calls is_reference_note and performs an additional check for the asset matching.

}

fn sum<'a, T: IntoIterator<Item = &'a AssetSupply>>(supplies: T) -> Option<ValueSum> {
fn sum<'a, T: IntoIterator<Item = &'a AssetSupply>>(supplies: T) -> Option<NoteValue> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'a is not required:
fn sum<T: IntoIterator<Item = AssetSupply>>(supplies: T) -> Option<NoteValue> {

Copy link
Author

@dmidem dmidem Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed 'a.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it is needed :/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

/// * Any asset in the `burn` vector has a zero value (`BurnError::ZeroAmount`).
/// * Any asset in the `burn` vector is not unique (`BurnError::DuplicateAsset`).
pub fn validate_bundle_burn(burn: &[(AssetBase, NoteValue)]) -> Result<(), BurnError> {
let mut burn_set = HashMap::new();
Copy link
Collaborator

@ConstanceBeguier ConstanceBeguier Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap could be replaced by a HashSet with only the AssetBase inside it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced.

Copy link
Collaborator

@PaulLaux PaulLaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice,
Please consider #133 (comment) and merge

@dmidem dmidem merged commit 69f92a3 into zsa1 Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants