github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/swarm/network/enr.go (about) 1 package network 2 3 import ( 4 "fmt" 5 "io" 6 7 "github.com/ethereum/go-ethereum/p2p" 8 "github.com/ethereum/go-ethereum/p2p/enode" 9 "github.com/ethereum/go-ethereum/p2p/protocols" 10 "github.com/ethereum/go-ethereum/rlp" 11 "github.com/ethereum/go-ethereum/swarm/log" 12 ) 13 14 // ENRAddrEntry is the entry type to store the bzz key in the enode 15 type ENRAddrEntry struct { 16 data []byte 17 } 18 19 func NewENRAddrEntry(addr []byte) *ENRAddrEntry { 20 return &ENRAddrEntry{ 21 data: addr, 22 } 23 } 24 25 func (b ENRAddrEntry) Address() []byte { 26 return b.data 27 } 28 29 // ENRKey implements enr.Entry 30 func (b ENRAddrEntry) ENRKey() string { 31 return "bzzkey" 32 } 33 34 // EncodeRLP implements rlp.Encoder 35 func (b ENRAddrEntry) EncodeRLP(w io.Writer) error { 36 log.Debug("in encoderlp", "b", b, "p", fmt.Sprintf("%p", &b)) 37 return rlp.Encode(w, &b.data) 38 } 39 40 // DecodeRLP implements rlp.Decoder 41 func (b *ENRAddrEntry) DecodeRLP(s *rlp.Stream) error { 42 byt, err := s.Bytes() 43 if err != nil { 44 return err 45 } 46 b.data = byt 47 log.Debug("in decoderlp", "b", b, "p", fmt.Sprintf("%p", &b)) 48 return nil 49 } 50 51 type ENRLightNodeEntry bool 52 53 func (b ENRLightNodeEntry) ENRKey() string { 54 return "bzzlightnode" 55 } 56 57 type ENRBootNodeEntry bool 58 59 func (b ENRBootNodeEntry) ENRKey() string { 60 return "bzzbootnode" 61 } 62 63 func getENRBzzPeer(p *p2p.Peer, rw p2p.MsgReadWriter, spec *protocols.Spec) *BzzPeer { 64 var lightnode ENRLightNodeEntry 65 var bootnode ENRBootNodeEntry 66 67 // retrieve the ENR Record data 68 record := p.Node().Record() 69 record.Load(&lightnode) 70 record.Load(&bootnode) 71 72 // get the address; separate function as long as we need swarm/network:NewAddr() to call it 73 addr := getENRBzzAddr(p.Node()) 74 75 // build the peer using the retrieved data 76 return &BzzPeer{ 77 Peer: protocols.NewPeer(p, rw, spec), 78 LightNode: bool(lightnode), 79 BzzAddr: addr, 80 } 81 } 82 83 func getENRBzzAddr(nod *enode.Node) *BzzAddr { 84 var addr ENRAddrEntry 85 86 record := nod.Record() 87 record.Load(&addr) 88 89 return &BzzAddr{ 90 OAddr: addr.data, 91 UAddr: []byte(nod.String()), 92 } 93 }