forked from gobitfly/eth2-beaconchain-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution_layer.go
95 lines (81 loc) · 3.08 KB
/
execution_layer.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
package services
import (
"fmt"
"sync"
"time"
"github.com/gobitfly/eth2-beaconchain-explorer/cache"
"github.com/gobitfly/eth2-beaconchain-explorer/db"
"github.com/gobitfly/eth2-beaconchain-explorer/utils"
)
const latestBlockNumberCacheKey = "latestEth1BlockNumber"
const latestBlockHashRootCacheKey = "latestEth1BlockRootHash"
// latestBlockUpdater updates the most recent eth1 block number variable
func latestBlockUpdater(wg *sync.WaitGroup) {
firstRun := true
for {
recent, err := db.BigtableClient.GetMostRecentBlockFromDataTable()
if err != nil {
utils.LogError(err, "error getting most recent eth1 block", 0)
}
cacheKey := fmt.Sprintf("%d:frontend:%s", utils.Config.Chain.ClConfig.DepositChainID, latestBlockNumberCacheKey)
err = cache.TieredCache.SetUint64(cacheKey, recent.GetNumber(), utils.Day)
if err != nil {
utils.LogError(err, fmt.Sprintf("error caching latest block number with cache key %s", latestBlockNumberCacheKey), 0)
}
if firstRun {
logger.Info("initialized eth1 block updater")
wg.Done()
firstRun = false
}
ReportStatus("latestBlockUpdater", "Running", nil)
time.Sleep(time.Second * 10)
}
}
// LatestEth1BlockNumber will return most recent eth1 block number
func LatestEth1BlockNumber() uint64 {
cacheKey := fmt.Sprintf("%d:frontend:%s", utils.Config.Chain.ClConfig.DepositChainID, latestBlockNumberCacheKey)
if wanted, err := cache.TieredCache.GetUint64WithLocalTimeout(cacheKey, time.Second*5); err == nil {
return wanted
} else {
utils.LogError(err, fmt.Sprintf("error retrieving latest block number from cache with key %s", latestBlockNumberCacheKey), 0)
}
return 0
}
// headBlockRootHashUpdater updates the hash of the current chain head block
func headBlockRootHashUpdater(wg *sync.WaitGroup) {
firstRun := true
for {
blockRootHash := []byte{}
err := db.ReaderDb.Get(&blockRootHash, `
SELECT blockroot
FROM blocks
WHERE status = '1'
ORDER BY slot DESC
LIMIT 1`)
if err != nil {
utils.LogError(err, "error getting blockroot hash for chain head", 0)
}
cacheKey := fmt.Sprintf("%d:frontend:%s", utils.Config.Chain.ClConfig.DepositChainID, latestBlockHashRootCacheKey)
err = cache.TieredCache.SetString(cacheKey, string(blockRootHash), utils.Day)
if err != nil {
utils.LogError(err, fmt.Sprintf("error caching latest blockroot hash with cache key %s", latestBlockHashRootCacheKey), 0)
}
if firstRun {
logger.Info("initialized eth1 head block root hash updater")
wg.Done()
firstRun = false
}
ReportStatus("headBlockRootHashUpdater", "Running", nil)
time.Sleep(time.Second * 10)
}
}
// Eth1HeadBlockRootHash will return the hash of the current chain head block
func Eth1HeadBlockRootHash() []byte {
cacheKey := fmt.Sprintf("%d:frontend:%s", utils.Config.Chain.ClConfig.DepositChainID, latestBlockHashRootCacheKey)
if wanted, err := cache.TieredCache.GetStringWithLocalTimeout(cacheKey, time.Second*5); err == nil {
return []byte(wanted)
} else {
utils.LogError(err, fmt.Sprintf("error retrieving latest blockroot hash from cache with key %s", latestBlockHashRootCacheKey), 0)
}
return []byte{}
}