Skip to content

Commit ca87e05

Browse files
committed
Add basic watcher command
1 parent 598c4a2 commit ca87e05

File tree

7 files changed

+177
-5
lines changed

7 files changed

+177
-5
lines changed

rolling-shutter/cmd/gnosiskeyper/gnosiskeyper.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/spf13/cobra"
1010

1111
"github.com/shutter-network/rolling-shutter/rolling-shutter/cmd/shversion"
12+
"github.com/shutter-network/rolling-shutter/rolling-shutter/gnosiskeyperwatcher"
1213
keyper "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis"
1314
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis/database"
1415
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/configuration/command"
@@ -28,6 +29,12 @@ Shuttermint node which have to be started separately in advance.`,
2829
command.WithDumpConfigSubcommand(),
2930
)
3031
builder.AddInitDBCommand(initDB)
32+
builder.AddFunctionSubcommand(
33+
watch,
34+
"watch",
35+
"Watch the keypers doing their work and log the generated decryption keys.",
36+
cobra.NoArgs,
37+
)
3138
return builder.Command()
3239
}
3340

@@ -36,7 +43,7 @@ func main(config *keyper.Config) error {
3643
Str("version", shversion.Version()).
3744
Str("address", config.GetAddress().Hex()).
3845
Str("shuttermint", config.Shuttermint.ShuttermintURL).
39-
Msg("starting gnosiskeyper")
46+
Msg("starting gnosis keyper")
4047

4148
kpr := keyper.New(config)
4249
return service.RunWithSighandler(context.Background(), kpr)
@@ -51,3 +58,8 @@ func initDB(cfg *keyper.Config) error {
5158
defer dbpool.Close()
5259
return db.InitDB(ctx, dbpool, database.Definition.Name(), database.Definition)
5360
}
61+
62+
func watch(cfg *keyper.Config) error {
63+
log.Info().Msg("starting monitor")
64+
return service.RunWithSighandler(context.Background(), gnosiskeyperwatcher.New(cfg))
65+
}

rolling-shutter/docs/rolling-shutter_gnosiskeyper.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ rolling-shutter gnosiskeyper [flags]
3232
* [rolling-shutter gnosiskeyper dump-config](rolling-shutter_gnosiskeyper_dump-config.md) - Dump a 'gnosiskeyper' configuration file, based on given config and env vars
3333
* [rolling-shutter gnosiskeyper generate-config](rolling-shutter_gnosiskeyper_generate-config.md) - Generate a 'gnosiskeyper' configuration file
3434
* [rolling-shutter gnosiskeyper initdb](rolling-shutter_gnosiskeyper_initdb.md) - Initialize the database of the 'gnosiskeyper'
35+
* [rolling-shutter gnosiskeyper watch](rolling-shutter_gnosiskeyper_watch.md) - Watch the keypers doing their work and log the generated decryption keys.
3536

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## rolling-shutter gnosiskeyper watch
2+
3+
Watch the keypers doing their work and log the generated decryption keys.
4+
5+
```
6+
rolling-shutter gnosiskeyper watch [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for watch
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--config string config file
19+
--logformat string set log format, possible values: min, short, long, max (default "long")
20+
--loglevel string set log level, possible values: warn, info, debug (default "info")
21+
--no-color do not write colored logs
22+
```
23+
24+
### SEE ALSO
25+
26+
* [rolling-shutter gnosiskeyper](rolling-shutter_gnosiskeyper.md) - Run a Shutter keyper for Gnosis Chain
27+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package gnosiskeyperwatcher
2+
3+
import (
4+
"context"
5+
6+
"github.com/ethereum/go-ethereum/core/types"
7+
"github.com/ethereum/go-ethereum/ethclient"
8+
"github.com/rs/zerolog/log"
9+
10+
keyper "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis"
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
12+
)
13+
14+
type BlocksWatcher struct {
15+
config *keyper.Config
16+
}
17+
18+
func NewBlocksWatcher(config *keyper.Config) *BlocksWatcher {
19+
return &BlocksWatcher{
20+
config: config,
21+
}
22+
}
23+
24+
func (w *BlocksWatcher) Start(ctx context.Context, runner service.Runner) error {
25+
runner.Go(func() error {
26+
ethClient, err := ethclient.Dial(w.config.Gnosis.EthereumURL)
27+
if err != nil {
28+
return err
29+
}
30+
31+
newHeads := make(chan *types.Header)
32+
sub, err := ethClient.SubscribeNewHead(ctx, newHeads)
33+
if err != nil {
34+
return err
35+
}
36+
defer sub.Unsubscribe()
37+
38+
for {
39+
select {
40+
case <-ctx.Done():
41+
return ctx.Err()
42+
case head := <-newHeads:
43+
w.logNewHead(head)
44+
case err := <-sub.Err():
45+
return err
46+
}
47+
}
48+
})
49+
return nil
50+
}
51+
52+
func (w *BlocksWatcher) logNewHead(head *types.Header) {
53+
log.Info().
54+
Int64("number", head.Number.Int64()).
55+
Hex("hash", head.Hash().Bytes()).
56+
Msg("new head")
57+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package gnosiskeyperwatcher
2+
3+
import (
4+
"context"
5+
6+
pubsub "github.com/libp2p/go-libp2p-pubsub"
7+
"github.com/rs/zerolog/log"
8+
9+
keyper "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis"
10+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2p"
12+
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg"
13+
)
14+
15+
type KeysWatcher struct {
16+
config *keyper.Config
17+
}
18+
19+
func NewKeysWatcher(config *keyper.Config) *KeysWatcher {
20+
return &KeysWatcher{
21+
config: config,
22+
}
23+
}
24+
25+
func (w *KeysWatcher) Start(_ context.Context, runner service.Runner) error {
26+
p2pService, err := p2p.New(w.config.P2P)
27+
if err != nil {
28+
return err
29+
}
30+
p2pService.AddMessageHandler(w)
31+
32+
return runner.StartService(p2pService)
33+
}
34+
35+
func (w *KeysWatcher) MessagePrototypes() []p2pmsg.Message {
36+
return []p2pmsg.Message{
37+
&p2pmsg.DecryptionKeys{},
38+
}
39+
}
40+
41+
func (w *KeysWatcher) ValidateMessage(_ context.Context, _ p2pmsg.Message) (pubsub.ValidationResult, error) {
42+
return pubsub.ValidationAccept, nil
43+
}
44+
45+
func (w *KeysWatcher) HandleMessage(_ context.Context, msgUntyped p2pmsg.Message) ([]p2pmsg.Message, error) {
46+
msg := msgUntyped.(*p2pmsg.DecryptionKeys)
47+
extra := msg.Extra.(*p2pmsg.DecryptionKeys_Gnosis).Gnosis
48+
log.Info().
49+
Uint64("block", extra.Slot).
50+
Int("num-keys", len(msg.Keys)).
51+
Msg("new keys")
52+
return []p2pmsg.Message{}, nil
53+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package gnosiskeyperwatcher
2+
3+
import (
4+
"context"
5+
6+
keyper "github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis"
7+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
8+
)
9+
10+
type Watcher struct {
11+
config *keyper.Config
12+
}
13+
14+
func New(config *keyper.Config) *Watcher {
15+
return &Watcher{
16+
config: config,
17+
}
18+
}
19+
20+
func (w *Watcher) Start(_ context.Context, runner service.Runner) error {
21+
blocksWatcher := NewBlocksWatcher(w.config)
22+
keysWatcher := NewKeysWatcher(w.config)
23+
return runner.StartService(blocksWatcher, keysWatcher)
24+
}

rolling-shutter/medley/configuration/command/command.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ func Build[T configuration.Config](
159159

160160
type CobraRunE func(cmd *cobra.Command, args []string) error
161161

162-
// AddInitDBCommand attaches an additional subcommand
163-
// 'initdb' to the command initially built by the Build method.
164-
// The initDB function argument is structured in the same way than the "main"
165-
// function passed in to the Build method.
162+
// AddFunctionSubcommand attaches an additional subcommand to the command initially built by the
163+
// Build method. The command executes the given function and takes the given arguments.
166164
func (cb *CommandBuilder[T]) AddFunctionSubcommand(
167165
fnc ConfigurableFunc[T],
168166
use, short string,

0 commit comments

Comments
 (0)