registry.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package p2p
  2. import (
  3. gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
  4. "github.com/certusone/wormhole/node/pkg/vaa"
  5. "sync"
  6. )
  7. // The p2p package implements a simple global metrics registry singleton for node status values transmitted on-chain.
  8. type registry struct {
  9. mu sync.Mutex
  10. // Mapping of chain IDs to network status messages.
  11. networkStats map[vaa.ChainID]*gossipv1.Heartbeat_Network
  12. // Per-chain error counters
  13. errorCounters map[vaa.ChainID]uint64
  14. errorCounterMu sync.Mutex
  15. // Value of Heartbeat.guardian_addr.
  16. guardianAddress string
  17. }
  18. func NewRegistry() *registry {
  19. return &registry{
  20. networkStats: map[vaa.ChainID]*gossipv1.Heartbeat_Network{},
  21. errorCounters: map[vaa.ChainID]uint64{},
  22. }
  23. }
  24. var (
  25. DefaultRegistry = NewRegistry()
  26. )
  27. // SetGuardianAddress stores the node's guardian address to broadcast in Heartbeat messages.
  28. // This should be called once during startup, when the guardian key is loaded.
  29. func (r *registry) SetGuardianAddress(addr string) {
  30. r.mu.Lock()
  31. r.guardianAddress = addr
  32. r.mu.Unlock()
  33. }
  34. // SetNetworkStats sets the current network status to be broadcast in Heartbeat messages.
  35. // The "Id" field is automatically set to the specified chain ID.
  36. func (r *registry) SetNetworkStats(chain vaa.ChainID, data *gossipv1.Heartbeat_Network) {
  37. r.mu.Lock()
  38. data.Id = uint32(chain)
  39. r.networkStats[chain] = data
  40. r.mu.Unlock()
  41. }
  42. func (r *registry) AddErrorCount(chain vaa.ChainID, delta uint64) {
  43. r.errorCounterMu.Lock()
  44. defer r.errorCounterMu.Unlock()
  45. r.errorCounters[chain] += 1
  46. }
  47. func (r *registry) GetErrorCount(chain vaa.ChainID) uint64 {
  48. r.errorCounterMu.Lock()
  49. defer r.errorCounterMu.Unlock()
  50. return r.errorCounters[chain]
  51. }