Skip to content

Commit 4d35b80

Browse files
committed
create ReorgEvent
1 parent 5ee36ad commit 4d35b80

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

rpc/handlers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type Handler struct {
9696

9797
version string
9898
newHeads *feed.Feed[*core.Header]
99-
reorgs *feed.Feed[*sync.ReorgData]
99+
reorgs *feed.Feed[*sync.ReorgBlockRange]
100100
pendingTxs *feed.Feed[[]core.Transaction]
101101

102102
idgen func() uint64
@@ -138,7 +138,7 @@ func New(bcReader blockchain.Reader, syncReader sync.Reader, virtualMachine vm.V
138138
},
139139
version: version,
140140
newHeads: feed.New[*core.Header](),
141-
reorgs: feed.New[*sync.ReorgData](),
141+
reorgs: feed.New[*sync.ReorgBlockRange](),
142142
pendingTxs: feed.New[[]core.Transaction](),
143143
subscriptions: make(map[uint64]*subscription),
144144

rpc/subscriptions.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func (h *Handler) processPendingTxs(
284284
// filterTxs filters the transactions based on the getDetails flag.
285285
// If getDetails is true, response will contain the transaction details.
286286
// If getDetails is false, response will only contain the transaction hashes.
287-
func (h *Handler) filterTxs(pendingTxs []core.Transaction, getDetails bool, senderAddr []felt.Felt) interface{} {
287+
func (h *Handler) filterTxs(pendingTxs []core.Transaction, getDetails bool, senderAddr []felt.Felt) any {
288288
if getDetails {
289289
return h.filterTxDetails(pendingTxs, senderAddr)
290290
}
@@ -386,8 +386,7 @@ func (h *Handler) resolveBlockRange(blockID *BlockID) (*core.Header, *core.Heade
386386
// sendHistoricalHeaders sends a range of headers from the start header until the latest header
387387
func (h *Handler) sendHistoricalHeaders(
388388
ctx context.Context,
389-
startHeader *core.Header,
390-
latestHeader *core.Header,
389+
startHeader, latestHeader *core.Header,
391390
w jsonrpc.Conn,
392391
id uint64,
393392
) error {
@@ -448,7 +447,7 @@ func (h *Handler) sendHeader(w jsonrpc.Conn, header *core.Header, id uint64) err
448447
return err
449448
}
450449

451-
func (h *Handler) processReorgs(ctx context.Context, reorgSub *feed.Subscription[*sync.ReorgData], w jsonrpc.Conn, id uint64) {
450+
func (h *Handler) processReorgs(ctx context.Context, reorgSub *feed.Subscription[*sync.ReorgBlockRange], w jsonrpc.Conn, id uint64) {
452451
for {
453452
select {
454453
case <-ctx.Done():
@@ -462,13 +461,25 @@ func (h *Handler) processReorgs(ctx context.Context, reorgSub *feed.Subscription
462461
}
463462
}
464463

465-
func (h *Handler) sendReorg(w jsonrpc.Conn, reorg *sync.ReorgData, id uint64) error {
464+
type ReorgEvent struct {
465+
StartBlockHash *felt.Felt `json:"starting_block_hash"`
466+
StartBlockNum uint64 `json:"starting_block_number"`
467+
EndBlockHash *felt.Felt `json:"ending_block_hash"`
468+
EndBlockNum uint64 `json:"ending_block_number"`
469+
}
470+
471+
func (h *Handler) sendReorg(w jsonrpc.Conn, reorg *sync.ReorgBlockRange, id uint64) error {
466472
resp, err := json.Marshal(jsonrpc.Request{
467473
Version: "2.0",
468474
Method: "starknet_subscriptionReorg",
469475
Params: map[string]any{
470476
"subscription_id": id,
471-
"result": reorg,
477+
"result": &ReorgEvent{
478+
StartBlockHash: reorg.StartBlockHash,
479+
StartBlockNum: reorg.StartBlockNum,
480+
EndBlockHash: reorg.EndBlockHash,
481+
EndBlockNum: reorg.EndBlockNum,
482+
},
472483
},
473484
})
474485
if err != nil {

rpc/subscriptions_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,14 @@ func TestSubscribeEvents(t *testing.T) {
332332

333333
type fakeSyncer struct {
334334
newHeads *feed.Feed[*core.Header]
335-
reorgs *feed.Feed[*sync.ReorgData]
335+
reorgs *feed.Feed[*sync.ReorgBlockRange]
336336
pendingTxs *feed.Feed[[]core.Transaction]
337337
}
338338

339339
func newFakeSyncer() *fakeSyncer {
340340
return &fakeSyncer{
341341
newHeads: feed.New[*core.Header](),
342-
reorgs: feed.New[*sync.ReorgData](),
342+
reorgs: feed.New[*sync.ReorgBlockRange](),
343343
pendingTxs: feed.New[[]core.Transaction](),
344344
}
345345
}
@@ -623,7 +623,7 @@ func TestSubscriptionReorg(t *testing.T) {
623623
}
624624

625625
// Simulate a reorg
626-
syncer.reorgs.Send(&sync.ReorgData{
626+
syncer.reorgs.Send(&sync.ReorgBlockRange{
627627
StartBlockHash: utils.HexToFelt(t, "0x4e1f77f39545afe866ac151ac908bd1a347a2a8a7d58bef1276db4f06fdf2f6"),
628628
StartBlockNum: 0,
629629
EndBlockHash: utils.HexToFelt(t, "0x34e815552e42c5eb5233b99de2d3d7fd396e575df2719bf98e7ed2794494f86"),

sync/sync.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type HeaderSubscription struct {
3939
}
4040

4141
type ReorgSubscription struct {
42-
*feed.Subscription[*ReorgData]
42+
*feed.Subscription[*ReorgBlockRange]
4343
}
4444

4545
type PendingTxSubscription struct {
@@ -77,15 +77,15 @@ func (n *NoopSynchronizer) SubscribeNewHeads() HeaderSubscription {
7777
}
7878

7979
func (n *NoopSynchronizer) SubscribeReorg() ReorgSubscription {
80-
return ReorgSubscription{feed.New[*ReorgData]().Subscribe()}
80+
return ReorgSubscription{feed.New[*ReorgBlockRange]().Subscribe()}
8181
}
8282

8383
func (n *NoopSynchronizer) SubscribePendingTxs() PendingTxSubscription {
8484
return PendingTxSubscription{feed.New[[]core.Transaction]().Subscribe()}
8585
}
8686

87-
// ReorgData represents data about reorganised blocks, starting and ending block number and hash
88-
type ReorgData struct {
87+
// ReorgBlockRange represents data about reorganised blocks, starting and ending block number and hash
88+
type ReorgBlockRange struct {
8989
// StartBlockHash is the hash of the first known block of the orphaned chain
9090
StartBlockHash *felt.Felt `json:"starting_block_hash"`
9191
// StartBlockNum is the number of the first known block of the orphaned chain
@@ -117,7 +117,7 @@ type Synchronizer struct {
117117
startingBlockNumber *uint64
118118
highestBlockHeader atomic.Pointer[core.Header]
119119
newHeads *feed.Feed[*core.Header]
120-
reorgFeed *feed.Feed[*ReorgData]
120+
reorgFeed *feed.Feed[*ReorgBlockRange]
121121
pendingTxsFeed *feed.Feed[[]core.Transaction]
122122

123123
log utils.SimpleLogger
@@ -128,7 +128,7 @@ type Synchronizer struct {
128128
catchUpMode bool
129129
plugin junoplugin.JunoPlugin
130130

131-
currReorg *ReorgData // If nil, no reorg is happening
131+
currReorg *ReorgBlockRange // If nil, no reorg is happening
132132
}
133133

134134
func New(bc *blockchain.Blockchain, starkNetData starknetdata.StarknetData, log utils.SimpleLogger,
@@ -140,7 +140,7 @@ func New(bc *blockchain.Blockchain, starkNetData starknetdata.StarknetData, log
140140
starknetData: starkNetData,
141141
log: log,
142142
newHeads: feed.New[*core.Header](),
143-
reorgFeed: feed.New[*ReorgData](),
143+
reorgFeed: feed.New[*ReorgBlockRange](),
144144
pendingTxsFeed: feed.New[[]core.Transaction](),
145145
pendingPollInterval: pendingPollInterval,
146146
listener: &SelectiveListener{},
@@ -446,7 +446,7 @@ func (s *Synchronizer) revertHead(forkBlock *core.Block) {
446446
}
447447

448448
if s.currReorg == nil { // first block of the reorg
449-
s.currReorg = &ReorgData{
449+
s.currReorg = &ReorgBlockRange{
450450
StartBlockHash: localHead,
451451
StartBlockNum: head.Number,
452452
EndBlockHash: localHead,

0 commit comments

Comments
 (0)