swarm/swap: locks and bugfixing

This commit is contained in:
Fabio Barone 2018-10-07 19:58:33 -05:00
parent 8e186d8a46
commit b1c51e7505
3 changed files with 14 additions and 2 deletions

View file

@ -16,6 +16,10 @@
package protocols
import (
"sync"
)
type PriceOracle interface {
Price(uint32, interface{}) (EntryDirection, uint64)
Accountable(interface{}) bool
@ -38,6 +42,7 @@ const (
type AccountingHook struct {
BalanceManager
PriceOracle
lock sync.RWMutex //lock the balances
}
func NewAccountingHook(mgr BalanceManager, po PriceOracle) *AccountingHook {
@ -49,6 +54,8 @@ func NewAccountingHook(mgr BalanceManager, po PriceOracle) *AccountingHook {
}
func (ah *AccountingHook) Send(peer *Peer, size uint32, msg interface{}) error {
ah.lock.Lock()
defer ah.lock.Unlock()
var err error
if !ah.PriceOracle.Accountable(msg) {
return nil
@ -63,6 +70,8 @@ func (ah *AccountingHook) Send(peer *Peer, size uint32, msg interface{}) error {
}
func (ah *AccountingHook) Receive(peer *Peer, size uint32, msg interface{}) error {
ah.lock.Lock()
defer ah.lock.Unlock()
var err error
if !ah.PriceOracle.Accountable(msg) {
return nil

View file

@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math"
"reflect"
"sync"
"time"
@ -193,7 +194,9 @@ func NewRegistry(localID enode.ID, delivery *Delivery, syncChunkStore storage.Sy
func (r *Registry) setupSpec() {
r.createSpec()
r.createPriceOracle()
if !reflect.ValueOf(r.balanceMgr).IsNil() {
r.spec.Hook = protocols.NewAccountingHook(r.balanceMgr, r.priceOracle)
}
}
// RegisterClient registers an incoming streamer constructor

View file

@ -319,7 +319,7 @@ func TestSwapNetworkAsymmetricFileUpload(t *testing.T) {
// or until the timeout is reached.
for {
if retrieve(sim, files, &checkStatusM, &nodeStatusM, &totalFoundCount) == 0 {
return nil
break
}
}