mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
swarm/swap: addressed PR comments
This commit is contained in:
parent
7eeb79ebba
commit
7750577184
4 changed files with 23 additions and 14 deletions
|
|
@ -734,6 +734,9 @@ func (c *clientParams) clientCreated() {
|
||||||
close(c.clientCreatedC)
|
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 {
|
func (r *Registry) GetSpec() *protocols.Spec {
|
||||||
return r.spec
|
return r.spec
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,15 @@ type Swap struct {
|
||||||
balances map[enode.ID]int64 //map of balances for each peer
|
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
|
//Swap implements the protocols.Balance interface
|
||||||
//Add is the (sole) accounting function
|
//Add is the (sole) accounting function
|
||||||
func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
|
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
|
s.balances[peer.ID()] += amount
|
||||||
//save the new balance to the state store
|
//save the new balance to the state store
|
||||||
peerBalance := s.balances[peer.ID()]
|
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)))
|
log.Debug(fmt.Sprintf("balance for peer %s: %s", peer.ID().String(), strconv.FormatInt(peerBalance, 10)))
|
||||||
return err
|
return err
|
||||||
|
|
@ -77,14 +86,5 @@ func (s *Swap) loadState(peer *protocols.Peer) {
|
||||||
s.stateStore.Get(peerID.String(), &peerBalance)
|
s.stateStore.Get(peerID.String(), &peerBalance)
|
||||||
s.balances[peerID] = 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
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
mrand "math/rand"
|
mrand "math/rand"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
|
@ -38,6 +39,7 @@ var (
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
mrand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
log.PrintOrigins(true)
|
log.PrintOrigins(true)
|
||||||
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(*loglevel), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(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
|
//try restoring a balance from state store
|
||||||
//this is simulated by creating a node,
|
//this is simulated by creating a node,
|
||||||
//assigning it an arbitrary balance,
|
//assigning it an arbitrary balance,
|
||||||
//send a message (triggers to save to store),
|
//then closing the state store.
|
||||||
//then create a different SwapPeer instance with same peerID,
|
//Then we re-open the state store and check that
|
||||||
//which will try to load a balance from the stateStore
|
//the balance is still the same
|
||||||
func TestRestoreBalanceFromStateStore(t *testing.T) {
|
func TestRestoreBalanceFromStateStore(t *testing.T) {
|
||||||
//create a test swap account
|
//create a test swap account
|
||||||
swap, testDir := createTestSwap(t)
|
swap, testDir := createTestSwap(t)
|
||||||
|
|
|
||||||
|
|
@ -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
|
self.netStore.NewNetFetcherFunc = network.NewFetcherFactory(delivery.RequestFromPeers, config.DeliverySkipCheck).New
|
||||||
|
|
||||||
if config.SwapEnabled {
|
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
|
var nodeID enode.ID
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue