Skip to content

Commit

Permalink
Merge branch 'master' into pablo/acx-2923-instructionsadminrs-add-eme…
Browse files Browse the repository at this point in the history
…rgency_delete_root_bundle
  • Loading branch information
md0x committed Oct 24, 2024
2 parents 054b115 + 79bc5b1 commit d14c663
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 22 deletions.
7 changes: 7 additions & 0 deletions programs/svm-spoke/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ pub struct EnabledDepositRoute {
pub enabled: bool,
}

#[event]
pub struct RelayedRootBundle {
pub root_bundle_id: u32,
pub relayer_refund_root: [u8; 32],
pub slow_relay_root: [u8; 32],
}

// Deposit events
#[event]
pub struct V3FundsDeposited {
Expand Down
12 changes: 10 additions & 2 deletions programs/svm-spoke/src/instructions/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
error::CustomError,
event::{
EmergencyDeletedRootBundle, EnabledDepositRoute, PausedDeposits, PausedFills,
SetXDomainAdmin,
RelayedRootBundle, SetXDomainAdmin,
},
initialize_current_time,
state::{RootBundle, Route, State},
Expand Down Expand Up @@ -212,6 +212,7 @@ pub fn set_enable_route(
Ok(())
}

#[event_cpi]
#[derive(Accounts)]
pub struct RelayRootBundle<'info> {
#[account(
Expand Down Expand Up @@ -244,9 +245,16 @@ pub fn relay_root_bundle(
let root_bundle = &mut ctx.accounts.root_bundle;
root_bundle.relayer_refund_root = relayer_refund_root;
root_bundle.slow_relay_root = slow_relay_root;

emit_cpi!(RelayedRootBundle {
root_bundle_id: state.root_bundle_id,
relayer_refund_root,
slow_relay_root,
});

// Finally, increment the root bundle id
state.root_bundle_id += 1;

// TODO: add event
Ok(())
}

Expand Down
10 changes: 5 additions & 5 deletions programs/svm-spoke/src/state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub struct State {
pub owner: Pubkey,
pub seed: u64, // Add a seed to the state to enable multiple deployments.
pub number_of_deposits: u32,
pub chain_id: u64, // Across definition of chainId for Solana.
pub current_time: u32, // Only used in testable mode, else set to 0 on mainnet.
pub remote_domain: u32, // CCTP domain for Mainnet Ethereum.
pub cross_domain_admin: Pubkey, // HubPool on Mainnet Ethereum.
pub root_bundle_id: u32,
pub chain_id: u64, // Across definition of chainId for Solana.
pub current_time: u32, // Only used in testable mode, else set to 0 on mainnet.
pub remote_domain: u32, // CCTP domain for Mainnet Ethereum.
pub cross_domain_admin: Pubkey, // HubPool on Mainnet Ethereum.
pub root_bundle_id: u32, // TODO rename to next_root_bundle_id
pub deposit_quote_time_buffer: u32, // Deposit quote times can't be set more than this amount into the past/future.
pub fill_deadline_buffer: u32, // Fill deadlines can't be set more than this amount into the future.
}
60 changes: 47 additions & 13 deletions test/svm/SvmSpoke.Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Try to relay root bundle as non-owner
let relayRootBundleAccounts = { state: state, rootBundle, signer: nonOwner.publicKey };
let relayRootBundleAccounts = { state: state, rootBundle, signer: nonOwner.publicKey, program: program.programId };
try {
await program.methods
.relayRootBundle(relayerRefundRootArray, slowRelayRootArray)
Expand All @@ -114,7 +114,7 @@ describe("svm_spoke.bundle", () => {
}

// Relay root bundle as owner
relayRootBundleAccounts = { state, rootBundle, signer: owner };
relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods
.relayRootBundle(relayerRefundRootArray, slowRelayRootArray)
.accounts(relayRootBundleAccounts)
Expand Down Expand Up @@ -146,7 +146,7 @@ describe("svm_spoke.bundle", () => {
const seeds2 = [Buffer.from("root_bundle"), state.toBuffer(), rootBundleIdBuffer2];
const [rootBundle2] = PublicKey.findProgramAddressSync(seeds2, program.programId);

relayRootBundleAccounts = { state, rootBundle: rootBundle2, signer: owner };
relayRootBundleAccounts = { state, rootBundle: rootBundle2, signer: owner, program: program.programId };
await program.methods
.relayRootBundle(relayerRefundRootArray2, slowRelayRootArray2)
.accounts(relayRootBundleAccounts)
Expand All @@ -156,6 +156,40 @@ describe("svm_spoke.bundle", () => {
assert.isTrue(stateAccountData.rootBundleId.toString() === "2", "Root bundle index should be 2");
});

it("Tests Event Emission in Relay Root Bundle", async () => {
const relayerRefundRootBuffer = crypto.randomBytes(32);
const relayerRefundRootArray = Array.from(relayerRefundRootBuffer);
const slowRelayRootBuffer = crypto.randomBytes(32);
const slowRelayRootArray = Array.from(slowRelayRootBuffer);

let stateAccountData = await program.account.state.fetch(state);
const rootBundleId = stateAccountData.rootBundleId;
const rootBundleIdBuffer = Buffer.alloc(4);
rootBundleIdBuffer.writeUInt32LE(rootBundleId);
const seeds = [Buffer.from("root_bundle"), state.toBuffer(), rootBundleIdBuffer];
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle as owner
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
const tx = await program.methods
.relayRootBundle(relayerRefundRootArray, slowRelayRootArray)
.accounts(relayRootBundleAccounts)
.rpc();

// Wait for event processing
await new Promise((resolve) => setTimeout(resolve, 1000));

// Check for the emitted event
const events = await readEvents(connection, tx, [program]);
const event = events.find((event) => event.name === "relayedRootBundle").data;
assert.isTrue(event.rootBundleId.toString() === rootBundleId.toString(), "Root bundle ID should match");
assert.isTrue(
event.relayerRefundRoot.toString() === relayerRefundRootArray.toString(),
"Relayer refund root should match"
);
assert.isTrue(event.slowRelayRoot.toString() === slowRelayRootArray.toString(), "Slow relay root should match");
});

it("Simple Leaf Refunds Relayers", async () => {
const relayerRefundLeaves: RelayerRefundLeafType[] = [];
const relayerARefund = new BN(400000);
Expand Down Expand Up @@ -186,7 +220,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
let relayRootBundleAccounts = { state, rootBundle, signer: owner };
let relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [
Expand Down Expand Up @@ -317,7 +351,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
let relayRootBundleAccounts = { state, rootBundle, signer: owner };
let relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [
Expand Down Expand Up @@ -458,7 +492,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
let relayRootBundleAccounts = { state, rootBundle, signer: owner };
let relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [
Expand Down Expand Up @@ -520,7 +554,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
let relayRootBundleAccounts = { state, rootBundle, signer: owner };
let relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [
Expand Down Expand Up @@ -582,7 +616,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
let relayRootBundleAccounts = { state, rootBundle, signer: owner };
let relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [{ pubkey: relayerTA, isWritable: true, isSigner: false }];
Expand Down Expand Up @@ -835,7 +869,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
const relayRootBundleAccounts = { state, rootBundle, signer: owner };
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

// Verify valid leaf
Expand Down Expand Up @@ -987,7 +1021,7 @@ describe("svm_spoke.bundle", () => {
rootBundleIdBuffer.writeUInt32LE(rootBundleId);
const seeds = [Buffer.from("root_bundle"), state.toBuffer(), rootBundleIdBuffer];
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);
let relayRootBundleAccounts = { state, rootBundle, signer: owner };
let relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();
const proofAsNumbers = proof.map((p) => Array.from(p));
const executeRelayerRefundLeafAccounts = {
Expand Down Expand Up @@ -1063,7 +1097,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
const relayRootBundleAccounts = { state, rootBundle, signer: owner };
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [{ pubkey: relayerTA, isWritable: true, isSigner: false }];
Expand Down Expand Up @@ -1137,7 +1171,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
const relayRootBundleAccounts = { state, rootBundle, signer: owner };
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

const remainingAccounts = [
Expand Down Expand Up @@ -1240,7 +1274,7 @@ describe("svm_spoke.bundle", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
const relayRootBundleAccounts = { state, rootBundle, signer: owner };
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

// Pass refund addresses in remaining accounts.
Expand Down
2 changes: 1 addition & 1 deletion test/svm/SvmSpoke.RefundClaims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("svm_spoke.refund_claims", () => {
const [rootBundle] = PublicKey.findProgramAddressSync(seeds, program.programId);

// Relay root bundle
const relayRootBundleAccounts = { state, rootBundle, signer: owner };
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods.relayRootBundle(Array.from(root), Array.from(root)).accounts(relayRootBundleAccounts).rpc();

// Pass claim account as relayer refund address.
Expand Down
2 changes: 1 addition & 1 deletion test/svm/SvmSpoke.SlowFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("svm_spoke.slow_fill", () => {
const relayerRefundRoot = crypto.randomBytes(32);

// Relay root bundle
const relayRootBundleAccounts = { state, rootBundle, signer: owner };
const relayRootBundleAccounts = { state, rootBundle, signer: owner, program: program.programId };
await program.methods
.relayRootBundle(Array.from(relayerRefundRoot), Array.from(slowRelayRoot))
.accounts(relayRootBundleAccounts)
Expand Down
1 change: 1 addition & 0 deletions test/svm/SvmSpoke.TokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe("svm_spoke.token_bridge", () => {
state,
rootBundle,
signer: owner,
program: program.programId,
};
await program.methods
.relayRootBundle(Array.from(root), Array.from(Buffer.alloc(32)))
Expand Down

0 comments on commit d14c663

Please sign in to comment.