Skip to content

Commit

Permalink
removed database functionality from lightclient (#7135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Mar 18, 2023
1 parent 35a0ba5 commit fc10aef
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 344 deletions.
51 changes: 4 additions & 47 deletions cmd/lightclient/lightclient/lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@ import (
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
"github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/rpc"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/rawdb"
)

const (
maxRecentHashes = 5 // 0.16 KB
safetyRange = 16 // 16 block of safety
maxChainExtension = 8 // 8 blocks of chain extension
maxChainExtension = 32 // 32 blocks of chain extension
)

type LightClient struct {
Expand All @@ -53,17 +51,17 @@ type LightClient struct {
lastEth2ParentRoot common2.Hash // Last ETH2 Parent root.
finalizedEth1Hash common2.Hash
recentHashesCache *lru.Cache
db kv.RwDB
rpc *rpc.BeaconRpcP2P
// Either execution server or client
execution remote.ETHBACKENDServer
executionClient remote.ETHBACKENDClient
store *LightClientStore
}

func NewLightClient(ctx context.Context, db kv.RwDB, genesisConfig *clparams.GenesisConfig, beaconConfig *clparams.BeaconChainConfig,
func NewLightClient(ctx context.Context, genesisConfig *clparams.GenesisConfig, beaconConfig *clparams.BeaconChainConfig,
execution remote.ETHBACKENDServer, executionClient remote.ETHBACKENDClient, sentinel sentinel.SentinelClient,
highestSeen uint64, verbose bool) (*LightClient, error) {

recentHashesCache, err := lru.New(maxRecentHashes)
rpc := rpc.NewBeaconRpcP2P(ctx, sentinel, beaconConfig, genesisConfig)
return &LightClient{
Expand All @@ -76,22 +74,16 @@ func NewLightClient(ctx context.Context, db kv.RwDB, genesisConfig *clparams.Gen
execution: execution,
verbose: verbose,
highestSeen: highestSeen,
db: db,
executionClient: executionClient,
}, err
}

func (l *LightClient) Start() {
var err error
if l.store == nil {
log.Error("No trusted setup")
return
}
tx, err := l.db.BeginRw(l.ctx)
if err != nil {
log.Error("Could not open MDBX transaction", "err", err)
return
}
defer tx.Rollback()
logPeers := time.NewTicker(time.Minute)

go l.chainTip.StartLoop()
Expand Down Expand Up @@ -159,41 +151,6 @@ func (l *LightClient) Start() {
log.Warn("could not compute root", "err", err)
continue
}
// Save to Database
if lastValidated.HasNextSyncCommittee() {
if err := rawdb.WriteLightClientUpdate(tx, lastValidated); err != nil {
log.Warn("Could not write lightclient update to db", "err", err)
}
}
if lastValidated.IsFinalityUpdate() {
if err := rawdb.WriteLightClientFinalityUpdate(tx, &cltypes.LightClientFinalityUpdate{
AttestedHeader: lastValidated.AttestedHeader,
FinalizedHeader: lastValidated.FinalizedHeader,
FinalityBranch: lastValidated.FinalityBranch,
SyncAggregate: lastValidated.SyncAggregate,
SignatureSlot: lastValidated.SignatureSlot,
}); err != nil {
log.Warn("Could not write finality lightclient update to db", "err", err)
}
}
if err := rawdb.WriteLightClientOptimisticUpdate(tx, &cltypes.LightClientOptimisticUpdate{
AttestedHeader: lastValidated.AttestedHeader,
SyncAggregate: lastValidated.SyncAggregate,
SignatureSlot: lastValidated.SignatureSlot,
}); err != nil {
log.Warn("Could not write optimistic lightclient update to db", "err", err)
}

if err := tx.Commit(); err != nil {
log.Error("[LightClient] could not commit to database", "err", err)
return
}
tx, err = l.db.BeginRw(l.ctx)
if err != nil {
log.Error("[LightClient] could not begin database transaction", "err", err)
return
}
defer tx.Rollback()

if l.verbose {
var m runtime.MemStats
Expand Down
17 changes: 2 additions & 15 deletions cmd/lightclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import (
"os"

"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
Expand Down Expand Up @@ -58,16 +55,6 @@ func runLightClientNode(cliCtx *cli.Context) error {
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))
log.Info("[LightClient]", "chain", cliCtx.String(flags.Chain.Name))
log.Info("[LightClient] Running lightclient", "cfg", cfg)
var db kv.RwDB
if cfg.Chaindata == "" {
log.Info("chaindata is in-memory")
db = memdb.New("" /* tmpDir */)
} else {
db, err = mdbx.Open(cfg.Chaindata, log.Root(), false)
if err != nil {
return err
}
}
state, err := core.RetrieveBeaconState(ctx, cfg.BeaconCfg, cfg.GenesisCfg, cfg.CheckpointUri)
if err != nil {
return err
Expand All @@ -86,7 +73,7 @@ func runLightClientNode(cliCtx *cli.Context) error {
NetworkConfig: cfg.NetworkCfg,
BeaconConfig: cfg.BeaconCfg,
NoDiscovery: cfg.NoDiscovery,
}, db, &service.ServerConfig{Network: cfg.ServerProtocol, Addr: cfg.ServerAddr}, nil, &cltypes.Status{
}, nil, &service.ServerConfig{Network: cfg.ServerProtocol, Addr: cfg.ServerAddr}, nil, &cltypes.Status{
ForkDigest: forkDigest,
FinalizedRoot: state.FinalizedCheckpoint().Root,
FinalizedEpoch: state.FinalizedCheckpoint().Epoch,
Expand All @@ -111,7 +98,7 @@ func runLightClientNode(cliCtx *cli.Context) error {
defer cc.Close()
execution = remote.NewETHBACKENDClient(cc)
}
lc, err := lightclient.NewLightClient(ctx, db, cfg.GenesisCfg, cfg.BeaconCfg, nil, execution, sentinel, 0, true)
lc, err := lightclient.NewLightClient(ctx, cfg.GenesisCfg, cfg.BeaconCfg, nil, execution, sentinel, 0, true)
if err != nil {
log.Error("Could not make Lightclient", "err", err)
return err
Expand Down
187 changes: 1 addition & 186 deletions cmd/sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@ package main

import (
"context"
"encoding/hex"
"fmt"
"os"
"time"

"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"
"go.uber.org/zap/buffer"

sentinelrpc "github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/cltypes/ssz"
"github.com/ledgerwatch/erigon/cl/fork"
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/handshake"
"github.com/ledgerwatch/erigon/cmd/sentinel/sentinel/service"
"github.com/ledgerwatch/erigon/common"
sentinelapp "github.com/ledgerwatch/erigon/turbo/app"
)

Expand All @@ -49,34 +40,8 @@ func main() {
}
}

func check(err error) {
if err != nil {
panic(err)
}
}

func constructBodyFreeRequest(t string) *sentinelrpc.RequestData {
return &sentinelrpc.RequestData{
Topic: t,
}
}

func constructRequest(t string, reqBody ssz.EncodableSSZ) (*sentinelrpc.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 &sentinelrpc.RequestData{
Data: data,
Topic: t,
}, nil
}

func runSentinelNode(cliCtx *cli.Context) error {
cfg, _ := lcCli.SetupConsensusClientCfg(cliCtx)
//ctx := context.Background()

log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(cfg.LogLvl), log.StderrHandler))
log.Info("[Sentinel] running sentinel with configuration", "cfg", cfg)
Expand All @@ -95,156 +60,6 @@ func runSentinelNode(cliCtx *cli.Context) error {
}
log.Info("[Sentinel] Sentinel started", "addr", cfg.ServerAddr)

digest, err := fork.ComputeForkDigest(cfg.BeaconCfg, cfg.GenesisCfg)
if err != nil {
log.Error("[Sentinel] Could not compute fork digeest", "err", err)
return err
}
log.Info("[Sentinel] Fork digest", "data", digest)

log.Info("[Sentinel] 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,
}
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
// Getting fork digest into bytes array.
forkDigest := "4a26c58b"
fdSlice, err := hex.DecodeString(forkDigest)
check(err)
var fdArr [4]byte
copy(fdArr[:], fdSlice)
// Getting CP block root into bytes array.
cpRootRaw := "60ee0f7170ed4984e4a7e735dff36bce28d2fb03a3f698287b73415d70fe7355"
cpRootSlice, err := hex.DecodeString(cpRootRaw)
check(err)
var cpRootArr [32]byte
copy(cpRootArr[:], cpRootSlice)
// Getting head block root into bytes array.
headRootRaw := "b52fc95e02471414451e6cf465709f53f65145fc5efbb18817252c33e301fa3a"
headRootSlice, err := hex.DecodeString(headRootRaw)
check(err)
var headRootArr [32]byte
copy(headRootArr[:], headRootSlice)
// USING: https://beaconcha.in/slot/5101760 as the checkpoint & current block.
statusReq := &cltypes.Status{
ForkDigest: fdArr,
FinalizedRoot: cpRootArr,
FinalizedEpoch: 160285,
HeadRoot: headRootArr,
HeadSlot: 5129184,
}
blocksByRangeReq := &cltypes.BeaconBlocksByRangeRequest{
StartSlot: 5142800,
Count: 2,
Step: 1, // deprecated, must be set to 1.
}
req, err := constructRequest(handlers.BeaconBlocksByRangeProtocolV2, blocksByRangeReq)
if err != nil {
log.Error("[Sentinel] could not construct request", "err", err)
}
sendRequest(ctx, s, req)
*/
/*roots := make([][32]byte, 1)
rawRoot1, err := hex.DecodeString("cc85056af7f6e3e4835436cb12a09a9d56e0aac15d08436af82dbe0cd7ae60e0")
check(err)
copy(roots[0][:], rawRoot1)
var blocksByRootReq cltypes.BeaconBlocksByRootRequest = roots
req, err := constructRequest(communication.BeaconBlocksByRootProtocolV2, &blocksByRootReq)
if err != nil {
log.Error("[Sentinel] could not construct request", "err", err)
return err
}
sendRequest(ctx, s, req)*/
<-context.Background().Done()
return nil
}

func debugGossip(ctx context.Context, s sentinelrpc.SentinelClient) {
subscription, err := s.SubscribeGossip(ctx, &sentinelrpc.EmptyMessage{})
if err != nil {
log.Error("[Sentinel] Could not start sentinel", "err", err)
return
}
for {
data, err := subscription.Recv()
if err != nil {
return
}
if data.Type != sentinelrpc.GossipType_AggregateAndProofGossipType {
continue
}
block := &cltypes.SignedAggregateAndProof{}
if err := block.DecodeSSZ(data.Data); err != nil {
log.Error("[Sentinel] Error", "err", err)
continue
}
log.Info("[Sentinel] Received", "msg", block)
}
}

// Debug function to recieve test packets on the req/resp domain.
func sendRequest(ctx context.Context, s sentinelrpc.SentinelClient, req *sentinelrpc.RequestData) {
newReqTicker := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-ctx.Done():
case <-newReqTicker.C:
go func() {
log.Info("[Sentinel] Sending request", "data", req)
message, err := s.SendRequest(ctx, req)
if err != nil {
log.Error("[Sentinel] Error returned", "err", err)
return
}
if message.Error {
log.Error("[Sentinel] received error", "err", string(message.Data))
return
}

log.Info("[Sentinel] Non-error response received", "data", message.Data)
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))
}
log.Info("[Sentinel] Hex representation", "data", hex.EncodeToString(message.Data))
f.Close()
}()
}
}
}
2 changes: 0 additions & 2 deletions cmd/sentinel/sentinel/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ func NewConsensusHandlers(ctx context.Context, db kv.RoDB, host host.Host,
protocol.ID(communication.MetadataProtocolV2): c.metadataV2Handler,
protocol.ID(communication.BeaconBlocksByRangeProtocolV1): c.blocksByRangeHandler,
protocol.ID(communication.BeaconBlocksByRootProtocolV1): c.beaconBlocksByRootHandler,
protocol.ID(communication.LightClientFinalityUpdateV1): c.lightClientFinalityUpdateHandler,
protocol.ID(communication.LightClientOptimisticUpdateV1): c.lightClientOptimisticUpdateHandler,
}
return c
}
Expand Down
Loading

0 comments on commit fc10aef

Please sign in to comment.