@@ -784,9 +784,8 @@ struct PreparedBlockItem {
784
784
events : Option < serde_json:: Value > ,
785
785
/// Reject reason the block item. Is none for successful block items.
786
786
reject : Option < serde_json:: Value > ,
787
- // This is an option temporarily, until we are able to handle every type of event.
788
787
/// Block item events prepared for inserting into the database.
789
- prepared_event : Option < PreparedEvent > ,
788
+ prepared_event : PreparedEventData ,
790
789
}
791
790
792
791
impl PreparedBlockItem {
@@ -843,7 +842,7 @@ impl PreparedBlockItem {
843
842
( None , Some ( reject) )
844
843
} ;
845
844
846
- let prepared_event = PreparedEvent :: prepare ( node_client, data, block_item) . await ?;
845
+ let prepared_event = PreparedEventData :: prepare ( node_client, data, block_item) . await ?;
847
846
848
847
Ok ( Self {
849
848
block_item_index,
@@ -888,13 +887,24 @@ VALUES
888
887
. execute ( tx. as_mut ( ) )
889
888
. await ?;
890
889
891
- if let Some ( prepared_event) = & self . prepared_event {
892
- prepared_event. save ( tx) . await ?;
893
- }
890
+ self . prepared_event . save ( tx) . await ?;
891
+
894
892
Ok ( ( ) )
895
893
}
896
894
}
897
895
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
+
898
908
/// Different types of block item events that can be prepared.
899
909
enum PreparedEvent {
900
910
/// A new account got created.
@@ -908,12 +918,12 @@ enum PreparedEvent {
908
918
/// No changes in the database was caused by this event.
909
919
NoOperation ,
910
920
}
911
- impl PreparedEvent {
921
+ impl PreparedEventData {
912
922
async fn prepare (
913
923
node_client : & mut v2:: Client ,
914
924
data : & BlockData ,
915
925
block_item : & BlockItemSummary ,
916
- ) -> anyhow:: Result < Option < Self > > {
926
+ ) -> anyhow:: Result < Self > {
917
927
let prepared_event = match & block_item. details {
918
928
BlockItemSummaryDetails :: AccountCreation ( details) => {
919
929
Some ( PreparedEvent :: AccountCreation ( PreparedAccountCreation :: prepare (
@@ -1057,23 +1067,49 @@ impl PreparedEvent {
1057
1067
None
1058
1068
}
1059
1069
} ;
1060
- Ok ( prepared_event)
1070
+ Ok ( PreparedEventData {
1071
+ event : prepared_event,
1072
+ success : block_item. is_success ( ) ,
1073
+ } )
1061
1074
}
1062
1075
1063
1076
async fn save (
1064
1077
& self ,
1065
1078
tx : & mut sqlx:: Transaction < ' static , sqlx:: Postgres > ,
1066
1079
) -> anyhow:: Result < ( ) > {
1067
- match self {
1080
+ let Some ( event) = & self . event else {
1081
+ return Ok ( ( ) ) ;
1082
+ } ;
1083
+
1084
+ match event {
1068
1085
PreparedEvent :: AccountCreation ( event) => event. save ( tx) . await ,
1086
+ // TODO: need to handle `rejected` baker events properly.
1069
1087
PreparedEvent :: BakerEvents ( events) => {
1070
1088
for event in events {
1071
1089
event. save ( tx) . await ?;
1072
1090
}
1073
1091
Ok ( ( ) )
1074
1092
}
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
+ }
1077
1113
PreparedEvent :: NoOperation => Ok ( ( ) ) ,
1078
1114
}
1079
1115
}
0 commit comments