p2p changes in protocol and p2p.peer

This commit is contained in:
zelig 2015-05-27 09:47:42 +01:00
parent 800f9374fd
commit fe3218016c
4 changed files with 70 additions and 30 deletions

38
bzz/manifest.go Normal file
View 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
}

View file

@ -3,7 +3,7 @@ package bzz
/* /*
BZZ implements the bzz wire protocol of swarm BZZ implements the bzz wire protocol of swarm
routing decoded storage and retrieval requests routing decoded storage and retrieval requests
registering peers with the DHT registering peers with the KAD DHT via hive
*/ */
import ( import (
@ -11,9 +11,11 @@ import (
"fmt" "fmt"
"net" "net"
"path" "path"
"strconv"
"time" "time"
"github.com/ethereum/go-ethereum/common/kademlia" "github.com/ethereum/go-ethereum/common/kademlia"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/errs" "github.com/ethereum/go-ethereum/errs"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
@ -64,6 +66,7 @@ type bzzProtocol struct {
node *discover.Node node *discover.Node
netStore *netStore netStore *netStore
peer *p2p.Peer peer *p2p.Peer
key Key
rw p2p.MsgReadWriter rw p2p.MsgReadWriter
errors *errs.Errors errors *errs.Errors
requestDb *LDBDatabase requestDb *LDBDatabase
@ -325,10 +328,14 @@ func (self *bzzProtocol) handleStatus() (err error) {
// send precanned status message // send precanned status message
sliceNodeID := self.netStore.self.ID sliceNodeID := self.netStore.self.ID
handshake := &statusMsgData{ handshake := &statusMsgData{
Version: uint64(Version), Version: uint64(Version),
ID: "honey", ID: "honey",
NodeID: sliceNodeID[:], NodeID: sliceNodeID[:],
Addr: newPeerAddrFromNode(self.netStore.self), Addr: &peerAddr{
ID: sliceNodeID[:],
IP: self.netStore.self.IP,
Port: self.netStore.self.TCP,
},
NetworkId: uint64(NetworkId), NetworkId: uint64(NetworkId),
Caps: []p2p.Cap{}, 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()) 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 { 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 // outgoing messages
@ -415,14 +422,18 @@ func (self *bzzProtocol) retrieve(req *retrieveRequestMsgData) {
} }
} }
func (self *bzzProtocol) key() []byte { func (self *bzzProtocol) addrKey() []byte {
return self.peer.Node().Sha().Bytes()[:] id := self.peer.ID()
if self.key == nil {
self.key = Key(crypto.Sha3(id[:]))
}
return self.key
} }
func (self *bzzProtocol) storeRequestLoop() { func (self *bzzProtocol) storeRequestLoop() {
start := make([]byte, 64) start := make([]byte, 64)
copy(start, self.key()) copy(start, self.addrKey())
key := make([]byte, 64) key := make([]byte, 64)
copy(key, start) copy(key, start)
@ -446,7 +457,7 @@ LOOP:
// dpaLogger.Debugf("checking key: %x <> %x ", key, self.key()) // dpaLogger.Debugf("checking key: %x <> %x ", key, self.key())
// reached the end of this peers range // 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) // dpaLogger.Debugf("reached the end of this peers range: %x", key)
n = 0 n = 0
continue continue
@ -493,7 +504,7 @@ func (self *bzzProtocol) store(req *storeRequestMsgData) {
func (self *bzzProtocol) storeRequest(key Key) { func (self *bzzProtocol) storeRequest(key Key) {
peerKey := make([]byte, 64) peerKey := make([]byte, 64)
copy(peerKey, self.key()) copy(peerKey, self.addrKey())
copy(peerKey[32:], key[:]) copy(peerKey[32:], key[:])
dpaLogger.Debugf("enter store request %x into db", peerKey) dpaLogger.Debugf("enter store request %x into db", peerKey)
self.requestDb.Put(peerKey, []byte{0}) self.requestDb.Put(peerKey, []byte{0})

View file

@ -827,7 +827,8 @@ func (js *jsre) newRegistry(call otto.FunctionCall) otto.Value {
return otto.UndefinedValue() 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` // internal transaction type which will allow us to resend transactions using `eth.resend`

View file

@ -88,16 +88,6 @@ func (p *Peer) LocalAddr() net.Addr {
return p.rw.fd.LocalAddr() 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. // Disconnect terminates the peer connection with the given reason.
// It returns immediately and does not wait until the connection is closed. // It returns immediately and does not wait until the connection is closed.
func (p *Peer) Disconnect(reason DiscReason) { func (p *Peer) Disconnect(reason DiscReason) {