diff --git a/p2p/protocols/accounting.go b/p2p/protocols/accounting.go index 4c3e95e67b..a81fe5c318 100644 --- a/p2p/protocols/accounting.go +++ b/p2p/protocols/accounting.go @@ -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 diff --git a/swarm/network/stream/stream.go b/swarm/network/stream/stream.go index 853d7f1932..ede6cea3a9 100644 --- a/swarm/network/stream/stream.go +++ b/swarm/network/stream/stream.go @@ -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() - r.spec.Hook = protocols.NewAccountingHook(r.balanceMgr, r.priceOracle) + if !reflect.ValueOf(r.balanceMgr).IsNil() { + r.spec.Hook = protocols.NewAccountingHook(r.balanceMgr, r.priceOracle) + } } // RegisterClient registers an incoming streamer constructor diff --git a/swarm/swap_test.go b/swarm/swap_test.go index 882d726f19..755a698c1c 100644 --- a/swarm/swap_test.go +++ b/swarm/swap_test.go @@ -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 } }