Skip to content

Commit

Permalink
Merge pull request #1115 from bloxapp/stage
Browse files Browse the repository at this point in the history
`stage` -> `main`
  • Loading branch information
Lior Rutenberg authored Aug 21, 2023
2 parents 9569452 + 814e093 commit beadd23
Show file tree
Hide file tree
Showing 49 changed files with 1,049 additions and 395 deletions.
21 changes: 17 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#
# STEP 1: Prepare environment
#
FROM golang:1.19 AS preparer
FROM golang:1.19.12 AS preparer

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
curl git zip unzip wget g++ gcc-aarch64-linux-gnu bzip2 make \
curl=7.88.1-10+deb12u1 \
git=1:2.39.2-1.1 \
zip=3.0-13 \
unzip=6.0-28 \
wget=1.21.3-1+b2 \
g++=4:12.2.0-3 \
gcc-aarch64-linux-gnu=4:12.2.0-3 \
bzip2=1.0.8-5+b1 \
make=4.3-4.1 \
&& rm -rf /var/lib/apt/lists/*

# install jemalloc
WORKDIR /tmp/jemalloc-temp
RUN curl -s -L "https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2" -o jemalloc.tar.bz2 \
Expand Down Expand Up @@ -45,10 +54,14 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
#
# STEP 3: Prepare image to run the binary
#
FROM alpine:3.12 AS runner
FROM alpine:3.18.3 AS runner

# Install ca-certificates, bash
RUN apk -v --update add ca-certificates bash make bind-tools && \
RUN apk -v --update add \
ca-certificates=20230506-r0 \
bash=5.2.15-r5 \
make=4.4.1-r1 \
bind-tools=9.18.16-r0 && \
rm /var/cache/apk/*

COPY --from=builder /go/bin/ssvnode /go/bin/ssvnode
Expand Down
54 changes: 52 additions & 2 deletions beacon/goclient/goclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goclient
import (
"context"
"math"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -66,9 +67,33 @@ func init() {
}
}

// NodeClient is the type of the Beacon node.
type NodeClient string

const (
NodeLighthouse NodeClient = "lighthouse"
NodePrysm NodeClient = "prysm"
NodeUnknown NodeClient = "unknown"
)

// ParseNodeClient derives the client from node's version string.
func ParseNodeClient(version string) NodeClient {
version = strings.ToLower(version)
switch {
case strings.Contains(version, "lighthouse"):
return NodeLighthouse
case strings.Contains(version, "prysm"):
return NodePrysm
default:
return NodeUnknown
}
}

// Client defines all go-eth2-client interfaces used in ssv
type Client interface {
eth2client.Service
eth2client.NodeVersionProvider
eth2client.NodeClientProvider

eth2client.AttestationDataProvider
eth2client.AggregateAttestationProvider
Expand Down Expand Up @@ -99,12 +124,20 @@ type Client interface {
eth2client.ValidatorRegistrationsSubmitter
}

type NodeClientProvider interface {
NodeClient() NodeClient
}

var _ NodeClientProvider = (*goClient)(nil)

// goClient implementing Beacon struct
type goClient struct {
log *zap.Logger
ctx context.Context
network beaconprotocol.Network
client Client
nodeVersion string
nodeClient NodeClient
graffiti []byte
gasLimit uint64
operatorID spectypes.OperatorID
Expand All @@ -128,8 +161,6 @@ func New(logger *zap.Logger, opt beaconprotocol.Options, operatorID spectypes.Op
return nil, errors.WithMessage(err, "failed to create http client")
}

logger.Info("consensus client: connected", fields.Name(httpClient.Name()), fields.Address(httpClient.Address()))

tickerChan := make(chan phase0.Slot, 32)
slotTicker.Subscribe(tickerChan)

Expand All @@ -144,11 +175,30 @@ func New(logger *zap.Logger, opt beaconprotocol.Options, operatorID spectypes.Op
registrationCache: map[phase0.BLSPubKey]*api.VersionedSignedValidatorRegistration{},
}

// Get the node's version and client.
client.nodeVersion, err = client.client.NodeVersion(opt.Context)
if err != nil {
return nil, errors.Wrap(err, "failed to get node version")
}
client.nodeClient = ParseNodeClient(client.nodeVersion)

logger.Info("consensus client connected",
fields.Name(httpClient.Name()),
fields.Address(httpClient.Address()),
zap.String("client", string(client.nodeClient)),
zap.String("version", client.nodeVersion),
)

// Start registration submitter.
go client.registrationSubmitter(tickerChan)

return client, nil
}

func (gc *goClient) NodeClient() NodeClient {
return gc.nodeClient
}

// IsReady returns if beacon node is currently ready: responds to requests, not in the syncing state, not optimistic
// (for optimistic see https://github.com/ethereum/consensus-specs/blob/dev/sync/optimistic.md#block-production).
func (gc *goClient) IsReady(ctx context.Context) (bool, error) {
Expand Down
32 changes: 0 additions & 32 deletions beacon/goclient/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func (gc *goClient) GetBeaconBlock(slot phase0.Slot, graffiti, randao []byte) (s
if beaconBlock.Bellatrix.Body.ExecutionPayload == nil {
return nil, DataVersionNil, fmt.Errorf("bellatrix block execution payload is nil")
}
gc.log.Info("got beacon block",
fields.BlockHash(beaconBlock.Bellatrix.Body.ExecutionPayload.BlockHash),
fields.BlockVersion(beaconBlock.Version),
fields.Slot(beaconBlock.Bellatrix.Slot))
return beaconBlock.Bellatrix, beaconBlock.Version, nil
case spec.DataVersionCapella:
if beaconBlock.Capella.Body == nil {
Expand All @@ -70,10 +66,6 @@ func (gc *goClient) GetBeaconBlock(slot phase0.Slot, graffiti, randao []byte) (s
if beaconBlock.Capella.Body.ExecutionPayload == nil {
return nil, DataVersionNil, fmt.Errorf("capella block execution payload is nil")
}
gc.log.Info("got beacon block",
fields.BlockHash(beaconBlock.Capella.Body.ExecutionPayload.BlockHash),
fields.BlockVersion(beaconBlock.Version),
fields.Slot(beaconBlock.Capella.Slot))
return beaconBlock.Capella, beaconBlock.Version, nil
default:
return nil, DataVersionNil, fmt.Errorf("beacon block version %s not supported", beaconBlock.Version)
Expand Down Expand Up @@ -104,10 +96,6 @@ func (gc *goClient) GetBlindedBeaconBlock(slot phase0.Slot, graffiti, randao []b
if beaconBlock.Bellatrix.Body.ExecutionPayloadHeader == nil {
return nil, DataVersionNil, fmt.Errorf("bellatrix block execution payload header is nil")
}
gc.log.Info("got blinded beacon block",
fields.BlockHash(beaconBlock.Bellatrix.Body.ExecutionPayloadHeader.BlockHash),
fields.BlockVersion(beaconBlock.Version),
fields.Slot(beaconBlock.Bellatrix.Slot))
return beaconBlock.Bellatrix, beaconBlock.Version, nil
case spec.DataVersionCapella:
if beaconBlock.Capella.Body == nil {
Expand All @@ -116,10 +104,6 @@ func (gc *goClient) GetBlindedBeaconBlock(slot phase0.Slot, graffiti, randao []b
if beaconBlock.Capella.Body.ExecutionPayloadHeader == nil {
return nil, DataVersionNil, fmt.Errorf("capella block execution payload header is nil")
}
gc.log.Info("got blinded beacon block",
fields.BlockHash(beaconBlock.Capella.Body.ExecutionPayloadHeader.BlockHash),
fields.BlockVersion(beaconBlock.Version),
fields.Slot(beaconBlock.Capella.Slot))
return beaconBlock.Capella, beaconBlock.Version, nil
default:
return nil, DataVersionNil, fmt.Errorf("beacon block version %s not supported", beaconBlock.Version)
Expand All @@ -139,10 +123,6 @@ func (gc *goClient) SubmitBlindedBeaconBlock(block *api.VersionedBlindedBeaconBl
Message: block.Bellatrix,
}
copy(signedBlock.Bellatrix.Signature[:], sig[:])
gc.log.Info("submitting blinded beacon block",
fields.BlockHash(block.Bellatrix.Body.ExecutionPayloadHeader.BlockHash),
fields.BlockVersion(block.Version),
fields.Slot(block.Bellatrix.Slot))
case spec.DataVersionCapella:
if block.Capella == nil {
return errors.New("capella blinded block is nil")
Expand All @@ -151,10 +131,6 @@ func (gc *goClient) SubmitBlindedBeaconBlock(block *api.VersionedBlindedBeaconBl
Message: block.Capella,
}
copy(signedBlock.Capella.Signature[:], sig[:])
gc.log.Info("submitting blinded beacon block",
fields.BlockHash(block.Capella.Body.ExecutionPayloadHeader.BlockHash),
fields.BlockVersion(block.Version),
fields.Slot(block.Capella.Slot))
default:
return errors.New("unknown block version")
}
Expand Down Expand Up @@ -192,10 +168,6 @@ func (gc *goClient) SubmitBeaconBlock(block *spec.VersionedBeaconBlock, sig phas
Message: block.Bellatrix,
}
copy(signedBlock.Bellatrix.Signature[:], sig[:])
gc.log.Info("submitting block",
fields.BlockHash(block.Bellatrix.Body.ExecutionPayload.BlockHash),
fields.BlockVersion(block.Version),
fields.Slot(block.Bellatrix.Slot))
case spec.DataVersionCapella:
if block.Capella == nil {
return errors.New("capella block is nil")
Expand All @@ -204,10 +176,6 @@ func (gc *goClient) SubmitBeaconBlock(block *spec.VersionedBeaconBlock, sig phas
Message: block.Capella,
}
copy(signedBlock.Capella.Signature[:], sig[:])
gc.log.Info("submitting block",
fields.BlockHash(block.Capella.Body.ExecutionPayload.BlockHash),
fields.BlockVersion(block.Version),
fields.Slot(block.Capella.Slot))
default:
return errors.New("unknown block version")
}
Expand Down
40 changes: 31 additions & 9 deletions cli/operator/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
beaconprotocol "github.com/bloxapp/ssv/protocol/v2/blockchain/beacon"
"github.com/bloxapp/ssv/protocol/v2/types"
registrystorage "github.com/bloxapp/ssv/registry/storage"
"github.com/bloxapp/ssv/storage"
"github.com/bloxapp/ssv/storage/basedb"
"github.com/bloxapp/ssv/storage/kv"
"github.com/bloxapp/ssv/utils/commons"
Expand Down Expand Up @@ -101,7 +100,7 @@ var StartNodeCmd = &cobra.Command{
defer logging.CapturePanic(logger)
networkConfig, forkVersion, err := setupSSVNetwork(logger)
if err != nil {
log.Fatal("could not setup network", err)
logger.Fatal("could not setup network", zap.Error(err))
}
cfg.DBOptions.Ctx = cmd.Context()
db, err := setupDB(logger, networkConfig.Beacon.GetNetwork())
Expand All @@ -111,9 +110,10 @@ var StartNodeCmd = &cobra.Command{

nodeStorage, operatorData := setupOperatorStorage(logger, db)

if err != nil {
logger.Fatal("could not run post storage migrations", zap.Error(err))
}
usingLocalEvents := len(cfg.LocalEventsPath) != 0

verifyConfig(logger, nodeStorage, networkConfig.Name, usingLocalEvents)

operatorKey, _, _ := nodeStorage.GetPrivateKey()
keyBytes := x509.MarshalPKCS1PrivateKey(operatorKey)
hashedKey, _ := rsaencryption.HashRsaKey(keyBytes)
Expand Down Expand Up @@ -250,7 +250,6 @@ var StartNodeCmd = &cobra.Command{
validatorCtrl,
storageMap,
metricsReporter,
nodeProber,
networkConfig,
nodeStorage,
)
Expand Down Expand Up @@ -293,6 +292,30 @@ var StartNodeCmd = &cobra.Command{
},
}

func verifyConfig(logger *zap.Logger, nodeStorage operatorstorage.Storage, networkName string, usingLocalEvents bool) {
storedConfig, foundConfig, err := nodeStorage.GetConfig(nil)
if err != nil {
logger.Fatal("could not check saved local events config", zap.Error(err))
}

currentConfig := &operatorstorage.ConfigLock{
NetworkName: networkName,
UsingLocalEvents: usingLocalEvents,
}

if foundConfig {
if err := storedConfig.EnsureSameWith(currentConfig); err != nil {
err = fmt.Errorf("incompatible config change: %w", err)
logger.Fatal(err.Error())
}
} else {
if err := nodeStorage.SaveConfig(nil, currentConfig); err != nil {
err = fmt.Errorf("failed to store config: %w", err)
logger.Fatal(err.Error())
}
}
}

func init() {
global_config.ProcessArgs(&cfg, &globalArgs, StartNodeCmd)
}
Expand Down Expand Up @@ -320,15 +343,15 @@ func setupGlobal(cmd *cobra.Command) (*zap.Logger, error) {
}

func setupDB(logger *zap.Logger, eth2Network beaconprotocol.Network) (*kv.BadgerDB, error) {
db, err := storage.GetStorageFactory(logger, cfg.DBOptions)
db, err := kv.New(logger, cfg.DBOptions)
if err != nil {
return nil, errors.Wrap(err, "failed to open db")
}
reopenDb := func() error {
if err := db.Close(); err != nil {
return errors.Wrap(err, "failed to close db")
}
db, err = storage.GetStorageFactory(logger, cfg.DBOptions)
db, err = kv.New(logger, cfg.DBOptions)
return errors.Wrap(err, "failed to reopen db")
}

Expand Down Expand Up @@ -483,7 +506,6 @@ func setupEventHandling(
validatorCtrl validator.Controller,
storageMap *ibftstorage.QBFTStores,
metricsReporter *metricsreporter.MetricsReporter,
nodeProber *nodeprobe.Prober,
networkConfig networkconfig.NetworkConfig,
nodeStorage operatorstorage.Storage,
) {
Expand Down
Loading

0 comments on commit beadd23

Please sign in to comment.