From d09eb150e73d965a732c89dc8827c9deb29862a8 Mon Sep 17 00:00:00 2001 From: Hendrik Hofstadt Date: Thu, 15 Apr 2021 11:36:29 +0200 Subject: [PATCH] move governance messages to new file Change-Id: I4b4b76974bb55b5f570c25072d5ffec59215a72c --- solana/bridge/src/governance.rs | 107 ++++++++++++++++++++++++++++++++ solana/bridge/src/lib.rs | 2 +- 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 solana/bridge/src/governance.rs diff --git a/solana/bridge/src/governance.rs b/solana/bridge/src/governance.rs new file mode 100644 index 000000000..226a0e806 --- /dev/null +++ b/solana/bridge/src/governance.rs @@ -0,0 +1,107 @@ +use solana_program::pubkey::Pubkey; +use std::io::{Cursor, Write, Read}; +use crate::error::Error; +use byteorder::{ReadBytesExt, WriteBytesExt, BigEndian}; + +#[derive(Clone, Debug, PartialEq)] +pub enum GovernanceCommand { + UpdateGuardianSet(BodyUpdateGuardianSet), + UpgradeContract(BodyContractUpgrade), +} + +impl GovernanceCommand { + fn action_id(&self) -> u8 { + match self { + GovernanceCommand::UpdateGuardianSet(_) => 0x01, + GovernanceCommand::UpgradeContract(_) => 0x02, + } + } + + fn deserialize(data: &Vec) -> Result { + let mut payload_data = Cursor::new(data); + let action = payload_data.read_u8()?; + + let payload = match action { + 0x01 => { + GovernanceCommand::UpdateGuardianSet(BodyUpdateGuardianSet::deserialize(&mut payload_data)?) + } + 0x02 => GovernanceCommand::UpgradeContract(BodyContractUpgrade::deserialize(&mut payload_data)?), + _ => { + return Err(Error::InvalidVAAAction); + } + }; + + Ok(payload) + } + + fn serialize(&self) -> Result, Error> { + match self { + GovernanceCommand::UpdateGuardianSet(b) => b.serialize(), + GovernanceCommand::UpgradeContract(b) => b.serialize(), + } + } +} + +#[derive(Clone, Debug, PartialEq)] +pub struct BodyUpdateGuardianSet { + pub new_index: u32, + pub new_keys: Vec<[u8; 20]>, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct BodyContractUpgrade { + pub chain_id: u8, + pub buffer: Pubkey, +} + +impl BodyContractUpgrade { + fn deserialize(data: &mut Cursor<&Vec>) -> Result { + let chain_id = data.read_u8()?; + let mut key: [u8; 32] = [0; 32]; + data.read(&mut key[..])?; + + Ok(BodyContractUpgrade { + chain_id, + buffer: Pubkey::new(&key[..]), + }) + } + + fn serialize(&self) -> Result, Error> { + let mut v: Cursor> = Cursor::new(Vec::new()); + v.write_u8(self.chain_id)?; + v.write(&self.buffer.to_bytes())?; + + Ok(v.into_inner()) + } +} + +impl BodyUpdateGuardianSet { + fn deserialize(data: &mut Cursor<&Vec>) -> Result { + let new_index = data.read_u32::()?; + + let keys_len = data.read_u8()?; + let mut keys = Vec::with_capacity(keys_len as usize); + for _ in 0..keys_len { + let mut key: [u8; 20] = [0; 20]; + data.read(&mut key)?; + keys.push(key); + } + + Ok(BodyUpdateGuardianSet { + new_index, + new_keys: keys, + }) + } + + fn serialize(&self) -> Result, Error> { + let mut v: Cursor> = Cursor::new(Vec::new()); + v.write_u32::(self.new_index)?; + v.write_u8(self.new_keys.len() as u8)?; + + for k in self.new_keys.iter() { + v.write(k)?; + } + + Ok(v.into_inner()) + } +} diff --git a/solana/bridge/src/lib.rs b/solana/bridge/src/lib.rs index 8947d3932..1e6aa1a76 100644 --- a/solana/bridge/src/lib.rs +++ b/solana/bridge/src/lib.rs @@ -5,8 +5,8 @@ extern crate solana_program; pub mod entrypoint; pub mod error; -pub mod error_program; pub mod instruction; pub mod processor; pub mod state; pub mod vaa; +pub mod governance;