mock: protect mockSpendNotifier map by mutex
This commit is contained in:
parent
9aa55b164e
commit
60d9ae02c7
5
mock.go
5
mock.go
@ -118,6 +118,7 @@ func (m *mockNotfier) RegisterSpendNtfn(outpoint *wire.OutPoint,
|
|||||||
type mockSpendNotifier struct {
|
type mockSpendNotifier struct {
|
||||||
*mockNotfier
|
*mockNotfier
|
||||||
spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail
|
spendMap map[wire.OutPoint][]chan *chainntnfs.SpendDetail
|
||||||
|
mtx sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeMockSpendNotifier() *mockSpendNotifier {
|
func makeMockSpendNotifier() *mockSpendNotifier {
|
||||||
@ -131,6 +132,8 @@ func makeMockSpendNotifier() *mockSpendNotifier {
|
|||||||
|
|
||||||
func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
|
func (m *mockSpendNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
|
||||||
heightHint uint32, _ bool) (*chainntnfs.SpendEvent, error) {
|
heightHint uint32, _ bool) (*chainntnfs.SpendEvent, error) {
|
||||||
|
m.mtx.Lock()
|
||||||
|
defer m.mtx.Unlock()
|
||||||
|
|
||||||
spendChan := make(chan *chainntnfs.SpendDetail)
|
spendChan := make(chan *chainntnfs.SpendDetail)
|
||||||
m.spendMap[*outpoint] = append(m.spendMap[*outpoint], spendChan)
|
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.
|
// will include the transaction and height provided by the caller.
|
||||||
func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
|
func (m *mockSpendNotifier) Spend(outpoint *wire.OutPoint, height int32,
|
||||||
txn *wire.MsgTx) {
|
txn *wire.MsgTx) {
|
||||||
|
m.mtx.Lock()
|
||||||
|
defer m.mtx.Unlock()
|
||||||
|
|
||||||
if spendChans, ok := m.spendMap[*outpoint]; ok {
|
if spendChans, ok := m.spendMap[*outpoint]; ok {
|
||||||
delete(m.spendMap, *outpoint)
|
delete(m.spendMap, *outpoint)
|
||||||
|
Loading…
Reference in New Issue
Block a user