Skip to content

Commit

Permalink
bls implement
Browse files Browse the repository at this point in the history
  • Loading branch information
olegshmuelov committed Dec 26, 2023
1 parent 0378c7f commit 416d779
Show file tree
Hide file tree
Showing 8 changed files with 609 additions and 80 deletions.
57 changes: 46 additions & 11 deletions e2e/cmd/ssv-e2e/logs_catcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ import (
)

type LogsCatcherCmd struct {
Mode string `required:"" env:"Mode" help:"Mode of the logs catcher. Can be Slashing or BlsVerification"`
Leader int `required:"" env:"Leader" help:"Leader to run the bls verification on"`
}

const (
SlashingMode = "Slashing"
BlsVerificationMode = "BlsVerification"
)

func (cmd *LogsCatcherCmd) Run(logger *zap.Logger, globals Globals) error {
// TODO: where do we stop?
ctx := context.Background()
Expand All @@ -25,19 +32,47 @@ func (cmd *LogsCatcherCmd) Run(logger *zap.Logger, globals Globals) error {

//TODO: run fataler and matcher in parallel?

// TODO: should be uncommented and see how we run multiple e2e tests
//err = logs_catcher.FatalListener(ctx, logger, cli)
//if err != nil {
// return err
//}
// Execute different logic based on the value of the Mode flag
switch cmd.Mode {
case SlashingMode:
logger.Info("Running slashing mode")
err = logs_catcher.FatalListener(ctx, logger, cli)
if err != nil {
return err
}
err = logs_catcher.Match(ctx, logger, cli)
if err != nil {
return err
}

case BlsVerificationMode:
logger.Info("Running BlsVerification mode")

var corruptedShare logs_catcher.CorruptedShare
switch cmd.Leader {
case 1:
corruptedShare = logs_catcher.CorruptedShare{
OperatorID: 2,
ValidatorPubKey: "8c5801d7a18e27fae47dfdd99c0ac67fbc6a5a56bb1fc52d0309626d805861e04eaaf67948c18ad50c96d63e44328ab0",
ValidatorIndex: fmt.Sprintf("v%d", 1476356),
}

case 2:
corruptedShare = logs_catcher.CorruptedShare{
OperatorID: 2,
ValidatorPubKey: "a238aa8e3bd1890ac5def81e1a693a7658da491ac087d92cee870ab4d42998a184957321d70cbd42f9d38982dd9a928c",
ValidatorIndex: fmt.Sprintf("v%d", 1476357),
}
default:
return fmt.Errorf("invalid leader: %d", cmd.Leader)
}

//err = logs_catcher.Match(ctx, logger, cli)
//if err != nil {
// return err
//}
if err = logs_catcher.VerifyBLSSignature(ctx, logger, cli, corruptedShare); err != nil {
return err
}

if err = logs_catcher.VerifyBLSSignature(ctx, logger, cli); err != nil {
return err
default:
return fmt.Errorf("invalid mode: %s", cmd.Mode)
}

return nil
Expand Down
1 change: 1 addition & 0 deletions e2e/cmd/ssv-e2e/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type CLI struct {
Globals
BeaconProxy BeaconProxyCmd `cmd:""`
LogsCatcher LogsCatcherCmd `cmd:""`
ShareUpdate ShareUpdateCmd `cmd:""`
}

func main() {
Expand Down
122 changes: 122 additions & 0 deletions e2e/cmd/ssv-e2e/share_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"crypto/x509"
"encoding/base64"
"encoding/hex"
"fmt"

"github.com/bloxapp/ssv/ekm"
"github.com/bloxapp/ssv/networkconfig"
operatorstorage "github.com/bloxapp/ssv/operator/storage"
"github.com/bloxapp/ssv/storage/basedb"
"github.com/bloxapp/ssv/storage/kv"
"github.com/bloxapp/ssv/utils/rsaencryption"
"github.com/herumi/bls-eth-go-binary/bls"
"go.uber.org/zap"
)

type ShareUpdateCmd struct {
NetworkName string `required:"" env:"NETWORK" env-description:"Network config name"`
DBPath string `required:"" env:"DB_PATH" help:"Path to the DB folder"`
OperatorPrivateKey string `required:"" env:"OPERATOR_KEY" env-description:"Operator private key"`
ValidatorPubKey string `required:"" env:"VALIDATOR_PUB_KEY" env-description:"Validator public key"`
}

const (
// secret key to be used for updated share
sk = "3548db63ab5701878daf25fa877638dc7809778815b9d9ecd5369da33ca9e64f"
vPK1 = "8c5801d7a18e27fae47dfdd99c0ac67fbc6a5a56bb1fc52d0309626d805861e04eaaf67948c18ad50c96d63e44328ab0" // leader 1
vPK2 = "a238aa8e3bd1890ac5def81e1a693a7658da491ac087d92cee870ab4d42998a184957321d70cbd42f9d38982dd9a928c" // leader 2
)

func (cmd *ShareUpdateCmd) Run(logger *zap.Logger, globals Globals) error {
// Setup DB
db, err := kv.New(logger, basedb.Options{
Path: cmd.DBPath,
})
if err != nil {
return fmt.Errorf("failed to open db: %w", err)
}
defer db.Close()

nodeStorage, err := operatorstorage.NewNodeStorage(logger, db)
if err != nil {
return fmt.Errorf("failed to create node storage: %w", err)
}

opSK, err := base64.StdEncoding.DecodeString(cmd.OperatorPrivateKey)
if err != nil {
return err
}
rsaPriv, err := rsaencryption.ConvertPemToPrivateKey(string(opSK))
if err != nil {
return fmt.Errorf("failed to convert PEM to private key: %w", err)
}

rsaPub, err := rsaencryption.ExtractPublicKey(rsaPriv)
if err != nil {
return fmt.Errorf("failed to extract public key: %w", err)
}

operatorData, found, err := nodeStorage.GetOperatorDataByPubKey(nil, []byte(rsaPub))
if err != nil {
return fmt.Errorf("failed to get operator data: %w", err)
}
if !found {
return fmt.Errorf("operator data not found")
}

logger.Info("operator data found", zap.Any("operator ID", operatorData.ID))

keyBytes := x509.MarshalPKCS1PrivateKey(rsaPriv)
hashedKey, _ := rsaencryption.HashRsaKey(keyBytes)

networkConfig, err := networkconfig.GetNetworkConfigByName(cmd.NetworkName)
if err != nil {
return fmt.Errorf("failed to get network config: %w", err)
}
keyManager, err := ekm.NewETHKeyManagerSigner(logger, db, networkConfig, false, hashedKey)
if err != nil {
return fmt.Errorf("failed to create key manager: %w", err)
}

pkBytes, err := hex.DecodeString(cmd.ValidatorPubKey)
if err != nil {
return fmt.Errorf("failed to decode validator public key: %w", err)
}

validatorShare := nodeStorage.Shares().Get(nil, pkBytes)
if validatorShare == nil {
return fmt.Errorf(fmt.Sprintf("validator share not found for %s", cmd.ValidatorPubKey))
}
for i, op := range validatorShare.Committee {
if op.OperatorID == operatorData.ID {

blsSK := &bls.SecretKey{}
if err = blsSK.SetHexString(sk); err != nil {
return fmt.Errorf("failed to set secret key: %w", err)
}

if err = keyManager.AddShare(blsSK); err != nil {
return fmt.Errorf("failed to add share: %w", err)
}

preChangePK := validatorShare.SharePubKey
validatorShare.SharePubKey = blsSK.GetPublicKey().Serialize()
validatorShare.Share.Committee[i].PubKey = validatorShare.SharePubKey
if err = nodeStorage.Shares().Save(nil, validatorShare); err != nil {
return fmt.Errorf("failed to save share: %w", err)
}

logger.Info("validator share was updated successfully",
zap.String("validator pub key", hex.EncodeToString(validatorShare.ValidatorPubKey)),
zap.String("BEFORE: share pub key", hex.EncodeToString(preChangePK)),
zap.String("AFTER: share pub key", hex.EncodeToString(validatorShare.SharePubKey)),
)
return nil
}
}

return fmt.Errorf("operator not found in validator share")
}
13 changes: 13 additions & 0 deletions e2e/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock

share_update:
build:
context: .
dockerfile: Dockerfile
image: share_update:latest
command: share-update
environment:
- NETWORK=holesky-e2e
volumes:
- ssv-node-2-data:/ssv-node-2-data
networks:
- blox-docker

x-base: &default-base
depends_on:
beacon_proxy:
Expand Down
56 changes: 53 additions & 3 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,81 @@ require (

require (
github.com/alecthomas/kong v0.8.1
github.com/bloxapp/ssv-spec v0.3.4
github.com/docker/docker v24.0.7+incompatible
github.com/herumi/bls-eth-go-binary v1.29.1
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/bloxapp/ssv-spec v0.3.4 // indirect
github.com/bloxapp/eth2-key-manager v1.3.2 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger/v4 v4.1.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.12.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/validator/v10 v10.13.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/herumi/bls-eth-go-binary v1.29.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.28.2 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr v0.9.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.2 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/wealdtech/go-bytesutil v1.2.1 // indirect
github.com/wealdtech/go-eth2-types/v2 v2.8.1 // indirect
github.com/wealdtech/go-eth2-util v1.8.1 // indirect
github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4 v1.1.3 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.9.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
)
Loading

0 comments on commit 416d779

Please sign in to comment.