Add account initialization for message, no seeds

Change-Id: I8fef4dd497457fc3ddbf2556e39fb57377cbb55f
This commit is contained in:
Reisen 2021-05-04 05:02:50 +00:00
parent 249fc68107
commit edfbfc93db
2 changed files with 71 additions and 34 deletions

View File

@ -5,6 +5,7 @@ use crate::{
anchor_bridge::Bridge,
types::{BridgeConfig, Index, Chain},
PublishMessage,
PostedMessage,
Result,
MAX_LEN_GUARDIAN_KEYS,
};
@ -16,6 +17,43 @@ const VAA_TX_FEE: u64 = 18 * 10000;
pub const MAX_PAYLOAD_SIZE: usize = 400;
pub fn publish_message(bridge: &mut Bridge, ctx: Context<PublishMessage>) -> Result<()> {
// Manually create message account, as Anchor can't do it.
let mut message: ProgramAccount<PostedMessage> = {
// First create the message account. 8 Bytes additional for the discriminator.
let space = 8 + PostedMessage::default().try_to_vec().unwrap().len();
let lamports = ctx.accounts.rent.minimum_balance(space);
let ix = solana_program::system_instruction::create_account(
ctx.accounts.payer.key,
ctx.accounts.message.key,
lamports,
space as u64,
ctx.program_id,
);
// let seeds = [
// ctx.accounts.poll.to_account_info().key.as_ref(),
// ctx.accounts.stake.member.to_account_info().key.as_ref(),
// &[nonce],
// ];
// let signer = &[&seeds[..]];
// solana_program::program::invoke_signed(
// &ix,
// &[
// ctx.accounts.stake.beneficiary.clone(),
// ctx.accounts.vote.clone(),
// ctx.accounts.system_program.clone(),
// ],
// signer,
// )?;
// Deserialize the newly created account into an object.
ProgramAccount::try_from_init(&ctx.accounts.message)?
};
// Do any initialization here.
// Manually persist changes since we manually created the account.
message.exit(ctx.program_id)?;
Ok(())
}
@ -57,32 +95,3 @@ pub struct ClaimedVAA {
/// time the vaa was submitted
pub vaa_time: u32,
}
/// Record of a posted wormhole message.
#[repr(C)]
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Debug)]
pub struct PostedMessage {
/// header of the posted VAA
pub vaa_version: u8,
/// time the vaa was submitted
pub vaa_time: u32,
/// Account where signatures are stored
pub vaa_signature_account: Pubkey,
/// time the posted message was created
pub submission_time: u32,
/// unique nonce for this message
pub nonce: u32,
/// emitter of the message
pub emitter_chain: Chain,
/// emitter of the message
pub emitter_address: [u8; 32],
/// message payload
pub payload: [[u8; 32]; 13],
}

View File

@ -3,7 +3,6 @@ use anchor_lang::{prelude::*, solana_program};
mod api;
mod types;
use api::PostedMessage;
use types::{Index, BridgeConfig, Chain};
// Without this, Anchor's derivation macros break. It requires names with no path components at all
@ -71,10 +70,6 @@ pub struct InitializeData {
#[derive(Accounts)]
pub struct PublishMessage<'info> {
/// Derived account verified to match the expected pubkey via Bridge::check_and_create_account.
#[account(init)]
pub message: AccountInfo<'info>,
/// No need to verify - only used as the fee payer for account creation.
#[account(signer)]
pub payer: AccountInfo<'info>,
@ -84,6 +79,10 @@ pub struct PublishMessage<'info> {
#[account(signer)]
pub emitter: AccountInfo<'info>,
/// The message account to store data in, note that this cannot be derived by serum and so the
/// pulish_message handler does this by hand.
pub message: AccountInfo<'info>,
/// State struct, derived by #[state], used for associated accounts.
pub state: ProgramState<'info, Bridge>,
@ -176,3 +175,32 @@ pub struct GuardianSetInfo {
/// expiration time when VAAs issued by this set are no longer valid
pub expiration_time: u32,
}
/// Record of a posted wormhole message.
#[account]
#[derive(Default)]
pub struct PostedMessage {
/// header of the posted VAA
pub vaa_version: u8,
/// time the vaa was submitted
pub vaa_time: u32,
/// Account where signatures are stored
pub vaa_signature_account: Pubkey,
/// time the posted message was created
pub submission_time: u32,
/// unique nonce for this message
pub nonce: u32,
/// emitter of the message
pub emitter_chain: Chain,
/// emitter of the message
pub emitter_address: [u8; 32],
/// message payload
pub payload: [[u8; 32]; 13],
}