Use Anchor state for Initialize

Change-Id: I4f61c3e74e037e873f669f240ebbc8fe26567103
This commit is contained in:
Reisen 2021-04-29 10:03:22 +00:00
parent 17bda088f1
commit 1c9facbb75
4 changed files with 41 additions and 13 deletions

View File

@ -1,11 +1,20 @@
use crate::{accounts, types::BridgeConfig, Initialize, InitializeData, MAX_LEN_GUARDIAN_KEYS};
use anchor_lang::{prelude::*, solana_program}; use anchor_lang::{prelude::*, solana_program};
use crate::{
accounts,
anchor_bridge::Bridge,
types::{BridgeConfig, Version},
Initialize, InitializeData, MAX_LEN_GUARDIAN_KEYS,
};
pub fn initialize( pub fn initialize(
ctx: Context<Initialize>, ctx: Context<Initialize>,
len_guardians: u8, len_guardians: u8,
initial_guardian_key: [[u8; 20]; MAX_LEN_GUARDIAN_KEYS], initial_guardian_key: [[u8; 20]; MAX_LEN_GUARDIAN_KEYS],
config: BridgeConfig, config: BridgeConfig,
) -> ProgramResult { ) -> Result<Bridge, ProgramError> {
Ok(()) Ok(Bridge {
guardian_set_version: Version(0),
config,
})
} }

View File

@ -1,5 +1,6 @@
use crate::{accounts, VerifySig, VerifySigsData};
use anchor_lang::{prelude::*, solana_program}; use anchor_lang::{prelude::*, solana_program};
use crate::{accounts, VerifySig, VerifySigsData};
use byteorder::ByteOrder; use byteorder::ByteOrder;
use sha3::Digest; use sha3::Digest;
use std::io::Write; use std::io::Write;

View File

@ -41,7 +41,23 @@ pub struct VerifySigsData {
} }
#[derive(Accounts)] #[derive(Accounts)]
pub struct Initialize {} pub struct Initialize<'info> {
/// Account used to pay for auxillary instructions.
#[account(signer)]
pub payer: AccountInfo<'info>,
/// Used for timestamping actions.
pub clock: Sysvar<'info, Clock>,
/// Information about the current guardian set.
pub guardian_set: ProgramState<'info, GuardianSetInfo>,
/// Required by Anchor for associated accounts.
pub rent: Sysvar<'info, Rent>,
/// Required by Anchor for associated accounts.
pub system_program: AccountInfo<'info>,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)] #[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)]
pub struct InitializeData { pub struct InitializeData {
@ -63,7 +79,8 @@ pub mod anchor_bridge {
pub config: types::BridgeConfig, pub config: types::BridgeConfig,
} }
pub fn initialize(ctx: Context<Initialize>, data: InitializeData) -> ProgramResult { impl Bridge {
pub fn new(ctx: Context<Initialize>, data: InitializeData) -> Result<Self, ProgramError> {
api::initialize( api::initialize(
ctx, ctx,
data.len_guardians, data.len_guardians,
@ -71,6 +88,7 @@ pub mod anchor_bridge {
data.config, data.config,
) )
} }
}
pub fn verify_signatures(ctx: Context<VerifySig>, data: VerifySigsData) -> ProgramResult { pub fn verify_signatures(ctx: Context<VerifySig>, data: VerifySigsData) -> ProgramResult {
api::verify_signatures(ctx, data) api::verify_signatures(ctx, data)

View File

@ -2,7 +2,7 @@ use anchor_lang::prelude::*;
// Distinguishes a Version number from a standard u32. // Distinguishes a Version number from a standard u32.
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)] #[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)]
pub struct Version(u32); pub struct Version(pub u32);
#[repr(C)] #[repr(C)]
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)] #[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)]