|
| 1 | +package subscriber |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + bridgeTypes "github.com/hyle-team/bridgeless-core/v12/x/bridge/types" |
| 10 | + database "github.com/hyle-team/tss-svc/internal/db" |
| 11 | + "github.com/hyle-team/tss-svc/internal/types" |
| 12 | + "github.com/pkg/errors" |
| 13 | + "github.com/tendermint/tendermint/rpc/client/http" |
| 14 | + coretypes "github.com/tendermint/tendermint/rpc/core/types" |
| 15 | + "gitlab.com/distributed_lab/logan/v3" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + OpServiceName = "op-subscriber" |
| 20 | + OpPoolSize = 50 |
| 21 | + |
| 22 | + OpQuerySubmit = "tm.event='Tx' AND message.action='/core.bridge.MsgSubmitTransactions'" |
| 23 | +) |
| 24 | + |
| 25 | +type Subscriber struct { |
| 26 | + db database.DepositsQ |
| 27 | + client *http.HTTP |
| 28 | + query string |
| 29 | + log *logan.Entry |
| 30 | +} |
| 31 | + |
| 32 | +func NewSubmitSubscriber(db database.DepositsQ, client *http.HTTP, logger *logan.Entry) *Subscriber { |
| 33 | + return &Subscriber{ |
| 34 | + db: db, |
| 35 | + client: client, |
| 36 | + query: OpQuerySubmit, |
| 37 | + log: logger, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (s *Subscriber) Run(ctx context.Context) error { |
| 42 | + out, err := s.client.Subscribe(ctx, OpServiceName, s.query, OpPoolSize) |
| 43 | + if err != nil { |
| 44 | + return errors.Wrap(err, "subscriber init failed") |
| 45 | + } |
| 46 | + |
| 47 | + go s.run(ctx, out) |
| 48 | + |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (s *Subscriber) run(ctx context.Context, out <-chan coretypes.ResultEvent) { |
| 53 | + for { |
| 54 | + select { |
| 55 | + case <-ctx.Done(): |
| 56 | + if err := s.client.Unsubscribe(ctx, OpServiceName, s.query); err != nil { |
| 57 | + s.log.WithError(err).Error("failed to unsubscribe from new operations") |
| 58 | + } |
| 59 | + |
| 60 | + s.log.Info("context finished") |
| 61 | + return |
| 62 | + case c, ok := <-out: |
| 63 | + if !ok { |
| 64 | + s.log.Warn("chanel closed, stopping receiving messages") |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + deposit, err := parseSubmittedDeposit(c.Events) |
| 69 | + if err != nil { |
| 70 | + s.log.WithError(err).Error("failed to parse submitted deposit") |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + tx, err := s.db.Get(deposit.DepositIdentifier) |
| 75 | + if err != nil { |
| 76 | + s.log.WithError(err).Error("failed to get deposit") |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + // if deposit does not exist in db insert it |
| 81 | + if tx == nil { |
| 82 | + s.log.Info("found new submitted deposit") |
| 83 | + if _, err = s.db.InsertProcessedDeposit(*deposit); err != nil { |
| 84 | + s.log.WithError(err).Error("failed to insert new deposit") |
| 85 | + } |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + // if deposit exists and pending or processing update signature,withdrawal tx hash and status |
| 90 | + switch tx.WithdrawalStatus { |
| 91 | + case types.WithdrawalStatus_WITHDRAWAL_STATUS_PROCESSED: |
| 92 | + s.log.Info("skipping processed deposit") |
| 93 | + case types.WithdrawalStatus_WITHDRAWAL_STATUS_PROCESSING: |
| 94 | + s.log.Info("found existing deposit submitted to core") |
| 95 | + if err = s.db.UpdateWithdrawalDetails(tx.DepositIdentifier, deposit.WithdrawalTxHash, deposit.Signature); err != nil { |
| 96 | + s.log.WithError(err).Error("failed to update deposit withdrawal details") |
| 97 | + } |
| 98 | + case types.WithdrawalStatus_WITHDRAWAL_STATUS_PENDING: |
| 99 | + s.log.Info("found submitted pending deposit") |
| 100 | + if err = s.db.UpdateWithdrawalDetails(tx.DepositIdentifier, deposit.WithdrawalTxHash, deposit.Signature); err != nil { |
| 101 | + s.log.WithError(err).Error("failed to update deposit withdrawal details") |
| 102 | + } |
| 103 | + default: |
| 104 | + s.log.Infof("nothing to do with deposit status %s", tx.WithdrawalStatus) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func parseSubmittedDeposit(attributes map[string][]string) (*database.Deposit, error) { |
| 111 | + deposit := &database.Deposit{} |
| 112 | + for keys, attribute := range attributes { |
| 113 | + |
| 114 | + parts := strings.SplitN(keys, ".", 2) |
| 115 | + if parts[0] != bridgeTypes.EventType_DEPOSIT_SUBMITTED.String() { |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + switch parts[1] { |
| 120 | + case bridgeTypes.AttributeKeyDepositTxHash: |
| 121 | + deposit.TxHash = attribute[0] |
| 122 | + case bridgeTypes.AttributeKeyDepositNonce: |
| 123 | + n, err := strconv.Atoi(attribute[0]) |
| 124 | + if err != nil { |
| 125 | + return nil, errors.Wrap(errors.New(fmt.Sprintf("got invalid nonce, got %s", attribute)), "invalid nonce") |
| 126 | + } |
| 127 | + deposit.TxNonce = n |
| 128 | + case bridgeTypes.AttributeKeyDepositChainId: |
| 129 | + deposit.ChainId = attribute[0] |
| 130 | + case bridgeTypes.AttributeKeyDepositAmount: |
| 131 | + deposit.DepositAmount = &attribute[0] |
| 132 | + case bridgeTypes.AttributeKeyDepositToken: |
| 133 | + deposit.DepositToken = &attribute[0] |
| 134 | + case bridgeTypes.AttributeKeyDepositBlock: |
| 135 | + b, err := strconv.ParseInt(attribute[0], 10, 64) |
| 136 | + if err != nil { |
| 137 | + return nil, errors.Wrap(err, "failed to parse deposit block") |
| 138 | + } |
| 139 | + deposit.DepositBlock = &b |
| 140 | + case bridgeTypes.AttributeKeyWithdrawalAmount: |
| 141 | + deposit.WithdrawalAmount = &attribute[0] |
| 142 | + case bridgeTypes.AttributeKeyDepositor: |
| 143 | + deposit.Depositor = &attribute[0] |
| 144 | + case bridgeTypes.AttributeKeyReceiver: |
| 145 | + deposit.Receiver = &attribute[0] |
| 146 | + case bridgeTypes.AttributeKeyWithdrawalChainID: |
| 147 | + deposit.WithdrawalChainId = &attribute[0] |
| 148 | + case bridgeTypes.AttributeKeyWithdrawalTxHash: |
| 149 | + if attribute[0] != "" { |
| 150 | + deposit.WithdrawalTxHash = &attribute[0] |
| 151 | + } |
| 152 | + case bridgeTypes.AttributeKeyWithdrawalToken: |
| 153 | + deposit.WithdrawalToken = &attribute[0] |
| 154 | + case bridgeTypes.AttributeKeySignature: |
| 155 | + if attribute[0] != "" { |
| 156 | + deposit.Signature = &attribute[0] |
| 157 | + } |
| 158 | + case bridgeTypes.AttributeKeyIsWrapped: |
| 159 | + isWrapped, err := strconv.ParseBool(attribute[0]) |
| 160 | + if err != nil { |
| 161 | + return nil, errors.Wrap(err, "failed to parse isWrapped attribute") |
| 162 | + } |
| 163 | + deposit.IsWrappedToken = &isWrapped |
| 164 | + default: |
| 165 | + |
| 166 | + return nil, errors.Wrap(errors.New(fmt.Sprintf("unknown attribute key: %s", parts[1])), "failed to parse attribute") |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return deposit, nil |
| 171 | +} |
0 commit comments