mock: protect mockSpendNotifier map by mutex

This commit is contained in:
Johan T. Halseth 2018-06-02 10:02:20 +02:00
parent 9aa55b164e
commit 60d9ae02c7
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -118,6 +118,7 @@ func (m *mockNotfier) RegisterSpendNtfn(outpoint *wire.OutPoint,
type mockSpendNotifier struct {
*mockNotfier
spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail
mtx sync.Mutex
}
func makeMockSpendNotifier() *mockSpendNotifier {
@ -131,6 +132,8 @@ func makeMockSpendNotifier() *mockSpendNotifier {
func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
heightHint uint32, _ bool) (*chainntnfs.SpendEvent, error) {
m.mtx.Lock()
defer m.mtx.Unlock()
spendChan := make(chan *chainntnfs.SpendDetail)
m.spendMap[*outpoint] = append(m.spendMap[*outpoint], spendChan)
@ -145,6 +148,8 @@ func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
// will include the transaction and height provided by the caller.
func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
txn *wire.MsgTx) {
m.mtx.Lock()
defer m.mtx.Unlock()
if spendChans, ok := m.spendMap[*outpoint]; ok {
delete(m.spendMap, *outpoint)