erigon-pulse/cmd/lightclient/rpc/common.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

125 lines
3.9 KiB
Go

package rpc
import (
"bytes"
"context"
"fmt"
ssz "github.com/ferranbt/fastssz"
"github.com/ledgerwatch/erigon/cmd/lightclient/cltypes"
"github.com/ledgerwatch/erigon/cmd/lightclient/rpc/lightrpc"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/communication/ssz_snappy"
"github.com/ledgerwatch/erigon/cmd/lightclient/sentinel/handlers"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/log/v3"
"go.uber.org/zap/buffer"
)
func DecodeGossipData(data *lightrpc.GossipData) (ssz.Unmarshaler, error) {
switch data.Type {
case lightrpc.GossipType_BeaconBlockGossipType:
pkt := &cltypes.SignedBeaconBlockBellatrix{}
err := pkt.UnmarshalSSZ(data.Data)
return pkt, err
case lightrpc.GossipType_AggregateAndProofGossipType:
pkt := &cltypes.SignedAggregateAndProof{}
err := pkt.UnmarshalSSZ(data.Data)
return pkt, err
case lightrpc.GossipType_LightClientOptimisticUpdateGossipType:
pkt := &cltypes.LightClientOptimisticUpdate{}
err := pkt.UnmarshalSSZ(data.Data)
return pkt, err
case lightrpc.GossipType_LightClientFinalityUpdateGossipType:
pkt := &cltypes.LightClientFinalityUpdate{}
err := pkt.UnmarshalSSZ(data.Data)
return pkt, err
default:
return nil, fmt.Errorf("invalid gossip type: %d", data.Type)
}
}
func SendLightClientFinaltyUpdateReqV1(ctx context.Context, client lightrpc.SentinelClient) (*cltypes.LightClientFinalityUpdate, error) {
responsePacket := &cltypes.LightClientFinalityUpdate{}
message, err := client.SendRequest(ctx, &lightrpc.RequestData{
Topic: handlers.LightClientFinalityUpdateV1,
})
if err != nil {
return nil, err
}
if message.Error {
log.Warn("received error", "err", string(message.Data))
return nil, nil
}
err = ssz_snappy.DecodeAndRead(bytes.NewReader(message.Data), responsePacket)
return responsePacket, err
}
func SendLightClientOptimisticUpdateReqV1(ctx context.Context, client lightrpc.SentinelClient) (*cltypes.LightClientOptimisticUpdate, error) {
responsePacket := &cltypes.LightClientOptimisticUpdate{}
message, err := client.SendRequest(ctx, &lightrpc.RequestData{
Topic: handlers.LightClientOptimisticUpdateV1,
})
if err != nil {
return nil, err
}
if message.Error {
log.Warn("received error", "err", string(message.Data))
return nil, nil
}
err = ssz_snappy.DecodeAndRead(bytes.NewReader(message.Data), responsePacket)
return responsePacket, err
}
func SendLightClientBootstrapReqV1(ctx context.Context, req *cltypes.SingleRoot, client lightrpc.SentinelClient) (*cltypes.LightClientBootstrap, error) {
var buffer buffer.Buffer
if err := ssz_snappy.EncodeAndWrite(&buffer, req); err != nil {
return nil, err
}
responsePacket := &cltypes.LightClientBootstrap{}
data := common.CopyBytes(buffer.Bytes())
message, err := client.SendRequest(ctx, &lightrpc.RequestData{
Data: data,
Topic: handlers.LightClientBootstrapV1,
})
if err != nil {
return nil, err
}
if message.Error {
log.Warn("received error", "err", string(message.Data))
return nil, nil
}
err = ssz_snappy.DecodeAndRead(bytes.NewReader(message.Data), responsePacket)
return responsePacket, err
}
func SendLightClientUpdatesReqV1(ctx context.Context, period uint64, client lightrpc.SentinelClient) (*cltypes.LightClientUpdate, error) {
// This is approximately one day worth of data, we dont need to receive more than 1.
req := &cltypes.LightClientUpdatesByRangeRequest{
Period: period,
Count: 1,
}
var buffer buffer.Buffer
if err := ssz_snappy.EncodeAndWrite(&buffer, req); err != nil {
return nil, err
}
data := common.CopyBytes(buffer.Bytes())
message, err := client.SendRequest(ctx, &lightrpc.RequestData{
Data: data,
Topic: handlers.LightClientUpdatesByRangeV1,
})
if err != nil {
return nil, err
}
if message.Error {
log.Warn("received error", "err", string(message.Data))
return nil, nil
}
//err = ssz_snappy.DecodeAndRead(bytes.NewReader(message.Data), responsePacket)
return ssz_snappy.DecodeLightClientUpdate(message.Data)
}