Skip to content
Closed
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/audit/src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl KafkaBundleEventPublisher {
{
Ok(_) => {
debug!(
bundle_id = %bundle_id,
bundle_id = ?bundle_id,
topic = %self.topic,
payload_size = payload.len(),
"successfully published event"
Expand All @@ -46,7 +46,7 @@ impl KafkaBundleEventPublisher {
}
Err((err, _)) => {
error!(
bundle_id = %bundle_id,
bundle_id = ?bundle_id,
topic = %self.topic,
error = %err,
"failed to publish event"
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Default for LoggingBundleEventPublisher {
impl BundleEventPublisher for LoggingBundleEventPublisher {
async fn publish(&self, event: BundleEvent) -> Result<()> {
info!(
bundle_id = %event.bundle_id(),
bundle_id = ?event.bundle_id(),
event = ?event,
"Received bundle event"
);
Expand Down
4 changes: 3 additions & 1 deletion crates/audit/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ impl EventReader for KafkaAuditLogReader {
let event: BundleEvent = serde_json::from_slice(payload)?;

debug!(
bundle_id = %event.bundle_id(),
event_name = %event.event_name(),
bundle_id = ?event.bundle_id(),
tx_hash = ?event.tx_hash(),
timestamp = timestamp,
offset = message.offset(),
partition = message.partition(),
Expand Down
182 changes: 173 additions & 9 deletions crates/audit/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,54 @@ pub enum BundleHistoryEvent {
timestamp: i64,
reason: DropReason,
},
TransactionReceived {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth documenting the expected audit lifecycle of a transaction/backrun bundle?

key: String,
timestamp: i64,
bundle: Box<AcceptedBundle>,
},
TransactionSent {
key: String,
timestamp: i64,
tx_hash: TxHash,
},
BackrunReceived {
key: String,
timestamp: i64,
bundle: Box<AcceptedBundle>,
},
BackrunSent {
key: String,
timestamp: i64,
target_tx_hash: TxHash,
},
BackrunInserted {
key: String,
timestamp: i64,
target_tx_hash: TxHash,
backrun_tx_hashes: Vec<TxHash>,
},
StartExecuting {
key: String,
timestamp: i64,
tx_hash: TxHash,
block_number: u64,
},
Executed {
key: String,
timestamp: i64,
tx_hash: TxHash,
block_number: u64,
gas_used: u64,
},
BackrunBundleExecuted {
key: String,
timestamp: i64,
target_tx_hash: TxHash,
backrun_tx_hash: TxHash,
block_number: u64,
gas_used: u64,
success: bool,
},
}

impl BundleHistoryEvent {
Expand All @@ -73,6 +121,14 @@ impl BundleHistoryEvent {
BundleHistoryEvent::BuilderIncluded { key, .. } => key,
BundleHistoryEvent::BlockIncluded { key, .. } => key,
BundleHistoryEvent::Dropped { key, .. } => key,
BundleHistoryEvent::TransactionReceived { key, .. } => key,
BundleHistoryEvent::TransactionSent { key, .. } => key,
BundleHistoryEvent::BackrunReceived { key, .. } => key,
BundleHistoryEvent::BackrunSent { key, .. } => key,
BundleHistoryEvent::BackrunInserted { key, .. } => key,
BundleHistoryEvent::StartExecuting { key, .. } => key,
BundleHistoryEvent::Executed { key, .. } => key,
BundleHistoryEvent::BackrunBundleExecuted { key, .. } => key,
}
}
}
Expand All @@ -87,7 +143,12 @@ fn update_bundle_history_transform(
event: &Event,
) -> Option<BundleHistory> {
let mut history = bundle_history.history;
let bundle_id = event.event.bundle_id();
let bundle_id = match event.event.bundle_id() {
Some(id) => id,
None => {
return None;
}
};

// Check for deduplication - if event with same key already exists, skip
if history.iter().any(|h| h.key() == event.key) {
Expand All @@ -99,15 +160,18 @@ fn update_bundle_history_transform(
return None;
}

// Use the event's internal timestamp_ms instead of Kafka timestamp for accurate ordering
let timestamp = event.event.timestamp_ms();

let history_event = match &event.event {
BundleEvent::Received { bundle, .. } => BundleHistoryEvent::Received {
key: event.key.clone(),
timestamp: event.timestamp,
timestamp,
bundle: bundle.clone(),
},
BundleEvent::Cancelled { .. } => BundleHistoryEvent::Cancelled {
key: event.key.clone(),
timestamp: event.timestamp,
timestamp,
},
BundleEvent::BuilderIncluded {
builder,
Expand All @@ -116,7 +180,7 @@ fn update_bundle_history_transform(
..
} => BundleHistoryEvent::BuilderIncluded {
key: event.key.clone(),
timestamp: event.timestamp,
timestamp,
builder: builder.clone(),
block_number: *block_number,
flashblock_index: *flashblock_index,
Expand All @@ -127,15 +191,85 @@ fn update_bundle_history_transform(
..
} => BundleHistoryEvent::BlockIncluded {
key: event.key.clone(),
timestamp: event.timestamp,
timestamp,
block_number: *block_number,
block_hash: *block_hash,
},
BundleEvent::Dropped { reason, .. } => BundleHistoryEvent::Dropped {
key: event.key.clone(),
timestamp: event.timestamp,
timestamp,
reason: reason.clone(),
},
BundleEvent::TransactionReceived { bundle, .. } => {
BundleHistoryEvent::TransactionReceived {
key: event.key.clone(),
timestamp,
bundle: bundle.clone(),
}
}
BundleEvent::TransactionSent { tx_hash, .. } => BundleHistoryEvent::TransactionSent {
key: event.key.clone(),
timestamp,
tx_hash: *tx_hash,
},
BundleEvent::BackrunReceived { bundle, .. } => BundleHistoryEvent::BackrunReceived {
key: event.key.clone(),
timestamp,
bundle: bundle.clone(),
},
BundleEvent::BackrunSent { target_tx_hash, .. } => BundleHistoryEvent::BackrunSent {
key: event.key.clone(),
timestamp,
target_tx_hash: *target_tx_hash,
},
BundleEvent::BackrunInserted {
target_tx_hash,
backrun_tx_hashes,
..
} => BundleHistoryEvent::BackrunInserted {
key: event.key.clone(),
timestamp,
target_tx_hash: *target_tx_hash,
backrun_tx_hashes: backrun_tx_hashes.clone(),
},
BundleEvent::StartExecuting {
tx_hash,
block_number,
..
} => BundleHistoryEvent::StartExecuting {
key: event.key.clone(),
timestamp,
tx_hash: *tx_hash,
block_number: *block_number,
},
BundleEvent::Executed {
tx_hash,
block_number,
gas_used,
..
} => BundleHistoryEvent::Executed {
key: event.key.clone(),
timestamp,
tx_hash: *tx_hash,
block_number: *block_number,
gas_used: *gas_used,
},
BundleEvent::BackrunBundleExecuted {
target_tx_hash,
backrun_tx_hash,
block_number,
gas_used,
success,
..
} => BundleHistoryEvent::BackrunBundleExecuted {
key: event.key.clone(),
timestamp,
target_tx_hash: *target_tx_hash,
backrun_tx_hash: *backrun_tx_hash,
block_number: *block_number,
gas_used: *gas_used,
success: *success,
},
};

history.push(history_event);
Expand Down Expand Up @@ -190,7 +324,25 @@ impl S3EventReaderWriter {
}

async fn update_bundle_history(&self, event: Event) -> Result<()> {
let s3_key = S3Key::Bundle(event.event.bundle_id()).to_string();
let event_name = event.event.event_name();

// Backrun events without bundle_ids are skipped from S3 storage
let Some(bundle_id) = event.event.bundle_id() else {
info!(
event_name = %event_name,
tx_hash = ?event.event.tx_hash(),
"Skipping S3 storage for event without bundle_id"
);
return Ok(());
};

info!(
event_name = %event_name,
bundle_id = %bundle_id,
"Processing event for S3 storage"
);

let s3_key = S3Key::Bundle(bundle_id).to_string();

self.idempotent_write::<BundleHistory, _>(&s3_key, |current_history| {
update_bundle_history_transform(current_history, &event)
Expand Down Expand Up @@ -325,7 +477,10 @@ impl S3EventReaderWriter {
#[async_trait]
impl EventWriter for S3EventReaderWriter {
async fn archive_event(&self, event: Event) -> Result<()> {
let bundle_id = event.event.bundle_id();
let Some(bundle_id) = event.event.bundle_id() else {
return Ok(());
};

let transaction_ids = event.event.transaction_ids();

self.update_bundle_history(event.clone()).await?;
Expand Down Expand Up @@ -384,6 +539,7 @@ mod tests {
let bundle_event = BundleEvent::Received {
bundle_id,
bundle: Box::new(bundle.clone()),
timestamp_ms: 1234567890,
};
let event = create_test_event("test-key", 1234567890, bundle_event);

Expand Down Expand Up @@ -423,6 +579,7 @@ mod tests {
let bundle_event = BundleEvent::Received {
bundle_id,
bundle: Box::new(bundle),
timestamp_ms: 1234567890,
};
let event = create_test_event("duplicate-key", 1234567890, bundle_event);

Expand All @@ -440,12 +597,16 @@ mod tests {
let bundle_event = BundleEvent::Received {
bundle_id,
bundle: Box::new(bundle),
timestamp_ms: 1234567890,
};
let event = create_test_event("test-key", 1234567890, bundle_event);
let result = update_bundle_history_transform(bundle_history.clone(), &event);
assert!(result.is_some());

let bundle_event = BundleEvent::Cancelled { bundle_id };
let bundle_event = BundleEvent::Cancelled {
bundle_id,
timestamp_ms: 1234567890,
};
let event = create_test_event("test-key-2", 1234567890, bundle_event);
let result = update_bundle_history_transform(bundle_history.clone(), &event);
assert!(result.is_some());
Expand All @@ -455,6 +616,7 @@ mod tests {
builder: "test-builder".to_string(),
block_number: 12345,
flashblock_index: 1,
timestamp_ms: 1234567890,
};
let event = create_test_event("test-key-3", 1234567890, bundle_event);
let result = update_bundle_history_transform(bundle_history.clone(), &event);
Expand All @@ -464,6 +626,7 @@ mod tests {
bundle_id,
block_number: 12345,
block_hash: TxHash::from([1u8; 32]),
timestamp_ms: 1234567890,
};
let event = create_test_event("test-key-4", 1234567890, bundle_event);
let result = update_bundle_history_transform(bundle_history.clone(), &event);
Expand All @@ -472,6 +635,7 @@ mod tests {
let bundle_event = BundleEvent::Dropped {
bundle_id,
reason: DropReason::TimedOut,
timestamp_ms: 1234567890,
};
let event = create_test_event("test-key-5", 1234567890, bundle_event);
let result = update_bundle_history_transform(bundle_history, &event);
Expand Down
Loading