This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
stats.go
110 lines (84 loc) · 2.56 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package node
import "sync"
// Stats exposes statistics of the relay node.
type Stats interface {
// HeadersRelayActive returns whether the headers relay process is active.
HeadersRelayActive() bool
// HeadersRelayErrors returns the total number of headers relay errors.
HeadersRelayErrors() int
// UniqueHeadersPulled returns the number of unique headers pulled during
// the relay node lifetime.
UniqueHeadersPulled() int
// UniqueHeadersPushed returns the number of unique headers pushed during
// the relay node lifetime.
UniqueHeadersPushed() int
}
// stats gathers and exposes statistics of the relay node.
type stats struct {
mutex sync.RWMutex
headersRelayActive bool
headersRelayErrors int
uniqueHeadersPulled map[int64]bool
uniqueHeadersPushed map[int64]bool
}
func newStats() *stats {
return &stats{
uniqueHeadersPulled: make(map[int64]bool),
uniqueHeadersPushed: make(map[int64]bool),
}
}
func (s *stats) notifyHeadersRelayActive() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.headersRelayActive = true
}
func (s *stats) notifyHeadersRelayInactive() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.headersRelayActive = false
}
func (s *stats) notifyHeadersRelayErrored() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.headersRelayErrors++
}
// NotifyHeaderPulled notifies about new header pulled from the Bitcoin chain.
func (s *stats) NotifyHeaderPulled(headerHeight int64) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.uniqueHeadersPulled[headerHeight] = true
}
// NotifyHeadersPushed notifies about new headers pushed to the host chain.
func (s *stats) NotifyHeadersPushed(headersHeights []int64) {
s.mutex.Lock()
defer s.mutex.Unlock()
for _, headerHeight := range headersHeights {
s.uniqueHeadersPushed[headerHeight] = true
}
}
// HeadersRelayActive returns whether the headers relay process is active.
func (s *stats) HeadersRelayActive() bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.headersRelayActive
}
// HeadersRelayErrors returns the total number of headers relay errors.
func (s *stats) HeadersRelayErrors() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.headersRelayErrors
}
// UniqueHeadersPulled returns the number of unique headers pulled during
// the relay node lifetime.
func (s *stats) UniqueHeadersPulled() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return len(s.uniqueHeadersPulled)
}
// UniqueHeadersPushed returns the number of unique headers pushed during
// the relay node lifetime.
func (s *stats) UniqueHeadersPushed() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return len(s.uniqueHeadersPushed)
}