-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
head.go
56 lines (46 loc) · 1.17 KB
/
head.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
package evm
import (
"context"
"fmt"
ocr2keepers "github.com/smartcontractkit/ocr2keepers/pkg/v2"
httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
)
type HeadProvider struct {
ht httypes.HeadTracker
hb httypes.HeadBroadcaster
chHead chan ocr2keepers.BlockKey
subscribed bool
}
// HeadTicker provides external access to the heads channel
func (hw *HeadProvider) HeadTicker() chan ocr2keepers.BlockKey {
if !hw.subscribed {
_, _ = hw.hb.Subscribe(&headWrapper{c: hw.chHead})
hw.subscribed = true
}
return hw.chHead
}
func (hw *HeadProvider) LatestBlock() int64 {
lc := hw.ht.LatestChain()
if lc == nil {
return 0
}
return lc.Number
}
// send does a non-blocking send of the key on c.
func send(c chan ocr2keepers.BlockKey, k ocr2keepers.BlockKey) {
select {
case c <- k:
default:
}
}
type headWrapper struct {
c chan ocr2keepers.BlockKey
}
func (w *headWrapper) OnNewLongestChain(ctx context.Context, head *evmtypes.Head) {
var bl int64
if head != nil {
bl = head.Number
}
send(w.c, ocr2keepers.BlockKey(fmt.Sprintf("%d", bl)))
}