diff options
author | lash <nolash@users.noreply.github.com> | 2019-03-15 18:27:17 +0800 |
---|---|---|
committer | Viktor TrĂ³n <viktor.tron@gmail.com> | 2019-03-15 18:27:17 +0800 |
commit | 4b4f03ca3788e16bc40737376f593623ac3f2cd8 (patch) | |
tree | 32876a858821d03529d2375b4184be1e07e03455 /swarm/network/enr.go | |
parent | df488975bd4bc514a01a3157d52dafd0118a4a05 (diff) | |
download | go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.tar go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.tar.gz go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.tar.bz2 go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.tar.lz go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.tar.xz go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.tar.zst go-tangerine-4b4f03ca3788e16bc40737376f593623ac3f2cd8.zip |
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 load
Diffstat (limited to 'swarm/network/enr.go')
-rw-r--r-- | swarm/network/enr.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/swarm/network/enr.go b/swarm/network/enr.go new file mode 100644 index 000000000..6d7fa2ae4 --- /dev/null +++ b/swarm/network/enr.go @@ -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()), + } +} |