broadcast.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package processor
  2. import (
  3. "encoding/hex"
  4. "github.com/prometheus/client_golang/prometheus"
  5. "github.com/prometheus/client_golang/prometheus/promauto"
  6. "time"
  7. ethcommon "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. "google.golang.org/protobuf/proto"
  10. gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
  11. "github.com/certusone/wormhole/node/pkg/vaa"
  12. )
  13. var (
  14. observationsBroadcastTotal = promauto.NewCounter(
  15. prometheus.CounterOpts{
  16. Name: "wormhole_observations_broadcast_total",
  17. Help: "Total number of signed observations queued for broadcast",
  18. })
  19. )
  20. func (p *Processor) broadcastSignature(v *vaa.VAA, signature []byte, txhash []byte) {
  21. digest, err := v.SigningMsg()
  22. if err != nil {
  23. panic(err)
  24. }
  25. obsv := gossipv1.SignedObservation{
  26. Addr: crypto.PubkeyToAddress(p.gk.PublicKey).Bytes(),
  27. Hash: digest.Bytes(),
  28. Signature: signature,
  29. TxHash: txhash,
  30. }
  31. w := gossipv1.GossipMessage{Message: &gossipv1.GossipMessage_SignedObservation{SignedObservation: &obsv}}
  32. msg, err := proto.Marshal(&w)
  33. if err != nil {
  34. panic(err)
  35. }
  36. p.sendC <- msg
  37. // Store our VAA in case we're going to submit it to Solana
  38. hash := hex.EncodeToString(digest.Bytes())
  39. if p.state.vaaSignatures[hash] == nil {
  40. p.state.vaaSignatures[hash] = &vaaState{
  41. firstObserved: time.Now(),
  42. signatures: map[ethcommon.Address][]byte{},
  43. source: "loopback",
  44. }
  45. }
  46. p.state.vaaSignatures[hash].ourVAA = v
  47. p.state.vaaSignatures[hash].ourMsg = msg
  48. p.state.vaaSignatures[hash].gs = p.gs // guaranteed to match ourVAA - there's no concurrent access to p.gs
  49. // Fast path for our own signature
  50. go func() { p.obsvC <- &obsv }()
  51. observationsBroadcastTotal.Inc()
  52. }