2022-10-12 21:47:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Erigon-Lightclient contributors
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-29 19:51:32 +00:00
|
|
|
"encoding/hex"
|
2022-10-16 17:54:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-10-29 11:02:26 +00:00
|
|
|
"time"
|
2022-10-12 21:47:06 +00:00
|
|
|
|
2022-10-29 19:51:32 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/fork"
|
|
|
|
"github.com/ledgerwatch/erigon/cl/rpc/consensusrpc"
|
|
|
|
lcCli "github.com/ledgerwatch/erigon/cmd/sentinel/cli"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/cli/flags"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel/communication/ssz_snappy"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel/handlers"
|
|
|
|
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel/service"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2022-10-16 17:54:12 +00:00
|
|
|
sentinelapp "github.com/ledgerwatch/erigon/turbo/app"
|
|
|
|
"github.com/urfave/cli"
|
2022-10-29 19:51:32 +00:00
|
|
|
"go.uber.org/zap/buffer"
|
2022-10-12 21:47:06 +00:00
|
|
|
|
2022-10-16 17:54:12 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
2022-10-12 21:47:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-10-16 17:54:12 +00:00
|
|
|
app := sentinelapp.MakeApp(runSentinelNode, flags.LightClientDefaultFlags)
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
_, printErr := fmt.Fprintln(os.Stderr, err)
|
|
|
|
if printErr != nil {
|
|
|
|
log.Warn("Fprintln error", "err", printErr)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 16:34:36 +00:00
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:51:32 +00:00
|
|
|
func constructBodyFreeRequest(t string) *consensusrpc.RequestData {
|
|
|
|
return &consensusrpc.RequestData{
|
2022-10-29 11:02:26 +00:00
|
|
|
Topic: t,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:51:32 +00:00
|
|
|
func constructRequest(t string, reqBody cltypes.ObjectSSZ) (*consensusrpc.RequestData, error) {
|
|
|
|
var buffer buffer.Buffer
|
|
|
|
if err := ssz_snappy.EncodeAndWrite(&buffer, reqBody); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to encode request body: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := common.CopyBytes(buffer.Bytes())
|
|
|
|
return &consensusrpc.RequestData{
|
|
|
|
Data: data,
|
|
|
|
Topic: t,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-16 17:54:12 +00:00
|
|
|
func runSentinelNode(cliCtx *cli.Context) {
|
2022-10-17 17:13:23 +00:00
|
|
|
lcCfg, _ := lcCli.SetUpLightClientCfg(cliCtx)
|
2022-10-12 21:47:06 +00:00
|
|
|
ctx := context.Background()
|
2022-10-16 17:54:12 +00:00
|
|
|
|
|
|
|
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(lcCfg.LogLvl), log.StderrHandler))
|
2022-10-17 17:13:23 +00:00
|
|
|
log.Info("[Sentinel] running sentinel with configuration", "cfg", lcCfg)
|
2022-10-29 11:02:26 +00:00
|
|
|
s, err := service.StartSentinelService(&sentinel.SentinelConfig{
|
2022-10-17 17:13:23 +00:00
|
|
|
IpAddr: lcCfg.Addr,
|
|
|
|
Port: int(lcCfg.Port),
|
|
|
|
TCPPort: lcCfg.ServerTcpPort,
|
|
|
|
GenesisConfig: lcCfg.GenesisCfg,
|
|
|
|
NetworkConfig: lcCfg.NetworkCfg,
|
|
|
|
BeaconConfig: lcCfg.BeaconCfg,
|
|
|
|
NoDiscovery: lcCfg.NoDiscovery,
|
2022-10-16 17:54:12 +00:00
|
|
|
}, &service.ServerConfig{Network: lcCfg.ServerProtocol, Addr: lcCfg.ServerAddr})
|
2022-10-12 21:47:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start sentinel", "err", err)
|
2022-10-23 18:22:33 +00:00
|
|
|
return
|
|
|
|
}
|
2022-10-29 11:02:26 +00:00
|
|
|
log.Info("Sentinel started", "addr", lcCfg.ServerAddr)
|
|
|
|
|
|
|
|
digest, err := fork.ComputeForkDigest(lcCfg.BeaconCfg, lcCfg.GenesisCfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not compute fork digeest", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Fork digest", "data", digest)
|
|
|
|
|
|
|
|
log.Info("Sending test request")
|
2022-11-04 16:34:36 +00:00
|
|
|
/*
|
|
|
|
sendRequest(ctx, s, constructBodyFreeRequest(handlers.LightClientFinalityUpdateV1))
|
|
|
|
sendRequest(ctx, s, constructBodyFreeRequest(handlers.MetadataProtocolV1))
|
|
|
|
sendRequest(ctx, s, constructBodyFreeRequest(handlers.MetadataProtocolV2))
|
|
|
|
sendRequest(ctx, s, constructBodyFreeRequest(handlers.LightClientOptimisticUpdateV1))
|
|
|
|
|
|
|
|
lcUpdateReq := &cltypes.LightClientUpdatesByRangeRequest{
|
|
|
|
Period: 604,
|
|
|
|
Count: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
blocksByRangeReq := &cltypes.BeaconBlocksByRangeRequest{
|
|
|
|
StartSlot: 5000005, // arbitrary slot (currently at ~5030000)
|
|
|
|
Count: 3,
|
|
|
|
Step: 1, // deprecated, must be set to 1.
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
roots := make([][32]byte, 3)
|
|
|
|
rawRoot1, err := hex.DecodeString("57dd8e0ee7ed614283fbf80ca19229752839d4a4e232148efd128e85edee9b12")
|
|
|
|
check(err)
|
|
|
|
rawRoot2, err := hex.DecodeString("1d527f21e17897198752838431821558d1b9864654bcaf476232da55458ed5ce")
|
|
|
|
check(err)
|
|
|
|
rawRoot3, err := hex.DecodeString("830eab2e3de70cc7ebc80b6c950ebb2a7946d8a6e3bb653f8b6dafd6a402d49b")
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
copy(roots[0][:], rawRoot1)
|
|
|
|
copy(roots[1][:], rawRoot2)
|
|
|
|
copy(roots[2][:], rawRoot3)
|
|
|
|
|
|
|
|
var blocksByRootReq cltypes.BeaconBlocksByRootRequest = roots
|
|
|
|
req, err := constructRequest(handlers.BeaconBlocksByRootProtocolV2, &blocksByRootReq)
|
2022-10-29 19:51:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("could not construct request", "err", err)
|
|
|
|
}
|
|
|
|
sendRequest(ctx, s, req)
|
2022-10-29 11:02:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 19:51:32 +00:00
|
|
|
func debugGossip(ctx context.Context, s consensusrpc.SentinelClient) {
|
|
|
|
subscription, err := s.SubscribeGossip(ctx, &consensusrpc.EmptyRequest{})
|
2022-10-23 18:22:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start sentinel", "err", err)
|
|
|
|
return
|
2022-10-12 21:47:06 +00:00
|
|
|
}
|
2022-10-23 18:22:33 +00:00
|
|
|
for {
|
|
|
|
data, err := subscription.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-29 19:51:32 +00:00
|
|
|
if data.Type != consensusrpc.GossipType_AggregateAndProofGossipType {
|
2022-10-23 18:22:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
block := &cltypes.SignedAggregateAndProof{}
|
|
|
|
if err := block.UnmarshalSSZ(data.Data); err != nil {
|
|
|
|
log.Error("Error", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Info("Received", "msg", block)
|
|
|
|
}
|
2022-10-12 21:47:06 +00:00
|
|
|
}
|
2022-10-29 11:02:26 +00:00
|
|
|
|
|
|
|
// Debug function to recieve test packets on the req/resp domain.
|
2022-10-29 19:51:32 +00:00
|
|
|
func sendRequest(ctx context.Context, s consensusrpc.SentinelClient, req *consensusrpc.RequestData) {
|
2022-10-29 11:02:26 +00:00
|
|
|
newReqTicker := time.NewTicker(1000 * time.Millisecond)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-newReqTicker.C:
|
|
|
|
go func() {
|
|
|
|
message, err := s.SendRequest(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if message.Error {
|
|
|
|
log.Error("received error", "err", string(message.Data))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Non-error response received", "data", message.Data)
|
2022-11-02 14:48:22 +00:00
|
|
|
f, err := os.Create("out")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to open file: %v\n", err))
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(hex.EncodeToString(message.Data))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to write to file: %v\n", err))
|
|
|
|
}
|
2022-10-29 19:51:32 +00:00
|
|
|
log.Info("Hex representation", "data", hex.EncodeToString(message.Data))
|
2022-11-03 18:04:49 +00:00
|
|
|
f.Close()
|
2022-10-29 11:02:26 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|