Skip to content

Commit

Permalink
client: Support non-8-byte discriminators (#3125)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jul 27, 2024
1 parent 9fce3df commit c5337c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Update `dispatch` function to support dynamic discriminators ([#3104](https://github.com/coral-xyz/anchor/pull/3104)).
- lang: Remove the fallback function shortcut in `try_entry` function ([#3109](https://github.com/coral-xyz/anchor/pull/3109)).
- ts: Get discriminator lengths dynamically ([#3120](https://github.com/coral-xyz/anchor/pull/3120)).
- client: Support non-8-byte discriminators ([#3125](https://github.com/coral-xyz/anchor/pull/3125)).

### Fixes

Expand Down
25 changes: 10 additions & 15 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,28 +373,23 @@ pub fn handle_program_log<T: anchor_lang::Event + anchor_lang::AnchorDeserialize
.strip_prefix(PROGRAM_LOG)
.or_else(|| l.strip_prefix(PROGRAM_DATA))
{
let borsh_bytes = match STANDARD.decode(log) {
Ok(borsh_bytes) => borsh_bytes,
let log_bytes = match STANDARD.decode(log) {
Ok(log_bytes) => log_bytes,
_ => {
#[cfg(feature = "debug")]
println!("Could not base64 decode log: {}", log);
return Ok((None, None, false));
}
};

let mut slice: &[u8] = &borsh_bytes[..];
let disc: [u8; 8] = {
let mut disc = [0; 8];
disc.copy_from_slice(&borsh_bytes[..8]);
slice = &slice[8..];
disc
};
let mut event = None;
if disc == T::discriminator() {
let e: T = anchor_lang::AnchorDeserialize::deserialize(&mut slice)
.map_err(|e| ClientError::LogParseError(e.to_string()))?;
event = Some(e);
}
let event = log_bytes
.starts_with(T::DISCRIMINATOR)
.then(|| {
let mut data = &log_bytes[T::DISCRIMINATOR.len()..];
T::deserialize(&mut data).map_err(|e| ClientError::LogParseError(e.to_string()))
})
.transpose()?;

Ok((event, None, false))
}
// System log.
Expand Down

0 comments on commit c5337c5

Please sign in to comment.