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

CU optimization 🛳️ #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 37 additions & 73 deletions computeOptimization/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use anchor_lang::prelude::*;

// This is your program's public key and it will update
// automatically when you build the project.
declare_id!("CUop95w5uGSSXFZRefVHMHZxTqWriyfLZxm8DRN5cntv");

const MAX_MEMO_SIZE: u64 = 787;

const MAX_MEMO_SIZE: u64 = 512;
const UNINITIALIZED: u64 = 0;
const INITIALIZED: u64 = 1;
const VALID: u64 = 2;
Expand All @@ -14,88 +11,44 @@ const INVALIDATED: u64 = 3;
#[program]
mod hello_anchor {
use super::*;
pub fn store_memo(ctx: Context<MemoContext>, _memo_index: u32, memo: String) -> Result<()> {
msg!("Program started!");

let mut memo_len = 0u64;
for _ch in memo.as_bytes() {
memo_len += 1;
}
if memo_len > MAX_MEMO_SIZE {
return err!(MyError::MemoTooLong);
}

msg!("Memo created by {}: {}", ctx.accounts.signer.key(), memo);

ctx.accounts.new_account.memo = memo;
pub fn store_memo(ctx: Context<MemoContext>, _memo_index: u32, memo: [u8; 512]) -> Result<()> {
ctx.accounts.new_account.state = INITIALIZED;
ctx.accounts.new_account.authority = ctx.accounts.signer.key().to_string();

msg!("Program successul!");
ctx.accounts.new_account.memo = memo;
ctx.accounts.new_account.authority = ctx.accounts.signer.key();
Ok(())
}

pub fn validate_memo(ctx: Context<ValidateContext>, _memo_index: u32) -> Result<()> {
msg!("Program started!");

if ctx.accounts.memo_account.state == UNINITIALIZED {
msg!("This memo has not been created yet.");
return err!(MyError::MemoUnititialized);
} else if ctx.accounts.memo_account.state == VALID {
msg!("This memo has already been validated.");
return err!(MyError::MemoValidated);
} else if ctx.accounts.memo_account.state == INVALIDATED {
msg!("This memo has already been invalidated.");
return err!(MyError::MemoInValidated);
}
msg!("Let's validated this memo");

msg!("Validating the owner...");
if ctx
.accounts
.memo_account
.authority
.eq(&ctx.accounts.signer.key().to_string())
{
msg!("owner valid.");
} else {
return err!(MyError::MemoOwnerDoesntMatch);
}

msg!(
"is the memo valid? {}",
is_valid_memo(ctx.accounts.memo_account.memo.clone())
);

if !is_valid_memo(ctx.accounts.memo_account.memo.clone()) {
return err!(MyError::MemoIsNotValid);
}
if is_valid_memo(ctx.accounts.memo_account.memo.clone()) {
require_neq!(ctx.accounts.memo_account.state, VALID, MyError::MemoValidated);
require_neq!(ctx.accounts.memo_account.state, INVALIDATED, MyError::MemoInValidated);
require_neq!(ctx.accounts.memo_account.state, UNINITIALIZED, MyError::MemoUnititialized);
require_keys_eq!(ctx.accounts.memo_account.authority, ctx.accounts.signer.key(), MyError::MemoOwnerDoesntMatch);
require!(is_valid, MyError::MemoIsNotValid);
if is_valid {
ctx.accounts.memo_account.state = VALID;
}

msg!("Program successul!");
Ok(())
}
}

fn is_valid_memo(memo: String) -> bool {
for i in 0..memo.len() {
let ch: char = memo.chars().nth(i).unwrap();
msg!("character: {}", ch);
if ch.is_ascii_alphanumeric() || ch.eq(&' ') {
} else {
return false;
}
}
true
fn is_valid_memo(memo: &[u8; 512]) -> bool {
memo.iter().all(|ch| ch.is_ascii_alphanumeric() || *ch == b' ')
}

#[derive(Accounts)]
#[instruction(memo_index: u32)]
pub struct MemoContext<'info> {
#[account(init, payer = signer, space = 8 + (MAX_MEMO_SIZE as usize) + 32,
seeds = [b"memo", signer.key().as_ref(), &memo_index.to_le_bytes()], bump)]
#[account(
init,
payer = signer,
space = 8 + MemoAccount::MAX_SIZE + 32,
seeds = [
b"memo",
signer.key().as_ref(),
&memo_index.to_le_bytes()
],
bump
)]
pub new_account: Account<'info, MemoAccount>,
#[account(mut)]
pub signer: Signer<'info>,
Expand All @@ -105,18 +58,29 @@ pub struct MemoContext<'info> {
#[derive(Accounts)]
#[instruction(memo_index: u32)]
pub struct ValidateContext<'info> {
#[account(mut, seeds = [b"memo", signer.key().as_ref(), &memo_index.to_le_bytes()], bump)]
#[account(
mut,
seeds = [
b"memo",
signer.key().as_ref(),
&memo_index.to_le_bytes()
],
bump
)]
pub memo_account: Account<'info, MemoAccount>,
#[account(mut)]
pub signer: Signer<'info>,
}

#[account]
pub struct MemoAccount {
memo: String,
authority: String,
memo: [u8; 512],
authority: Pubkey,
state: u64,
}
impl MemoAccount {
const MAX_SIZE: usize = 512 + 32 + 8;
}

#[error_code]
pub enum MyError {
Expand Down