Skip to content

Commit 4b5f240

Browse files
committed
Show latency
1 parent ca87e05 commit 4b5f240

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed

rolling-shutter/gnosiskeyperwatcher/blocks.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gnosiskeyperwatcher
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/ethereum/go-ethereum/core/types"
78
"github.com/ethereum/go-ethereum/ethclient"
@@ -12,12 +13,19 @@ import (
1213
)
1314

1415
type BlocksWatcher struct {
15-
config *keyper.Config
16+
config *keyper.Config
17+
blocksChannel chan *BlockReceivedEvent
1618
}
1719

18-
func NewBlocksWatcher(config *keyper.Config) *BlocksWatcher {
20+
type BlockReceivedEvent struct {
21+
Header *types.Header
22+
Time time.Time
23+
}
24+
25+
func NewBlocksWatcher(config *keyper.Config, blocksChannel chan *BlockReceivedEvent) *BlocksWatcher {
1926
return &BlocksWatcher{
20-
config: config,
27+
config: config,
28+
blocksChannel: blocksChannel,
2129
}
2230
}
2331

@@ -41,6 +49,11 @@ func (w *BlocksWatcher) Start(ctx context.Context, runner service.Runner) error
4149
return ctx.Err()
4250
case head := <-newHeads:
4351
w.logNewHead(head)
52+
ev := &BlockReceivedEvent{
53+
Header: head,
54+
Time: time.Now(),
55+
}
56+
w.blocksChannel <- ev
4457
case err := <-sub.Err():
4558
return err
4659
}

rolling-shutter/gnosiskeyperwatcher/keys.go

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package gnosiskeyperwatcher
22

33
import (
44
"context"
5+
"fmt"
6+
"sync"
7+
"time"
58

69
pubsub "github.com/libp2p/go-libp2p-pubsub"
710
"github.com/rs/zerolog/log"
@@ -13,22 +16,33 @@ import (
1316
)
1417

1518
type KeysWatcher struct {
16-
config *keyper.Config
19+
config *keyper.Config
20+
blocksChannel chan *BlockReceivedEvent
21+
22+
recentBlocksMux sync.Mutex
23+
recentBlocks map[uint64]*BlockReceivedEvent
24+
mostRecentBlock uint64
1725
}
1826

19-
func NewKeysWatcher(config *keyper.Config) *KeysWatcher {
27+
func NewKeysWatcher(config *keyper.Config, blocksChannel chan *BlockReceivedEvent) *KeysWatcher {
2028
return &KeysWatcher{
21-
config: config,
29+
config: config,
30+
blocksChannel: blocksChannel,
31+
recentBlocksMux: sync.Mutex{},
32+
recentBlocks: make(map[uint64]*BlockReceivedEvent),
33+
mostRecentBlock: 0,
2234
}
2335
}
2436

25-
func (w *KeysWatcher) Start(_ context.Context, runner service.Runner) error {
37+
func (w *KeysWatcher) Start(ctx context.Context, runner service.Runner) error {
2638
p2pService, err := p2p.New(w.config.P2P)
2739
if err != nil {
2840
return err
2941
}
3042
p2pService.AddMessageHandler(w)
3143

44+
runner.Go(func() error { return w.insertBlocks(ctx) })
45+
3246
return runner.StartService(p2pService)
3347
}
3448

@@ -43,11 +57,70 @@ func (w *KeysWatcher) ValidateMessage(_ context.Context, _ p2pmsg.Message) (pubs
4357
}
4458

4559
func (w *KeysWatcher) HandleMessage(_ context.Context, msgUntyped p2pmsg.Message) ([]p2pmsg.Message, error) {
60+
t := time.Now()
4661
msg := msgUntyped.(*p2pmsg.DecryptionKeys)
4762
extra := msg.Extra.(*p2pmsg.DecryptionKeys_Gnosis).Gnosis
63+
64+
ev, ok := w.getRecentBlock(extra.Slot)
65+
if !ok {
66+
log.Warn().
67+
Uint64("keys-block", extra.Slot).
68+
Uint64("most-recent-block", w.mostRecentBlock).
69+
Msg("received keys for unknown block")
70+
return []p2pmsg.Message{}, nil
71+
}
72+
73+
dt := t.Sub(ev.Time)
4874
log.Info().
4975
Uint64("block", extra.Slot).
5076
Int("num-keys", len(msg.Keys)).
77+
Str("latency", fmt.Sprintf("%.2fs", dt.Seconds())).
5178
Msg("new keys")
5279
return []p2pmsg.Message{}, nil
5380
}
81+
82+
func (w *KeysWatcher) insertBlocks(ctx context.Context) error {
83+
for {
84+
select {
85+
case <-ctx.Done():
86+
return ctx.Err()
87+
case ev, ok := <-w.blocksChannel:
88+
if !ok {
89+
return nil
90+
}
91+
w.insertBlock(ev)
92+
w.clearOldBlocks(ev)
93+
}
94+
}
95+
}
96+
97+
func (w *KeysWatcher) insertBlock(ev *BlockReceivedEvent) {
98+
w.recentBlocksMux.Lock()
99+
defer w.recentBlocksMux.Unlock()
100+
w.recentBlocks[ev.Header.Number.Uint64()] = ev
101+
if ev.Header.Number.Uint64() > w.mostRecentBlock {
102+
w.mostRecentBlock = ev.Header.Number.Uint64()
103+
}
104+
}
105+
106+
func (w *KeysWatcher) clearOldBlocks(latestEv *BlockReceivedEvent) {
107+
w.recentBlocksMux.Lock()
108+
defer w.recentBlocksMux.Unlock()
109+
110+
tooOld := []uint64{}
111+
for block := range w.recentBlocks {
112+
if block < latestEv.Header.Number.Uint64()-100 {
113+
tooOld = append(tooOld, block)
114+
}
115+
}
116+
for _, block := range tooOld {
117+
delete(w.recentBlocks, block)
118+
}
119+
}
120+
121+
func (w *KeysWatcher) getRecentBlock(blockNumber uint64) (*BlockReceivedEvent, bool) {
122+
w.recentBlocksMux.Lock()
123+
defer w.recentBlocksMux.Unlock()
124+
ev, ok := w.recentBlocks[blockNumber]
125+
return ev, ok
126+
}

rolling-shutter/gnosiskeyperwatcher/watcher.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func New(config *keyper.Config) *Watcher {
1818
}
1919

2020
func (w *Watcher) Start(_ context.Context, runner service.Runner) error {
21-
blocksWatcher := NewBlocksWatcher(w.config)
22-
keysWatcher := NewKeysWatcher(w.config)
21+
blocksChannel := make(chan *BlockReceivedEvent)
22+
blocksWatcher := NewBlocksWatcher(w.config, blocksChannel)
23+
keysWatcher := NewKeysWatcher(w.config, blocksChannel)
2324
return runner.StartService(blocksWatcher, keysWatcher)
2425
}

0 commit comments

Comments
 (0)