swarm/swap: addressed PR comments

This commit is contained in:
Fabio Barone 2018-11-08 08:37:47 -05:00
parent 7eeb79ebba
commit 7750577184
4 changed files with 23 additions and 14 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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)

View file

@ -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