Skip to content

Commit

Permalink
feat: library for stateful Solana event parsing (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
roberts-pumpurs authored Nov 27, 2024
1 parent ed968fa commit 0be172e
Show file tree
Hide file tree
Showing 11 changed files with 454 additions and 88 deletions.
14 changes: 13 additions & 1 deletion solana/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions solana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ gateway = { path = "programs/gateway", package = "gmp-gateway" }
axelar-solana-gateway = { path = "programs/axelar-solana-gateway" }

# helper crates
gateway-event-stack = { path = "crates/gateway-event-stack" }
axelar-solana-encoding = { path = "crates/axelar-solana-encoding" }
axelar-executable = { path = "crates/axelar-executable" }
axelar-executable-old = { path = "helpers/axelar-executable-old" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ed25519-dalek = { workspace = true, features = ["rand_core", "digest"] }
libsecp256k1.workspace = true
rand.workspace = true
libsecp-rand.workspace = true
base64.workspace = true
gateway-event-stack.workspace = true

[lints]
workspace = true
63 changes: 8 additions & 55 deletions solana/crates/axelar-solana-gateway-test-fixtures/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use axelar_solana_gateway::state::GatewayConfig;
use axelar_solana_gateway::{
bytemuck, get_gateway_root_config_pda, get_incoming_message_pda, get_verifier_set_tracker_pda,
};
use base64::engine::general_purpose;
use base64::Engine;
pub use gateway_event_stack::{MatchContext, ProgramInvocationState};
use rand::Rng as _;
use solana_program::pubkey::Pubkey;
use solana_program_test::{BanksTransactionResultWithMetadata, ProgramTest};
Expand Down Expand Up @@ -501,59 +500,13 @@ impl SolanaAxelarIntegration {
#[must_use]
pub fn get_gateway_events(
tx: &solana_program_test::BanksTransactionResultWithMetadata,
) -> Vec<GatewayEvent> {
tx.metadata
.as_ref()
.unwrap()
.log_messages
.iter()
.map(|log| {
log.trim()
.trim_start_matches("Program data:")
.split_whitespace()
.filter_map(decode_base64)
})
.filter_map(|mut raw_log| {
use axelar_solana_gateway::event_prefixes::*;
let disc = raw_log.next()?.try_into().ok()?;
match &disc {
CALL_CONTRACT => {
let event =
axelar_solana_gateway::processor::CallContractEvent::new(raw_log).ok()?;
Some(GatewayEvent::CallContract(event))
}
MESSAGE_APPROVED => {
let event =
axelar_solana_gateway::processor::MessageEvent::new(raw_log).ok()?;
Some(GatewayEvent::MessageApproved(event))
}
MESSAGE_EXECUTED => {
let event =
axelar_solana_gateway::processor::MessageEvent::new(raw_log).ok()?;
Some(GatewayEvent::MessageExecuted(event))
}
OPERATORSHIP_TRANSFERRED => {
let event =
axelar_solana_gateway::processor::OperatorshipTransferredEvent::new(
raw_log,
)
.ok()?;
Some(GatewayEvent::OperatorshipTransferred(event))
}
SIGNERS_ROTATED => {
let event =
axelar_solana_gateway::processor::VerifierSetRotated::new(raw_log).ok()?;
Some(GatewayEvent::VerifierSetRotated(event))
}
_ => None,
}
})
.collect::<Vec<_>>()
}

#[inline]
fn decode_base64(input: &str) -> Option<Vec<u8>> {
general_purpose::STANDARD.decode(input).ok()
) -> Vec<ProgramInvocationState<GatewayEvent>> {
let match_context = MatchContext::new(&axelar_solana_gateway::ID.to_string());
gateway_event_stack::build_program_event_stack(
&match_context,
tx.metadata.as_ref().unwrap().log_messages.as_slice(),
gateway_event_stack::parse_gateway_logs,
)
}

/// Create a new verifier set
Expand Down
21 changes: 21 additions & 0 deletions solana/crates/gateway-event-stack/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "gateway-event-stack"
version.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true
license.workspace = true
edition.workspace = true

[dependencies]
axelar-solana-gateway.workspace = true
tracing.workspace = true
solana-sdk.workspace = true
base64.workspace = true

[dev-dependencies]
pretty_assertions.workspace = true
test-log.workspace = true

[lints]
workspace = true
Loading

0 comments on commit 0be172e

Please sign in to comment.