p2p/enr: remove redundant rlp encoding

This commit is contained in:
Anton Evangelatov 2017-12-04 13:02:07 +01:00 committed by Felix Lange
parent 74eda001aa
commit a2572e399a
4 changed files with 12 additions and 50 deletions

View file

@ -16,26 +16,8 @@
package enr package enr
import ( type DiscPort uint16
"io"
"github.com/ethereum/go-ethereum/rlp" func (DiscPort) ENRKey() string {
)
type DiscV5 uint32
func (DiscV5) ENRKey() string {
return "discv5" return "discv5"
} }
func (v DiscV5) EncodeRLP(w io.Writer) error {
port := uint32(v)
return rlp.Encode(w, port)
}
func (v *DiscV5) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode((*uint32)(v)); err != nil {
return err
}
return nil
}

View file

@ -47,7 +47,7 @@ type Key interface {
} }
type pair struct { type pair struct {
k []byte k string
v []byte v []byte
} }
@ -70,7 +70,7 @@ func (r *Record) SetSeq(s uint32) {
func (r *Record) Load(k Key) (bool, error) { func (r *Record) Load(k Key) (bool, error) {
for _, p := range r.pairs { for _, p := range r.pairs {
if string(p.k) == k.ENRKey() { if p.k == k.ENRKey() {
err := rlp.DecodeBytes(p.v, k) err := rlp.DecodeBytes(p.v, k)
return true, err return true, err
} }
@ -85,7 +85,7 @@ func (r *Record) Set(k Key) error {
if err != nil { if err != nil {
return err return err
} }
r.pairs = append(r.pairs, pair{[]byte(k.ENRKey()), blob}) r.pairs = append(r.pairs, pair{k.ENRKey(), blob})
return nil return nil
} }
@ -128,7 +128,7 @@ func (r *Record) DecodeRLP(s *rlp.Stream) error {
return err2 return err2
} }
r.pairs = append(r.pairs, pair{k: key, v: value}) r.pairs = append(r.pairs, pair{k: string(key), v: value})
} }
if err != rlp.EOL { if err != rlp.EOL {
@ -201,9 +201,7 @@ func (r *Record) NodeAddr() ([]byte, error) {
func (r *Record) Sign(privkey *ecdsa.PrivateKey) error { func (r *Record) Sign(privkey *ecdsa.PrivateKey) error {
r.seq = r.seq + 1 r.seq = r.seq + 1
id := ID(ID_SECP256k1_KECCAK) r.Set(ID(ID_SECP256k1_KECCAK))
r.Set(id)
pk := (*btcec.PublicKey)(&privkey.PublicKey).SerializeCompressed() pk := (*btcec.PublicKey)(&privkey.PublicKey).SerializeCompressed()
secp256k1 := Secp256k1(pk) secp256k1 := Secp256k1(pk)
@ -214,7 +212,7 @@ func (r *Record) Sign(privkey *ecdsa.PrivateKey) error {
func (r *Record) serialisedContent() ([]byte, error) { func (r *Record) serialisedContent() ([]byte, error) {
sort.Slice(r.pairs, func(i, j int) bool { sort.Slice(r.pairs, func(i, j int) bool {
return string(r.pairs[i].k) < string(r.pairs[j].k) return r.pairs[i].k < r.pairs[j].k
}) })
list := []interface{}{r.seq} list := []interface{}{r.seq}

View file

@ -83,12 +83,12 @@ func TestGetSetIP6(t *testing.T) {
} }
} }
func TestGetSetDiscv5(t *testing.T) { func TestGetSetDiscPort(t *testing.T) {
port := DiscV5(30309) port := DiscPort(30309)
var r Record var r Record
r.Set(port) r.Set(port)
var port2 DiscV5 var port2 DiscPort
_, err := r.Load(&port2) _, err := r.Load(&port2)
if err != nil { if err != nil {
@ -157,7 +157,7 @@ func TestSignEncodeAndDecode(t *testing.T) {
} }
var r Record var r Record
port := DiscV5(30303) port := DiscPort(30303)
r.Set(port) r.Set(port)
ipv4 := IP4(net.ParseIP("127.0.0.1")) ipv4 := IP4(net.ParseIP("127.0.0.1"))

View file

@ -16,26 +16,8 @@
package enr package enr
import (
"io"
"github.com/ethereum/go-ethereum/rlp"
)
type ID string type ID string
func (ID) ENRKey() string { func (ID) ENRKey() string {
return "id" return "id"
} }
func (v ID) EncodeRLP(w io.Writer) error {
id := string(v)
return rlp.Encode(w, id)
}
func (v *ID) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode((*string)(v)); err != nil {
return err
}
return nil
}