diff --git a/swarm/network/stream/common_test.go b/swarm/network/stream/common_test.go
index 72fdb2bd96..7b536db3fd 100644
--- a/swarm/network/stream/common_test.go
+++ b/swarm/network/stream/common_test.go
@@ -114,7 +114,7 @@ func newStreamerTester(t *testing.T, registryOptions *RegistryOptions) (*p2ptest
delivery := NewDelivery(to, netStore)
netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, true).New
- streamer := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), registryOptions)
+ streamer := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), registryOptions, nil)
teardown := func() {
streamer.Close()
removeDataDir()
diff --git a/swarm/network/stream/delivery_test.go b/swarm/network/stream/delivery_test.go
index c77682e0e1..0431751358 100644
--- a/swarm/network/stream/delivery_test.go
+++ b/swarm/network/stream/delivery_test.go
@@ -481,7 +481,7 @@ func testDeliveryFromNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
SkipCheck: skipCheck,
Syncing: SyncingDisabled,
Retrieval: RetrievalEnabled,
- })
+ }, nil)
bucket.Store(bucketKeyRegistry, r)
fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams())
@@ -656,7 +656,7 @@ func benchmarkDeliveryFromNodes(b *testing.B, nodes, conns, chunkCount int, skip
Syncing: SyncingDisabled,
Retrieval: RetrievalDisabled,
SyncUpdateDelay: 0,
- })
+ }, nil)
fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams())
bucket.Store(bucketKeyFileStore, fileStore)
diff --git a/swarm/network/stream/intervals_test.go b/swarm/network/stream/intervals_test.go
index 0c95fabb70..38accd5ebf 100644
--- a/swarm/network/stream/intervals_test.go
+++ b/swarm/network/stream/intervals_test.go
@@ -86,7 +86,7 @@ func testIntervals(t *testing.T, live bool, history *Range, skipCheck bool) {
Retrieval: RetrievalDisabled,
Syncing: SyncingRegisterOnly,
SkipCheck: skipCheck,
- })
+ }, nil)
bucket.Store(bucketKeyRegistry, r)
r.RegisterClientFunc(externalStreamName, func(p *Peer, t string, live bool) (Client, error) {
diff --git a/swarm/network/stream/snapshot_retrieval_test.go b/swarm/network/stream/snapshot_retrieval_test.go
index ad15193414..5ea0b1511b 100644
--- a/swarm/network/stream/snapshot_retrieval_test.go
+++ b/swarm/network/stream/snapshot_retrieval_test.go
@@ -130,7 +130,7 @@ func retrievalStreamerFunc(ctx *adapters.ServiceContext, bucket *sync.Map) (s no
Retrieval: RetrievalEnabled,
Syncing: SyncingAutoSubscribe,
SyncUpdateDelay: 3 * time.Second,
- })
+ }, nil)
fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams())
bucket.Store(bucketKeyFileStore, fileStore)
diff --git a/swarm/network/stream/snapshot_sync_test.go b/swarm/network/stream/snapshot_sync_test.go
index 2ddbed9368..fc37a0cd1c 100644
--- a/swarm/network/stream/snapshot_sync_test.go
+++ b/swarm/network/stream/snapshot_sync_test.go
@@ -168,7 +168,7 @@ func streamerFunc(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Servic
Retrieval: RetrievalDisabled,
Syncing: SyncingAutoSubscribe,
SyncUpdateDelay: 3 * time.Second,
- })
+ }, nil)
bucket.Store(bucketKeyRegistry, r)
@@ -362,7 +362,7 @@ func testSyncingViaDirectSubscribe(t *testing.T, chunkCount int, nodeCount int)
r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{
Retrieval: RetrievalDisabled,
Syncing: SyncingRegisterOnly,
- })
+ }, nil)
bucket.Store(bucketKeyRegistry, r)
fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams())
diff --git a/swarm/network/stream/stream.go b/swarm/network/stream/stream.go
index 695ff0c502..26b74b155c 100644
--- a/swarm/network/stream/stream.go
+++ b/swarm/network/stream/stream.go
@@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"math"
+ "reflect"
"sync"
"time"
@@ -87,6 +88,9 @@ type Registry struct {
intervalsStore state.Store
autoRetrieval bool //automatically subscribe to retrieve request stream
maxPeerServers int
+ balance protocols.Balance //implements protocols.Balance, for accounting
+ prices protocols.Prices //implements protocols.Prices, provides prices to accounting
+ spec *protocols.Spec //this protocol's spec
}
// RegistryOptions holds optional values for NewRegistry constructor.
@@ -99,7 +103,7 @@ type RegistryOptions struct {
}
// NewRegistry is Streamer constructor
-func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.SyncChunkStore, intervalsStore state.Store, options *RegistryOptions) *Registry {
+func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.SyncChunkStore, intervalsStore state.Store, options *RegistryOptions, balance protocols.Balance) *Registry {
if options == nil {
options = &RegistryOptions{}
}
@@ -119,7 +123,10 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
intervalsStore: intervalsStore,
autoRetrieval: retrieval,
maxPeerServers: options.MaxPeerServers,
+ balance: balance,
}
+ streamer.setupSpec()
+
streamer.api = NewAPI(streamer)
delivery.getPeer = streamer.getPeer
@@ -228,6 +235,21 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
return streamer
}
+//This is an accounted protocol, therefore we need to provide a pricing Hook to the spec
+//For simulations to be able to run multiple nodes and not override the hook's balance,
+//we need to construct a spec instance per node instance
+func (r *Registry) setupSpec() {
+ //first create the "bare" spec
+ r.createSpec()
+ //now create the pricing object
+ r.createPriceOracle()
+ //if balance is nil, this node has been started without swap support (swapEnabled flag is false)
+ if r.balance != nil && !reflect.ValueOf(r.balance).IsNil() {
+ //swap is enabled, so setup the hook
+ r.spec.Hook = protocols.NewAccounting(r.balance, r.prices)
+ }
+}
+
// RegisterClient registers an incoming streamer constructor
func (r *Registry) RegisterClientFunc(stream string, f func(*Peer, string, bool) (Client, error)) {
r.clientMu.Lock()
@@ -492,7 +514,7 @@ func (r *Registry) updateSyncing() {
}
func (r *Registry) runProtocol(p *p2p.Peer, rw p2p.MsgReadWriter) error {
- peer := protocols.NewPeer(p, rw, Spec)
+ peer := protocols.NewPeer(p, rw, r.spec)
bp := network.NewBzzPeer(peer)
np := network.NewPeer(bp, r.delivery.kad)
r.delivery.kad.On(np)
@@ -716,32 +738,74 @@ func (c *clientParams) clientCreated() {
close(c.clientCreatedC)
}
-// Spec is the spec of the streamer protocol
-var Spec = &protocols.Spec{
- Name: "stream",
- Version: 8,
- MaxMsgSize: 10 * 1024 * 1024,
- Messages: []interface{}{
- UnsubscribeMsg{},
- OfferedHashesMsg{},
- WantedHashesMsg{},
- TakeoverProofMsg{},
- SubscribeMsg{},
- RetrieveRequestMsg{},
- ChunkDeliveryMsgRetrieval{},
- SubscribeErrorMsg{},
- RequestSubscriptionMsg{},
- QuitMsg{},
- ChunkDeliveryMsgSyncing{},
- },
+func (r *Registry) GetSpec() *protocols.Spec {
+ return r.spec
+}
+
+func (r *Registry) createSpec() {
+
+ // Spec is the spec of the streamer protocol
+ var spec = &protocols.Spec{
+ Name: "stream",
+ Version: 7,
+ MaxMsgSize: 10 * 1024 * 1024,
+ Messages: []interface{}{
+ UnsubscribeMsg{},
+ OfferedHashesMsg{},
+ WantedHashesMsg{},
+ TakeoverProofMsg{},
+ SubscribeMsg{},
+ RetrieveRequestMsg{},
+ ChunkDeliveryMsgRetrieval{},
+ SubscribeErrorMsg{},
+ RequestSubscriptionMsg{},
+ QuitMsg{},
+ ChunkDeliveryMsgSyncing{},
+ },
+ }
+ r.spec = spec
+}
+
+//An accountable message needs some meta information attached to it
+//in order to evaluate the correct price
+type StreamerPrices struct {
+ priceMatrix map[reflect.Type]*protocols.Price
+ registry *Registry
+}
+
+func (spo *StreamerPrices) Price(msg interface{}) *protocols.Price {
+ typ := reflect.TypeOf(msg).Elem()
+ return spo.priceMatrix[typ]
+}
+
+func (r *Registry) createPriceOracle() {
+
+ po := &StreamerPrices{
+ registry: r,
+ }
+ po.priceMatrix = map[reflect.Type]*protocols.Price{
+
+ reflect.TypeOf(ChunkDeliveryMsgRetrieval{}): &protocols.Price{
+ Value: uint64(100),
+ PerByte: true,
+ Payer: protocols.Receiver,
+ },
+
+ reflect.TypeOf(RetrieveRequestMsg{}): &protocols.Price{
+ Value: uint64(10),
+ PerByte: false,
+ Payer: protocols.Sender,
+ },
+ }
+ r.prices = po
}
func (r *Registry) Protocols() []p2p.Protocol {
return []p2p.Protocol{
{
- Name: Spec.Name,
- Version: Spec.Version,
- Length: Spec.Length(),
+ Name: r.spec.Name,
+ Version: r.spec.Version,
+ Length: r.spec.Length(),
Run: r.runProtocol,
// NodeInfo: ,
// PeerInfo: ,
diff --git a/swarm/network/stream/syncer_test.go b/swarm/network/stream/syncer_test.go
index b0e35b0db3..13ae962c47 100644
--- a/swarm/network/stream/syncer_test.go
+++ b/swarm/network/stream/syncer_test.go
@@ -120,7 +120,7 @@ func testSyncBetweenNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
Retrieval: RetrievalDisabled,
Syncing: SyncingAutoSubscribe,
SkipCheck: skipCheck,
- })
+ }, nil)
fileStore := storage.NewFileStore(netStore, storage.NewFileStoreParams())
bucket.Store(bucketKeyFileStore, fileStore)
diff --git a/swarm/swap/swap.go b/swarm/swap/swap.go
new file mode 100644
index 0000000000..e610e1483b
--- /dev/null
+++ b/swarm/swap/swap.go
@@ -0,0 +1,90 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package swap
+
+import (
+ "errors"
+ "fmt"
+ "strconv"
+ "sync"
+
+ "github.com/ethereum/go-ethereum/p2p/enode"
+ "github.com/ethereum/go-ethereum/p2p/protocols"
+ "github.com/ethereum/go-ethereum/swarm/log"
+ "github.com/ethereum/go-ethereum/swarm/state"
+)
+
+// SwAP Swarm Accounting Protocol
+// a peer to peer micropayment system
+// A node maintains an individual balance with every peer
+// Only messages which have a price will be accounted for
+type Swap struct {
+ stateStore state.Store //stateStore is needed in order to keep balances across sessions
+ lock sync.RWMutex //lock the balances
+ balances map[enode.ID]int64 //map of balances for each peer
+}
+
+//Swap implements the protocols.Balance interface
+//Add is the (sole) accounting function
+func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
+ s.lock.Lock()
+ defer s.lock.Unlock()
+
+ //load existing balances from the state store
+ s.loadState(peer)
+ //adjust the balance
+ //if amount is negative, it will decrease, otherwise increase
+ s.balances[peer.ID()] += amount
+ //save the new balance to the state store
+ peerBalance := s.balances[peer.ID()]
+ s.stateStore.Put(peer.ID().String(), &peerBalance)
+
+ log.Debug(fmt.Sprintf("balance for peer %s: %s", peer.ID().String(), strconv.FormatInt(peerBalance, 10)))
+ return err
+}
+
+//Get a peer's balance
+func (swap *Swap) GetPeerBalance(peer enode.ID) (int64, error) {
+ swap.lock.RLock()
+ defer swap.lock.RUnlock()
+ if p, ok := swap.balances[peer]; ok {
+ return p, nil
+ }
+ return 0, errors.New("Peer not found")
+}
+
+//load balances from the state store (persisted)
+func (s *Swap) loadState(peer *protocols.Peer) {
+ var peerBalance int64
+ peerID := peer.ID()
+ //only load if the current instance doesn't already have this peer's
+ //balance in memory
+ if _, ok := s.balances[peerID]; !ok {
+ s.stateStore.Get(peerID.String(), &peerBalance)
+ s.balances[peerID] = peerBalance
+ }
+}
+
+// New - swap constructor
+func New(stateStore state.Store) (swap *Swap) {
+
+ swap = &Swap{
+ stateStore: stateStore,
+ balances: make(map[enode.ID]int64),
+ }
+ return
+}
diff --git a/swarm/swap/swap_test.go b/swarm/swap/swap_test.go
new file mode 100644
index 0000000000..d1b48a2f4f
--- /dev/null
+++ b/swarm/swap/swap_test.go
@@ -0,0 +1,154 @@
+// Copyreeeeight 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package swap
+
+import (
+ "flag"
+ "fmt"
+ "io/ioutil"
+ mrand "math/rand"
+ "os"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/p2p/protocols"
+ "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
+ "github.com/ethereum/go-ethereum/swarm/state"
+ colorable "github.com/mattn/go-colorable"
+)
+
+var (
+ loglevel = flag.Int("loglevel", 2, "verbosity of logs")
+)
+
+func init() {
+ flag.Parse()
+
+ log.PrintOrigins(true)
+ log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true))))
+}
+
+//Test that repeated bookings do correct accounting
+func TestRepeatedBookings(t *testing.T) {
+ //create a test swap account
+ swap, testDir := createTestSwap(t)
+ defer os.RemoveAll(testDir)
+
+ testPeer := newDummyPeer()
+ amount := mrand.Intn(100)
+ cnt := 1 + mrand.Intn(10)
+ for i := 0; i < cnt; i++ {
+ swap.Add(int64(amount), testPeer.Peer)
+ }
+ expectedBalance := int64(cnt * amount)
+ realBalance := swap.balances[testPeer.ID()]
+ if expectedBalance != realBalance {
+ t.Fatal(fmt.Sprintf("After %d credits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance))
+ }
+
+ testPeer2 := newDummyPeer()
+ amount = mrand.Intn(100)
+ cnt = 1 + mrand.Intn(10)
+ for i := 0; i < cnt; i++ {
+ swap.Add(0-int64(amount), testPeer2.Peer)
+ }
+ expectedBalance = int64(0 - (cnt * amount))
+ realBalance = swap.balances[testPeer2.ID()]
+ if expectedBalance != realBalance {
+ t.Fatal(fmt.Sprintf("After %d debits of %d, expected balance to be: %d, but is: %d", cnt, amount, expectedBalance, realBalance))
+ }
+
+ //mixed debits and credits
+ amount1 := mrand.Intn(100)
+ amount2 := mrand.Intn(55)
+ amount3 := mrand.Intn(999)
+ swap.Add(int64(amount1), testPeer2.Peer)
+ swap.Add(int64(0-amount2), testPeer2.Peer)
+ swap.Add(int64(0-amount3), testPeer2.Peer)
+
+ expectedBalance = expectedBalance + int64(amount1-amount2-amount3)
+ realBalance = swap.balances[testPeer2.ID()]
+
+ if expectedBalance != realBalance {
+ t.Fatal(fmt.Sprintf("After mixed debits and credits, expected balance to be: %d, but is: %d", expectedBalance, realBalance))
+ }
+}
+
+//try restoring a balance from state store
+//this is simulated by creating a node,
+//assigning it an arbitrary balance,
+//send a message (triggers to save to store),
+//then create a different SwapPeer instance with same peerID,
+//which will try to load a balance from the stateStore
+func TestRestoreBalanceFromStateStore(t *testing.T) {
+ //create a test swap account
+ swap, testDir := createTestSwap(t)
+ defer os.RemoveAll(testDir)
+
+ testPeer := newDummyPeer()
+ swap.balances[testPeer.ID()] = -8888
+
+ tmpBalance := swap.balances[testPeer.ID()]
+ swap.stateStore.Put(testPeer.ID().String(), &tmpBalance)
+
+ swap.stateStore.Close()
+ swap.stateStore = nil
+
+ stateStore, err := state.NewDBStore(testDir)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ var newBalance int64
+ stateStore.Get(testPeer.ID().String(), &newBalance)
+
+ //compare the balances
+ if tmpBalance != newBalance {
+ t.Fatal(fmt.Sprintf("Unexpected balance value after sending cheap message test. Expected balance: %d, balance is: %d",
+ tmpBalance, newBalance))
+ }
+}
+
+//create a test swap account
+//creates a stateStore for persistence and a Swap account
+func createTestSwap(t *testing.T) (*Swap, string) {
+ dir, err := ioutil.TempDir("", "swap_test_store")
+ if err != nil {
+ t.Fatal(err)
+ }
+ stateStore, err2 := state.NewDBStore(dir)
+ if err2 != nil {
+ t.Fatal(err2)
+ }
+ swap := New(stateStore)
+ return swap, dir
+}
+
+type dummyPeer struct {
+ *protocols.Peer
+}
+
+//creates a dummy protocols.Peer with dummy MsgReadWriter
+func newDummyPeer() *dummyPeer {
+ id := adapters.RandomNodeConfig().ID
+ protoPeer := protocols.NewPeer(p2p.NewPeer(id, "testPeer", nil), nil, nil)
+ dummy := &dummyPeer{
+ Peer: protoPeer,
+ }
+ return dummy
+}
diff --git a/swarm/swap_test.go b/swarm/swap_test.go
new file mode 100644
index 0000000000..8a3faee10b
--- /dev/null
+++ b/swarm/swap_test.go
@@ -0,0 +1,429 @@
+// Copyright 2018 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Lesser General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// The go-ethereum library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the go-ethereum library. If not, see .
+
+package swarm
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io/ioutil"
+ "math/rand"
+ "os"
+ "strconv"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/metrics"
+ "github.com/ethereum/go-ethereum/node"
+ "github.com/ethereum/go-ethereum/p2p/enode"
+ "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
+ "github.com/ethereum/go-ethereum/swarm/api"
+ "github.com/ethereum/go-ethereum/swarm/log"
+ "github.com/ethereum/go-ethereum/swarm/network/simulation"
+ "github.com/ethereum/go-ethereum/swarm/storage"
+)
+
+//In TestSwapNetworkSymmetricFileUpload we set up a network with arbitrary number of nodes
+//(16), and each of the nodes uploads a file of same size
+//Afterwards we check that every node's balance WITH ANOTHER PEER
+//has the same value but opposite sign
+func TestSwapNetworkSymmetricFileUpload(t *testing.T) {
+ //default hardcoded network size
+ nodeCount := 16
+
+ //setup the simulation
+ //use a complete node setup via `NewSwam`
+ sim := simulation.New(map[string]simulation.ServiceFunc{
+ "swarm": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) {
+ config := api.NewConfig()
+ config.Port = strconv.Itoa(8500 + rand.Intn(9999))
+
+ dir, err := ioutil.TempDir("", "swap-network-test-node"+config.Port)
+ if err != nil {
+ return nil, nil, err
+ }
+ cleanup = func() {
+ err := os.RemoveAll(dir)
+ if err != nil {
+ log.Error("cleaning up swarm temp dir", "err", err)
+ }
+ }
+
+ config.Path = dir
+
+ privkey, err := crypto.GenerateKey()
+ if err != nil {
+ return nil, cleanup, err
+ }
+
+ config.Init(privkey)
+
+ //set Swap to be enabled for this test
+ config.SwapEnabled = true
+
+ swarm, err := NewSwarm(config, nil)
+ if err != nil {
+ return nil, cleanup, err
+ }
+
+ bucket.Store(bucketKeySwarm, swarm)
+ log.Info("new swarm", "bzzKey", config.BzzKey, "baseAddr", fmt.Sprintf("%x", swarm.bzz.BaseAddr()))
+ return swarm, cleanup, nil
+ },
+ })
+ defer sim.Close()
+
+ ctx := context.Background()
+ files := make([]file, 0)
+
+ var checkStatusM sync.Map
+ var nodeStatusM sync.Map
+ var totalFoundCount uint64
+
+ //connect all nodes in a chain
+ _, err := sim.AddNodesAndConnectChain(nodeCount)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ //run the simulation
+ result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error {
+ //wait for kademlia to be healthy
+ if _, err := sim.WaitTillHealthy(ctx, 2); err != nil {
+ return err
+ }
+
+ nodeIDs := sim.UpNodeIDs()
+ shuffle(len(nodeIDs), func(i, j int) {
+ nodeIDs[i], nodeIDs[j] = nodeIDs[j], nodeIDs[i]
+ })
+ //upload a file for every node
+ for _, id := range nodeIDs {
+ item, ok := sim.NodeItem(id, bucketKeySwarm)
+ if !ok {
+ log.Error("No swarm")
+ return errors.New("No swarm")
+ }
+ swarm := item.(*Swarm)
+ key, data, err := uploadFile(swarm)
+ if err != nil {
+ return err
+ }
+ log.Trace("file uploaded", "node", id, "key", key.String())
+ files = append(files, file{
+ addr: key,
+ data: data,
+ nodeID: id,
+ })
+ }
+
+ // File retrieval check is repeated until all uploaded files are retrieved from all nodes
+ // or until the timeout is reached.
+ for {
+ if retrieve(sim, files, &checkStatusM, &nodeStatusM, &totalFoundCount) == 0 {
+ break
+ }
+ }
+
+ time.Sleep(5 * time.Second)
+ //every node has a map to all nodes it had interactions
+ //each entry in the map is a map of the other node with all the balances
+ balancesMap := make(map[enode.ID]map[enode.ID]int64)
+
+ //iterate all nodes
+ for _, node := range sim.NodeIDs() {
+ item, ok := sim.NodeItem(node, bucketKeySwarm)
+ if !ok {
+ log.Error("No swarm")
+ return errors.New("No swarm")
+ }
+ swarm := item.(*Swarm)
+
+ //submap for each node is a map of all nodes with the balance for that node
+ subBalances := make(map[enode.ID]int64)
+
+ //iterate all nodes again...
+ //get all balances with other peers for every node
+ for _, n := range sim.NodeIDs() {
+ if node == n {
+ continue
+ }
+
+ //get the peer's balance with this node
+ balance, err := swarm.swap.GetPeerBalance(n)
+ if err == nil {
+ subBalances[n] = balance
+ log.Debug(fmt.Sprintf("Balance of node %s to node %s: %d", node.TerminalString(), n.TerminalString(), balance))
+ } else {
+ log.Debug(fmt.Sprintf("Node %s has no balance with node %s", node.TerminalString(), n.TerminalString()))
+ }
+ }
+ //update the map for this node
+ balancesMap[node] = subBalances
+ }
+
+ //print all the balances if requested
+ if *printStats {
+ for k, v := range balancesMap {
+ fmt.Println(fmt.Sprintf("node %s balances:", k.TerminalString()))
+ for kk, vv := range v {
+ fmt.Println(fmt.Sprintf(".........with node %s: balance %d", kk.TerminalString(), vv))
+ }
+ }
+ //NOTE: this are currently metrics over ALL nodes, not per node
+ fmt.Println(fmt.Sprintf("Total units credited: %d", metrics.Get("account.balance.credit").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Total units debited: %d", metrics.Get("account.balance.debit").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Total bytes credited: %d", metrics.Get("account.bytes.credit").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Total bytes debited: %d", metrics.Get("account.bytes.debit").(metrics.Counter).Count()))
+ //fmt.Println(fmt.Sprintf("Cheques issued: %d", metrics.Get("account.cheques.issued").(metrics.Counter).Count()))
+ //fmt.Println(fmt.Sprintf("Cheques received: %d", metrics.Get("account.cheques.received").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Number of messages credited: %d", metrics.Get("account.msg.credit").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Number of messages debited: %d", metrics.Get("account.msg.debit").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Peers dropped: %d", metrics.Get("account.peerdrops").(metrics.Counter).Count()))
+ fmt.Println(fmt.Sprintf("Number of times node dropped itself: %d", metrics.Get("account.selfdrops").(metrics.Counter).Count()))
+ }
+
+ //now iterate the whole map
+ //and check that every node k has the same
+ //balance with a peer as that peer with the node,
+ //but in inverted signs
+
+ //iterate the map
+ success := true
+ for k, mapForK := range balancesMap {
+ //iterate the submap
+ for n, balanceKwithN := range mapForK {
+ //iterate the main map again
+ for subK, mapForSubK := range balancesMap {
+ //if the node and the peer are the same...
+ if n == subK {
+ log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN))
+ log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k]))
+ //...check that they have the same balance in Abs terms and that it is not 0
+ if balanceKwithN+mapForSubK[k] != 0 && balanceKwithN != 0 {
+ log.Error(fmt.Sprintf("Expected balances to be a+b = 0 AND balance(a) != 0, but they are not, balance k with n: %d, balance n with k: %d", balanceKwithN, mapForSubK[k]))
+ success = false
+ }
+ }
+ }
+ }
+ }
+ if success {
+ return nil
+ }
+ return errors.New("some conditions could not be met")
+ })
+
+ if result.Error != nil {
+ t.Fatal(result.Error)
+ }
+ log.Debug("test terminated")
+}
+
+//TestSwapNetworkAsymmetricFileUpload is a swap test too,
+//but this time the number and size of files are random
+func TestSwapNetworkAsymmetricFileUpload(t *testing.T) {
+ nodeCount := 16
+
+ sim := simulation.New(map[string]simulation.ServiceFunc{
+ "swarm": func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Service, cleanup func(), err error) {
+ config := api.NewConfig()
+ config.Port = strconv.Itoa(8500 + rand.Intn(9999))
+
+ dir, err := ioutil.TempDir("", "swap-network-test-node"+config.Port)
+ if err != nil {
+ return nil, nil, err
+ }
+ cleanup = func() {
+ err := os.RemoveAll(dir)
+ if err != nil {
+ log.Error("cleaning up swarm temp dir", "err", err)
+ }
+ }
+
+ config.Path = dir
+
+ privkey, err := crypto.GenerateKey()
+ if err != nil {
+ return nil, cleanup, err
+ }
+
+ config.Init(privkey)
+ //enable swap
+ config.SwapEnabled = true
+
+ swarm, err := NewSwarm(config, nil)
+ if err != nil {
+ return nil, cleanup, err
+ }
+ bucket.Store(bucketKeySwarm, swarm)
+ log.Info("new swarm", "bzzKey", config.BzzKey, "baseAddr", fmt.Sprintf("%x", swarm.bzz.BaseAddr()))
+ return swarm, cleanup, nil
+ },
+ })
+ defer sim.Close()
+
+ ctx := context.Background()
+ files := make([]file, 0)
+
+ var checkStatusM sync.Map
+ var nodeStatusM sync.Map
+ var totalFoundCount uint64
+
+ _, err := sim.AddNodesAndConnectChain(nodeCount)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ //NOTE: maxFileSize is 4 kB, this in order to provide faster tests
+ //it would be interesting to run these tests with bigger files
+ //(to see how drop limits are affected etc.)
+ const maxFileSize = 1024 * 4 //1024 bytes * 4 = 4kB
+ const minfileSize = 1024
+
+ //pseudo random algo to define if a node will upload or not
+ //if a bit is 0, do not upload
+ pseudoRandomNum := rand.Int63()
+ pseudoRandomBitMask := strconv.FormatInt(pseudoRandomNum, 2)
+
+ result := sim.Run(ctx, func(ctx context.Context, sim *simulation.Simulation) error {
+ if _, err := sim.WaitTillHealthy(ctx, 2); err != nil {
+ return err
+ }
+
+ nodeIDs := sim.UpNodeIDs()
+ shuffle(len(nodeIDs), func(i, j int) {
+ nodeIDs[i], nodeIDs[j] = nodeIDs[j], nodeIDs[i]
+ })
+ for i, id := range nodeIDs {
+ //if the position in random num is 0, don't upload
+ if string(pseudoRandomBitMask[i]) != "0" {
+ size := rand.Intn(maxFileSize-minfileSize) + minfileSize
+ key, data, err := uploadRandomFileSize(sim.Service("swarm", id).(*Swarm), size)
+ if err != nil {
+ return err
+ }
+ log.Trace("file uploaded", "node", id, "key", key.String())
+ files = append(files, file{
+ addr: key,
+ data: data,
+ nodeID: id,
+ })
+ }
+ }
+
+ // File retrieval check is repeated until all uploaded files are retrieved from all nodes
+ // or until the timeout is reached.
+ for {
+ if retrieve(sim, files, &checkStatusM, &nodeStatusM, &totalFoundCount) == 0 {
+ break
+ }
+ }
+
+ time.Sleep(5 * time.Second)
+
+ balancesMap := make(map[enode.ID]map[enode.ID]int64)
+
+ for _, node := range sim.NodeIDs() {
+ item, ok := sim.NodeItem(node, bucketKeySwarm)
+ if !ok {
+ log.Error("No swarm")
+ return errors.New("no swarm")
+ }
+ swarm := item.(*Swarm)
+
+ subBalances := make(map[enode.ID]int64)
+
+ for _, n := range sim.NodeIDs() {
+ if node == n {
+ continue
+ }
+ balance, err := swarm.swap.GetPeerBalance(n)
+ if err == nil {
+ subBalances[n] = balance
+ log.Debug(fmt.Sprintf("Balance of node %s to node %s: %d", node.TerminalString(), n.TerminalString(), balance))
+ } else {
+ log.Debug(fmt.Sprintf("Node %s has no balance with node %s", node.TerminalString(), n.TerminalString()))
+ }
+ }
+ balancesMap[node] = subBalances
+ }
+
+ if *printStats {
+ for k, v := range balancesMap {
+ fmt.Println(fmt.Sprintf("node %s balances:", k.TerminalString()))
+ for kk, vv := range v {
+ fmt.Println(fmt.Sprintf(".........with node %s: balance %d", kk.TerminalString(), vv))
+ }
+ }
+ }
+
+ /*
+ Assuming that in this case, balances should be symmetric too I
+ */
+
+ success := true
+ for k, mapForK := range balancesMap {
+ for n, balanceKwithN := range mapForK {
+ for subK, mapForSubK := range balancesMap {
+ if n == subK {
+ log.Trace(fmt.Sprintf("balance of %s with %s: %d", k.TerminalString(), n.TerminalString(), balanceKwithN))
+ log.Trace(fmt.Sprintf("balance of %s with %s: %d", n.TerminalString(), k.TerminalString(), mapForSubK[k]))
+ if balanceKwithN+mapForSubK[k] != 0 && balanceKwithN != 0 {
+ log.Error(fmt.Sprintf("Expected balances to be a+b = 0 AND balance(a) != 0, but they are not, balance k with n: %d, balance n with k: %d", balanceKwithN, mapForSubK[k]))
+ success = false
+ }
+ }
+ }
+ }
+ }
+
+ if success {
+ return nil
+ }
+
+ return errors.New("some conditions could not be met")
+ })
+
+ if result.Error != nil {
+ t.Fatal(result.Error)
+ }
+ log.Debug("test terminated")
+}
+
+// uploadFile, uploads a short file to the swarm instance
+// using the api.Put method.
+func uploadRandomFileSize(swarm *Swarm, size int) (storage.Address, string, error) {
+ b := make([]byte, size)
+ _, err := rand.Read(b)
+ if err != nil {
+ return nil, "", err
+ }
+ // uniqueness is very certain.
+ data := fmt.Sprintf("test content %s %x", time.Now().Round(0), b)
+ ctx := context.TODO()
+ k, wait, err := swarm.api.Put(ctx, data, "text/plain", false)
+ if err != nil {
+ return nil, "", err
+ }
+ if wait != nil {
+ err = wait(ctx)
+ }
+ return k, data, err
+}
diff --git a/swarm/swarm.go b/swarm/swarm.go
index 1fb5443fd4..a128b5ac8b 100644
--- a/swarm/swarm.go
+++ b/swarm/swarm.go
@@ -78,6 +78,7 @@ type Swarm struct {
netStore *storage.NetStore
sfs *fuse.SwarmFS // need this to cleanup all the active mounts on node exit
ps *pss.Pss
+ swap *swap.Swap
tracerClose io.Closer
}
@@ -171,6 +172,10 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
delivery := stream.NewDelivery(to, self.netStore)
self.netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, config.DeliverySkipCheck).New
+ if config.SwapEnabled {
+ self.swap = swap.New(stateStore)
+ }
+
var nodeID enode.ID
if err := nodeID.UnmarshalText([]byte(config.NodeID)); err != nil {
return nil, err
@@ -193,7 +198,11 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
SyncUpdateDelay: config.SyncUpdateDelay,
MaxPeerServers: config.MaxStreamPeerServers,
}
- self.streamer = stream.NewRegistry(nodeID, delivery, self.netStore, stateStore, registryOptions)
+ if config.LightNodeEnabled {
+ registryOptions.DoSync = false
+ registryOptions.DoRetrieve = false
+ }
+ self.streamer = stream.NewRegistry(nodeID, delivery, self.netStore, stateStore, registryOptions, self.swap)
// Swarm Hash Merklised Chunking for Arbitrary-length Document/File storage
self.fileStore = storage.NewFileStore(self.netStore, self.config.FileStoreParams)
@@ -216,7 +225,7 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
log.Debug("Setup local storage")
- self.bzz = network.NewBzz(bzzconfig, to, stateStore, stream.Spec, self.streamer.Run)
+ self.bzz = network.NewBzz(bzzconfig, to, stateStore, self.streamer.GetSpec(), self.streamer.Run)
// Pss = postal service over swarm (devp2p over bzz)
self.ps, err = pss.NewPss(to, config.Pss)
@@ -353,7 +362,7 @@ func (self *Swarm) Start(srv *p2p.Server) error {
newaddr := self.bzz.UpdateLocalAddr([]byte(srv.Self().String()))
log.Info("Updated bzz local addr", "oaddr", fmt.Sprintf("%x", newaddr.OAddr), "uaddr", fmt.Sprintf("%s", newaddr.UAddr))
// set chequebook
- if self.config.SwapEnabled {
+ if self.config.SwapEnabled && self.config.SwapAPI != "" {
ctx := context.Background() // The initial setup has no deadline.
err := self.SetChequebook(ctx)
if err != nil {