Skip to content

Commit

Permalink
Merge pull request #8 from ASCorreia/main
Browse files Browse the repository at this point in the history
Updated space calculation
  • Loading branch information
mikemaccana authored Jun 3, 2024
2 parents a6e4780 + 7f030b0 commit 9b5e576
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 8 additions & 0 deletions programs/anchor-movie-review-program/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub const ANCHOR_DISCRIMINATOR: usize = 8;
pub const PUBKEY_SIZE: usize = 32;
pub const U8_SIZE: usize = 1;
pub const STRING_LENGTH_PREFIX: usize = 4;
pub const MAX_DESCRIPTION_LENGTH: usize = 50;
pub const MAX_TITLE_LENGTH: usize = 20;
pub const MAX_RATING: u8 = 5;
pub const MIN_RATING: u8 = 1;
42 changes: 40 additions & 2 deletions programs/anchor-movie-review-program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
use anchor_lang::prelude::*;

pub mod constants;

pub use constants::*;

declare_id!("FmzAVsBmJWcfkfe7VrvEi7pLA9ALLDWB3NoU2MvLrCZj");

#[program]
pub mod anchor_movie_review_program {
use super::*;

pub fn add_movie_review(ctx: Context<AddMovieReview>, title: String, description: String, rating: u8) -> Result<()> {
// We require that the rating is between 1 and 5
require!(rating >= MIN_RATING && rating <= MAX_RATING, MovieReviewError::InvalidRating);

// We require that the title is not longer than 20 characters
require!(title.len() <= MAX_TITLE_LENGTH, MovieReviewError::TitleTooLong);

// We require that the description is not longer than 50 characters
require!(description.len() <= MAX_DESCRIPTION_LENGTH, MovieReviewError::DescriptionTooLong);

msg!("Movie review account created");
msg!("Title: {}", title);
msg!("Description: {}", description);
Expand All @@ -22,6 +35,12 @@ pub mod anchor_movie_review_program {
}

pub fn update_movie_review(ctx: Context<UpdateMovieReview>, title: String, description: String, rating: u8) -> Result<()> {
// We require that the rating is between 1 and 5
require!(rating >= MIN_RATING && rating <= MAX_RATING, MovieReviewError::InvalidRating);

// We require that the description is not longer than 50 characters
require!(description.len() <= MAX_DESCRIPTION_LENGTH, MovieReviewError::DescriptionTooLong);

msg!("Movie review account space reallocated");
msg!("Title: {}", title);
msg!("Description: {}", description);
Expand All @@ -48,7 +67,7 @@ pub struct AddMovieReview<'info> {
seeds=[title.as_bytes(), initializer.key().as_ref()],
bump,
payer = initializer,
space = 8 + 32 + 1 + 4 + title.len() + 4 + description.len()
space = MovieAccountState::INIT_SPACE + title.len() + description.len() // We add the length of the title and description to the init space
)]
pub movie_review: Account<'info, MovieAccountState>,
#[account(mut)]
Expand All @@ -63,7 +82,7 @@ pub struct UpdateMovieReview<'info> {
mut,
seeds=[title.as_bytes(), initializer.key().as_ref()],
bump,
realloc = 8 + 32 + 1 + 4 + title.len() + 4 + description.len(),
realloc = MovieAccountState::INIT_SPACE + title.len() + description.len(), // We add the length of the title and description to the init space
realloc::payer = initializer,
realloc::zero = true
)]
Expand Down Expand Up @@ -95,3 +114,22 @@ pub struct MovieAccountState {
pub title: String,
pub description: String,
}

/*
For the MovieAccountState account, since it is dynamic, we implement the Space trait to calculate the space required for the account.
We add the STRING_LENGTH_PREFIX twice to the space to account for the title and description string prefix.
We need to add the length of the title and description to the space upon initialization.
*/
impl Space for MovieAccountState {
const INIT_SPACE: usize = ANCHOR_DISCRIMINATOR + PUBKEY_SIZE + U8_SIZE + STRING_LENGTH_PREFIX + STRING_LENGTH_PREFIX;
}

#[error_code]
enum MovieReviewError {
#[msg("Rating must be between 1 and 5")]
InvalidRating,
#[msg("Movie Title too long")]
TitleTooLong,
#[msg("Movie Description too long")]
DescriptionTooLong,
}

0 comments on commit 9b5e576

Please sign in to comment.