@@ -27,6 +27,10 @@ import (
2727
2828var emptyCommitments = core.BlockCommitments {}
2929
30+ const (
31+ testResponse = `{"jsonrpc":"2.0","method":"starknet_subscriptionNewHeads","params":{"result":{"block_hash":"0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6","parent_hash":"0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb","block_number":2,"new_root":"0x3ceee867d50b5926bb88c0ec7e0b9c20ae6b537e74aac44b8fcf6bb6da138d9","timestamp":1637084470,"sequencer_address":"0x0","l1_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_data_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_da_mode":"CALLDATA","starknet_version":""},"subscription_id":%d}}`
32+ )
33+
3034func TestEvents (t * testing.T ) {
3135 testDB := pebble .NewMemTest (t )
3236 n := utils .Ptr (utils .Sepolia )
@@ -282,26 +286,10 @@ func TestSubscribeNewHeadsAndUnsubscribe(t *testing.T) {
282286 require .Nil (t , rpcErr )
283287
284288 // Simulate a new block
285- syncer .newHeads .Send (& core.Header {
286- Hash : utils .HexToFelt (t , "0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6" ),
287- ParentHash : utils .HexToFelt (t , "0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb" ),
288- Number : 2 ,
289- GlobalStateRoot : utils .HexToFelt (t , "0x3ceee867d50b5926bb88c0ec7e0b9c20ae6b537e74aac44b8fcf6bb6da138d9" ),
290- Timestamp : 1637084470 ,
291- SequencerAddress : utils .HexToFelt (t , "0x0" ),
292- L1DataGasPrice : & core.GasPrice {
293- PriceInFri : utils .HexToFelt (t , "0x0" ),
294- PriceInWei : utils .HexToFelt (t , "0x0" ),
295- },
296- GasPrice : utils .HexToFelt (t , "0x0" ),
297- GasPriceSTRK : utils .HexToFelt (t , "0x0" ),
298- L1DAMode : core .Calldata ,
299- ProtocolVersion : "" ,
300- })
289+ syncer .newHeads .Send (testHeader (t ))
301290
302291 // Receive a block header.
303- want := `{"jsonrpc":"2.0","method":"starknet_subscriptionNewHeads","params":{"result":{"block_hash":"0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6","parent_hash":"0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb","block_number":2,"new_root":"0x3ceee867d50b5926bb88c0ec7e0b9c20ae6b537e74aac44b8fcf6bb6da138d9","timestamp":1637084470,"sequencer_address":"0x0","l1_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_data_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_da_mode":"CALLDATA","starknet_version":""},"subscription_id":%d}}`
304- want = fmt .Sprintf (want , id .ID )
292+ want := fmt .Sprintf (testResponse , id .ID )
305293 got := make ([]byte , len (want ))
306294 _ , err := clientConn .Read (got )
307295 require .NoError (t , err )
@@ -386,30 +374,14 @@ func TestMultipleSubscribeNewHeadsAndUnsubscribe(t *testing.T) {
386374 require .Equal (t , secondWant , string (secondGot ))
387375
388376 // Simulate a new block
389- syncer .newHeads .Send (& core.Header {
390- Hash : utils .HexToFelt (t , "0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6" ),
391- ParentHash : utils .HexToFelt (t , "0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb" ),
392- Number : 2 ,
393- GlobalStateRoot : utils .HexToFelt (t , "0x3ceee867d50b5926bb88c0ec7e0b9c20ae6b537e74aac44b8fcf6bb6da138d9" ),
394- Timestamp : 1637084470 ,
395- SequencerAddress : utils .HexToFelt (t , "0x0" ),
396- L1DataGasPrice : & core.GasPrice {
397- PriceInFri : utils .HexToFelt (t , "0x0" ),
398- PriceInWei : utils .HexToFelt (t , "0x0" ),
399- },
400- GasPrice : utils .HexToFelt (t , "0x0" ),
401- GasPriceSTRK : utils .HexToFelt (t , "0x0" ),
402- L1DAMode : core .Calldata ,
403- ProtocolVersion : "" ,
404- })
377+ syncer .newHeads .Send (testHeader (t ))
405378
406379 // Receive a block header.
407- want = `{"jsonrpc":"2.0","method":"starknet_subscriptionNewHeads","params":{"result":{"block_hash":"0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6","parent_hash":"0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb","block_number":2,"new_root":"0x3ceee867d50b5926bb88c0ec7e0b9c20ae6b537e74aac44b8fcf6bb6da138d9","timestamp":1637084470,"sequencer_address":"0x0","l1_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_data_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_da_mode":"CALLDATA","starknet_version":""},"subscription_id":%d}}`
408- firstWant = fmt .Sprintf (want , firstID )
380+ firstWant = fmt .Sprintf (testResponse , firstID )
409381 _ , firstGot , err = conn1 .Read (ctx )
410382 require .NoError (t , err )
411383 require .Equal (t , firstWant , string (firstGot ))
412- secondWant = fmt .Sprintf (want , secondID )
384+ secondWant = fmt .Sprintf (testResponse , secondID )
413385 _ , secondGot , err = conn2 .Read (ctx )
414386 require .NoError (t , err )
415387 require .Equal (t , secondWant , string (secondGot ))
@@ -475,7 +447,20 @@ func TestSubscribeNewHeadsHistorical(t *testing.T) {
475447 require .Equal (t , want , string (got ))
476448
477449 // Simulate a new block
478- syncer .newHeads .Send (& core.Header {
450+ syncer .newHeads .Send (testHeader (t ))
451+
452+ // Check new block content
453+ want = fmt .Sprintf (testResponse , id .ID )
454+ got = make ([]byte , len (want ))
455+ _ , err = clientConn .Read (got )
456+ require .NoError (t , err )
457+ require .Equal (t , want , string (got ))
458+ }
459+
460+ func testHeader (t * testing.T ) * core.Header {
461+ t .Helper ()
462+
463+ header := & core.Header {
479464 Hash : utils .HexToFelt (t , "0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6" ),
480465 ParentHash : utils .HexToFelt (t , "0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb" ),
481466 Number : 2 ,
@@ -490,13 +475,6 @@ func TestSubscribeNewHeadsHistorical(t *testing.T) {
490475 GasPriceSTRK : utils .HexToFelt (t , "0x0" ),
491476 L1DAMode : core .Calldata ,
492477 ProtocolVersion : "" ,
493- })
494-
495- // Check new block content
496- want = `{"jsonrpc":"2.0","method":"starknet_subscriptionNewHeads","params":{"result":{"block_hash":"0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6","parent_hash":"0x2a70fb03fe363a2d6be843343a1d81ce6abeda1e9bd5cc6ad8fa9f45e30fdeb","block_number":2,"new_root":"0x3ceee867d50b5926bb88c0ec7e0b9c20ae6b537e74aac44b8fcf6bb6da138d9","timestamp":1637084470,"sequencer_address":"0x0","l1_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_data_gas_price":{"price_in_fri":"0x0","price_in_wei":"0x0"},"l1_da_mode":"CALLDATA","starknet_version":""},"subscription_id":%d}}`
497- want = fmt .Sprintf (want , id .ID )
498- got = make ([]byte , len (want ))
499- _ , err = clientConn .Read (got )
500- require .NoError (t , err )
501- require .Equal (t , want , string (got ))
478+ }
479+ return header
502480}
0 commit comments