Skip to content

Commit 3948370

Browse files
committed
Monitor main chain in gnosis keyper
1 parent d8143b6 commit 3948370

File tree

5 files changed

+103
-6
lines changed

5 files changed

+103
-6
lines changed

rolling-shutter/contract/deployment/deployment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ func LoadChainID(dir string) (uint64, error) {
334334
return 0, errors.Wrapf(err, "failed to load chain id file at %s", path)
335335
}
336336

337-
chainID, err := strconv.ParseInt(string(data), 10, 64)
337+
chainIDStr := strings.TrimSpace(string(data))
338+
chainID, err := strconv.ParseInt(chainIDStr, 10, 64)
338339
if err != nil {
339340
return 0, errors.Wrapf(err, "failed to parse chain id in %s", path)
340341
}

rolling-shutter/keyperimpl/gnosis/config.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ type Config struct {
3737
Gnosis *configuration.EthnodeConfig
3838
Shuttermint *kprconfig.ShuttermintConfig
3939
Metrics *metricsserver.MetricsConfig
40+
41+
GnosisContracts *GnosisContracts `shconfig:",required"`
42+
}
43+
44+
type GnosisContracts struct {
45+
KeyperSetManager common.Address `shconfig:",required"`
46+
KeyBroadcastContract common.Address `shconfig:",required"`
47+
Sequencer common.Address `shconfig:",required"`
4048
}
4149

4250
func (c *Config) Validate() error {
@@ -50,6 +58,11 @@ func (c *Config) Name() string {
5058
func (c *Config) SetDefaultValues() error {
5159
c.HTTPEnabled = false
5260
c.HTTPListenAddress = ":3000"
61+
c.GnosisContracts = &GnosisContracts{
62+
KeyperSetManager: common.Address{},
63+
KeyBroadcastContract: common.Address{},
64+
Sequencer: common.Address{},
65+
}
5366
return nil
5467
}
5568

@@ -60,6 +73,7 @@ func (c *Config) SetExampleValues() error {
6073
}
6174
c.InstanceID = 42
6275
c.DatabaseURL = "postgres://pguser:pgpassword@localhost:5432/shutter"
76+
6377
return nil
6478
}
6579

rolling-shutter/keyperimpl/gnosis/keyper.go

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@ package gnosis
22

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

7+
"github.com/jackc/pgx/v4"
8+
"github.com/jackc/pgx/v4/pgxpool"
69
"github.com/pkg/errors"
10+
"github.com/rs/zerolog/log"
711

12+
obskeyper "github.com/shutter-network/rolling-shutter/rolling-shutter/chainobserver/db/keyper"
813
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper"
914
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/epochkghandler"
1015
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/kprconfig"
1116
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyperimpl/gnosis/database"
17+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley"
1218
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/broker"
19+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/chainsync"
20+
syncevent "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/chainsync/event"
1321
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/db"
1422
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
23+
"github.com/shutter-network/rolling-shutter/rolling-shutter/shdb"
1524
)
1625

26+
var ErrParseKeyperSet = errors.New("can't parse KeyperSet")
27+
1728
type Keyper struct {
18-
core *keyper.KeyperCore
19-
config *Config
29+
core *keyper.KeyperCore
30+
config *Config
31+
dbpool *pgxpool.Pool
32+
chainSyncClient *chainsync.Client
2033
}
2134

2235
func New(c *Config) *Keyper {
@@ -26,10 +39,12 @@ func New(c *Config) *Keyper {
2639
}
2740

2841
func (kpr *Keyper) Start(ctx context.Context, runner service.Runner) error {
42+
var err error
43+
2944
decrTrigChan := make(chan *broker.Event[*epochkghandler.DecryptionTrigger])
3045
runner.Defer(func() { close(decrTrigChan) })
3146

32-
dbpool, err := db.Connect(ctx, runner, kpr.config.DatabaseURL, database.Definition.Name())
47+
kpr.dbpool, err = db.Connect(ctx, runner, kpr.config.DatabaseURL, database.Definition.Name())
3348
if err != nil {
3449
return errors.Wrap(err, "failed to connect to database")
3550
}
@@ -46,11 +61,74 @@ func (kpr *Keyper) Start(ctx context.Context, runner service.Runner) error {
4661
Metrics: kpr.config.Metrics,
4762
},
4863
decrTrigChan,
49-
keyper.WithDBPool(dbpool),
64+
keyper.WithDBPool(kpr.dbpool),
65+
keyper.NoBroadcastEonPublicKey(),
66+
keyper.WithEonPublicKeyHandler(kpr.newEonPublicKey),
5067
)
5168
if err != nil {
5269
return errors.Wrap(err, "can't instantiate keyper core")
5370
}
5471

55-
return runner.StartService(kpr.core)
72+
kpr.chainSyncClient, err = chainsync.NewClient(
73+
ctx,
74+
chainsync.WithClientURL(kpr.config.Gnosis.EthereumURL),
75+
chainsync.WithKeyperSetManager(kpr.config.GnosisContracts.KeyperSetManager),
76+
chainsync.WithKeyBroadcastContract(kpr.config.GnosisContracts.KeyBroadcastContract),
77+
chainsync.WithSyncNewBlock(kpr.newBlock),
78+
chainsync.WithSyncNewKeyperSet(kpr.newKeyperSet),
79+
chainsync.WithPrivateKey(kpr.config.Gnosis.PrivateKey.Key),
80+
)
81+
if err != nil {
82+
return err
83+
}
84+
85+
return runner.StartService(kpr.core, kpr.chainSyncClient)
86+
}
87+
88+
func (kpr *Keyper) newBlock(_ context.Context, ev *syncevent.LatestBlock) error {
89+
log.Info().
90+
Uint64("number", ev.Number.Uint64()).
91+
Str("hash", ev.BlockHash.Hex()).
92+
Msg("new latest block")
93+
return nil
94+
}
95+
96+
func (kpr *Keyper) newKeyperSet(ctx context.Context, ev *syncevent.KeyperSet) error {
97+
log.Info().
98+
Uint64("activation-block", ev.ActivationBlock).
99+
Uint64("eon", ev.Eon).
100+
Msg("new keyper set added")
101+
fmt.Printf("%+v\n", ev.Members)
102+
103+
return kpr.dbpool.BeginFunc(ctx, func(tx pgx.Tx) error {
104+
obskeyperdb := obskeyper.New(tx)
105+
106+
keyperConfigIndex, err := medley.Uint64ToInt64Safe(ev.Eon)
107+
if err != nil {
108+
return errors.Wrap(err, ErrParseKeyperSet.Error())
109+
}
110+
activationBlockNumber, err := medley.Uint64ToInt64Safe(ev.ActivationBlock)
111+
if err != nil {
112+
return errors.Wrap(err, ErrParseKeyperSet.Error())
113+
}
114+
threshold, err := medley.Uint64ToInt64Safe(ev.Threshold)
115+
if err != nil {
116+
return errors.Wrap(err, ErrParseKeyperSet.Error())
117+
}
118+
119+
return obskeyperdb.InsertKeyperSet(ctx, obskeyper.InsertKeyperSetParams{
120+
KeyperConfigIndex: keyperConfigIndex,
121+
ActivationBlockNumber: activationBlockNumber,
122+
Keypers: shdb.EncodeAddresses(ev.Members),
123+
Threshold: int32(threshold),
124+
})
125+
})
126+
}
127+
128+
func (kpr *Keyper) newEonPublicKey(_ context.Context, pubKey keyper.EonPublicKey) error {
129+
log.Info().
130+
Uint64("eon", pubKey.Eon).
131+
Uint64("activation-block", pubKey.ActivationBlock).
132+
Msg("new eon pk")
133+
return nil
56134
}

rolling-shutter/keyperimpl/optimism/bootstrap/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"crypto/rand"
55
"io"
66

7+
"github.com/ethereum/go-ethereum/common"
8+
79
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/configuration"
810
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/keys"
911
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/number"
@@ -27,6 +29,7 @@ type Config struct {
2729

2830
JSONRPCURL string ` comment:"The op-geth JSON RPC endpoint"`
2931
ByActivationBlockNumber *number.BlockNumber
32+
KeyperSetManager common.Address
3033
ByIndex *uint64
3134
KeyperSetFilePath string
3235

rolling-shutter/keyperimpl/optimism/bootstrap/keyperset.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func GetKeyperSet(ctx context.Context, config *Config) error {
1515
sl2, err := chainsync.NewClient(
1616
ctx,
1717
chainsync.WithClientURL(config.JSONRPCURL),
18+
chainsync.WithKeyperSetManager(config.KeyperSetManager),
1819
)
1920
if err != nil {
2021
return err

0 commit comments

Comments
 (0)