-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.go
More file actions
229 lines (187 loc) · 6.44 KB
/
hooks.go
File metadata and controls
229 lines (187 loc) · 6.44 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package narwhal
import "time"
// Hooks provides optional callbacks for observability events.
// All hooks are invoked synchronously - keep implementations fast.
type Hooks[H Hash, T Transaction[H]] struct {
// Worker events
// OnBatchCreated is called when a worker creates a new batch.
OnBatchCreated func(BatchCreatedEvent[H, T])
// OnTransactionReceived is called when a transaction is added to a worker.
OnTransactionReceived func(TransactionReceivedEvent[H])
// Primary events
// OnHeaderCreated is called when a primary creates a new header.
OnHeaderCreated func(HeaderCreatedEvent[H])
// OnHeaderReceived is called when a primary receives a header from another validator.
OnHeaderReceived func(HeaderReceivedEvent[H])
// OnVoteReceived is called when a primary receives a vote for its header.
OnVoteReceived func(VoteReceivedEvent[H])
// OnVoteSent is called when a primary sends a vote for another validator's header.
OnVoteSent func(VoteSentEvent[H])
// OnCertificateFormed is called when a primary forms a certificate (collected quorum).
OnCertificateFormed func(CertificateFormedEvent[H])
// OnCertificateReceived is called when a certificate is received from another validator.
OnCertificateReceived func(CertificateReceivedEvent[H])
// OnHeaderTimeout is called when a header times out waiting for votes.
OnHeaderTimeout func(HeaderTimeoutEvent[H])
// DAG events
// OnVertexInserted is called when a certificate is inserted into the DAG.
OnVertexInserted func(VertexInsertedEvent[H])
// OnRoundAdvanced is called when the DAG advances to a new round.
OnRoundAdvanced func(RoundAdvancedEvent)
// OnEquivocationDetected is called when equivocation is detected.
// Note: This is in addition to the DAG's OnEquivocation callback,
// which provides the full evidence for slashing.
OnEquivocationDetected func(EquivocationDetectedEvent[H])
// OnCertificatePending is called when a certificate is queued as pending
// due to missing parents.
OnCertificatePending func(CertificatePendingEvent[H])
// Fetcher events
// OnFetchStarted is called when a fetch operation begins.
OnFetchStarted func(FetchStartedEvent[H])
// OnFetchCompleted is called when a fetch operation completes (success or failure).
OnFetchCompleted func(FetchCompletedEvent[H])
// GC events
// OnGarbageCollected is called when garbage collection runs.
OnGarbageCollected func(GarbageCollectedEvent)
}
// Clone returns a shallow copy of the hooks.
func (h *Hooks[H, T]) Clone() *Hooks[H, T] {
if h == nil {
return nil
}
clone := *h
return &clone
}
// BatchCreatedEvent contains information about a newly created batch.
type BatchCreatedEvent[H Hash, T Transaction[H]] struct {
Batch *Batch[H, T]
WorkerID uint16
TransactionCount int
SizeBytes int
CreatedAt time.Time
}
// TransactionReceivedEvent contains information about a received transaction.
type TransactionReceivedEvent[H Hash] struct {
TxHash H
WorkerID uint16
ReceivedAt time.Time
}
// HeaderCreatedEvent contains information about a newly created header.
type HeaderCreatedEvent[H Hash] struct {
Header *Header[H]
BatchCount int
CreatedAt time.Time
}
// HeaderReceivedEvent contains information about a received header.
type HeaderReceivedEvent[H Hash] struct {
Header *Header[H]
From uint16
ReceivedAt time.Time
}
// VoteReceivedEvent contains information about a received vote.
type VoteReceivedEvent[H Hash] struct {
HeaderDigest H
Voter uint16
TotalVotes int // Current vote count for this header
QuorumRequired int
ReceivedAt time.Time
}
// VoteSentEvent contains information about a vote we sent.
type VoteSentEvent[H Hash] struct {
HeaderDigest H
HeaderAuthor uint16
SentAt time.Time
}
// CertificateFormedEvent contains information about a newly formed certificate.
type CertificateFormedEvent[H Hash] struct {
Certificate *Certificate[H]
SignerCount int
Latency time.Duration // Time from header creation to certificate formation
FormedAt time.Time
}
// CertificateReceivedEvent contains information about a received certificate.
type CertificateReceivedEvent[H Hash] struct {
Certificate *Certificate[H]
From uint16
ReceivedAt time.Time
}
// HeaderTimeoutEvent contains information about a header that timed out.
type HeaderTimeoutEvent[H Hash] struct {
HeaderDigest H
Round uint64
RetryCount int
WillRetry bool
VotesReceived int
QuorumNeeded int
TimeoutAt time.Time
}
// VertexInsertedEvent contains information about a vertex inserted into the DAG.
type VertexInsertedEvent[H Hash] struct {
Certificate *Certificate[H]
Round uint64
Author uint16
ParentCount int
TotalInRound int // Total certificates in this round after insertion
InsertedAt time.Time
}
// RoundAdvancedEvent contains information about a round advancement.
type RoundAdvancedEvent struct {
OldRound uint64
NewRound uint64
CertificatesInRound int // Number of certificates that triggered advancement
AdvancedAt time.Time
}
// EquivocationDetectedEvent contains information about detected equivocation.
type EquivocationDetectedEvent[H Hash] struct {
Author uint16
Round uint64
FirstDigest H
SecondDigest H
DetectedAt time.Time
}
// CertificatePendingEvent contains information about a certificate queued as pending.
type CertificatePendingEvent[H Hash] struct {
Certificate *Certificate[H]
MissingParents []H
QueuedAt time.Time
}
// FetchType indicates what is being fetched.
type FetchType uint8
const (
FetchTypeBatch FetchType = iota
FetchTypeCertificate
)
func (t FetchType) String() string {
switch t {
case FetchTypeBatch:
return "batch"
case FetchTypeCertificate:
return "certificate"
default:
return "unknown"
}
}
// FetchStartedEvent contains information about a fetch operation starting.
type FetchStartedEvent[H Hash] struct {
Type FetchType
Digest H
PreferredPeer uint16
StartedAt time.Time
}
// FetchCompletedEvent contains information about a completed fetch operation.
type FetchCompletedEvent[H Hash] struct {
Type FetchType
Digest H
Success bool
Error error
Attempts int
FromPeer uint16 // Peer that provided the data (if successful)
Latency time.Duration
CompletedAt time.Time
}
// GarbageCollectedEvent contains information about a GC cycle.
type GarbageCollectedEvent struct {
BeforeRound uint64
CommittedRound uint64
CollectedAt time.Time
}