Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/util/printer/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func New(out io.Writer) Printer {

// Text implements Printer interface.
func (p *StandardPrinter) Text(text string) {
fmt.Fprintln(p.out, text)
_, _ = fmt.Fprintln(p.out, text)
}

// JSON implements Printer interface.
Expand All @@ -47,9 +47,9 @@ func (p *StandardPrinter) JSON(obj interface{}) error {
return nil
}

// JSON implements Printer interface.
// Error implements Printer interface.
func (p *StandardPrinter) Error(err error) {
if err != nil {
fmt.Fprintln(p.out, "Error:", err.Error())
_, _ = fmt.Fprintln(p.out, "Error:", err.Error())
}
}
27 changes: 19 additions & 8 deletions core/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package core

import (
"encoding/hex"
"runtime/debug"
"time"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/sirupsen/logrus"
)

// logFatalWithStack logs a fatal message with a stack trace
func logFatalWithStack(field string, value interface{}, message string) {
stackTrace := string(debug.Stack())
logrus.WithFields(logrus.Fields{
field: value,
"stacktrace": stackTrace,
}).Fatal(message)
}

// Network represents the network.
type Network string

Expand Down Expand Up @@ -39,7 +49,7 @@ func (n Network) GenesisForkVersion() phase0.Version {
case MainNetwork:
return phase0.Version{0, 0, 0, 0}
default:
logrus.WithField("network", n).Fatal("undefined network")
logFatalWithStack("network", n, "undefined network")
return phase0.Version{}
}
}
Expand All @@ -58,7 +68,7 @@ func (n Network) GenesisValidatorsRoot() phase0.Root {
rootBytes, _ := hex.DecodeString("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95")
copy(genValidatorsRoot[:], rootBytes)
default:
logrus.WithField("network", n).Fatal("undefined network")
logFatalWithStack("network", n, "undefined network")
}
return genValidatorsRoot
}
Expand All @@ -75,7 +85,7 @@ func (n Network) DepositContractAddress() string {
case MainNetwork:
return "0x00000000219ab540356cBB839Cbe05303d7705Fa"
default:
logrus.WithField("network", n).Fatal("undefined network")
logFatalWithStack("network", n, "undefined network")
return ""
}
}
Expand All @@ -97,7 +107,7 @@ func (n Network) MinGenesisTime() uint64 {
case MainNetwork:
return 1606824023
default:
logrus.WithField("network", n).Fatal("undefined network")
logFatalWithStack("network", n, "undefined network")
return 0
}
}
Expand All @@ -114,16 +124,17 @@ func (n Network) SlotsPerEpoch() uint64 {

// EstimatedCurrentSlot returns the estimation of the current slot
func (n Network) EstimatedCurrentSlot() phase0.Slot {
return n.EstimatedSlotAtTime(time.Now().Unix())
return n.EstimatedSlotAtTime(time.Now())
}

// EstimatedSlotAtTime estimates slot at the given time
func (n Network) EstimatedSlotAtTime(time int64) phase0.Slot {
func (n Network) EstimatedSlotAtTime(t time.Time) phase0.Slot {
timeUnix := t.Unix()
genesis := int64(n.MinGenesisTime())
if time < genesis {
if timeUnix < genesis {
return 0
}
return phase0.Slot(uint64(time-genesis) / uint64(n.SlotDurationSec().Seconds()))
return phase0.Slot(uint64(timeUnix-genesis) / uint64(n.SlotDurationSec().Seconds()))
}

// EstimatedCurrentEpoch estimates the current epoch
Expand Down
2 changes: 1 addition & 1 deletion core/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ func TestNetworkMainnet(t *testing.T) {
secondsPassedSinceGenesis := time.Now().Unix() - 1606824023
require.EqualValues(t, phase0.Epoch(secondsPassedSinceGenesis/(12*32)), net.EstimatedCurrentEpoch())
require.EqualValues(t, phase0.Epoch(secondsPassedSinceGenesis/12), net.EstimatedCurrentSlot())
require.EqualValues(t, phase0.Epoch(secondsPassedSinceGenesis/12), net.EstimatedSlotAtTime(time.Now().Unix()))
require.EqualValues(t, phase0.Epoch(secondsPassedSinceGenesis/12), net.EstimatedSlotAtTime(time.Now()))
require.EqualValues(t, phase0.Epoch(101010/32), net.EstimatedEpochAtSlot(phase0.Slot(101010)))
}
12 changes: 5 additions & 7 deletions signer/far_future_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import (
"time"

"github.com/attestantio/go-eth2-client/spec/phase0"

"github.com/ssvlabs/eth2-key-manager/core"
)

// FarFutureMaxValidEpoch is the max epoch of far future signing
var FarFutureMaxValidEpoch = int64(time.Minute.Seconds() * 20)
var FarFutureMaxValidEpoch = time.Minute * 20

// IsValidFarFutureEpoch prevents far into the future signing request, verify a slot is within the current epoch
// https://github.com/ethereum/eth2.0-specs/blob/dev/specs/phase0/validator.md#protection-best-practices
func IsValidFarFutureEpoch(network core.Network, epoch phase0.Epoch) bool {
maxValidEpoch := network.EstimatedEpochAtSlot(network.EstimatedSlotAtTime(time.Now().Unix() + FarFutureMaxValidEpoch))
func IsValidFarFutureEpoch(network network, epoch phase0.Epoch) bool {
maxValidEpoch := network.EstimatedEpochAtSlot(network.EstimatedSlotAtTime(time.Now().Add(FarFutureMaxValidEpoch)))
return epoch <= maxValidEpoch
}

// IsValidFarFutureSlot returns true if the given slot is valid
func IsValidFarFutureSlot(network core.Network, slot phase0.Slot) bool {
maxValidSlot := network.EstimatedSlotAtTime(time.Now().Unix() + FarFutureMaxValidEpoch)
func IsValidFarFutureSlot(network network, slot phase0.Slot) bool {
maxValidSlot := network.EstimatedSlotAtTime(time.Now().Add(FarFutureMaxValidEpoch))
return slot <= maxValidSlot
}
2 changes: 1 addition & 1 deletion signer/sign_attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func TestAttestationSignatures(t *testing.T) {
func TestFarFutureAttestationSignature(t *testing.T) {
seed := _byteArray("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1fff")
network := core.PraterNetwork
maxValidEpoch := network.EstimatedEpochAtSlot(network.EstimatedSlotAtTime(time.Now().Unix() + FarFutureMaxValidEpoch))
maxValidEpoch := network.EstimatedEpochAtSlot(network.EstimatedSlotAtTime(time.Now().Add(FarFutureMaxValidEpoch)))

t.Run("max valid source", func(tt *testing.T) {
signer, err := setupWithSlashingProtection(t, seed, true, true)
Expand Down
2 changes: 1 addition & 1 deletion signer/sign_beacon_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func TestProposalSlashingSignatures(t *testing.T) {
func TestFarFutureProposalSignature(t *testing.T) {
seed := _byteArray("0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1fff")
network := core.PraterNetwork
maxValidSlot := network.EstimatedSlotAtTime(time.Now().Unix() + FarFutureMaxValidEpoch)
maxValidSlot := network.EstimatedSlotAtTime(time.Now().Add(FarFutureMaxValidEpoch))

t.Run("max valid source", func(tt *testing.T) {
signer, err := setupWithSlashingProtection(t, seed, true, true)
Expand Down
10 changes: 8 additions & 2 deletions signer/validator_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package signer

import (
"sync"
"time"

"github.com/attestantio/go-eth2-client/api"
"github.com/attestantio/go-eth2-client/spec"
Expand Down Expand Up @@ -30,17 +31,22 @@ type ValidatorSigner interface {
SignBLSToExecutionChange(blsToExecutionChange *capella.BLSToExecutionChange, domain phase0.Domain, pubKey []byte) (sig []byte, root []byte, err error)
}

type network interface {
EstimatedEpochAtSlot(slot phase0.Slot) phase0.Epoch
EstimatedSlotAtTime(time time.Time) phase0.Slot
}

// SimpleSigner implements ValidatorSigner interface
type SimpleSigner struct {
wallet core.Wallet
slashingProtector core.SlashingProtector
network core.Network
network network
signLocks map[string]*sync.RWMutex
mapLock *sync.RWMutex
}

// NewSimpleSigner is the constructor of SimpleSigner
func NewSimpleSigner(wallet core.Wallet, slashingProtector core.SlashingProtector, network core.Network) *SimpleSigner {
func NewSimpleSigner(wallet core.Wallet, slashingProtector core.SlashingProtector, network network) *SimpleSigner {
return &SimpleSigner{
wallet: wallet,
slashingProtector: slashingProtector,
Expand Down
7 changes: 4 additions & 3 deletions stores/inmemory/marshalable.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,22 @@ func (store *InMemStore) UnmarshalJSON(data []byte) error {
return err
}

if walletType == core.HDWallet {
switch walletType {
case core.HDWallet:
hd := &hd2.Wallet{}
err = json.Unmarshal(byts, &hd)
if err != nil {
return err
}
store.wallet = hd
} else if walletType == core.NDWallet {
case core.NDWallet:
nd := &nd.Wallet{}
err = json.Unmarshal(byts, &nd)
if err != nil {
return err
}
store.wallet = nd
} else {
default:
return errors.Errorf("unknown wallet type %s", walletType)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions stores/inmemory/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestWalletStorage(t *testing.T) {
err = storage.SaveWallet(wallet)
if err != nil {
if test.error != nil {
require.Equal(t, test.error.Error(), err.Error())
require.Equal(t, test.error, err)
} else {
t.Error(err)
}
Expand All @@ -298,7 +298,7 @@ func TestWalletStorage(t *testing.T) {
fetched, err := storage.OpenWallet()
if err != nil {
if test.error != nil {
require.Equal(t, test.error.Error(), err.Error())
require.Equal(t, test.error, err)
} else {
t.Error(err)
}
Expand Down