diff --git a/swarm/network/stream/stream.go b/swarm/network/stream/stream.go index 03a6b1edb4..62fc5db7f9 100644 --- a/swarm/network/stream/stream.go +++ b/swarm/network/stream/stream.go @@ -734,6 +734,9 @@ func (c *clientParams) clientCreated() { close(c.clientCreatedC) } +//Return the streamer spec to callers +//This used to be a global variable but for simulations with +//multiple nodes its fields (notably the Hook) would be overwritten func (r *Registry) GetSpec() *protocols.Spec { return r.spec } diff --git a/swarm/swap/swap.go b/swarm/swap/swap.go index e610e1483b..5c95288447 100644 --- a/swarm/swap/swap.go +++ b/swarm/swap/swap.go @@ -38,6 +38,15 @@ type Swap struct { balances map[enode.ID]int64 //map of balances for each peer } +// New - swap constructor +func New(stateStore state.Store) (swap *Swap) { + swap = &Swap{ + stateStore: stateStore, + balances: make(map[enode.ID]int64), + } + return +} + //Swap implements the protocols.Balance interface //Add is the (sole) accounting function func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) { @@ -51,7 +60,7 @@ func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) { 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) + err = 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 @@ -77,14 +86,5 @@ func (s *Swap) loadState(peer *protocols.Peer) { 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 index d1b48a2f4f..c9c1445f0d 100644 --- a/swarm/swap/swap_test.go +++ b/swarm/swap/swap_test.go @@ -23,6 +23,7 @@ import ( mrand "math/rand" "os" "testing" + "time" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" @@ -38,6 +39,7 @@ var ( func init() { flag.Parse() + mrand.Seed(time.Now().UnixNano()) log.PrintOrigins(true) log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))) @@ -92,9 +94,9 @@ func TestRepeatedBookings(t *testing.T) { //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 +//then closing the state store. +//Then we re-open the state store and check that +//the balance is still the same func TestRestoreBalanceFromStateStore(t *testing.T) { //create a test swap account swap, testDir := createTestSwap(t) diff --git a/swarm/swarm.go b/swarm/swarm.go index 70cb0898ff..dbc33c1629 100644 --- a/swarm/swarm.go +++ b/swarm/swarm.go @@ -174,7 +174,11 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e self.netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, config.DeliverySkipCheck).New if config.SwapEnabled { - self.swap = swap.New(stateStore) + balancesStore, err := state.NewDBStore(filepath.Join(config.Path, "balances.db")) + if err != nil { + return + } + self.swap = swap.New(balancesStore) } var nodeID enode.ID