/
post_health_check.go
84 lines (71 loc) 路 2.29 KB
/
post_health_check.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
package http
import (
"log/slog"
"net/http"
"github.com/ethereum/go-ethereum/crypto"
echo "github.com/labstack/echo/v4"
guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check"
)
var (
msg = crypto.Keccak256Hash([]byte("HEART_BEAT")).Bytes()
)
type healthCheckReq struct {
ProverAddress string `json:"prover"`
HeartBeatSignature string `json:"heartBeatSignature"`
LatestL1Block uint64 `json:"latestL1Block"`
LatestL2Block uint64 `json:"latestL2Block"`
}
// PostHealthCheck
//
// post a health check from a guardian prover
//
// @Summary Post healthcheck
// @ID post-health-check
// @Accept json
// @Produce json
// @Success 200 null
// @Router /healthCheck [post]
func (srv *Server) PostHealthCheck(c echo.Context) error {
req := &healthCheckReq{}
// bind incoming request
if err := c.Bind(req); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
recoveredGuardianProver, err := guardianproverhealthcheck.SignatureToGuardianProver(
msg,
req.HeartBeatSignature,
srv.guardianProvers,
)
// if not, we want to return an error
if err != nil {
return c.JSON(http.StatusBadRequest, err)
}
// otherwise, we can store it in the database.
// expected address and recovered address will be the same until we have an auth
// mechanism which will allow us to store health checks that ecrecover to an unexpected
// address.
if err := srv.healthCheckRepo.Save(guardianproverhealthcheck.SaveHealthCheckOpts{
GuardianProverID: recoveredGuardianProver.ID.Uint64(),
Alive: true,
ExpectedAddress: recoveredGuardianProver.Address.Hex(),
RecoveredAddress: recoveredGuardianProver.Address.Hex(),
SignedResponse: req.HeartBeatSignature,
LatestL1Block: req.LatestL1Block,
LatestL2Block: req.LatestL2Block,
}); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
// increment health check metric
for _, v := range srv.guardianProvers {
if v.Address.Hex() == recoveredGuardianProver.Address.Hex() {
v.HealthCheckCounter.Inc()
}
}
slog.Info("successful health check",
"id", recoveredGuardianProver.ID.Uint64(),
"guardianProver", recoveredGuardianProver.Address.Hex(),
"latestL1Block", req.LatestL1Block,
"latestL2Block", req.LatestL2Block,
)
return c.JSON(http.StatusOK, nil)
}