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

View file

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

View file

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