Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lang/syn/src/codegen/program/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,17 @@ pub fn generate(program: &Program) -> proc_macro2::TokenStream {
accounts: &'info [AccountInfo<'info>],
data: &'info [u8],
) -> anchor_lang::Result<()> {
#(#global_ixs)*

// Legacy IDL instructions have been removed in favor of Program Metadata
// No IDL instructions are injected into programs anymore

// Dispatch Event CPI instruction
// Dispatch the Event CPI instruction before user instructions so a
// custom discriminator cannot shadow the reserved event-CPI tag.
if data.starts_with(anchor_lang::event::EVENT_IX_TAG_LE) {
return #event_cpi_handler;
}

#(#global_ixs)*

#fallback_fn
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/events/programs/events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub mod events {
});
Ok(())
}

#[instruction(discriminator = 0xe4)]
pub fn event_cpi_shadow_probe(_ctx: Context<EventCpiShadowProbe>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
Expand All @@ -43,6 +48,9 @@ pub struct TestEvent {}
#[derive(Accounts)]
pub struct TestEventCpi {}

#[derive(Accounts)]
pub struct EventCpiShadowProbe {}

#[event]
pub struct MyEvent {
pub data: u64,
Expand Down
13 changes: 13 additions & 0 deletions tests/events/tests/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,18 @@ describe("Events", () => {

throw new Error("Was able to invoke the self-CPI instruction");
});

it("Allows short custom discriminator overlapping event CPI tag", async () => {
const tx = new anchor.web3.Transaction();
tx.add(
new anchor.web3.TransactionInstruction({
programId: program.programId,
keys: [],
data: Buffer.from([0xe4]),
})
);

await program.provider.sendAndConfirm(tx, [], confirmOptions);
});
});
});
Loading