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

Implementing get_size for everything #197

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
18 changes: 17 additions & 1 deletion programs/mpl-core/src/plugins/external_plugin_adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use strum::EnumCount;
use crate::{
error::MplCoreError,
plugins::{approve, reject},
state::{AssetV1, SolanaAccount},
state::{AssetV1, DataBlob, SolanaAccount},
};

use super::{
Expand Down Expand Up @@ -39,6 +39,14 @@ pub enum ExternalPluginAdapterType {
DataSection,
}

impl DataBlob for ExternalPluginAdapterType {
const BASE_LEN: usize = 1;

fn len(&self) -> usize {
Self::BASE_LEN
}
}

impl From<&ExternalPluginAdapterKey> for ExternalPluginAdapterType {
fn from(key: &ExternalPluginAdapterKey) -> Self {
match key {
Expand Down Expand Up @@ -398,6 +406,14 @@ pub enum HookableLifecycleEvent {
Update,
}

impl DataBlob for HookableLifecycleEvent {
const BASE_LEN: usize = 1;

fn len(&self) -> usize {
Self::BASE_LEN
}
}

/// Prefix used with some of the `ExtraAccounts` that are PDAs.
pub const MPL_CORE_PREFIX: &str = "mpl-core";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ use crate::{
pub struct AddBlocker {}

impl DataBlob for AddBlocker {
fn get_initial_size() -> usize {
0
}
const BASE_LEN: usize = 0;

fn get_size(&self) -> usize {
0
fn len(&self) -> usize {
Self::BASE_LEN
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ use borsh::{BorshDeserialize, BorshSerialize};
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Default)]
pub struct Attribute {
/// The Key of the attribute.
pub key: String, // 4
pub key: String, // 4 + len
/// The Value of the attribute.
pub value: String, // 4
pub value: String, // 4 + len
}

impl DataBlob for Attribute {
const BASE_LEN: usize = 4 // The length of the Key string
+ 4; // The length of the Value string

fn len(&self) -> usize {
Self::BASE_LEN + self.key.len() + self.value.len()
}
}

/// The Attributes plugin allows the authority to add arbitrary Key-Value pairs to the asset.
#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Default)]
pub struct Attributes {
/// A vector of Key-Value pairs.
pub attribute_list: Vec<Attribute>, // 4
pub attribute_list: Vec<Attribute>, // 4 + len * Attribute
}

impl Attributes {
Expand All @@ -27,12 +36,15 @@ impl Attributes {
}

impl DataBlob for Attributes {
fn get_initial_size() -> usize {
4
}
const BASE_LEN: usize = 4; // The length of the attribute list

fn get_size(&self) -> usize {
4 // TODO: Implement this.
fn len(&self) -> usize {
Self::BASE_LEN
+ self
.attribute_list
.iter()
.map(|attr| attr.len())
.sum::<usize>()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ use crate::{
pub struct ImmutableMetadata {}

impl DataBlob for ImmutableMetadata {
fn get_initial_size() -> usize {
0
}
const BASE_LEN: usize = 0;

fn get_size(&self) -> usize {
fn len(&self) -> usize {
0
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
use borsh::{BorshDeserialize, BorshSerialize};

use crate::plugins::PluginValidation;
use crate::{plugins::PluginValidation, state::DataBlob};

/// The master edition plugin allows the creator to specify details on the master edition including max supply, name, and uri.
/// The default authority for this plugin is the creator.
#[repr(C)]
#[derive(Clone, BorshSerialize, BorshDeserialize, Default, Debug, PartialEq, Eq)]
pub struct MasterEdition {
/// The max supply of editions
pub max_supply: Option<u32>,
pub max_supply: Option<u32>, // 1 + optional 4
/// optional master edition name
pub name: Option<String>,
pub name: Option<String>, // 1 + optional 4
/// optional master edition uri
pub uri: Option<String>,
pub uri: Option<String>, // 1 + optional 4
}

impl PluginValidation for MasterEdition {}

impl DataBlob for MasterEdition {
const BASE_LEN: usize = 1 // The max_supply option
+ 1 // The name option
+ 1; // The uri option

fn len(&self) -> usize {
Self::BASE_LEN
+ self.max_supply.map_or(0, |_| 4)
+ self.name.as_ref().map_or(0, |name| 4 + name.len())
+ self.uri.as_ref().map_or(0, |uri| 4 + uri.len())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,70 @@ use crate::error::MplCoreError;
use crate::plugins::{
abstain, reject, Plugin, PluginValidation, PluginValidationContext, ValidationResult,
};
use crate::state::DataBlob;

/// The creator on an asset and whether or not they are verified.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct Creator {
address: Pubkey,
percentage: u8,
address: Pubkey, // 32
percentage: u8, // 1
}

impl DataBlob for Creator {
const BASE_LEN: usize = 32 // The address
+ 1; // The percentage

fn len(&self) -> usize {
Self::BASE_LEN
}
}

/// The rule set for an asset indicating where it is allowed to be transferred.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub enum RuleSet {
/// No rules are enforced.
None,
None, // 1
/// Allow list of programs that are allowed to transfer, receive, or send the asset.
ProgramAllowList(Vec<Pubkey>),
ProgramAllowList(Vec<Pubkey>), // 4
/// Deny list of programs that are not allowed to transfer, receive, or send the asset.
ProgramDenyList(Vec<Pubkey>),
ProgramDenyList(Vec<Pubkey>), // 4
Comment on lines +37 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: consistency, sometimes the commented length is the initial/default length of zero, other times it is listed as like 4 + len * Object

}

impl DataBlob for RuleSet {
const BASE_LEN: usize = 1; // The rule set discriminator

fn len(&self) -> usize {
Self::BASE_LEN
+ match self {
RuleSet::ProgramAllowList(allow_list) => 4 + allow_list.len() * 32,
RuleSet::ProgramDenyList(deny_list) => 4 + deny_list.len() * 32,
RuleSet::None => 0,
}
}
}

/// Traditional royalties structure for an asset.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
pub struct Royalties {
/// The percentage of royalties to be paid to the creators.
basis_points: u16,
basis_points: u16, // 2
/// A list of creators to receive royalties.
creators: Vec<Creator>,
creators: Vec<Creator>, // 4
/// The rule set for the asset to enforce royalties.
rule_set: RuleSet,
rule_set: RuleSet, // 1
}

impl DataBlob for Royalties {
const BASE_LEN: usize = 2 // basis_points
+ 4 // creators length
+ RuleSet::BASE_LEN; // rule_set

fn len(&self) -> usize {
2 // basis_points
+ 4 // creators length
+ self.creators.iter().map(|creator| creator.len()).sum::<usize>()
+ self.rule_set.len() // rule_set
}
}

fn validate_royalties(royalties: &Royalties) -> Result<ValidationResult, ProgramError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::plugins::{
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct UpdateDelegate {
/// Additional update delegates. Not currently available to be used.
pub additional_delegates: Vec<Pubkey>, // 4
pub additional_delegates: Vec<Pubkey>, // 4 + len * 32
}

impl UpdateDelegate {
Expand All @@ -38,12 +38,10 @@ impl Default for UpdateDelegate {
}

impl DataBlob for UpdateDelegate {
fn get_initial_size() -> usize {
0
}
const BASE_LEN: usize = 4; // The additional delegates length

fn get_size(&self) -> usize {
0
fn len(&self) -> usize {
Self::BASE_LEN + self.additional_delegates.len() * 32
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,37 @@ use crate::error::MplCoreError;
use crate::plugins::{
abstain, Plugin, PluginValidation, PluginValidationContext, ValidationResult,
};
use crate::state::DataBlob;

/// The creator on an asset and whether or not they are verified.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Hash)]
pub struct VerifiedCreatorsSignature {
address: Pubkey,
verified: bool,
address: Pubkey, // 32
verified: bool, // 1
}

impl DataBlob for VerifiedCreatorsSignature {
const BASE_LEN: usize = 32 // The address
+ 1; // The verified boolean

fn len(&self) -> usize {
Self::BASE_LEN
}
}

/// Structure for storing verified creators, often used in conjunction with the Royalties plugin
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
pub struct VerifiedCreators {
/// A list of signatures
signatures: Vec<VerifiedCreatorsSignature>,
signatures: Vec<VerifiedCreatorsSignature>, // 4 + len * VerifiedCreatorsSignature
}

impl DataBlob for VerifiedCreators {
const BASE_LEN: usize = 4; // The signatures length

fn len(&self) -> usize {
Self::BASE_LEN + self.signatures.iter().map(|sig| sig.len()).sum::<usize>()
}
}

struct SignatureChangeIndices {
Expand Down
24 changes: 21 additions & 3 deletions programs/mpl-core/src/plugins/internal/owner_managed/autograph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ use crate::{
plugins::{
abstain, approve, Plugin, PluginValidation, PluginValidationContext, ValidationResult,
},
state::DataBlob,
};

/// The creator on an asset and whether or not they are verified.
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Hash)]
pub struct AutographSignature {
address: Pubkey,
message: String,
address: Pubkey, // 32
message: String, // 4 + len
}

impl DataBlob for AutographSignature {
const BASE_LEN: usize = 32 // The address
+ 4; // The message length

fn len(&self) -> usize {
Self::BASE_LEN + self.message.len()
}
}

/// Structure for an autograph book, often used in conjunction with the Royalties plugin for verified creators
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, Eq, PartialEq)]
pub struct Autograph {
/// A list of signatures with option message
signatures: Vec<AutographSignature>,
signatures: Vec<AutographSignature>, // 4 + len * Autograph len
}

fn validate_autograph(
Expand Down Expand Up @@ -122,3 +132,11 @@ impl PluginValidation for Autograph {
}
}
}

impl DataBlob for Autograph {
const BASE_LEN: usize = 4; // The signatures length

fn len(&self) -> usize {
Self::BASE_LEN + self.signatures.iter().map(|sig| sig.len()).sum::<usize>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ impl Default for BurnDelegate {
}

impl DataBlob for BurnDelegate {
fn get_initial_size() -> usize {
0
}
const BASE_LEN: usize = 0;

fn get_size(&self) -> usize {
fn len(&self) -> usize {
0
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ impl Default for FreezeDelegate {
}

impl DataBlob for FreezeDelegate {
fn get_initial_size() -> usize {
1
}
const BASE_LEN: usize = 1; // The frozen boolean

fn get_size(&self) -> usize {
1
fn len(&self) -> usize {
Self::BASE_LEN
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ impl Default for TransferDelegate {
}

impl DataBlob for TransferDelegate {
fn get_initial_size() -> usize {
0
}
const BASE_LEN: usize = 0;

fn get_size(&self) -> usize {
0
fn len(&self) -> usize {
Self::BASE_LEN
}
}

Expand Down
Loading
Loading