|
| 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