Skip to content

Commit 2d3943b

Browse files
Larry RuaneLarryRuane
authored andcommitted
add required locking (mutex) to GetMempoolTx
1 parent af0f348 commit 2d3943b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

frontend/service.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sort"
1616
"strconv"
1717
"strings"
18+
"sync"
1819
"sync/atomic"
1920
"time"
2021

@@ -27,6 +28,7 @@ type lwdStreamer struct {
2728
cache *common.BlockCache
2829
chainName string
2930
pingEnable bool
31+
mutex sync.Mutex
3032
walletrpc.UnimplementedCompactTxStreamerServer
3133
}
3234

@@ -57,12 +59,15 @@ func checkTaddress(taddr string) error {
5759

5860
// GetLatestBlock returns the height of the best chain, according to zcashd.
5961
func (s *lwdStreamer) GetLatestBlock(ctx context.Context, placeholder *walletrpc.ChainSpec) (*walletrpc.BlockID, error) {
62+
// Lock to ensure we return consistent height and hash
63+
s.mutex.Lock()
64+
defer s.mutex.Unlock()
6065
latestBlock := s.cache.GetLatestHeight()
61-
latestHash := s.cache.GetLatestHash()
6266

6367
if latestBlock == -1 {
6468
return nil, errors.New("cache is empty, server is probably not yet ready")
6569
}
70+
latestHash := s.cache.GetLatestHash()
6671

6772
return &walletrpc.BlockID{Height: uint64(latestBlock), Hash: latestHash}, nil
6873
}
@@ -151,11 +156,10 @@ func (s *lwdStreamer) GetBlock(ctx context.Context, id *walletrpc.BlockID) (*wal
151156
// 'end' inclusively.
152157
func (s *lwdStreamer) GetBlockRange(span *walletrpc.BlockRange, resp walletrpc.CompactTxStreamer_GetBlockRangeServer) error {
153158
blockChan := make(chan *walletrpc.CompactBlock)
154-
errChan := make(chan error)
155159
if span.Start == nil || span.End == nil {
156160
return errors.New("must specify start and end heights")
157161
}
158-
162+
errChan := make(chan error)
159163
go common.GetBlockRange(s.cache, blockChan, errChan, int(span.Start.Height), int(span.End.Height))
160164

161165
for {
@@ -410,6 +414,9 @@ var mempoolList []string
410414
var lastMempool time.Time
411415

412416
func (s *lwdStreamer) GetMempoolTx(exclude *walletrpc.Exclude, resp walletrpc.CompactTxStreamer_GetMempoolTxServer) error {
417+
s.mutex.Lock()
418+
defer s.mutex.Unlock()
419+
413420
if time.Since(lastMempool).Seconds() >= 2 {
414421
lastMempool = time.Now()
415422
// Refresh our copy of the mempool.

0 commit comments

Comments
 (0)