erigon-pulse/cmd/sentinel_node/main.go
Mike Neuder db1c2d2d82
Adding the AggregateAndProof global topic to the sentinel pubsub service (#5841)
This is the first PR in support of
https://github.com/ledgerwatch/erigon/issues/5824.

The phase 0 sepc
https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/p2p-interface.md#global-topics
specifies 6 global pubsub topics that CL nodes need to handle.

This PR implements the `beacob_aggregate_and_proof` topic: 

https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/p2p-interface.md#beacon_aggregate_and_proof.

The `AggregateAndProof` and `SignedAggregateAndProof` types are defined
here:
https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/validator.md#aggregateandproof.

I followed the implementation of `SignedBeaconBlockBellatrix`, which has
the following references:

1. cmd/lightclient/cltypes/types.go‎: defines the struct with relevant
SSZ annotations on the fields.
2. cmd/lightclient/cltypes/clone.go: this just returns a reference to an
empty object, so not super clear to me if it is necessary:
3. cmd/lightclient/rpc/common.go: this decodes gossip data, switching on
the type of gossip message that is received.
4. ‎cmd/lightclient/sentinel/service/service.go: this listens on the
pubsub channel and notifies when a packet of the relevant type comes in.
5. cmd/lightclient/sentinel/pubsub.go: this defines the gossip topic
struct.
6. cmd/lightclient/lightclient/subscriber.go: this is the lightclient
interface for the incoming messages that come from the sentinel.
2022-10-23 20:22:33 +02:00

85 lines
2.7 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"
"fmt"
"os"
"github.com/ledgerwatch/erigon/cmd/lightclient/cltypes"
"github.com/ledgerwatch/erigon/cmd/lightclient/rpc/lightrpc"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/service"
lcCli "github.com/ledgerwatch/erigon/cmd/sentinel_node/cli"
"github.com/ledgerwatch/erigon/cmd/sentinel_node/cli/flags"
sentinelapp "github.com/ledgerwatch/erigon/turbo/app"
"github.com/urfave/cli"
"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 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)
sent, 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
}
subscription, err := sent.SubscribeGossip(ctx, &lightrpc.EmptyRequest{})
if err != nil {
log.Error("Could not start sentinel", "err", err)
return
}
log.Info("Sentinel started", "addr", lcCfg.ServerAddr)
for {
data, err := subscription.Recv()
if err != nil {
return
}
if data.Type != lightrpc.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)
}
}