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:
Felix Lange 2017-12-05 21:19:30 +01:00
parent 260989e77d
commit f65565edc1

View file

@ -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 {