Expose Governance types/functions

Change-Id: I10fb14fa5d9d283cf831ea3d8585141e834e40b5
This commit is contained in:
Reisen 2021-06-29 13:01:25 +00:00
parent 21c7399d55
commit 44384e635f
3 changed files with 180 additions and 4 deletions

View File

@ -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<u8> {

View File

@ -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<Error> 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,
}

View File

@ -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,