mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
p2p changes in protocol and p2p.peer
This commit is contained in:
parent
800f9374fd
commit
fe3218016c
4 changed files with 70 additions and 30 deletions
38
bzz/manifest.go
Normal file
38
bzz/manifest.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package bzz
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const (
|
||||
manifestType = "application/bzz-manifest+json"
|
||||
)
|
||||
|
||||
var (
|
||||
hashMatcher = regexp.MustCompile("^/[0-9A-Fa-f]{64}")
|
||||
)
|
||||
|
||||
type manifest struct {
|
||||
Entries []*manifestEntry `"json:entries"`
|
||||
}
|
||||
|
||||
type manifestEntry struct {
|
||||
Path string `"json:path"`
|
||||
Hash string `"json:hash"`
|
||||
ContentType string `"json:contentType"`
|
||||
Status int `"json:status"`
|
||||
}
|
||||
|
||||
func (self *manifest) getEntry(path string) (entry *manifestEntry, pos int) {
|
||||
for _, entry = range self.Entries {
|
||||
pos = len(entry.Path)
|
||||
if len(path) >= pos && path[:pos] == entry.Path {
|
||||
dpaLogger.Debugf("Swarm: \"%s\" matches \"%s\".", path, entry.Path)
|
||||
return
|
||||
}
|
||||
}
|
||||
entry = nil
|
||||
dpaLogger.Debugf("Path '%s' on manifest not found.", path)
|
||||
return
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ package bzz
|
|||
/*
|
||||
BZZ implements the bzz wire protocol of swarm
|
||||
routing decoded storage and retrieval requests
|
||||
registering peers with the DHT
|
||||
registering peers with the KAD DHT via hive
|
||||
*/
|
||||
|
||||
import (
|
||||
|
|
@ -11,9 +11,11 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/kademlia"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/errs"
|
||||
"github.com/ethereum/go-ethereum/logger"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
|
|
@ -64,6 +66,7 @@ type bzzProtocol struct {
|
|||
node *discover.Node
|
||||
netStore *netStore
|
||||
peer *p2p.Peer
|
||||
key Key
|
||||
rw p2p.MsgReadWriter
|
||||
errors *errs.Errors
|
||||
requestDb *LDBDatabase
|
||||
|
|
@ -328,7 +331,11 @@ func (self *bzzProtocol) handleStatus() (err error) {
|
|||
Version: uint64(Version),
|
||||
ID: "honey",
|
||||
NodeID: sliceNodeID[:],
|
||||
Addr: newPeerAddrFromNode(self.netStore.self),
|
||||
Addr: &peerAddr{
|
||||
ID: sliceNodeID[:],
|
||||
IP: self.netStore.self.IP,
|
||||
Port: self.netStore.self.TCP,
|
||||
},
|
||||
NetworkId: uint64(NetworkId),
|
||||
Caps: []p2p.Cap{},
|
||||
}
|
||||
|
|
@ -394,16 +401,16 @@ func (self *bzzProtocol) String() string {
|
|||
return fmt.Sprintf("%4x: %v\n", self.node.Sha().Bytes()[:4], self.Url())
|
||||
}
|
||||
|
||||
func newPeerAddrFromNode(node *discover.Node) *peerAddr {
|
||||
return &peerAddr{
|
||||
ID: node.ID[:],
|
||||
IP: node.IP,
|
||||
Port: node.TCP,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *bzzProtocol) peerAddr() *peerAddr {
|
||||
return newPeerAddrFromNode(self.peer.Node())
|
||||
p := self.peer
|
||||
id := p.ID()
|
||||
host, port, _ := net.SplitHostPort(p.RemoteAddr().String())
|
||||
intport, _ := strconv.Atoi(port)
|
||||
return &peerAddr{
|
||||
ID: id[:],
|
||||
IP: net.ParseIP(host),
|
||||
Port: uint16(intport),
|
||||
}
|
||||
}
|
||||
|
||||
// outgoing messages
|
||||
|
|
@ -415,14 +422,18 @@ func (self *bzzProtocol) retrieve(req *retrieveRequestMsgData) {
|
|||
}
|
||||
}
|
||||
|
||||
func (self *bzzProtocol) key() []byte {
|
||||
return self.peer.Node().Sha().Bytes()[:]
|
||||
func (self *bzzProtocol) addrKey() []byte {
|
||||
id := self.peer.ID()
|
||||
if self.key == nil {
|
||||
self.key = Key(crypto.Sha3(id[:]))
|
||||
}
|
||||
return self.key
|
||||
}
|
||||
|
||||
func (self *bzzProtocol) storeRequestLoop() {
|
||||
|
||||
start := make([]byte, 64)
|
||||
copy(start, self.key())
|
||||
copy(start, self.addrKey())
|
||||
|
||||
key := make([]byte, 64)
|
||||
copy(key, start)
|
||||
|
|
@ -446,7 +457,7 @@ LOOP:
|
|||
// dpaLogger.Debugf("checking key: %x <> %x ", key, self.key())
|
||||
|
||||
// reached the end of this peers range
|
||||
if !bytes.Equal(key[:32], self.key()) {
|
||||
if !bytes.Equal(key[:32], self.addrKey()) {
|
||||
// dpaLogger.Debugf("reached the end of this peers range: %x", key)
|
||||
n = 0
|
||||
continue
|
||||
|
|
@ -493,7 +504,7 @@ func (self *bzzProtocol) store(req *storeRequestMsgData) {
|
|||
|
||||
func (self *bzzProtocol) storeRequest(key Key) {
|
||||
peerKey := make([]byte, 64)
|
||||
copy(peerKey, self.key())
|
||||
copy(peerKey, self.addrKey())
|
||||
copy(peerKey[32:], key[:])
|
||||
dpaLogger.Debugf("enter store request %x into db", peerKey)
|
||||
self.requestDb.Put(peerKey, []byte{0})
|
||||
|
|
|
|||
|
|
@ -827,7 +827,8 @@ func (js *jsre) newRegistry(call otto.FunctionCall) otto.Value {
|
|||
return otto.UndefinedValue()
|
||||
}
|
||||
|
||||
return js.re.ToVal([]string{hashReg, urlHint})
|
||||
v, _ := call.Otto.ToValue([]string{hashReg, urlHint})
|
||||
return v
|
||||
}
|
||||
|
||||
// internal transaction type which will allow us to resend transactions using `eth.resend`
|
||||
|
|
|
|||
10
p2p/peer.go
10
p2p/peer.go
|
|
@ -88,16 +88,6 @@ func (p *Peer) LocalAddr() net.Addr {
|
|||
return p.rw.fd.LocalAddr()
|
||||
}
|
||||
|
||||
// LocalAddr returns the local address of the network connection.
|
||||
func (p *Peer) Node() *discover.Node {
|
||||
return discover.NewNode(
|
||||
p.rw.ID,
|
||||
net.ParseIP(p.conn.RemoteAddr().String()),
|
||||
uint16(p.rw.ListenPort), //
|
||||
uint16(p.rw.ListenPort), //
|
||||
)
|
||||
}
|
||||
|
||||
// Disconnect terminates the peer connection with the given reason.
|
||||
// It returns immediately and does not wait until the connection is closed.
|
||||
func (p *Peer) Disconnect(reason DiscReason) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue