Skip to content

Commit c22cfda

Browse files
committed
Send trigger at each block
1 parent 3948370 commit c22cfda

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

rolling-shutter/keyperimpl/gnosis/keyper.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func (kpr *Keyper) Start(ctx context.Context, runner service.Runner) error {
6969
return errors.Wrap(err, "can't instantiate keyper core")
7070
}
7171

72+
triggerer := NewDecryptionTriggerer(kpr.config.Gnosis, decrTrigChan)
73+
7274
kpr.chainSyncClient, err = chainsync.NewClient(
7375
ctx,
7476
chainsync.WithClientURL(kpr.config.Gnosis.EthereumURL),
@@ -82,7 +84,7 @@ func (kpr *Keyper) Start(ctx context.Context, runner service.Runner) error {
8284
return err
8385
}
8486

85-
return runner.StartService(kpr.core, kpr.chainSyncClient)
87+
return runner.StartService(kpr.core, triggerer, kpr.chainSyncClient)
8688
}
8789

8890
func (kpr *Keyper) newBlock(_ context.Context, ev *syncevent.LatestBlock) error {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package gnosis
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+
"github.com/shutter-network/rolling-shutter/rolling-shutter/keyper/epochkghandler"
11+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/broker"
12+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/configuration"
13+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/identitypreimage"
14+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
15+
)
16+
17+
type DecryptionTriggerer struct {
18+
config *configuration.EthnodeConfig
19+
decryptionTriggerChannel chan *broker.Event[*epochkghandler.DecryptionTrigger]
20+
}
21+
22+
func NewDecryptionTriggerer(
23+
config *configuration.EthnodeConfig,
24+
decryptionTriggerChannel chan *broker.Event[*epochkghandler.DecryptionTrigger],
25+
) *DecryptionTriggerer {
26+
return &DecryptionTriggerer{
27+
config: config,
28+
decryptionTriggerChannel: decryptionTriggerChannel,
29+
}
30+
}
31+
32+
func (t *DecryptionTriggerer) Start(ctx context.Context, runner service.Runner) error {
33+
client, err := ethclient.DialContext(ctx, t.config.EthereumURL)
34+
if err != nil {
35+
return err
36+
}
37+
runner.Go(func() error {
38+
headers := make(chan *types.Header)
39+
headSubscription, err := client.SubscribeNewHead(ctx, headers)
40+
if err != nil {
41+
return err
42+
}
43+
44+
for {
45+
select {
46+
case err := <-headSubscription.Err():
47+
return err
48+
case header := <-headers:
49+
err := t.handleNewHead(ctx, header)
50+
if err != nil {
51+
return err
52+
}
53+
case <-ctx.Done():
54+
return ctx.Err()
55+
}
56+
}
57+
})
58+
return nil
59+
}
60+
61+
func (t *DecryptionTriggerer) handleNewHead(_ context.Context, header *types.Header) error {
62+
log.Debug().Uint64("block-number", header.Number.Uint64()).Msg("handling new head")
63+
64+
// create some random identities for testing
65+
maxNumIdentityPreimages := uint64(2)
66+
numIdentityPreimages := header.Number.Uint64()%maxNumIdentityPreimages + 1
67+
identityPreimages := []identitypreimage.IdentityPreimage{}
68+
for i := 0; i < int(numIdentityPreimages); i++ {
69+
n := header.Number.Uint64()*maxNumIdentityPreimages + uint64(i)
70+
identityPreimage := identitypreimage.Uint64ToIdentityPreimage(n)
71+
identityPreimages = append(identityPreimages, identityPreimage)
72+
}
73+
trigger := epochkghandler.DecryptionTrigger{
74+
BlockNumber: header.Number.Uint64(),
75+
IdentityPreimages: identityPreimages,
76+
}
77+
event := broker.NewEvent(&trigger)
78+
t.decryptionTriggerChannel <- event
79+
return nil
80+
}

0 commit comments

Comments
 (0)