integrate to backend, fix self/remote addr for peers

This commit is contained in:
zelig 2015-05-12 14:33:29 +02:00
parent d5330b0dcd
commit 0fddda3969
2 changed files with 31 additions and 27 deletions

View file

@ -57,7 +57,8 @@ var errorToString = map[int]string{
// bzzProtocol represents the swarm wire protocol // bzzProtocol represents the swarm wire protocol
// instance is running on each peer // instance is running on each peer
type bzzProtocol struct { type bzzProtocol struct {
self *discover.Node self func() *discover.Node
node *discover.Node
netStore *NetStore netStore *NetStore
peer *p2p.Peer peer *p2p.Peer
rw p2p.MsgReadWriter rw p2p.MsgReadWriter
@ -186,7 +187,7 @@ main entrypoint, wrappers starting a server running the bzz protocol
use this constructor to attach the protocol ("class") to server caps use this constructor to attach the protocol ("class") to server caps
the Dev p2p layer then runs the protocol instance on each peer the Dev p2p layer then runs the protocol instance on each peer
*/ */
func BzzProtocol(netStore *NetStore, self *discover.Node) p2p.Protocol { func BzzProtocol(netStore *NetStore, self func() *discover.Node) p2p.Protocol {
return p2p.Protocol{ return p2p.Protocol{
Name: "bzz", Name: "bzz",
Version: Version, Version: Version,
@ -199,10 +200,9 @@ func BzzProtocol(netStore *NetStore, self *discover.Node) p2p.Protocol {
// the main loop that handles incoming messages // the main loop that handles incoming messages
// note RemovePeer in the post-disconnect hook // note RemovePeer in the post-disconnect hook
func runBzzProtocol(netStore *NetStore, selfNode *discover.Node, p *p2p.Peer, rw p2p.MsgReadWriter) (err error) { func runBzzProtocol(netStore *NetStore, selfF func() *discover.Node, p *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
self := &bzzProtocol{ self := &bzzProtocol{
self: selfNode, self: selfF,
netStore: netStore, netStore: netStore,
rw: rw, rw: rw,
peer: p, peer: p,
@ -283,12 +283,12 @@ func (self *bzzProtocol) handle() error {
func (self *bzzProtocol) handleStatus() (err error) { func (self *bzzProtocol) handleStatus() (err error) {
// send precanned status message // send precanned status message
sliceNodeID := self.self.ID sliceNodeID := self.self().ID
handshake := &statusMsgData{ handshake := &statusMsgData{
Version: uint64(Version), Version: uint64(Version),
ID: "honey", ID: "honey",
NodeID: sliceNodeID[:], NodeID: sliceNodeID[:],
Addr: newPeerAddrFromNode(self.self), Addr: newPeerAddrFromNode(self.self()),
NetworkId: uint64(NetworkId), NetworkId: uint64(NetworkId),
Caps: []p2p.Cap{}, Caps: []p2p.Cap{},
} }
@ -327,6 +327,8 @@ func (self *bzzProtocol) handleStatus() (err error) {
glog.V(logger.Info).Infof("Peer is [bzz] capable (%d/%d)\n", status.Version, status.NetworkId) glog.V(logger.Info).Infof("Peer is [bzz] capable (%d/%d)\n", status.Version, status.NetworkId)
self.node = status.Addr.node()
self.netStore.hive.addPeer(peer{bzzProtocol: self}) self.netStore.hive.addPeer(peer{bzzProtocol: self})
return nil return nil
@ -334,11 +336,11 @@ func (self *bzzProtocol) handleStatus() (err error) {
// protocol instance implements kademlia.Node interface (embedded hive.peer) // protocol instance implements kademlia.Node interface (embedded hive.peer)
func (self *bzzProtocol) Addr() (a kademlia.Address) { func (self *bzzProtocol) Addr() (a kademlia.Address) {
return kademlia.Address(self.self.Sha()) return kademlia.Address(self.node.Sha())
} }
func (self *bzzProtocol) Url() string { func (self *bzzProtocol) Url() string {
return self.self.String() return self.node.String()
} }
func (self *bzzProtocol) LastActive() time.Time { func (self *bzzProtocol) LastActive() time.Time {

View file

@ -285,28 +285,12 @@ func New(config *Config) (*Ethereum, error) {
protocols := []p2p.Protocol{eth.protocolManager.SubProtocol} protocols := []p2p.Protocol{eth.protocolManager.SubProtocol}
if config.Bzz {
netStore := bzz.NewNetStore(config.DataDir + "/bzz")
chunker := &bzz.TreeChunker{}
chunker.Init()
dpa := &bzz.DPA{
Chunker: chunker,
ChunkStore: netStore,
}
dpa.Start()
protocols = append(protocols, bzz.BzzProtocol(netStore))
go bzz.StartHttpServer(dpa)
}
if config.Shh {
protocols = append(protocols, eth.whisper.Protocol())
}
eth.net = &p2p.Server{ eth.net = &p2p.Server{
PrivateKey: netprv, PrivateKey: netprv,
Name: config.Name, Name: config.Name,
MaxPeers: config.MaxPeers, MaxPeers: config.MaxPeers,
MaxPendingPeers: config.MaxPendingPeers, MaxPendingPeers: config.MaxPendingPeers,
Protocols: protocols, // Protocols: protocols,
NAT: config.NAT, NAT: config.NAT,
NoDial: !config.Dial, NoDial: !config.Dial,
BootstrapNodes: config.parseBootNodes(), BootstrapNodes: config.parseBootNodes(),
@ -317,6 +301,24 @@ func New(config *Config) (*Ethereum, error) {
if len(config.Port) > 0 { if len(config.Port) > 0 {
eth.net.ListenAddr = ":" + config.Port eth.net.ListenAddr = ":" + config.Port
} }
if config.Bzz {
netStore := bzz.NewNetStore(eth.net.Self().Sha(), path.Join(config.DataDir, "bzz"), path.Join(config.DataDir, "bzzpeers.json"))
chunker := &bzz.TreeChunker{}
chunker.Init()
dpa := &bzz.DPA{
Chunker: chunker,
ChunkStore: netStore,
}
dpa.Start()
protocols = append(protocols, bzz.BzzProtocol(netStore, eth.net.Self))
go bzz.StartHttpServer(dpa)
}
if config.Shh {
protocols = append(protocols, eth.whisper.Protocol())
}
eth.net.Protocols = protocols
vm.Debug = config.VmDebug vm.Debug = config.VmDebug