Skip to content
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

Concurrent slashing protection tests #1809

Open
wants to merge 1 commit into
base: stage
Choose a base branch
from
Open
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
126 changes: 126 additions & 0 deletions ekm/signer_key_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ekm

import (
"encoding/hex"
"sync"
"testing"
"time"

Expand All @@ -20,6 +21,7 @@ import (
genesisspecqbft "github.com/ssvlabs/ssv-spec-pre-cc/qbft"
genesisspectypes "github.com/ssvlabs/ssv-spec-pre-cc/types"
spectypes "github.com/ssvlabs/ssv-spec/types"
"github.com/ssvlabs/ssv-spec/types/testingutils"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

Expand Down Expand Up @@ -788,3 +790,127 @@ func TestEkmListAccounts(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, len(accounts))
}

func TestConcurrentSlashingProtectionAttData(t *testing.T) {
require.NoError(t, bls.Init(bls.BLS12_381))

km := testKeyManager(t, nil)

sk1 := &bls.SecretKey{}
require.NoError(t, sk1.SetHexString(sk1Str))
require.NoError(t, km.AddShare(sk1))

currentSlot := km.(*ethKeyManagerSigner).storage.Network().EstimatedCurrentSlot()
currentEpoch := km.(*ethKeyManagerSigner).storage.Network().EstimatedEpochAtSlot(currentSlot)

highestTarget := currentEpoch + minSPAttestationEpochGap + 1
highestSource := highestTarget - 1

attestationData := &phase0.AttestationData{
Slot: currentSlot,
Index: 1,
BeaconBlockRoot: [32]byte{1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2},
Source: &phase0.Checkpoint{
Epoch: highestSource,
Root: [32]byte{},
},
Target: &phase0.Checkpoint{
Epoch: highestTarget,
Root: [32]byte{},
},
}

// Define function to concurrently attempt signing.
signAttestation := func(wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()
_, _, err := km.(*ethKeyManagerSigner).SignBeaconObject(
attestationData,
phase0.Domain{},
sk1.GetPublicKey().Serialize(),
spectypes.DomainAttester,
)
errChan <- err
}

// Set up concurrency.
const goroutineCount = 100
var wg sync.WaitGroup
errChan := make(chan error, goroutineCount)

for i := 0; i < goroutineCount; i++ {
wg.Add(1)
go signAttestation(&wg, errChan)
}

// Wait for all goroutines to complete.
wg.Wait()
close(errChan)

// Count errors and successes.
var slashableErrors, successCount int
for err := range errChan {
if err != nil && err.Error() == "slashable attestation (HighestAttestationVote), not signing" {
slashableErrors++
} else if err == nil {
successCount++
}
Comment on lines +852 to +856
Copy link

@iurii-ssv iurii-ssv Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check here for the absence of errors other than "slashable attestation (HighestAttestationVote), not signing" (or leave a comment explaining why not) ? Same for another test.

Edit: I guess the require checks below implicitly do this, but it's way easier to understand/debug when "failing fast" (and explicitly).

}

require.Equal(t, 1, successCount, "expected exactly one successful signing")
require.Equal(t, goroutineCount-1, slashableErrors, "expected slashing errors for remaining goroutines")
}

func TestConcurrentSlashingProtectionBeaconBlock(t *testing.T) {
require.NoError(t, bls.Init(bls.BLS12_381))

km := testKeyManager(t, nil)

sk1 := &bls.SecretKey{}
require.NoError(t, sk1.SetHexString(sk1Str))
require.NoError(t, km.AddShare(sk1))

currentSlot := km.(*ethKeyManagerSigner).storage.Network().EstimatedCurrentSlot()
highestProposal := currentSlot + minSPProposalSlotGap + 1

blockContents := testingutils.TestingBlockContentsDeneb
blockContents.Block.Slot = highestProposal

// Define function to concurrently attempt signing.
signBeaconBlock := func(wg *sync.WaitGroup, errChan chan error) {
defer wg.Done()
_, _, err := km.(*ethKeyManagerSigner).SignBeaconObject(
blockContents.Block,
phase0.Domain{},
sk1.GetPublicKey().Serialize(),
spectypes.DomainProposer,
)
errChan <- err
}

// Set up concurrency.
const goroutineCount = 100
Comment on lines +890 to +891

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably set to 1000 (or even 10000) to get the most out of this test. Same for another test.

var wg sync.WaitGroup
errChan := make(chan error, goroutineCount)

for i := 0; i < goroutineCount; i++ {
wg.Add(1)
go signBeaconBlock(&wg, errChan)
}

// Wait for all goroutines to complete.
wg.Wait()
close(errChan)

// Count errors and successes.
var slashableErrors, successCount int
for err := range errChan {
if err != nil && err.Error() == "slashable proposal (HighestProposalVote), not signing" {
slashableErrors++
} else if err == nil {
successCount++
}
}

require.Equal(t, 1, successCount, "expected exactly one successful signing")
require.Equal(t, goroutineCount-1, slashableErrors, "expected slashing errors for remaining goroutines")
}
Loading