mirror of https://github.com/ethereum/go-ethereum
swarm, p2p: Prerequities for ENR replacing handshake (#19275)
* swarm/api, swarm/network, p2p/simulations: Prerequisites for handshake remove * swarm, p2p: Add full sim node configs for protocoltester * swarm/network: Make stream package pass tests * swarm/network: Extract peer and addr types out of protocol file * p2p, swarm: Make p2p/protocols tests pass + rename types.go * swarm/network: Deactivate ExecAdapter test until binary ENR prep * swarm/api: Remove comments * swarm/network: Uncomment bootnode record loadpull/19280/head
parent
df488975bd
commit
4b4f03ca37
@ -0,0 +1,93 @@ |
||||
package network |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
|
||||
"github.com/ethereum/go-ethereum/p2p" |
||||
"github.com/ethereum/go-ethereum/p2p/enode" |
||||
"github.com/ethereum/go-ethereum/p2p/protocols" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
"github.com/ethereum/go-ethereum/swarm/log" |
||||
) |
||||
|
||||
// ENRAddrEntry is the entry type to store the bzz key in the enode
|
||||
type ENRAddrEntry struct { |
||||
data []byte |
||||
} |
||||
|
||||
func NewENRAddrEntry(addr []byte) *ENRAddrEntry { |
||||
return &ENRAddrEntry{ |
||||
data: addr, |
||||
} |
||||
} |
||||
|
||||
func (b ENRAddrEntry) Address() []byte { |
||||
return b.data |
||||
} |
||||
|
||||
// ENRKey implements enr.Entry
|
||||
func (b ENRAddrEntry) ENRKey() string { |
||||
return "bzzkey" |
||||
} |
||||
|
||||
// EncodeRLP implements rlp.Encoder
|
||||
func (b ENRAddrEntry) EncodeRLP(w io.Writer) error { |
||||
log.Debug("in encoderlp", "b", b, "p", fmt.Sprintf("%p", &b)) |
||||
return rlp.Encode(w, &b.data) |
||||
} |
||||
|
||||
// DecodeRLP implements rlp.Decoder
|
||||
func (b *ENRAddrEntry) DecodeRLP(s *rlp.Stream) error { |
||||
byt, err := s.Bytes() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
b.data = byt |
||||
log.Debug("in decoderlp", "b", b, "p", fmt.Sprintf("%p", &b)) |
||||
return nil |
||||
} |
||||
|
||||
type ENRLightNodeEntry bool |
||||
|
||||
func (b ENRLightNodeEntry) ENRKey() string { |
||||
return "bzzlightnode" |
||||
} |
||||
|
||||
type ENRBootNodeEntry bool |
||||
|
||||
func (b ENRBootNodeEntry) ENRKey() string { |
||||
return "bzzbootnode" |
||||
} |
||||
|
||||
func getENRBzzPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *protocols.Spec) *BzzPeer { |
||||
var lightnode ENRLightNodeEntry |
||||
var bootnode ENRBootNodeEntry |
||||
|
||||
// retrieve the ENR Record data
|
||||
record := p.Node().Record() |
||||
record.Load(&lightnode) |
||||
record.Load(&bootnode) |
||||
|
||||
// get the address; separate function as long as we need swarm/network:NewAddr() to call it
|
||||
addr := getENRBzzAddr(p.Node()) |
||||
|
||||
// build the peer using the retrieved data
|
||||
return &BzzPeer{ |
||||
Peer: protocols.NewPeer(p, rw, spec), |
||||
LightNode: bool(lightnode), |
||||
BzzAddr: addr, |
||||
} |
||||
} |
||||
|
||||
func getENRBzzAddr(nod *enode.Node) *BzzAddr { |
||||
var addr ENRAddrEntry |
||||
|
||||
record := nod.Record() |
||||
record.Load(&addr) |
||||
|
||||
return &BzzAddr{ |
||||
OAddr: addr.data, |
||||
UAddr: []byte(nod.String()), |
||||
} |
||||
} |
@ -0,0 +1,70 @@ |
||||
package network |
||||
|
||||
import ( |
||||
"crypto/ecdsa" |
||||
"fmt" |
||||
"net" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/p2p/enode" |
||||
) |
||||
|
||||
// BzzAddr implements the PeerAddr interface
|
||||
type BzzAddr struct { |
||||
OAddr []byte |
||||
UAddr []byte |
||||
} |
||||
|
||||
// Address implements OverlayPeer interface to be used in Overlay.
|
||||
func (a *BzzAddr) Address() []byte { |
||||
return a.OAddr |
||||
} |
||||
|
||||
// Over returns the overlay address.
|
||||
func (a *BzzAddr) Over() []byte { |
||||
return a.OAddr |
||||
} |
||||
|
||||
// Under returns the underlay address.
|
||||
func (a *BzzAddr) Under() []byte { |
||||
return a.UAddr |
||||
} |
||||
|
||||
// ID returns the node identifier in the underlay.
|
||||
func (a *BzzAddr) ID() enode.ID { |
||||
n, err := enode.ParseV4(string(a.UAddr)) |
||||
if err != nil { |
||||
return enode.ID{} |
||||
} |
||||
return n.ID() |
||||
} |
||||
|
||||
// Update updates the underlay address of a peer record
|
||||
func (a *BzzAddr) Update(na *BzzAddr) *BzzAddr { |
||||
return &BzzAddr{a.OAddr, na.UAddr} |
||||
} |
||||
|
||||
// String pretty prints the address
|
||||
func (a *BzzAddr) String() string { |
||||
return fmt.Sprintf("%x <%s>", a.OAddr, a.UAddr) |
||||
} |
||||
|
||||
// RandomAddr is a utility method generating an address from a public key
|
||||
func RandomAddr() *BzzAddr { |
||||
key, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
panic("unable to generate key") |
||||
} |
||||
node := enode.NewV4(&key.PublicKey, net.IP{127, 0, 0, 1}, 30303, 30303) |
||||
return NewAddr(node) |
||||
} |
||||
|
||||
// NewAddr constucts a BzzAddr from a node record.
|
||||
func NewAddr(node *enode.Node) *BzzAddr { |
||||
return &BzzAddr{OAddr: node.ID().Bytes(), UAddr: []byte(node.String())} |
||||
} |
||||
|
||||
func PrivateKeyToBzzKey(prvKey *ecdsa.PrivateKey) []byte { |
||||
pubkeyBytes := crypto.FromECDSAPub(&prvKey.PublicKey) |
||||
return crypto.Keccak256Hash(pubkeyBytes).Bytes() |
||||
} |
Loading…
Reference in new issue