/
post_signed_block.go
73 lines (61 loc) 路 2 KB
/
post_signed_block.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
package http
import (
"log/slog"
"net/http"
"github.com/ethereum/go-ethereum/common"
echo "github.com/labstack/echo/v4"
guardianproverhealthcheck "github.com/taikoxyz/taiko-mono/packages/guardian-prover-health-check"
)
type signedBlock struct {
BlockID uint64 `json:"blockID"`
BlockHash string `json:"blockHash"`
Signature string `json:"signature"`
Prover common.Address `json:"proverAddress"`
}
// PostSignedBlock
//
// post a signed block to store in the database
//
// @Summary Post signed block
// @ID post-signed-block
// @Accept json
// @Produce json
// @Success 200 null
// @Router /signedBlock [post]
func (srv *Server) PostSignedBlock(c echo.Context) error {
req := &signedBlock{}
// bind incoming request
if err := c.Bind(req); err != nil {
slog.Error("error binding request", "error", err)
return c.JSON(http.StatusBadRequest, err)
}
recoveredGuardianProver, err := guardianproverhealthcheck.SignatureToGuardianProver(
common.HexToHash(req.BlockHash).Bytes(),
req.Signature,
srv.guardianProvers,
)
// if not, we want to return an error
if err != nil {
slog.Error("error recovering guardian prover", "error", err)
return c.JSON(http.StatusBadRequest, err)
}
// otherwise, we can store it in the database.
if err := srv.signedBlockRepo.Save(guardianproverhealthcheck.SaveSignedBlockOpts{
GuardianProverID: recoveredGuardianProver.ID.Uint64(),
BlockID: req.BlockID,
BlockHash: req.BlockHash,
Signature: req.Signature,
RecoveredAddress: recoveredGuardianProver.Address.Hex(),
}); err != nil {
slog.Error("error saving signed block to db", "error", err)
return c.JSON(http.StatusBadRequest, err)
}
// increment signed block metric
for _, v := range srv.guardianProvers {
if v.Address.Hex() == recoveredGuardianProver.Address.Hex() {
v.SignedBlockCounter.Inc()
}
}
slog.Info("successful signed block", "guardianProver", recoveredGuardianProver.Address.Hex())
return c.JSON(http.StatusOK, nil)
}