|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/gagliardetto/solana-go" |
| 9 | + "github.com/gagliardetto/solana-go/rpc" |
| 10 | + "go.blockdaemon.com/pyth_exporter/metrics" |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +// txScraper polls publisher transactions. |
| 15 | +type txScraper struct { |
| 16 | + tailers []*txTailer |
| 17 | + log *zap.Logger |
| 18 | + rpc *rpc.Client |
| 19 | +} |
| 20 | + |
| 21 | +func newTxScraper(rpcURL string, log *zap.Logger, publishers []solana.PublicKey) *txScraper { |
| 22 | + scraper := &txScraper{ |
| 23 | + rpc: rpc.New(rpcURL), |
| 24 | + log: log, |
| 25 | + tailers: make([]*txTailer, len(publishers)), |
| 26 | + } |
| 27 | + |
| 28 | + for i, pubkey := range publishers { |
| 29 | + scraper.tailers[i] = newTxTailer(scraper, pubkey) |
| 30 | + } |
| 31 | + |
| 32 | + return scraper |
| 33 | +} |
| 34 | + |
| 35 | +func (s *txScraper) run(ctx context.Context, interval time.Duration) { |
| 36 | + ticker := time.NewTicker(interval) |
| 37 | + defer ticker.Stop() |
| 38 | + |
| 39 | + for { |
| 40 | + wait: |
| 41 | + select { |
| 42 | + case <-ctx.Done(): |
| 43 | + return |
| 44 | + case <-ticker.C: |
| 45 | + break wait |
| 46 | + } |
| 47 | + |
| 48 | + s.poll(ctx, interval) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (s *txScraper) poll(ctx context.Context, interval time.Duration) { |
| 53 | + ctx, cancel := context.WithTimeout(ctx, interval) |
| 54 | + defer cancel() |
| 55 | + |
| 56 | + var wg sync.WaitGroup |
| 57 | + wg.Add(len(s.tailers)) |
| 58 | + for _, tailer := range s.tailers { |
| 59 | + go s.pollOne(ctx, &wg, tailer) |
| 60 | + } |
| 61 | + wg.Wait() |
| 62 | +} |
| 63 | + |
| 64 | +func (s *txScraper) pollOne(ctx context.Context, wg *sync.WaitGroup, tailer *txTailer) { |
| 65 | + defer wg.Done() |
| 66 | + for { |
| 67 | + isEnd, err := tailer.poll(ctx) |
| 68 | + if isEnd { |
| 69 | + break |
| 70 | + } |
| 71 | + if err != nil { |
| 72 | + s.log.Warn("Failed to poll account txs", zap.Error(err)) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// txTailer "tails" the transaction log of an account. |
| 78 | +type txTailer struct { |
| 79 | + *txScraper |
| 80 | + |
| 81 | + pubkey solana.PublicKey |
| 82 | + pubkeyStr string |
| 83 | + lastSlot uint64 |
| 84 | + lastSig solana.Signature |
| 85 | +} |
| 86 | + |
| 87 | +func newTxTailer(scraper *txScraper, pubkey solana.PublicKey) *txTailer { |
| 88 | + return &txTailer{ |
| 89 | + txScraper: scraper, |
| 90 | + pubkey: pubkey, |
| 91 | + pubkeyStr: pubkey.String(), |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func (t *txTailer) refreshLastSig(ctx context.Context) error { |
| 96 | + oneInt := 1 |
| 97 | + sigs, err := t.rpc.GetSignaturesForAddressWithOpts(ctx, t.pubkey, &rpc.GetSignaturesForAddressOpts{ |
| 98 | + Limit: &oneInt, |
| 99 | + }) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + metrics.RpcRequestsTotal.Inc() |
| 104 | + |
| 105 | + if len(sigs) == 0 { |
| 106 | + return nil // empty account |
| 107 | + } |
| 108 | + t.lastSlot = sigs[0].Slot |
| 109 | + t.lastSig = sigs[0].Signature |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// poll retrieves the latest transactions. |
| 114 | +func (t *txTailer) poll(ctx context.Context) (end bool, err error) { |
| 115 | + t.log.Debug("Polling new txs", |
| 116 | + zap.String("publisher", t.pubkeyStr), |
| 117 | + zap.Stringer("last_sig", t.lastSig)) |
| 118 | + |
| 119 | + // Get starting sig if none. |
| 120 | + if t.lastSig.IsZero() { |
| 121 | + return true, t.refreshLastSig(ctx) |
| 122 | + } |
| 123 | + |
| 124 | + // Get sigs since last check. |
| 125 | + var tailLimit = 100 |
| 126 | + sigs, err := t.rpc.GetSignaturesForAddressWithOpts(ctx, t.pubkey, &rpc.GetSignaturesForAddressOpts{ |
| 127 | + Limit: &tailLimit, |
| 128 | + Until: t.lastSig, |
| 129 | + }) |
| 130 | + if err != nil { |
| 131 | + return true, err |
| 132 | + } |
| 133 | + metrics.RpcRequestsTotal.Inc() |
| 134 | + |
| 135 | + if len(sigs) == 0 { |
| 136 | + return true, nil |
| 137 | + } |
| 138 | + |
| 139 | + // Iteration is newest to latest. |
| 140 | + // So write down the first sig as the newest sig, so we can later continue. |
| 141 | + stopSlot := t.lastSlot |
| 142 | + if sigs[0].Slot > t.lastSlot { |
| 143 | + t.lastSlot = sigs[0].Slot |
| 144 | + t.lastSig = sigs[0].Signature |
| 145 | + } |
| 146 | + |
| 147 | + // If the number of returned sigs matches exactly our requested limit, there is probably more. |
| 148 | + end = len(sigs) != tailLimit |
| 149 | + |
| 150 | + // Scroll through page. |
| 151 | + for len(sigs) > 0 && sigs[0].Slot > stopSlot { |
| 152 | + sig := sigs[0] |
| 153 | + |
| 154 | + var status string |
| 155 | + if sig.Err != nil { |
| 156 | + status = metrics.TxStatusFailed |
| 157 | + } else { |
| 158 | + status = metrics.TxStatusSuccess |
| 159 | + } |
| 160 | + metrics.TxCount.WithLabelValues(t.pubkeyStr, status).Inc() |
| 161 | + |
| 162 | + sigs = sigs[1:] |
| 163 | + } |
| 164 | + |
| 165 | + return end, nil |
| 166 | +} |
0 commit comments