p2p/protocols: fix race condition

This commit is contained in:
Fabio Barone 2019-03-06 16:43:22 -05:00
parent ce3ea8c9b1
commit d58bae177f

View file

@ -159,8 +159,9 @@ func TestAccountingSimulation(t *testing.T) {
// (n entries in the array will not be filled - // (n entries in the array will not be filled -
// the balance of a node with itself) // the balance of a node with itself)
type matrix struct { type matrix struct {
n int //number of nodes n int //number of nodes
m []int64 //array of balances m []int64 //array of balances
lock sync.RWMutex
} }
// create a new matrix // create a new matrix
@ -177,7 +178,9 @@ func (m *matrix) add(i, j int, v int64) error {
// i * number of nodes + remote node // i * number of nodes + remote node
mi := i*m.n + j mi := i*m.n + j
// register that balance // register that balance
m.lock.Lock()
m.m[mi] += v m.m[mi] += v
m.lock.Unlock()
return nil return nil
} }