|
|
@ -31,13 +31,13 @@ The bzz protocol component speaks the bzz protocol |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"net" |
|
|
|
"net" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/contracts/chequebook" |
|
|
|
"github.com/ethereum/go-ethereum/contracts/chequebook" |
|
|
|
"github.com/ethereum/go-ethereum/errs" |
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
"github.com/ethereum/go-ethereum/log" |
|
|
|
"github.com/ethereum/go-ethereum/p2p" |
|
|
|
"github.com/ethereum/go-ethereum/p2p" |
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover" |
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover" |
|
|
@ -53,32 +53,6 @@ const ( |
|
|
|
NetworkId = 3 |
|
|
|
NetworkId = 3 |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
|
|
ErrMsgTooLarge = iota |
|
|
|
|
|
|
|
ErrDecode |
|
|
|
|
|
|
|
ErrInvalidMsgCode |
|
|
|
|
|
|
|
ErrVersionMismatch |
|
|
|
|
|
|
|
ErrNetworkIdMismatch |
|
|
|
|
|
|
|
ErrNoStatusMsg |
|
|
|
|
|
|
|
ErrExtraStatusMsg |
|
|
|
|
|
|
|
ErrSwap |
|
|
|
|
|
|
|
ErrSync |
|
|
|
|
|
|
|
ErrUnwanted |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var errorToString = map[int]string{ |
|
|
|
|
|
|
|
ErrMsgTooLarge: "Message too long", |
|
|
|
|
|
|
|
ErrDecode: "Invalid message", |
|
|
|
|
|
|
|
ErrInvalidMsgCode: "Invalid message code", |
|
|
|
|
|
|
|
ErrVersionMismatch: "Protocol version mismatch", |
|
|
|
|
|
|
|
ErrNetworkIdMismatch: "NetworkId mismatch", |
|
|
|
|
|
|
|
ErrNoStatusMsg: "No status message", |
|
|
|
|
|
|
|
ErrExtraStatusMsg: "Extra status message", |
|
|
|
|
|
|
|
ErrSwap: "SWAP error", |
|
|
|
|
|
|
|
ErrSync: "Sync error", |
|
|
|
|
|
|
|
ErrUnwanted: "Unwanted peer", |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// bzz represents the swarm wire protocol
|
|
|
|
// bzz represents the swarm wire protocol
|
|
|
|
// an instance is running on each peer
|
|
|
|
// an instance is running on each peer
|
|
|
|
type bzz struct { |
|
|
|
type bzz struct { |
|
|
@ -91,7 +65,6 @@ type bzz struct { |
|
|
|
remoteAddr *peerAddr // remote peers address
|
|
|
|
remoteAddr *peerAddr // remote peers address
|
|
|
|
peer *p2p.Peer // the p2p peer object
|
|
|
|
peer *p2p.Peer // the p2p peer object
|
|
|
|
rw p2p.MsgReadWriter // messageReadWriter to send messages to
|
|
|
|
rw p2p.MsgReadWriter // messageReadWriter to send messages to
|
|
|
|
errors *errs.Errors // errors table
|
|
|
|
|
|
|
|
backend chequebook.Backend |
|
|
|
backend chequebook.Backend |
|
|
|
lastActive time.Time |
|
|
|
lastActive time.Time |
|
|
|
NetworkId uint64 |
|
|
|
NetworkId uint64 |
|
|
@ -162,17 +135,13 @@ the main protocol loop that |
|
|
|
func run(requestDb *storage.LDBDatabase, depo StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess *DbAccess, sp *bzzswap.SwapParams, sy *SyncParams, networkId uint64, p *p2p.Peer, rw p2p.MsgReadWriter) (err error) { |
|
|
|
func run(requestDb *storage.LDBDatabase, depo StorageHandler, backend chequebook.Backend, hive *Hive, dbaccess *DbAccess, sp *bzzswap.SwapParams, sy *SyncParams, networkId uint64, p *p2p.Peer, rw p2p.MsgReadWriter) (err error) { |
|
|
|
|
|
|
|
|
|
|
|
self := &bzz{ |
|
|
|
self := &bzz{ |
|
|
|
storage: depo, |
|
|
|
storage: depo, |
|
|
|
backend: backend, |
|
|
|
backend: backend, |
|
|
|
hive: hive, |
|
|
|
hive: hive, |
|
|
|
dbAccess: dbaccess, |
|
|
|
dbAccess: dbaccess, |
|
|
|
requestDb: requestDb, |
|
|
|
requestDb: requestDb, |
|
|
|
peer: p, |
|
|
|
peer: p, |
|
|
|
rw: rw, |
|
|
|
rw: rw, |
|
|
|
errors: &errs.Errors{ |
|
|
|
|
|
|
|
Package: "BZZ", |
|
|
|
|
|
|
|
Errors: errorToString, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
swapParams: sp, |
|
|
|
swapParams: sp, |
|
|
|
syncParams: sy, |
|
|
|
syncParams: sy, |
|
|
|
swapEnabled: hive.swapEnabled, |
|
|
|
swapEnabled: hive.swapEnabled, |
|
|
@ -225,7 +194,7 @@ func (self *bzz) handle() error { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
if msg.Size > ProtocolMaxMsgSize { |
|
|
|
if msg.Size > ProtocolMaxMsgSize { |
|
|
|
return self.protoError(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) |
|
|
|
return fmt.Errorf("message too long: %v > %v", msg.Size, ProtocolMaxMsgSize) |
|
|
|
} |
|
|
|
} |
|
|
|
// make sure that the payload has been fully consumed
|
|
|
|
// make sure that the payload has been fully consumed
|
|
|
|
defer msg.Discard() |
|
|
|
defer msg.Discard() |
|
|
@ -236,16 +205,16 @@ func (self *bzz) handle() error { |
|
|
|
// no extra status message allowed. The one needed already handled by
|
|
|
|
// no extra status message allowed. The one needed already handled by
|
|
|
|
// handleStatus
|
|
|
|
// handleStatus
|
|
|
|
log.Debug(fmt.Sprintf("Status message: %v", msg)) |
|
|
|
log.Debug(fmt.Sprintf("Status message: %v", msg)) |
|
|
|
return self.protoError(ErrExtraStatusMsg, "") |
|
|
|
return errors.New("extra status message") |
|
|
|
|
|
|
|
|
|
|
|
case storeRequestMsg: |
|
|
|
case storeRequestMsg: |
|
|
|
// store requests are dispatched to netStore
|
|
|
|
// store requests are dispatched to netStore
|
|
|
|
var req storeRequestMsgData |
|
|
|
var req storeRequestMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(req.SData) < 9 { |
|
|
|
if n := len(req.SData); n < 9 { |
|
|
|
return self.protoError(ErrDecode, "<- %v: Data too short (%v)", msg) |
|
|
|
return fmt.Errorf("<- %v: Data too short (%v)", msg, n) |
|
|
|
} |
|
|
|
} |
|
|
|
// last Active time is set only when receiving chunks
|
|
|
|
// last Active time is set only when receiving chunks
|
|
|
|
self.lastActive = time.Now() |
|
|
|
self.lastActive = time.Now() |
|
|
@ -257,14 +226,14 @@ func (self *bzz) handle() error { |
|
|
|
// retrieve Requests are dispatched to netStore
|
|
|
|
// retrieve Requests are dispatched to netStore
|
|
|
|
var req retrieveRequestMsgData |
|
|
|
var req retrieveRequestMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
req.from = &peer{bzz: self} |
|
|
|
req.from = &peer{bzz: self} |
|
|
|
// if request is lookup and not to be delivered
|
|
|
|
// if request is lookup and not to be delivered
|
|
|
|
if req.isLookup() { |
|
|
|
if req.isLookup() { |
|
|
|
log.Trace(fmt.Sprintf("self lookup for %v: responding with peers only...", req.from)) |
|
|
|
log.Trace(fmt.Sprintf("self lookup for %v: responding with peers only...", req.from)) |
|
|
|
} else if req.Key == nil { |
|
|
|
} else if req.Key == nil { |
|
|
|
return self.protoError(ErrDecode, "protocol handler: req.Key == nil || req.Timeout == nil") |
|
|
|
return fmt.Errorf("protocol handler: req.Key == nil || req.Timeout == nil") |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// swap accounting is done within netStore
|
|
|
|
// swap accounting is done within netStore
|
|
|
|
self.storage.HandleRetrieveRequestMsg(&req, &peer{bzz: self}) |
|
|
|
self.storage.HandleRetrieveRequestMsg(&req, &peer{bzz: self}) |
|
|
@ -277,7 +246,7 @@ func (self *bzz) handle() error { |
|
|
|
// dispatches new peer data to the hive that adds them to KADDB
|
|
|
|
// dispatches new peer data to the hive that adds them to KADDB
|
|
|
|
var req peersMsgData |
|
|
|
var req peersMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
req.from = &peer{bzz: self} |
|
|
|
req.from = &peer{bzz: self} |
|
|
|
log.Trace(fmt.Sprintf("<- peer addresses: %v", req)) |
|
|
|
log.Trace(fmt.Sprintf("<- peer addresses: %v", req)) |
|
|
@ -286,7 +255,7 @@ func (self *bzz) handle() error { |
|
|
|
case syncRequestMsg: |
|
|
|
case syncRequestMsg: |
|
|
|
var req syncRequestMsgData |
|
|
|
var req syncRequestMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Debug(fmt.Sprintf("<- sync request: %v", req)) |
|
|
|
log.Debug(fmt.Sprintf("<- sync request: %v", req)) |
|
|
|
self.lastActive = time.Now() |
|
|
|
self.lastActive = time.Now() |
|
|
@ -296,13 +265,13 @@ func (self *bzz) handle() error { |
|
|
|
// coming from parent node offering
|
|
|
|
// coming from parent node offering
|
|
|
|
var req unsyncedKeysMsgData |
|
|
|
var req unsyncedKeysMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Debug(fmt.Sprintf("<- unsynced keys : %s", req.String())) |
|
|
|
log.Debug(fmt.Sprintf("<- unsynced keys : %s", req.String())) |
|
|
|
err := self.storage.HandleUnsyncedKeysMsg(&req, &peer{bzz: self}) |
|
|
|
err := self.storage.HandleUnsyncedKeysMsg(&req, &peer{bzz: self}) |
|
|
|
self.lastActive = time.Now() |
|
|
|
self.lastActive = time.Now() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case deliveryRequestMsg: |
|
|
|
case deliveryRequestMsg: |
|
|
@ -310,13 +279,13 @@ func (self *bzz) handle() error { |
|
|
|
// also relays the last synced state to the source
|
|
|
|
// also relays the last synced state to the source
|
|
|
|
var req deliveryRequestMsgData |
|
|
|
var req deliveryRequestMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<-msg %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<-msg %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Debug(fmt.Sprintf("<- delivery request: %s", req.String())) |
|
|
|
log.Debug(fmt.Sprintf("<- delivery request: %s", req.String())) |
|
|
|
err := self.storage.HandleDeliveryRequestMsg(&req, &peer{bzz: self}) |
|
|
|
err := self.storage.HandleDeliveryRequestMsg(&req, &peer{bzz: self}) |
|
|
|
self.lastActive = time.Now() |
|
|
|
self.lastActive = time.Now() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case paymentMsg: |
|
|
|
case paymentMsg: |
|
|
@ -324,7 +293,7 @@ func (self *bzz) handle() error { |
|
|
|
if self.swapEnabled { |
|
|
|
if self.swapEnabled { |
|
|
|
var req paymentMsgData |
|
|
|
var req paymentMsgData |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
if err := msg.Decode(&req); err != nil { |
|
|
|
return self.protoError(ErrDecode, "<- %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Debug(fmt.Sprintf("<- payment: %s", req.String())) |
|
|
|
log.Debug(fmt.Sprintf("<- payment: %s", req.String())) |
|
|
|
self.swap.Receive(int(req.Units), req.Promise) |
|
|
|
self.swap.Receive(int(req.Units), req.Promise) |
|
|
@ -332,7 +301,7 @@ func (self *bzz) handle() error { |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
// no other message is allowed
|
|
|
|
// no other message is allowed
|
|
|
|
return self.protoError(ErrInvalidMsgCode, "%v", msg.Code) |
|
|
|
return fmt.Errorf("invalid message code: %v", msg.Code) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -352,7 +321,7 @@ func (self *bzz) handleStatus() (err error) { |
|
|
|
|
|
|
|
|
|
|
|
err = p2p.Send(self.rw, statusMsg, handshake) |
|
|
|
err = p2p.Send(self.rw, statusMsg, handshake) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
self.protoError(ErrNoStatusMsg, err.Error()) |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// read and handle remote status
|
|
|
|
// read and handle remote status
|
|
|
@ -363,24 +332,24 @@ func (self *bzz) handleStatus() (err error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if msg.Code != statusMsg { |
|
|
|
if msg.Code != statusMsg { |
|
|
|
self.protoError(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, statusMsg) |
|
|
|
return fmt.Errorf("first msg has code %x (!= %x)", msg.Code, statusMsg) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if msg.Size > ProtocolMaxMsgSize { |
|
|
|
if msg.Size > ProtocolMaxMsgSize { |
|
|
|
return self.protoError(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) |
|
|
|
return fmt.Errorf("message too long: %v > %v", msg.Size, ProtocolMaxMsgSize) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var status statusMsgData |
|
|
|
var status statusMsgData |
|
|
|
if err := msg.Decode(&status); err != nil { |
|
|
|
if err := msg.Decode(&status); err != nil { |
|
|
|
return self.protoError(ErrDecode, " %v: %v", msg, err) |
|
|
|
return fmt.Errorf("<- %v: %v", msg, err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if status.NetworkId != self.NetworkId { |
|
|
|
if status.NetworkId != self.NetworkId { |
|
|
|
return self.protoError(ErrNetworkIdMismatch, "%d (!= %d)", status.NetworkId, self.NetworkId) |
|
|
|
return fmt.Errorf("network id mismatch: %d (!= %d)", status.NetworkId, self.NetworkId) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if Version != status.Version { |
|
|
|
if Version != status.Version { |
|
|
|
return self.protoError(ErrVersionMismatch, "%d (!= %d)", status.Version, Version) |
|
|
|
return fmt.Errorf("protocol version mismatch: %d (!= %d)", status.Version, Version) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.remoteAddr = self.peerAddr(status.Addr) |
|
|
|
self.remoteAddr = self.peerAddr(status.Addr) |
|
|
@ -390,14 +359,14 @@ func (self *bzz) handleStatus() (err error) { |
|
|
|
// set remote profile for accounting
|
|
|
|
// set remote profile for accounting
|
|
|
|
self.swap, err = bzzswap.NewSwap(self.swapParams, status.Swap, self.backend, self) |
|
|
|
self.swap, err = bzzswap.NewSwap(self.swapParams, status.Swap, self.backend, self) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return self.protoError(ErrSwap, "%v", err) |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.Info(fmt.Sprintf("Peer %08x is capable (%d/%d)", self.remoteAddr.Addr[:4], status.Version, status.NetworkId)) |
|
|
|
log.Info(fmt.Sprintf("Peer %08x is capable (%d/%d)", self.remoteAddr.Addr[:4], status.Version, status.NetworkId)) |
|
|
|
err = self.hive.addPeer(&peer{bzz: self}) |
|
|
|
err = self.hive.addPeer(&peer{bzz: self}) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return self.protoError(ErrUnwanted, "%v", err) |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// hive sets syncstate so sync should start after node added
|
|
|
|
// hive sets syncstate so sync should start after node added
|
|
|
@ -410,7 +379,7 @@ func (self *bzz) handleStatus() (err error) { |
|
|
|
func (self *bzz) sync(state *syncState) error { |
|
|
|
func (self *bzz) sync(state *syncState) error { |
|
|
|
// syncer setup
|
|
|
|
// syncer setup
|
|
|
|
if self.syncer != nil { |
|
|
|
if self.syncer != nil { |
|
|
|
return self.protoError(ErrSync, "sync request can only be sent once") |
|
|
|
return errors.New("sync request can only be sent once") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cnt := self.dbAccess.counter() |
|
|
|
cnt := self.dbAccess.counter() |
|
|
@ -440,7 +409,7 @@ func (self *bzz) sync(state *syncState) error { |
|
|
|
self.syncParams, state, func() bool { return self.syncEnabled }, |
|
|
|
self.syncParams, state, func() bool { return self.syncEnabled }, |
|
|
|
) |
|
|
|
) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return self.protoError(ErrSync, "%v", err) |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
log.Trace(fmt.Sprintf("syncer set for peer %v", self)) |
|
|
|
log.Trace(fmt.Sprintf("syncer set for peer %v", self)) |
|
|
|
return nil |
|
|
|
return nil |
|
|
@ -531,12 +500,6 @@ func (self *bzz) peers(req *peersMsgData) error { |
|
|
|
return self.send(peersMsg, req) |
|
|
|
return self.send(peersMsg, req) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *bzz) protoError(code int, format string, params ...interface{}) (err *errs.Error) { |
|
|
|
|
|
|
|
err = self.errors.New(code, format, params...) |
|
|
|
|
|
|
|
log.Info(err.Error()) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self *bzz) send(msg uint64, data interface{}) error { |
|
|
|
func (self *bzz) send(msg uint64, data interface{}) error { |
|
|
|
if self.hive.blockWrite { |
|
|
|
if self.hive.blockWrite { |
|
|
|
return fmt.Errorf("network write blocked") |
|
|
|
return fmt.Errorf("network write blocked") |
|
|
|