erigon-pulse/cmd/sentinel/main.go
2022-10-29 21:51:32 +02:00

161 lines
4.9 KiB
Go

/*
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"
"encoding/hex"
"fmt"
"os"
"time"
"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"
sentinelapp "github.com/ledgerwatch/erigon/turbo/app"
"github.com/urfave/cli"
"go.uber.org/zap/buffer"
"github.com/ledgerwatch/log/v3"
)
func main() {
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)
}
}
func constructBodyFreeRequest(t string) *consensusrpc.RequestData {
return &consensusrpc.RequestData{
Topic: t,
}
}
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
}
func runSentinelNode(cliCtx *cli.Context) {
lcCfg, _ := lcCli.SetUpLightClientCfg(cliCtx)
ctx := context.Background()
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(lcCfg.LogLvl), log.StderrHandler))
log.Info("[Sentinel] running sentinel with configuration", "cfg", lcCfg)
s, err := service.StartSentinelService(&sentinel.SentinelConfig{
IpAddr: lcCfg.Addr,
Port: int(lcCfg.Port),
TCPPort: lcCfg.ServerTcpPort,
GenesisConfig: lcCfg.GenesisCfg,
NetworkConfig: lcCfg.NetworkCfg,
BeaconConfig: lcCfg.BeaconCfg,
NoDiscovery: lcCfg.NoDiscovery,
}, &service.ServerConfig{Network: lcCfg.ServerProtocol, Addr: lcCfg.ServerAddr})
if err != nil {
log.Error("Could not start sentinel", "err", err)
return
}
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")
// 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,
}
req, err := constructRequest(handlers.LightClientUpdatesByRangeV1, lcUpdateReq)
if err != nil {
log.Error("could not construct request", "err", err)
}
sendRequest(ctx, s, req)
}
func debugGossip(ctx context.Context, s consensusrpc.SentinelClient) {
subscription, err := s.SubscribeGossip(ctx, &consensusrpc.EmptyRequest{})
if err != nil {
log.Error("Could not start sentinel", "err", err)
return
}
for {
data, err := subscription.Recv()
if err != nil {
return
}
if data.Type != consensusrpc.GossipType_AggregateAndProofGossipType {
continue
}
block := &cltypes.SignedAggregateAndProof{}
if err := block.UnmarshalSSZ(data.Data); err != nil {
log.Error("Error", "err", err)
continue
}
log.Info("Received", "msg", block)
}
}
// Debug function to recieve test packets on the req/resp domain.
func sendRequest(ctx context.Context, s consensusrpc.SentinelClient, req *consensusrpc.RequestData) {
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)
log.Info("Hex representation", "data", hex.EncodeToString(message.Data))
}()
}
}
}