go-ethereum/swarm/network/forwarding.go
zelig 03b9ae7cf6 SWORM - swarm poc 0.2 homestead RC1
synced up to go-ethereum 1.5.0-unstable develop branch

major features:
* api overhaul due to rpc v2 and node service stack interface changes
* blockchain/ethereum contract interaction rewritten using abi/abigen (chequebook, ens)
* swarm - cluster control CLI - migration and revamp of prehistoric eth-utils repo
* poor man's end to end testing: scripted scenarios in swarm/test using swarm CLI
* http proxy now handles 3 url schemes for 1) ens-enabled [bzz], 2) immutable [bzzi] and 3) raw manifest [bzzr] resolution
* fixes issues with remote address setting, forwarding and syncing
* new control flags to switch swap and sync on and off
* placeholder basic implementation Ethereum Name Service

regression:
* uri based versioning support is dropped temporarily since state tree pruning does not guarantee historical record
* registrar related functionality temporarily restricted - current ENS provides basic free and unrestricted Register/Resolve

accounts/abi:
  * bind: repeated attempt deployment of contracts, validation against known code, transactor creation from private keys
  * accountmanager: getUnlocked snatch private key when unlocked

cmd:
  * unlockAccount  moved to utils/cmd   and exported
  * getPassPhrase  moved to utils/input and exported
  * accountcmds: reflect the change
  * js: GlobalRegistrar is dropped (ens)

flags:
  * chequebook, bzzaccount,  bzzport, bzzconfig, bzznoswap, bzznosync

chequebook:
  * move from common/ to swarm/services
  * abigen-ised
  * specifies its own API (removed chequebook api from swarm/api)

kademlia:
  * move from common to swarm/network/kademlia
  * address abstracted out to separate file + tests

dns/ens/registrar:
  * moved from swarm/api to swarm/services/ens
  * implementation is basic placeholder before ENS is implemented
  * temporary rpc api via ens namespace
  * the old common/registrar is removed (also from eth/backend apis)

swap:
  * the abstract swap module moved from common to swarm/services
  * now embedded in the swarm and chequebook specific setup (this will change)
  * safer chequebook deployment using abigen helpers

eth:
  * public accessor for GPO, needed to construct a PublicBlockChainAPI
  * extends ContractBackend in eth/bind.go with BalanceAt, GetTxReceipt and CodeAt API calls

internal/web3ext
  * add js bindings for bzz, chequebook rpc apis

swarm/api:
  * refactored api into smaller modules filesystem/storage/testapi
  * ethereum backend (needed for dns, swap, etc) moved to abi/bind

swarm/api/http:
  * now supports the 3 uri schemes
  * examples/album updated

swarm/cmd:
  * migrate old eth-utils and modify into a cluster control CLI
  * bzzup now allows non-local gateway, endpoint specified as second argument

swarm/network:
  * forwarder improved log messages, fixup condition on whether syncer is nil
  * hive extended with controls for testing support block read/write, swap/sync enabled/disabled
  * hive keepAlive launches with alarm in case no discover and no kaddb
  * fix IP address formatting issue [::1] -> became ::1 which refused to dial, now use discover.NewNode#String
  * integrate functionality for enabling/disabling sync and swap
  * allow nil sync state - improve syncer interface in protocol

swarm/test
  * poor man's testing framework. scripts invoking swarm/cmd/swarm
  * added tests for basic scenarios connections, swap, sync

swarm:
  * rewrite api using rpc v2
  * blockchain comms via abi/abigen + eth.ContractBackend
  * integrate new flags
2016-05-10 09:21:01 +01:00

134 lines
3.8 KiB
Go

