Add governance set_fees test

Change-Id: I13cda0ff055daa0fcfd9f0f2993f8e32d2670181
This commit is contained in:
Reisen 2021-07-05 11:33:42 +00:00
parent fd7b53a2a5
commit 366a045dd4
4 changed files with 82 additions and 16 deletions

View File

@ -289,14 +289,19 @@ pub fn upgrade_guardian_set(
}
}
pub fn set_fees(program_id: Pubkey, payer: Pubkey, fee: u32) -> Instruction {
pub fn set_fees(
program_id: Pubkey,
payer: Pubkey,
message: Pubkey,
emitter: Pubkey,
sequence: u64,
) -> 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_address: emitter.to_bytes(),
emitter_chain: CHAIN_ID_SOLANA,
sequence: 0,
sequence,
},
&program_id,
);
@ -307,7 +312,7 @@ pub fn set_fees(program_id: Pubkey, payer: Pubkey, fee: u32) -> Instruction {
accounts: vec![
AccountMeta::new(payer, true),
AccountMeta::new(bridge, false),
AccountMeta::new(payload_message, false),
AccountMeta::new(message, false),
AccountMeta::new(claim, false),
AccountMeta::new_readonly(solana_program::system_program::id(), false),
],

View File

@ -299,6 +299,14 @@ pub struct GovernancePayloadSetMessageFee {
pub fee: u64,
}
impl SerializePayload for GovernancePayloadSetMessageFee {
fn serialize<W: Write>(&self, v: &mut W) -> std::result::Result<(), SolitaireError> {
use byteorder::WriteBytesExt;
v.write_u64::<BigEndian>(self.fee)?;
Ok(())
}
}
impl DeserializePayload for GovernancePayloadSetMessageFee
where
Self: DeserializeGovernancePayload,

View File

@ -256,6 +256,7 @@ mod helpers {
emitter: &Keypair,
nonce: u32,
data: Vec<u8>,
fee: u64,
) -> Result<Pubkey, ClientError> {
// Transfer money into the fee collector as it needs a balance/must exist.
let fee_collector = FeeCollector::<'_>::key(None, program);
@ -270,7 +271,7 @@ mod helpers {
payer,
&[payer, emitter],
&[
system_instruction::transfer(&payer.pubkey(), &fee_collector, 10_000),
system_instruction::transfer(&payer.pubkey(), &fee_collector, fee),
instruction,
],
)?;
@ -371,12 +372,25 @@ mod helpers {
)
}
pub fn set_fees(client: &RpcClient, program: &Pubkey, payer: &Keypair, fee: u32) -> Result<Signature, ClientError> {
pub fn set_fees(
client: &RpcClient,
program: &Pubkey,
payer: &Keypair,
message: Pubkey,
emitter: Pubkey,
sequence: u64,
) -> Result<Signature, ClientError> {
execute(
client,
payer,
&[payer],
&[instructions::set_fees(*program, payer.pubkey(), fee)],
&[instructions::set_fees(
*program,
payer.pubkey(),
message,
emitter,
sequence,
)],
)
}

View File

@ -55,6 +55,7 @@ use bridge::{
types::{
BridgeConfig,
GovernancePayloadGuardianSetChange,
GovernancePayloadSetMessageFee,
PostedMessage,
PostedMessageData,
SequenceTracker,
@ -96,6 +97,7 @@ fn run_integration_tests() {
test_bridge_messages(&mut context);
test_guardian_set_change(&mut context);
test_guardian_set_change_fails(&mut context);
test_set_fees(&mut context);
}
fn test_bridge_messages(context: &mut Context) {
@ -107,7 +109,7 @@ fn test_bridge_messages(context: &mut Context) {
let emitter = Keypair::new();
// Post the message, publishing the data for guardian consumption.
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()).unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone(), 10_000).unwrap();
// Emulate Guardian behaviour, verifying the data and publishing signatures/VAA.
let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 0);
@ -125,7 +127,7 @@ fn test_guardian_set_change(context: &mut Context) {
let emitter = Keypair::from_bytes(&GOV_KEY).unwrap();
// Post the message, publishing the data for guardian consumption.
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()).unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone(), 10_000).unwrap();
// Emulate Guardian behaviour, verifying the data and publishing signatures/VAA.
let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 0);
@ -141,7 +143,7 @@ fn test_guardian_set_change(context: &mut Context) {
new_guardian_set: new_public_keys.clone(),
}.try_to_vec().unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()).unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone(), 10_000).unwrap();
let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 0);
common::verify_signatures(client, program, payer, body, body_hash, &context.secret, 0).unwrap();
common::post_vaa(client, program, payer, vaa).unwrap();
@ -159,7 +161,7 @@ fn test_guardian_set_change(context: &mut Context) {
// Submit the message a second time with a new nonce.
let nonce = 12399;
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()).unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone(), 10_000).unwrap();
context.public = new_public_keys;
context.secret = new_secret_keys;
@ -183,7 +185,7 @@ fn test_guardian_set_change_fails(context: &mut Context) {
new_guardian_set: new_public_keys.clone(),
}.try_to_vec().unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone()).unwrap();
let message_key = common::post_message(client, program, payer, &emitter, nonce, message.clone(), 10_000).unwrap();
let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 1);
assert!(common::upgrade_guardian_set(
@ -196,7 +198,44 @@ fn test_guardian_set_change_fails(context: &mut Context) {
2,
0,
).is_err());
context.public = new_public_keys;
context.secret = new_secret_keys;
}
fn test_set_fees(context: &mut Context) {
// Initialize a wormhole bridge on Solana to test with.
let (ref payer, ref client, ref program) = common::setup();
let emitter = Keypair::from_bytes(&GOV_KEY).unwrap();
let nonce = 12401;
let message = GovernancePayloadSetMessageFee { fee: 100 }
.try_to_vec()
.unwrap();
let message_key = common::post_message(
client,
program,
payer,
&emitter,
nonce,
message.clone(),
10_000,
)
.unwrap();
let (vaa, body, body_hash) = common::generate_vaa(&emitter, message.clone(), nonce, 1);
common::verify_signatures(client, program, payer, body, body_hash, &context.secret, 1).unwrap();
common::post_vaa(client, program, payer, vaa).unwrap();
common::set_fees(client, program, payer, message_key, emitter.pubkey(), 3).unwrap();
// Check that posting a new message fails with too small a fee.
let emitter = Keypair::new();
let nonce = 12402;
let message = b"Fail to Pay".to_vec();
assert!(
common::post_message(client, program, payer, &emitter, nonce, message.clone(), 50).is_err()
);
// And succeeds with the new.
let emitter = Keypair::new();
let nonce = 12402;
let message = b"Fail to Pay".to_vec();
common::post_message(client, program, payer, &emitter, nonce, message.clone(), 100).unwrap();
}