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

token-2022: Add pod-compatible mint / account / multisig types #6337

Merged
merged 3 commits into from
Mar 6, 2024

Conversation

joncinque
Copy link
Contributor

Problem

In order to use the Pod-compatible types in #6336, we need Pod-compatible base types.

Solution

Mostly copy-paste of the types in state.rs, but only using types that align to 1. Also introduces PodCOption<T> for a Pod-compatible COption

These will eventually be used for the extension tests.

spl_pod::primitives::PodU64,
};

/// Mint data stored as a Pod type
Copy link
Contributor

@CriesofCarrots CriesofCarrots Mar 5, 2024

Choose a reason for hiding this comment

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

Maybe link to the original type doc?
It would also be nice to have a unit test or something that ensures the Pod structs can never diverge from the original types, perhaps by comparing packed versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly, done!

pub supply: PodU64,
/// Number of base 10 digits to the right of the decimal place.
pub decimals: u8,
/// Is not 0 if this structure has been initialized
Copy link
Contributor

Choose a reason for hiding this comment

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

Just "not 0"? Or "exactly 1"?
Also, don't we have a PodBool in spl-pod? Any reason not to use that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah sure, can do!

/// If `delegate` is `Some` then `delegated_amount` represents
/// the amount authorized by the delegate
pub delegate: PodCOption<Pubkey>,
/// The account's state, stored as u8
Copy link
Contributor

Choose a reason for hiding this comment

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

Link to AccountState doc? Not clear what these u8s will mean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

pub m: u8,
/// Number of valid signers
pub n: u8,
/// If not 0, this structure has been initialized
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto about non-0 and PodBool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

Comment on lines 93 to 99
/// COption<T> stored as a Pod type
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
pub struct PodCOption<T: Pod> {
option: [u8; 4],
value: T,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Any value in adding this to spl-pod?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd prefer to keep it here since we have the OptionalNonZero* types in spl-pod, which are more space-efficient and more strictly Pod types. I'm worried it would create confusion by having this type in there too

}
}
impl PackedSizeOf for PodMint {
const SIZE_OF: usize = std::mem::size_of::<Self>();
Copy link
Contributor

Choose a reason for hiding this comment

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

While this expression is simple enough, maybe use spl_pod::pod_get_packed_len::<Self>() for consistency? Don't feel too strongly, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point, will do!

@joncinque
Copy link
Contributor Author

All points should be addressed now, thanks for your careful review!

CriesofCarrots
CriesofCarrots previously approved these changes Mar 6, 2024
Copy link
Contributor

@CriesofCarrots CriesofCarrots left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for all the polish.
One last nit, take it or leave it.

}
impl IsInitialized for PodMint {
fn is_initialized(&self) -> bool {
self.is_initialized == PodBool::from_bool(true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.is_initialized == PodBool::from_bool(true)
self.is_initialized.into()

Better?

It would actually be kind of cute if PodBool implemented PartialEq<bool> for PodBool so that this line could be:
self.is_initialized == true
I can't really find a way to simplify it further to just self.is_initialized, though. There's a trait for Not, but not one for Is aside from PartialEq/Eq.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah, much better! We might need to address the PartialEq / Eq question in a follow-up PR, but your suggestion is much clearer

}
impl IsInitialized for PodMultisig {
fn is_initialized(&self) -> bool {
self.is_initialized == PodBool::from_bool(true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too, if you like my suggestion above.

@mergify mergify bot dismissed CriesofCarrots’s stale review March 6, 2024 12:08

Pull request has been modified.

@joncinque joncinque merged commit 4b38452 into solana-labs:master Mar 6, 2024
35 checks passed
@joncinque joncinque deleted the tk22type branch March 6, 2024 12:44
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.

2 participants