@@ -60,16 +60,105 @@ export class BlockchainDatabase {
6060 return stmt . all ( blockHash ) ;
6161 }
6262
63- // Get posted messages for a bundle
63+ // Get posted messages for a bundle with SystemMessage fields
6464 getPostedMessages ( bundleId ) {
6565 const stmt = this . db . prepare ( `
66- SELECT * FROM posted_messages
66+ SELECT *, system_target, system_amount, system_source, system_owner, system_recipient,
67+ message_type, application_id, system_message_type
68+ FROM posted_messages
6769 WHERE bundle_id = ?
6870 ORDER BY message_index ASC
6971 ` ) ;
7072 return stmt . all ( bundleId ) ;
7173 }
7274
75+ // Get block with all bundles and their messages in a single query
76+ getBlockWithBundlesAndMessages ( blockHash ) {
77+ const stmt = this . db . prepare ( `
78+ SELECT
79+ ib.id as bundle_id,
80+ ib.bundle_index,
81+ ib.origin_chain_id,
82+ ib.action,
83+ ib.source_height,
84+ ib.source_timestamp,
85+ ib.source_cert_hash,
86+ ib.transaction_index as bundle_transaction_index,
87+ ib.created_at as bundle_created_at,
88+ pm.id as message_id,
89+ pm.message_index,
90+ pm.authenticated_signer,
91+ pm.grant_amount,
92+ pm.refund_grant_to,
93+ pm.message_kind,
94+ pm.message_type,
95+ pm.application_id,
96+ pm.system_message_type,
97+ pm.system_target,
98+ pm.system_amount,
99+ pm.system_source,
100+ pm.system_owner,
101+ pm.system_recipient,
102+ pm.message_data,
103+ pm.created_at as message_created_at
104+ FROM incoming_bundles ib
105+ LEFT JOIN posted_messages pm ON ib.id = pm.bundle_id
106+ WHERE ib.block_hash = ?
107+ ORDER BY ib.bundle_index, pm.message_index
108+ ` ) ;
109+
110+ const rows = stmt . all ( blockHash ) ;
111+
112+ // Group results by bundle
113+ const bundlesMap = new Map ( ) ;
114+
115+ for ( const row of rows ) {
116+ const bundleId = row . bundle_id ;
117+
118+ if ( ! bundlesMap . has ( bundleId ) ) {
119+ bundlesMap . set ( bundleId , {
120+ id : bundleId ,
121+ block_hash : blockHash ,
122+ bundle_index : row . bundle_index ,
123+ origin_chain_id : row . origin_chain_id ,
124+ action : row . action ,
125+ source_height : row . source_height ,
126+ source_timestamp : row . source_timestamp ,
127+ source_cert_hash : row . source_cert_hash ,
128+ transaction_index : row . bundle_transaction_index ,
129+ created_at : row . bundle_created_at ,
130+ messages : [ ]
131+ } ) ;
132+ }
133+
134+ // Add message if it exists (LEFT JOIN may produce NULL messages)
135+ if ( row . message_id ) {
136+ const bundle = bundlesMap . get ( bundleId ) ;
137+ bundle . messages . push ( {
138+ id : row . message_id ,
139+ bundle_id : bundleId ,
140+ message_index : row . message_index ,
141+ authenticated_signer : row . authenticated_signer ,
142+ grant_amount : row . grant_amount ,
143+ refund_grant_to : row . refund_grant_to ,
144+ message_kind : row . message_kind ,
145+ message_type : row . message_type ,
146+ application_id : row . application_id ,
147+ system_message_type : row . system_message_type ,
148+ system_target : row . system_target ,
149+ system_amount : row . system_amount ,
150+ system_source : row . system_source ,
151+ system_owner : row . system_owner ,
152+ system_recipient : row . system_recipient ,
153+ message_data : row . message_data ? Buffer . from ( row . message_data ) . toString ( 'base64' ) : null ,
154+ created_at : row . message_created_at
155+ } ) ;
156+ }
157+ }
158+
159+ return Array . from ( bundlesMap . values ( ) ) ;
160+ }
161+
73162 // Get all unique chains with stats
74163 getChains ( ) {
75164 const stmt = this . db . prepare ( `
@@ -104,6 +193,71 @@ export class BlockchainDatabase {
104193 return result . count ;
105194 }
106195
196+ // Get operations for a block
197+ getOperations ( blockHash ) {
198+ const stmt = this . db . prepare ( `
199+ SELECT * FROM operations
200+ WHERE block_hash = ?
201+ ORDER BY operation_index ASC
202+ ` ) ;
203+ const operations = stmt . all ( blockHash ) ;
204+
205+ // Convert binary data to base64 for JSON transport
206+ return operations . map ( op => ( {
207+ ...op ,
208+ data : op . data ? Buffer . from ( op . data ) . toString ( 'base64' ) : null
209+ } ) ) ;
210+ }
211+
212+ // Get messages for a block
213+ getMessages ( blockHash ) {
214+ const stmt = this . db . prepare ( `
215+ SELECT *, system_target, system_amount, system_source, system_owner, system_recipient FROM outgoing_messages
216+ WHERE block_hash = ?
217+ ORDER BY transaction_index ASC, message_index ASC
218+ ` ) ;
219+ const messages = stmt . all ( blockHash ) ;
220+
221+ // Convert binary data to base64 for JSON transport
222+ return messages . map ( msg => ( {
223+ ...msg ,
224+ data : msg . data ? Buffer . from ( msg . data ) . toString ( 'base64' ) : null
225+ } ) ) ;
226+ }
227+
228+ // Get events for a block
229+ getEvents ( blockHash ) {
230+ const stmt = this . db . prepare ( `
231+ SELECT * FROM events
232+ WHERE block_hash = ?
233+ ORDER BY transaction_index ASC, event_index ASC
234+ ` ) ;
235+ const events = stmt . all ( blockHash ) ;
236+
237+ // Convert binary data to base64 for JSON transport
238+ return events . map ( event => ( {
239+ ...event ,
240+ data : event . data ? Buffer . from ( event . data ) . toString ( 'base64' ) : null
241+ } ) ) ;
242+ }
243+
244+ // Get oracle responses for a block
245+ getOracleResponses ( blockHash ) {
246+ const stmt = this . db . prepare ( `
247+ SELECT * FROM oracle_responses
248+ WHERE block_hash = ?
249+ ORDER BY transaction_index ASC, response_index ASC
250+ ` ) ;
251+ const responses = stmt . all ( blockHash ) ;
252+
253+ // Convert binary data to base64 for JSON transport
254+ return responses . map ( resp => ( {
255+ ...resp ,
256+ data : resp . data ? Buffer . from ( resp . data ) . toString ( 'base64' ) : null
257+ } ) ) ;
258+ }
259+
260+
107261 close ( ) {
108262 this . db . close ( ) ;
109263 }
0 commit comments