forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 70
feat(p2p): ENR updates for discovery v4 compatibility #16679 #2010
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
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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -29,21 +29,16 @@ package enr | |||||
|
|
||||||
| import ( | ||||||
| "bytes" | ||||||
| "crypto/ecdsa" | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "sort" | ||||||
|
|
||||||
| "github.com/XinFinOrg/XDPoSChain/crypto" | ||||||
| "github.com/XinFinOrg/XDPoSChain/rlp" | ||||||
| "golang.org/x/crypto/sha3" | ||||||
| ) | ||||||
|
|
||||||
| const SizeLimit = 300 // maximum encoded size of a node record in bytes | ||||||
|
|
||||||
| const ID_SECP256k1_KECCAK = ID("secp256k1-keccak") // the default identity scheme | ||||||
|
|
||||||
| var ( | ||||||
| errNoID = errors.New("unknown or unspecified identity scheme") | ||||||
| errInvalidSig = errors.New("invalid signature") | ||||||
|
|
@@ -80,8 +75,8 @@ func (r *Record) Seq() uint64 { | |||||
| } | ||||||
|
|
||||||
| // SetSeq updates the record sequence number. This invalidates any signature on the record. | ||||||
| // Calling SetSeq is usually not required because signing the redord increments the | ||||||
| // sequence number. | ||||||
| // Calling SetSeq is usually not required because setting any key in a signed record | ||||||
| // increments the sequence number. | ||||||
| func (r *Record) SetSeq(s uint64) { | ||||||
| r.signature = nil | ||||||
| r.raw = nil | ||||||
|
|
@@ -104,33 +99,42 @@ func (r *Record) Load(e Entry) error { | |||||
| return &KeyError{Key: e.ENRKey(), Err: errNotFound} | ||||||
| } | ||||||
|
|
||||||
| // Set adds or updates the given entry in the record. | ||||||
| // It panics if the value can't be encoded. | ||||||
| // Set adds or updates the given entry in the record. It panics if the value can't be | ||||||
| // encoded. If the record is signed, Set increments the sequence number and invalidates | ||||||
| // the sequence number. | ||||||
| func (r *Record) Set(e Entry) { | ||||||
| r.signature = nil | ||||||
| r.raw = nil | ||||||
| blob, err := rlp.EncodeToBytes(e) | ||||||
| if err != nil { | ||||||
| panic(fmt.Errorf("enr: can't encode %s: %v", e.ENRKey(), err)) | ||||||
| } | ||||||
| r.invalidate() | ||||||
|
|
||||||
| i := sort.Search(len(r.pairs), func(i int) bool { return r.pairs[i].k >= e.ENRKey() }) | ||||||
|
|
||||||
| if i < len(r.pairs) && r.pairs[i].k == e.ENRKey() { | ||||||
| pairs := make([]pair, len(r.pairs)) | ||||||
| copy(pairs, r.pairs) | ||||||
| i := sort.Search(len(pairs), func(i int) bool { return pairs[i].k >= e.ENRKey() }) | ||||||
| switch { | ||||||
| case i < len(pairs) && pairs[i].k == e.ENRKey(): | ||||||
| // element is present at r.pairs[i] | ||||||
| r.pairs[i].v = blob | ||||||
| return | ||||||
| } else if i < len(r.pairs) { | ||||||
| pairs[i].v = blob | ||||||
| case i < len(r.pairs): | ||||||
| // insert pair before i-th elem | ||||||
| el := pair{e.ENRKey(), blob} | ||||||
| r.pairs = append(r.pairs, pair{}) | ||||||
| copy(r.pairs[i+1:], r.pairs[i:]) | ||||||
| r.pairs[i] = el | ||||||
| return | ||||||
| pairs = append(pairs, pair{}) | ||||||
| copy(pairs[i+1:], pairs[i:]) | ||||||
| pairs[i] = el | ||||||
| default: | ||||||
| // element should be placed at the end of r.pairs | ||||||
| pairs = append(pairs, pair{e.ENRKey(), blob}) | ||||||
| } | ||||||
| r.pairs = pairs | ||||||
| } | ||||||
|
|
||||||
| // element should be placed at the end of r.pairs | ||||||
| r.pairs = append(r.pairs, pair{e.ENRKey(), blob}) | ||||||
| func (r *Record) invalidate() { | ||||||
| if r.signature == nil { | ||||||
| r.seq++ | ||||||
| } | ||||||
| r.signature = nil | ||||||
| r.raw = nil | ||||||
| } | ||||||
|
|
||||||
| // EncodeRLP implements rlp.Encoder. Encoding fails if | ||||||
|
|
@@ -196,96 +200,79 @@ func (r *Record) DecodeRLP(s *rlp.Stream) error { | |||||
| return err | ||||||
| } | ||||||
|
|
||||||
| // Verify signature. | ||||||
| if err = dec.verifySignature(); err != nil { | ||||||
| _, scheme := dec.idScheme() | ||||||
| if scheme == nil { | ||||||
| return errNoID | ||||||
| } | ||||||
| if err := scheme.Verify(&dec, dec.signature); err != nil { | ||||||
| return err | ||||||
| } | ||||||
| *r = dec | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| type s256raw []byte | ||||||
|
|
||||||
| func (s256raw) ENRKey() string { return "secp256k1" } | ||||||
|
|
||||||
| // NodeAddr returns the node address. The return value will be nil if the record is | ||||||
| // unsigned. | ||||||
| func (r *Record) NodeAddr() []byte { | ||||||
| var entry s256raw | ||||||
| if r.Load(&entry) != nil { | ||||||
| _, scheme := r.idScheme() | ||||||
| if scheme == nil { | ||||||
| return nil | ||||||
| } | ||||||
| return crypto.Keccak256(entry) | ||||||
| return scheme.NodeAddr(r) | ||||||
| } | ||||||
|
|
||||||
| // Sign signs the record with the given private key. It updates the record's identity | ||||||
| // scheme, public key and increments the sequence number. Sign returns an error if the | ||||||
| // encoded record is larger than the size limit. | ||||||
| func (r *Record) Sign(privkey *ecdsa.PrivateKey) error { | ||||||
| r.seq = r.seq + 1 | ||||||
| r.Set(ID_SECP256k1_KECCAK) | ||||||
| r.Set(Secp256k1(privkey.PublicKey)) | ||||||
| return r.signAndEncode(privkey) | ||||||
| // SetSig sets the record signature. It returns an error if the encoded record is larger | ||||||
| // than the size limit or if the signature is invalid according to the passed scheme. | ||||||
| func (r *Record) SetSig(idscheme string, sig []byte) error { | ||||||
| // Check that "id" is set and matches the given scheme. This panics because | ||||||
| // inconsitencies here are always implementation bugs in the signing function calling | ||||||
|
||||||
| // inconsitencies here are always implementation bugs in the signing function calling | |
| // inconsistencies here are always implementation bugs in the signing function calling |
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.
The comment for Set states that, "If the record is signed, Set increments the sequence number and invalidates the sequence number", but the actual behavior in invalidate is to increment seq only when r.signature is nil (i.e. when the record is not signed) and to clear the signature/raw fields. This is confusing for users of Record and makes it unclear whether seq is meant to advance on updates to already-signed records or only while building an unsigned record. Please either adjust the invalidate logic so that seq is incremented when the record previously had a signature, or update the comment to accurately describe the current semantics (and clarify that Set invalidates the signature/encoded form, not the sequence number).