-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Check BLS changes when requesting from pool #11718
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
179e860
Check BLS changes when requesting from pool
potuz 6b970e0
Merge branch 'develop' into check_bls_changes
terencechain 2193d95
Merge branch 'develop' into check_bls_changes
terencechain 189d700
Terence's suggestions
potuz 4faa207
Radek's suggestion
potuz ad99f1d
Merge branch 'develop' into check_bls_changes
rauljordan 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
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
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
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
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 |
---|---|---|
|
@@ -4,17 +4,22 @@ import ( | |
"math" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/blocks" | ||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state" | ||
"github.com/prysmaticlabs/prysm/v3/config/params" | ||
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives" | ||
doublylinkedlist "github.com/prysmaticlabs/prysm/v3/container/doubly-linked-list" | ||
"github.com/prysmaticlabs/prysm/v3/crypto/bls/blst" | ||
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// PoolManager maintains pending and seen BLS-to-execution-change objects. | ||
// This pool is used by proposers to insert BLS-to-execution-change objects into new blocks. | ||
type PoolManager interface { | ||
PendingBLSToExecChanges() ([]*ethpb.SignedBLSToExecutionChange, error) | ||
BLSToExecChangesForInclusion() ([]*ethpb.SignedBLSToExecutionChange, error) | ||
BLSToExecChangesForInclusion(state.BeaconState) ([]*ethpb.SignedBLSToExecutionChange, error) | ||
InsertBLSToExecChange(change *ethpb.SignedBLSToExecutionChange) | ||
MarkIncluded(change *ethpb.SignedBLSToExecutionChange) error | ||
ValidatorExists(idx types.ValidatorIndex) bool | ||
|
@@ -58,25 +63,69 @@ func (p *Pool) PendingBLSToExecChanges() ([]*ethpb.SignedBLSToExecutionChange, e | |
|
||
// BLSToExecChangesForInclusion returns objects that are ready for inclusion at the given slot. | ||
// This method will not return more than the block enforced MaxBlsToExecutionChanges. | ||
func (p *Pool) BLSToExecChangesForInclusion() ([]*ethpb.SignedBLSToExecutionChange, error) { | ||
func (p *Pool) BLSToExecChangesForInclusion(st state.BeaconState) ([]*ethpb.SignedBLSToExecutionChange, error) { | ||
p.lock.RLock() | ||
defer p.lock.RUnlock() | ||
|
||
length := int(math.Min(float64(params.BeaconConfig().MaxBlsToExecutionChanges), float64(p.pending.Len()))) | ||
result := make([]*ethpb.SignedBLSToExecutionChange, length) | ||
result := make([]*ethpb.SignedBLSToExecutionChange, 0, length) | ||
node := p.pending.First() | ||
var err error | ||
for i := 0; node != nil && i < length; i++ { | ||
result[i], err = node.Value() | ||
for node != nil && len(result) < length { | ||
change, err := node.Value() | ||
if err != nil { | ||
p.lock.RUnlock() | ||
return nil, err | ||
} | ||
_, err = blocks.ValidateBLSToExecutionChange(st, change) | ||
if err != nil { | ||
logrus.WithError(err).Warning("removing invalid BLSToExecutionChange from pool") | ||
// MarkIncluded removes the invalid change from the pool | ||
p.lock.RUnlock() | ||
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. These lock/unlock scares me. Any reason to pick this over 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. MarkIncluded needs a lock, so we can't defer. |
||
if err := p.MarkIncluded(change); err != nil { | ||
return nil, errors.Wrap(err, "could not mark BLSToExecutionChange as included") | ||
} | ||
p.lock.RLock() | ||
} else { | ||
result = append(result, change) | ||
} | ||
node, err = node.Next() | ||
if err != nil { | ||
p.lock.RUnlock() | ||
return nil, err | ||
} | ||
} | ||
return result, nil | ||
p.lock.RUnlock() | ||
potuz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(result) == 0 { | ||
return result, nil | ||
} | ||
// We now verify the signatures in batches | ||
cSet, err := blocks.BLSChangesSignatureBatch(st, result) | ||
if err != nil { | ||
logrus.WithError(err).Warning("could not get BLSToExecutionChanges signatures") | ||
} else { | ||
ok, err := cSet.Verify() | ||
if err != nil { | ||
logrus.WithError(err).Warning("could not batch verify BLSToExecutionChanges signatures") | ||
} else if ok { | ||
return result, nil | ||
} | ||
} | ||
// Batch signature failed, check signatures individually | ||
verified := make([]*ethpb.SignedBLSToExecutionChange, 0, length) | ||
for i, sig := range cSet.Signatures { | ||
signature, err := blst.SignatureFromBytes(sig) | ||
if err != nil { | ||
logrus.WithError(err).Warning("could not get signature from bytes") | ||
continue | ||
} | ||
if !signature.Verify(cSet.PublicKeys[i], cSet.Messages[i][:]) { | ||
logrus.Warning("removing BLSToExecutionChange with invalid signature from pool") | ||
if err := p.MarkIncluded(result[i]); err != nil { | ||
return nil, errors.Wrap(err, "could not mark BLSToExecutionChange as included") | ||
} | ||
} else { | ||
verified = append(verified, result[i]) | ||
} | ||
} | ||
return verified, nil | ||
} | ||
|
||
// InsertBLSToExecChange inserts an object into the pool. | ||
|
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
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
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
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
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
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 you apply this for
PendingBLSToExecChanges
or we just simply care less for that?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.
I can see the value in returning the full list, someone may be interested in knowing what are all changes in the node. No one uses this endpoint yet so I am not sure what are the applications yet. Also, performing a linear scan with signature verification in an unbounded list scares me, so I'd rather have the caller making these checks.
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.
That's fair. I don't know whether beacon API explicitly calls this out. We can add this behavior to the commentary if it doesn't