package network
import (
"math/rand"
"time"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/swarm/storage"
)
const requesterCount = 3
/*
forwarder implements the CloudStore interface (use by storage.NetStore)
and serves as the cloud store backend orchestrating storage/retrieval/delivery
via the native bzz protocol
which uses an MSB logarithmic distance-based semi-permanent Kademlia table for
* recursive forwarding style routing for retrieval
* smart syncronisation
*/
type forwarder struct {
hive *Hive
}
func NewForwarder(hive *Hive) *forwarder {
return &forwarder{hive: hive}
}
// generate a unique id uint64
func generateId() uint64 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return uint64(r.Int63())
}
var searchTimeout = 3 * time.Second
// forwarding logic
// logic propagating retrieve requests to peers given by the kademlia hive
func (self *forwarder) Retrieve(chunk *storage.Chunk) {
peers := self.hive.getPeers(chunk.Key, 0)
glog.V(logger.Detail).Infof("[BZZ] forwarder.Retrieve: %v - received %d peers from KΛÐΞMLIΛ...", chunk.Key.Log(), len(peers))
OUT:
for _, p := range peers {
glog.V(logger.Detail).Infof("[BZZ] forwarder.Retrieve: sending retrieveRequest %v to peer [%v]", chunk.Key.Log(), p)
for _, recipients := range chunk.Req.Requesters {
for _, recipient := range recipients {
req := recipient.(*retrieveRequestMsgData)
if req.from.Addr() == p.Addr() {
continue OUT
}
}
}
req := &retrieveRequestMsgData{
Key: chunk.Key,
Id: generateId(),
}
var err error
if p.swap != nil {
err = p.swap.Add(-1)
}
if err == nil {
p.retrieve(req)
break OUT
}
glog.V(logger.Warn).Infof("[BZZ] forwarder.Retrieve: unable to send retrieveRequest to peer [%v]: %v", chunk.Key.Log(), err)
}
}
// requests to specific peers given by the kademlia hive
// except for peers that the store request came from (if any)
// delivery queueing taken care of by syncer
func (self *forwarder) Store(chunk *storage.Chunk) {
var n int
msg := &storeRequestMsgData{
Key: chunk.Key,
SData: chunk.SData,
}
var source *peer
if chunk.Source != nil {
source = chunk.Source.(*peer)
}
for _, p := range self.hive.getPeers(chunk.Key, 0) {
glog.V(logger.Detail).Infof("[BZZ] forwarder.Store: %v %v", p, chunk)
if p.syncer != nil && (source == nil || p.Addr() != source.Addr()) {
n++
Deliver(p, msg, PropagateReq)
}
}
glog.V(logger.Detail).Infof("[BZZ] forwarder.Store: sent to %v peers (chunk = %v)", n, chunk)
}
// once a chunk is found deliver it to its requesters unless timed out
func (self *forwarder) Deliver(chunk *storage.Chunk) {
// iterate over request entries
for id, requesters := range chunk.Req.Requesters {
counter := requesterCount
msg := &storeRequestMsgData{
Key: chunk.Key,
SData: chunk.SData,
}
var n int
var req *retrieveRequestMsgData
// iterate over requesters with the same id
for id, r := range requesters {
req = r.(*retrieveRequestMsgData)
if req.timeout == nil || req.timeout.After(time.Now()) {
glog.V(logger.Detail).Infof("[BZZ] forwarder.Deliver: %v -> %v", req.Id, req.from)
msg.Id = uint64(id)
Deliver(req.from, msg, DeliverReq)
n++
counter--
if counter <= 0 {
break
}
}
}
glog.V(logger.Detail).Infof("[BZZ] forwarder.Deliver: submit chunk %v (request id %v) for delivery to %v peers", chunk.Key.Log(), id, n)
}
}
// initiate delivery of a chunk to a particular peer via syncer#addRequest
// depending on syncer mode and priority settings and sync request type
// this either goes via confirmation roundtrip or queued or pushed directly
func Deliver(p *peer, req interface{}, ty int) {
p.syncer.addRequest(req, ty)
}
// push chunk over to peer
func Push(p *peer, key storage.Key, priority uint) {
p.syncer.doDelivery(key, priority, p.syncer.quit)
}