-
Notifications
You must be signed in to change notification settings - Fork 94
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
olegshmuelov
wants to merge
1
commit into
stage
Choose a base branch
from
concurrent-slashing-protection-tests
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package ekm | |
|
||
import ( | ||
"encoding/hex" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -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" | ||
|
||
|
@@ -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++ | ||
} | ||
} | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).