Skip to content

Commit df047ad

Browse files
committed
Handle rejected events correctly
1 parent 8cfc9c7 commit df047ad

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

backend-rust/src/indexer.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,8 @@ struct PreparedBlockItem {
784784
events: Option<serde_json::Value>,
785785
/// Reject reason the block item. Is none for successful block items.
786786
reject: Option<serde_json::Value>,
787-
// This is an option temporarily, until we are able to handle every type of event.
788787
/// Block item events prepared for inserting into the database.
789-
prepared_event: Option<PreparedEvent>,
788+
prepared_event: PreparedEventData,
790789
}
791790

792791
impl PreparedBlockItem {
@@ -843,7 +842,7 @@ impl PreparedBlockItem {
843842
(None, Some(reject))
844843
};
845844

846-
let prepared_event = PreparedEvent::prepare(node_client, data, block_item).await?;
845+
let prepared_event = PreparedEventData::prepare(node_client, data, block_item).await?;
847846

848847
Ok(Self {
849848
block_item_index,
@@ -888,13 +887,24 @@ VALUES
888887
.execute(tx.as_mut())
889888
.await?;
890889

891-
if let Some(prepared_event) = &self.prepared_event {
892-
prepared_event.save(tx).await?;
893-
}
890+
self.prepared_event.save(tx).await?;
891+
894892
Ok(())
895893
}
896894
}
897895

896+
/// Different types of block item events that can be prepared and the
897+
/// status of the event (if the event was in a successful or rejected
898+
/// transaction).
899+
struct PreparedEventData {
900+
/// The prepared event. Note: this is optional temporarily until we can
901+
/// handle all events.
902+
event: Option<PreparedEvent>,
903+
/// The status of the event (if the event belongs to a successful or
904+
/// rejected transaction).
905+
success: bool,
906+
}
907+
898908
/// Different types of block item events that can be prepared.
899909
enum PreparedEvent {
900910
/// A new account got created.
@@ -908,12 +918,12 @@ enum PreparedEvent {
908918
/// No changes in the database was caused by this event.
909919
NoOperation,
910920
}
911-
impl PreparedEvent {
921+
impl PreparedEventData {
912922
async fn prepare(
913923
node_client: &mut v2::Client,
914924
data: &BlockData,
915925
block_item: &BlockItemSummary,
916-
) -> anyhow::Result<Option<Self>> {
926+
) -> anyhow::Result<Self> {
917927
let prepared_event = match &block_item.details {
918928
BlockItemSummaryDetails::AccountCreation(details) => {
919929
Some(PreparedEvent::AccountCreation(PreparedAccountCreation::prepare(
@@ -1057,23 +1067,49 @@ impl PreparedEvent {
10571067
None
10581068
}
10591069
};
1060-
Ok(prepared_event)
1070+
Ok(PreparedEventData {
1071+
event: prepared_event,
1072+
success: block_item.is_success(),
1073+
})
10611074
}
10621075

10631076
async fn save(
10641077
&self,
10651078
tx: &mut sqlx::Transaction<'static, sqlx::Postgres>,
10661079
) -> anyhow::Result<()> {
1067-
match self {
1080+
let Some(event) = &self.event else {
1081+
return Ok(());
1082+
};
1083+
1084+
match event {
10681085
PreparedEvent::AccountCreation(event) => event.save(tx).await,
1086+
// TODO: need to handle `rejected` baker events properly.
10691087
PreparedEvent::BakerEvents(events) => {
10701088
for event in events {
10711089
event.save(tx).await?;
10721090
}
10731091
Ok(())
10741092
}
1075-
PreparedEvent::ModuleDeployed(event) => event.save(tx).await,
1076-
PreparedEvent::ContractInitialized(event) => event.save(tx).await,
1093+
PreparedEvent::ModuleDeployed(event) =>
1094+
// Only save the module into the `modules` table if the transaction was
1095+
// successful.
1096+
{
1097+
if self.success {
1098+
event.save(tx).await
1099+
} else {
1100+
Ok(())
1101+
}
1102+
}
1103+
PreparedEvent::ContractInitialized(event) =>
1104+
// Only save the contract into the `contracts` table if the transaction was
1105+
// successful.
1106+
{
1107+
if self.success {
1108+
event.save(tx).await
1109+
} else {
1110+
Ok(())
1111+
}
1112+
}
10771113
PreparedEvent::NoOperation => Ok(()),
10781114
}
10791115
}

0 commit comments

Comments
 (0)