mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
p2p/enr: make Set panic for encoding errors
Nobody will ever check the return value of Set. Make it panic to avoid hidden bugs.
This commit is contained in:
parent
260989e77d
commit
f65565edc1
1 changed files with 6 additions and 5 deletions
|
|
@ -83,28 +83,29 @@ func (r *Record) Load(k Key) (bool, error) {
|
||||||
return false, errors.New("record does not exist")
|
return false, errors.New("record does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Record) Set(k Key) error {
|
// Set adds or updates the given key in the record.
|
||||||
|
// It panics if the value can't be encoded.
|
||||||
|
func (r *Record) Set(k Key) {
|
||||||
r.signature = nil
|
r.signature = nil
|
||||||
blob, err := rlp.EncodeToBytes(k)
|
blob, err := rlp.EncodeToBytes(k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
panic(fmt.Errorf("enr: can't encode %s: %v", k.ENRKey(), err))
|
||||||
}
|
}
|
||||||
for i, p := range r.pairs {
|
for i, p := range r.pairs {
|
||||||
if p.k == k.ENRKey() {
|
if p.k == k.ENRKey() {
|
||||||
// replace value of pair
|
// replace value of pair
|
||||||
r.pairs[i].v = blob
|
r.pairs[i].v = blob
|
||||||
return nil
|
return
|
||||||
} else if p.k > k.ENRKey() {
|
} else if p.k > k.ENRKey() {
|
||||||
// insert pair before i-th elem
|
// insert pair before i-th elem
|
||||||
el := pair{k.ENRKey(), blob}
|
el := pair{k.ENRKey(), blob}
|
||||||
r.pairs = append(r.pairs, pair{})
|
r.pairs = append(r.pairs, pair{})
|
||||||
copy(r.pairs[i+1:], r.pairs[i:])
|
copy(r.pairs[i+1:], r.pairs[i:])
|
||||||
r.pairs[i] = el
|
r.pairs[i] = el
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.pairs = append(r.pairs, pair{k.ENRKey(), blob})
|
r.pairs = append(r.pairs, pair{k.ENRKey(), blob})
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Record) EncodeRLP(w io.Writer) error {
|
func (r Record) EncodeRLP(w io.Writer) error {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue