@@ -169,6 +169,60 @@ func (node *blockNode) Ancestor(height int32) *blockNode {
169
169
return n
170
170
}
171
171
172
+ // Height returns the blockNode's height in the chain.
173
+ //
174
+ // NOTE: Part of the HeaderCtx interface.
175
+ func (node * blockNode ) Height () int32 {
176
+ return node .height
177
+ }
178
+
179
+ // Bits returns the blockNode's nBits.
180
+ //
181
+ // NOTE: Part of the HeaderCtx interface.
182
+ func (node * blockNode ) Bits () uint32 {
183
+ return node .bits
184
+ }
185
+
186
+ // Timestamp returns the blockNode's timestamp.
187
+ //
188
+ // NOTE: Part of the HeaderCtx interface.
189
+ func (node * blockNode ) Timestamp () int64 {
190
+ return node .timestamp
191
+ }
192
+
193
+ // Parent returns the blockNode's parent.
194
+ //
195
+ // NOTE: Part of the HeaderCtx interface.
196
+ func (node * blockNode ) Parent () HeaderCtx {
197
+ if node .parent == nil {
198
+ // This is required since node.parent is a *blockNode and if we
199
+ // do not explicitly return nil here, the caller may fail when
200
+ // nil-checking this.
201
+ return nil
202
+ }
203
+
204
+ return node .parent
205
+ }
206
+
207
+ // RelativeAncestorCtx returns the blockNode's ancestor that is distance blocks
208
+ // before it in the chain. This is equivalent to the RelativeAncestor function
209
+ // below except that the return type is different.
210
+ //
211
+ // This function is safe for concurrent access.
212
+ //
213
+ // NOTE: Part of the HeaderCtx interface.
214
+ func (node * blockNode ) RelativeAncestorCtx (distance int32 ) HeaderCtx {
215
+ ancestor := node .RelativeAncestor (distance )
216
+ if ancestor == nil {
217
+ // This is required since RelativeAncestor returns a *blockNode
218
+ // and if we do not explicitly return nil here, the caller may
219
+ // fail when nil-checking this.
220
+ return nil
221
+ }
222
+
223
+ return ancestor
224
+ }
225
+
172
226
// RelativeAncestor returns the ancestor block node a relative 'distance' blocks
173
227
// before this node. This is equivalent to calling Ancestor with the node's
174
228
// height minus provided distance.
@@ -182,17 +236,17 @@ func (node *blockNode) RelativeAncestor(distance int32) *blockNode {
182
236
// prior to, and including, the block node.
183
237
//
184
238
// This function is safe for concurrent access.
185
- func (node * blockNode ) CalcPastMedianTime ( ) time.Time {
239
+ func CalcPastMedianTime (node HeaderCtx ) time.Time {
186
240
// Create a slice of the previous few block timestamps used to calculate
187
241
// the median per the number defined by the constant medianTimeBlocks.
188
242
timestamps := make ([]int64 , medianTimeBlocks )
189
243
numNodes := 0
190
244
iterNode := node
191
245
for i := 0 ; i < medianTimeBlocks && iterNode != nil ; i ++ {
192
- timestamps [i ] = iterNode .timestamp
246
+ timestamps [i ] = iterNode .Timestamp ()
193
247
numNodes ++
194
248
195
- iterNode = iterNode .parent
249
+ iterNode = iterNode .Parent ()
196
250
}
197
251
198
252
// Prune the slice to the actual number of available timestamps which
@@ -217,6 +271,10 @@ func (node *blockNode) CalcPastMedianTime() time.Time {
217
271
return time .Unix (medianTimestamp , 0 )
218
272
}
219
273
274
+ // A compile-time assertion to ensure blockNode implements the HeaderCtx
275
+ // interface.
276
+ var _ HeaderCtx = (* blockNode )(nil )
277
+
220
278
// blockIndex provides facilities for keeping track of an in-memory index of the
221
279
// block chain. Although the name block chain suggests a single chain of
222
280
// blocks, it is actually a tree-shaped structure where any node can have
0 commit comments