broadcast.go 1.7 KB

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