|
| 1 | +package eonkeypublisher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/accounts/abi/bind" |
| 9 | + "github.com/ethereum/go-ethereum/common" |
| 10 | + "github.com/ethereum/go-ethereum/core/types" |
| 11 | + ethcrypto "github.com/ethereum/go-ethereum/crypto" |
| 12 | + "github.com/ethereum/go-ethereum/ethclient" |
| 13 | + "github.com/jackc/pgx/v4/pgxpool" |
| 14 | + "github.com/pkg/errors" |
| 15 | + "github.com/rs/zerolog/log" |
| 16 | + "github.com/shutter-network/shop-contracts/bindings" |
| 17 | + |
| 18 | + obskeyperdb "github.com/shutter-network/rolling-shutter/rolling-shutter/chainobserver/db/keyper" |
| 19 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/keyper" |
| 20 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/retry" |
| 21 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + eonKeyChannelSize = 32 |
| 26 | + retryInterval = time.Second * 12 |
| 27 | +) |
| 28 | + |
| 29 | +// EonKeyPublisher is a service that publishes eon keys via a eon key publisher contract. |
| 30 | +type EonKeyPublisher struct { |
| 31 | + dbpool *pgxpool.Pool |
| 32 | + client *ethclient.Client |
| 33 | + contract *bindings.EonKeyPublish |
| 34 | + privateKey *ecdsa.PrivateKey |
| 35 | + |
| 36 | + keys chan keyper.EonPublicKey |
| 37 | +} |
| 38 | + |
| 39 | +func NewEonKeyPublisher( |
| 40 | + dbpool *pgxpool.Pool, |
| 41 | + client *ethclient.Client, |
| 42 | + eonKeyPublishAddress common.Address, |
| 43 | + privateKey *ecdsa.PrivateKey, |
| 44 | +) (*EonKeyPublisher, error) { |
| 45 | + contract, err := bindings.NewEonKeyPublish(eonKeyPublishAddress, client) |
| 46 | + if err != nil { |
| 47 | + return nil, errors.Wrap(err, "failed to instantiate eon key publisher contract") |
| 48 | + } |
| 49 | + return &EonKeyPublisher{ |
| 50 | + dbpool: dbpool, |
| 51 | + client: client, |
| 52 | + contract: contract, |
| 53 | + privateKey: privateKey, |
| 54 | + |
| 55 | + keys: make(chan keyper.EonPublicKey, eonKeyChannelSize), |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (p *EonKeyPublisher) Start(ctx context.Context, runner service.Runner) error { //nolint: unparam |
| 60 | + runner.Go(func() error { |
| 61 | + for { |
| 62 | + select { |
| 63 | + case key := <-p.keys: |
| 64 | + p.publish(ctx, key) |
| 65 | + case <-ctx.Done(): |
| 66 | + return ctx.Err() |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// Publish schedules a eon key to be published. |
| 74 | +func (p *EonKeyPublisher) Publish(key keyper.EonPublicKey) { |
| 75 | + p.keys <- key |
| 76 | +} |
| 77 | + |
| 78 | +func (p *EonKeyPublisher) publish(ctx context.Context, key keyper.EonPublicKey) { |
| 79 | + _, err := retry.FunctionCall[struct{}](ctx, func(ctx context.Context) (struct{}, error) { |
| 80 | + return struct{}{}, p.tryPublish(ctx, key) |
| 81 | + }, retry.Interval(retryInterval)) |
| 82 | + if err != nil { |
| 83 | + log.Error(). |
| 84 | + Err(err). |
| 85 | + Uint64("keyper-set-index", key.KeyperConfigIndex). |
| 86 | + Hex("key", key.PublicKey). |
| 87 | + Msg("failed to publish eon key") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func (p *EonKeyPublisher) tryPublish(ctx context.Context, key keyper.EonPublicKey) error { |
| 92 | + db := obskeyperdb.New(p.dbpool) |
| 93 | + keyperSet, err := db.GetKeyperSetByKeyperConfigIndex(ctx, int64(key.Eon)) |
| 94 | + if err != nil { |
| 95 | + return errors.Wrapf(err, "failed to query keyper set %d by index from db", key.KeyperConfigIndex) |
| 96 | + } |
| 97 | + keyperAddress := ethcrypto.PubkeyToAddress(p.privateKey.PublicKey) |
| 98 | + keyperIndex, err := keyperSet.GetIndex(keyperAddress) |
| 99 | + if err != nil { |
| 100 | + log.Info(). |
| 101 | + Uint64("keyper-set-index", key.KeyperConfigIndex). |
| 102 | + Str("keyper-address", keyperAddress.Hex()). |
| 103 | + Msg("not publishing eon key as keyper is not part of corresponding keyper set") |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + hasAlreadyVoted, err := p.contract.HasKeyperVoted(&bind.CallOpts{}, keyperAddress) |
| 108 | + if err != nil { |
| 109 | + return errors.Wrap(err, "failed to query eon key publisher contract if keyper has already voted") |
| 110 | + } |
| 111 | + if hasAlreadyVoted { |
| 112 | + log.Info(). |
| 113 | + Uint64("keyper-set-index", key.KeyperConfigIndex). |
| 114 | + Str("keyper-address", keyperAddress.Hex()). |
| 115 | + Msg("not publishing eon key as keyper has already voted") |
| 116 | + return nil |
| 117 | + } |
| 118 | + isAlreadyConfirmed, err := p.contract.EonKeyConfirmed(&bind.CallOpts{}, key.PublicKey) |
| 119 | + if err != nil { |
| 120 | + return errors.Wrap(err, "failed to query eon key publisher contract if eon key is confirmed") |
| 121 | + } |
| 122 | + if isAlreadyConfirmed { |
| 123 | + log.Info(). |
| 124 | + Uint64("keyper-set-index", key.KeyperConfigIndex). |
| 125 | + Hex("key", key.PublicKey). |
| 126 | + Msg("not publishing eon key as it is already confirmed") |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + chainID, err := p.client.ChainID(ctx) |
| 131 | + if err != nil { |
| 132 | + return errors.Wrap(err, "failed to get chain ID") |
| 133 | + } |
| 134 | + opts, err := bind.NewKeyedTransactorWithChainID(p.privateKey, chainID) |
| 135 | + if err != nil { |
| 136 | + return errors.Wrap(err, "failed to construct tx opts") |
| 137 | + } |
| 138 | + tx, err := p.contract.PublishEonKey(opts, key.PublicKey, keyperIndex) |
| 139 | + if err != nil { |
| 140 | + return errors.Wrap(err, "failed to send publish eon key tx") |
| 141 | + } |
| 142 | + log.Info(). |
| 143 | + Uint64("keyper-set-index", key.KeyperConfigIndex). |
| 144 | + Hex("key", key.PublicKey). |
| 145 | + Hex("tx-hash", tx.Hash().Bytes()). |
| 146 | + Msg("eon key publish tx sent") |
| 147 | + receipt, err := bind.WaitMined(ctx, p.client, tx) |
| 148 | + if err != nil { |
| 149 | + log.Error().Err(err).Msg("error waiting for eon key publish tx to be mined") |
| 150 | + return err |
| 151 | + } |
| 152 | + if receipt.Status != types.ReceiptStatusSuccessful { |
| 153 | + log.Error(). |
| 154 | + Hex("tx-hash", tx.Hash().Bytes()). |
| 155 | + Interface("receipt", receipt). |
| 156 | + Msg("eon key publish tx failed") |
| 157 | + return errors.New("eon key publish tx failed") |
| 158 | + } |
| 159 | + log.Info().Msg("successfully published eon key") |
| 160 | + return nil |
| 161 | +} |
0 commit comments