Skip to content

Commit

Permalink
Minor refactor to change name and make base size a constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Dec 6, 2024
1 parent d8aae30 commit 55f12f6
Show file tree
Hide file tree
Showing 30 changed files with 287 additions and 244 deletions.
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 @@ -12,12 +12,11 @@ pub struct Attribute {
}

impl DataBlob for Attribute {
fn get_initial_size() -> usize {
4 + 4
}
const BASE_LEN: usize = 4 // The length of the Key string
+ 4; // The length of the Value string

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

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

impl DataBlob for Attributes {
fn get_initial_size() -> usize {
4
}

fn get_size(&self) -> usize {
4 + self
.attribute_list
.iter()
.fold(0, |acc, attr| acc + attr.get_size())
const BASE_LEN: usize = 4; // The length of the attribute list

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
Expand Up @@ -18,15 +18,14 @@ pub struct MasterEdition {
impl PluginValidation for MasterEdition {}

impl DataBlob for MasterEdition {
fn get_initial_size() -> usize {
1 + 1 + 1
}
const BASE_LEN: usize = 1 // The max_supply option
+ 1 // The name option
+ 1; // The uri option

fn get_size(&self) -> usize {
1 + self.max_supply.map_or(0, |_| 4)
+ 1
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())
+ 1
+ self.uri.as_ref().map_or(0, |uri| 4 + uri.len())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ pub struct Creator {
}

impl DataBlob for Creator {
fn get_initial_size() -> usize {
33
}
const BASE_LEN: usize = 32 // The address
+ 1; // The percentage

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

Expand All @@ -39,16 +38,15 @@ pub enum RuleSet {
}

impl DataBlob for RuleSet {
fn get_initial_size() -> usize {
1
}

fn get_size(&self) -> usize {
1 + match self {
RuleSet::ProgramAllowList(allow_list) => 4 + allow_list.len() * 32,
RuleSet::ProgramDenyList(deny_list) => 4 + deny_list.len() * 32,
RuleSet::None => 0,
}
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,
}
}
}

Expand All @@ -64,14 +62,15 @@ pub struct Royalties {
}

impl DataBlob for Royalties {
fn get_initial_size() -> usize {
1
}
const BASE_LEN: usize = 2 // basis_points
+ 4 // creators length
+ RuleSet::BASE_LEN; // rule_set

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

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

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

fn get_size(&self) -> usize {
4 + self.additional_delegates.len() * 32
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 @@ -18,12 +18,11 @@ pub struct VerifiedCreatorsSignature {
}

impl DataBlob for VerifiedCreatorsSignature {
fn get_initial_size() -> usize {
32 + 1
}
const BASE_LEN: usize = 32 // The address
+ 1; // The verified boolean

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

Expand All @@ -35,12 +34,10 @@ pub struct VerifiedCreators {
}

impl DataBlob for VerifiedCreators {
fn get_initial_size() -> usize {
4
}
const BASE_LEN: usize = 4; // The signatures length

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

Expand Down
20 changes: 7 additions & 13 deletions programs/mpl-core/src/plugins/internal/owner_managed/autograph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ pub struct AutographSignature {
}

impl DataBlob for AutographSignature {
fn get_initial_size() -> usize {
32 + 4
}
const BASE_LEN: usize = 32 // The address
+ 4; // The message length

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

Expand Down Expand Up @@ -135,14 +134,9 @@ impl PluginValidation for Autograph {
}

impl DataBlob for Autograph {
fn get_initial_size() -> usize {
4
}
const BASE_LEN: usize = 4; // The signatures length

fn get_size(&self) -> usize {
4 + self
.signatures
.iter()
.fold(0, |acc, sig| acc + sig.get_size())
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
8 changes: 3 additions & 5 deletions programs/mpl-core/src/plugins/internal/permanent/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ impl PluginValidation for Edition {
}

impl DataBlob for Edition {
fn get_initial_size() -> usize {
4
}
const BASE_LEN: usize = 4; // The edition number

fn get_size(&self) -> usize {
4
fn len(&self) -> usize {
Self::BASE_LEN
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ use crate::{
pub struct PermanentBurnDelegate {}

impl DataBlob for PermanentBurnDelegate {
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 @@ -31,12 +31,10 @@ impl Default for PermanentFreezeDelegate {
}

impl DataBlob for PermanentFreezeDelegate {
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 @@ -15,12 +15,10 @@ use crate::plugins::{
pub struct PermanentTransferDelegate {}

impl DataBlob for PermanentTransferDelegate {
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

0 comments on commit 55f12f6

Please sign in to comment.