From 44384e635f6c7d7bc5e9f3f100e5148f24ea948c Mon Sep 17 00:00:00 2001 From: Reisen Date: Tue, 29 Jun 2021 13:01:25 +0000 Subject: [PATCH] Expose Governance types/functions Change-Id: I10fb14fa5d9d283cf831ea3d8585141e834e40b5 --- solana/bridge/program/src/instructions.rs | 158 ++++++++++++++++++++++ solana/bridge/program/src/lib.rs | 23 +++- solana/bridge/program/tests/common.rs | 3 +- 3 files changed, 180 insertions(+), 4 deletions(-) diff --git a/solana/bridge/program/src/instructions.rs b/solana/bridge/program/src/instructions.rs index 069aaf80..d332ef42 100644 --- a/solana/bridge/program/src/instructions.rs +++ b/solana/bridge/program/src/instructions.rs @@ -16,6 +16,8 @@ use solitaire::{ use crate::{ accounts::{ Bridge, + Claim, + ClaimDerivationData, FeeCollector, GuardianSet, GuardianSetDerivationData, @@ -26,9 +28,21 @@ use crate::{ SignatureSet, SignatureSetDerivationData, }, + types::{ + GovernancePayloadUpgrade, + PostedMessage, + }, BridgeConfig, + InitializeData, + PayloadMessage, PostMessageData, PostVAAData, + SetFees, + SetFeesData, + TransferFees, + TransferFeesData, + UpgradeContractData, + UpgradeGuardianSetData, VerifySignaturesData, }; @@ -193,6 +207,150 @@ pub fn post_vaa(program_id: Pubkey, payer: Pubkey, vaa: PostVAAData) -> Instruct } } +pub fn upgrade_contract( + program_id: Pubkey, + payer: Pubkey, + payload_message: Pubkey, + spill: Pubkey, +) -> Instruction { + let claim = Claim::<'_, { AccountState::Uninitialized }>::key( + &ClaimDerivationData { + emitter_address: [0u8; 32], + emitter_chain: 1, + sequence: 0, + }, + &program_id, + ); + + let (upgrade_authority, _) = + Pubkey::find_program_address(&["upgrade_authority".as_bytes()], &program_id); + + Instruction { + program_id, + + accounts: vec![ + AccountMeta::new(payer, true), + AccountMeta::new(payload_message, false), + AccountMeta::new(claim, false), + AccountMeta::new(upgrade_authority, false), + AccountMeta::new(spill, false), + ], + + data: crate::instruction::Instruction::UpgradeContract(UpgradeContractData {}) + .try_to_vec() + .unwrap(), + } +} + +pub fn upgrade_guardian_set( + program_id: Pubkey, + payer: Pubkey, + payload_message: Pubkey, + emitter: Pubkey, + old_index: u32, + new_index: u32, +) -> Instruction { + let bridge = Bridge::<'_, { AccountState::Uninitialized }>::key(None, &program_id); + let claim = Claim::<'_, { AccountState::Uninitialized }>::key( + &ClaimDerivationData { + emitter_address: emitter.to_bytes(), + emitter_chain: 1, + sequence: 0, + }, + &program_id, + ); + + let guardian_set_old = GuardianSet::<'_, { AccountState::Initialized }>::key( + &GuardianSetDerivationData { index: old_index }, + &program_id, + ); + + let guardian_set_new = GuardianSet::<'_, { AccountState::Uninitialized }>::key( + &GuardianSetDerivationData { index: new_index }, + &program_id, + ); + + Instruction { + program_id, + + accounts: vec![ + AccountMeta::new(payer, true), + AccountMeta::new(bridge, false), + AccountMeta::new(payload_message, false), + AccountMeta::new(claim, false), + AccountMeta::new(guardian_set_old, false), + AccountMeta::new(guardian_set_new, false), + AccountMeta::new_readonly(solana_program::system_program::id(), false), + ], + + data: crate::instruction::Instruction::UpgradeGuardianSet(UpgradeGuardianSetData {}) + .try_to_vec() + .unwrap(), + } +} + +pub fn set_fees(program_id: Pubkey, payer: Pubkey, fee: u32) -> Instruction { + let bridge = Bridge::<'_, { AccountState::Uninitialized }>::key(None, &program_id); + let payload_message = Pubkey::new_unique(); + let claim = Claim::<'_, { AccountState::Uninitialized }>::key( + &ClaimDerivationData { + emitter_address: [0u8; 32], + emitter_chain: 1, + sequence: 0, + }, + &program_id, + ); + + Instruction { + program_id, + + accounts: vec![ + AccountMeta::new(payer, true), + AccountMeta::new(bridge, false), + AccountMeta::new(payload_message, false), + AccountMeta::new(claim, false), + AccountMeta::new_readonly(solana_program::system_program::id(), false), + ], + + data: crate::instruction::Instruction::SetFees(SetFeesData {}) + .try_to_vec() + .unwrap(), + } +} + +pub fn transfer_fees(program_id: Pubkey, payer: Pubkey, recipient: Pubkey) -> Instruction { + let bridge = Bridge::<'_, { AccountState::Uninitialized }>::key(None, &program_id); + let payload_message = Pubkey::new_unique(); + let claim = Claim::<'_, { AccountState::Uninitialized }>::key( + &ClaimDerivationData { + emitter_address: [0u8; 32], + emitter_chain: 1, + sequence: 0, + }, + &program_id, + ); + + let fee_collector = FeeCollector::key(None, &program_id); + + Instruction { + program_id, + + accounts: vec![ + AccountMeta::new(payer, true), + AccountMeta::new(bridge, false), + AccountMeta::new(payload_message, false), + AccountMeta::new(claim, false), + AccountMeta::new(fee_collector, false), + AccountMeta::new(recipient, false), + AccountMeta::new_readonly(solana_program::system_program::id(), false), + ], + + data: crate::instruction::Instruction::TransferFees(TransferFeesData {}) + .try_to_vec() + .unwrap(), + } +} + // Convert a full VAA structure into the serialization of its unique components, this structure is // what is hashed and verified by Guardians. pub fn serialize_vaa(vaa: &PostVAAData) -> Vec { diff --git a/solana/bridge/program/src/lib.rs b/solana/bridge/program/src/lib.rs index d041875a..f4028086 100644 --- a/solana/bridge/program/src/lib.rs +++ b/solana/bridge/program/src/lib.rs @@ -18,15 +18,22 @@ pub use api::{ initialize, post_message, post_vaa, + set_fees, + transfer_fees, upgrade_contract, upgrade_guardian_set, verify_signatures, Initialize, + InitializeData, PostMessage, PostMessageData, PostVAA, PostVAAData, + SetFees, + SetFeesData, Signature, + TransferFees, + TransferFeesData, UninitializedMessage, UpgradeContract, UpgradeContractData, @@ -35,6 +42,14 @@ pub use api::{ VerifySignatures, VerifySignaturesData, }; + +pub use vaa::{ + DeserializePayload, + PayloadMessage, + SerializePayload, +}; + +// BridgeConfig is the type of the main state the program maintains for itself. use types::BridgeConfig; const MAX_LEN_GUARDIAN_KEYS: usize = 19; @@ -67,10 +82,12 @@ impl From for SolitaireError { } solitaire! { - Initialize(BridgeConfig) => initialize, - PostVAA(PostVAAData) => post_vaa, + Initialize(InitializeData) => initialize, PostMessage(PostMessageData) => post_message, - VerifySignatures(VerifySignaturesData) => verify_signatures, + PostVAA(PostVAAData) => post_vaa, + SetFees(SetFeesData) => set_fees, + TransferFees(TransferFeesData) => transfer_fees, UpgradeContract(UpgradeContractData) => upgrade_contract, UpgradeGuardianSet(UpgradeGuardianSetData) => upgrade_guardian_set, + VerifySignatures(VerifySignaturesData) => verify_signatures, } diff --git a/solana/bridge/program/tests/common.rs b/solana/bridge/program/tests/common.rs index 50033af9..c6a98dea 100644 --- a/solana/bridge/program/tests/common.rs +++ b/solana/bridge/program/tests/common.rs @@ -44,13 +44,13 @@ use solana_sdk::{ use bridge::{ accounts::{ + FeeCollector, GuardianSet, GuardianSetDerivationData, Message, MessageDerivationData, SignatureSet, SignatureSetDerivationData, - FeeCollector, }, instruction, instructions, @@ -60,6 +60,7 @@ use bridge::{ SequenceTracker, }, Initialize, + InitializeData, PostMessageData, PostVAAData, UninitializedMessage,