11//! Storage for transaction execution info per block (input to the starknet OS).
22
33use blockifier:: transaction:: objects:: TransactionExecutionInfo ;
4+ use starknet_api:: block:: BlockNumber ;
45
56use crate :: compression_utils:: { compress, decompress} ;
67use crate :: db:: serialization:: { StorageSerde , StorageSerdeError } ;
8+ use crate :: db:: table_types:: Table ;
9+ use crate :: db:: { TransactionKind , RW } ;
10+ use crate :: { OffsetKind , StorageResult , StorageTxn } ;
711
812/// Per-block container of transaction execution infos, stored as a compressed JSON blob.
913#[ derive( Debug ) ]
@@ -22,3 +26,70 @@ impl StorageSerde for TxExecutionInfos {
2226 serde_json:: from_slice ( & data) . ok ( ) . map ( TxExecutionInfos )
2327 }
2428}
29+
30+ /// Interface for reading transaction execution infos from storage.
31+ pub trait TxExecutionInfoStorageReader < Mode : TransactionKind > {
32+ /// Returns the transaction execution infos for the given block, or `None` if not stored.
33+ fn get_tx_execution_infos (
34+ & self ,
35+ block_number : BlockNumber ,
36+ ) -> StorageResult < Option < Vec < TransactionExecutionInfo > > > ;
37+ }
38+
39+ /// Interface for writing transaction execution infos to storage.
40+ pub trait TxExecutionInfoStorageWriter
41+ where
42+ Self : Sized ,
43+ {
44+ /// Appends the transaction execution infos for the given block to storage.
45+ fn append_tx_execution_infos (
46+ self ,
47+ block_number : BlockNumber ,
48+ tx_execution_infos : Vec < TransactionExecutionInfo > ,
49+ ) -> StorageResult < Self > ;
50+
51+ /// Removes the transaction execution infos for the given block from storage.
52+ /// If no entry exists for the block, returns without error.
53+ fn revert_tx_execution_infos ( self , block_number : BlockNumber ) -> StorageResult < Self > ;
54+ }
55+
56+ impl < Mode : TransactionKind > TxExecutionInfoStorageReader < Mode > for StorageTxn < ' _ , Mode > {
57+ fn get_tx_execution_infos (
58+ & self ,
59+ block_number : BlockNumber ,
60+ ) -> StorageResult < Option < Vec < TransactionExecutionInfo > > > {
61+ let table = self . open_table ( & self . tables . tx_execution_infos ) ?;
62+ let Some ( location) = table. get ( & self . txn , & block_number) ? else {
63+ return Ok ( None ) ;
64+ } ;
65+ Ok ( Some ( self . file_handlers . get_tx_execution_infos_unchecked ( location) ?. 0 ) )
66+ }
67+ }
68+
69+ impl TxExecutionInfoStorageWriter for StorageTxn < ' _ , RW > {
70+ fn append_tx_execution_infos (
71+ self ,
72+ block_number : BlockNumber ,
73+ tx_execution_infos : Vec < TransactionExecutionInfo > ,
74+ ) -> StorageResult < Self > {
75+ let file_offset_table = self . txn . open_table ( & self . tables . file_offsets ) ?;
76+ let tx_execution_infos_table = self . open_table ( & self . tables . tx_execution_infos ) ?;
77+
78+ let infos = TxExecutionInfos ( tx_execution_infos) ;
79+ let location = self . file_handlers . append_tx_execution_infos ( & infos) ;
80+ tx_execution_infos_table. upsert ( & self . txn , & block_number, & location) ?;
81+ file_offset_table. upsert (
82+ & self . txn ,
83+ & OffsetKind :: TxExecutionInfo ,
84+ & location. next_offset ( ) ,
85+ ) ?;
86+
87+ Ok ( self )
88+ }
89+
90+ fn revert_tx_execution_infos ( self , block_number : BlockNumber ) -> StorageResult < Self > {
91+ let tx_execution_infos_table = self . open_table ( & self . tables . tx_execution_infos ) ?;
92+ tx_execution_infos_table. delete ( & self . txn , & block_number) ?;
93+ Ok ( self )
94+ }
95+ }
0 commit comments