mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 09:23:48 +00:00
p2p/enr: remove redundant rlp encoding
This commit is contained in:
parent
74eda001aa
commit
a2572e399a
4 changed files with 12 additions and 50 deletions
|
|
@ -16,26 +16,8 @@
|
|||
|
||||
package enr
|
||||
|
||||
import (
|
||||
"io"
|
||||
type DiscPort uint16
|
||||
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
type DiscV5 uint32
|
||||
|
||||
func (DiscV5) ENRKey() string {
|
||||
func (DiscPort) ENRKey() string {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ type Key interface {
|
|||
}
|
||||
|
||||
type pair struct {
|
||||
k []byte
|
||||
k string
|
||||
v []byte
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ func (r *Record) SetSeq(s uint32) {
|
|||
|
||||
func (r *Record) Load(k Key) (bool, error) {
|
||||
for _, p := range r.pairs {
|
||||
if string(p.k) == k.ENRKey() {
|
||||
if p.k == k.ENRKey() {
|
||||
err := rlp.DecodeBytes(p.v, k)
|
||||
return true, err
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ func (r *Record) Set(k Key) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.pairs = append(r.pairs, pair{[]byte(k.ENRKey()), blob})
|
||||
r.pairs = append(r.pairs, pair{k.ENRKey(), blob})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -128,7 +128,7 @@ func (r *Record) DecodeRLP(s *rlp.Stream) error {
|
|||
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 {
|
||||
|
|
@ -201,9 +201,7 @@ func (r *Record) NodeAddr() ([]byte, error) {
|
|||
func (r *Record) Sign(privkey *ecdsa.PrivateKey) error {
|
||||
r.seq = r.seq + 1
|
||||
|
||||
id := ID(ID_SECP256k1_KECCAK)
|
||||
|
||||
r.Set(id)
|
||||
r.Set(ID(ID_SECP256k1_KECCAK))
|
||||
|
||||
pk := (*btcec.PublicKey)(&privkey.PublicKey).SerializeCompressed()
|
||||
secp256k1 := Secp256k1(pk)
|
||||
|
|
@ -214,7 +212,7 @@ func (r *Record) Sign(privkey *ecdsa.PrivateKey) error {
|
|||
|
||||
func (r *Record) serialisedContent() ([]byte, error) {
|
||||
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}
|
||||
|
|
|
|||
|
|
@ -83,12 +83,12 @@ func TestGetSetIP6(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGetSetDiscv5(t *testing.T) {
|
||||
port := DiscV5(30309)
|
||||
func TestGetSetDiscPort(t *testing.T) {
|
||||
port := DiscPort(30309)
|
||||
var r Record
|
||||
r.Set(port)
|
||||
|
||||
var port2 DiscV5
|
||||
var port2 DiscPort
|
||||
|
||||
_, err := r.Load(&port2)
|
||||
if err != nil {
|
||||
|
|
@ -157,7 +157,7 @@ func TestSignEncodeAndDecode(t *testing.T) {
|
|||
}
|
||||
|
||||
var r Record
|
||||
port := DiscV5(30303)
|
||||
port := DiscPort(30303)
|
||||
r.Set(port)
|
||||
|
||||
ipv4 := IP4(net.ParseIP("127.0.0.1"))
|
||||
|
|
|
|||
|
|
@ -16,26 +16,8 @@
|
|||
|
||||
package enr
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
type ID string
|
||||
|
||||
func (ID) ENRKey() string {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue