Skip to content

SimpleSigner: change network to interface #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
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
8 changes: 4 additions & 4 deletions core/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ 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(time time.Time) phase0.Slot {
genesis := int64(n.MinGenesisTime())
if time < genesis {
if time.Unix() < genesis {
return 0
}
return phase0.Slot(uint64(time-genesis) / uint64(n.SlotDurationSec().Seconds()))
return phase0.Slot(uint64(time.Unix()-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 @@ -16,6 +16,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)))
}
16 changes: 6 additions & 10 deletions signer/far_future_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ 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)
// MaxFarFutureDelta is the max delta of far future signing
var MaxFarFutureDelta = 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(MaxFarFutureDelta)))
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(MaxFarFutureDelta))
return slot <= maxValidSlot
}
5 changes: 2 additions & 3 deletions signer/sign_attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestLockSameValidatorInParallel(t *testing.T) {
require.NoError(t, err)
require.NoError(t, wallet.AddValidatorAccount(acc))

//// setup signer
// // setup signer
signer := NewSimpleSigner(wallet, &prot.NoProtection{}, core.MainNetwork)

attestationDataByts := _byteArray("000000000000000000000000000000003a43a4bf26fb5947e809c1f24f7dc6857c8ac007e535d48e6e4eca2122fd776b0000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000003a43a4bf26fb5947e809c1f24f7dc6857c8ac007e535d48e6e4eca2122fd776b")
Expand Down Expand Up @@ -192,7 +192,6 @@ func TestManyValidatorsParallel(t *testing.T) {
require.NoError(t, err)
}

// setup signer
signer := NewSimpleSigner(wallet, protector, core.PraterNetwork)

// Sign attestation in parallel.
Expand Down Expand Up @@ -682,7 +681,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(MaxFarFutureDelta)))

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 @@ -492,7 +492,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(MaxFarFutureDelta))

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