Skip to content

Commit

Permalink
rpcserver: cache the results of getcoinsupply
Browse files Browse the repository at this point in the history
  • Loading branch information
dajohi committed Feb 17, 2016
1 parent 4868ed4 commit 2edc483
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rpcserver.go
Expand Up @@ -2836,6 +2836,15 @@ func handleGetCoinSupply(s *rpcServer, cmd interface{}, closeChan <-chan struct{
Message: "Error getting best block hash",
}
}

s.coinSupplyMtx.Lock()
defer s.coinSupplyMtx.Unlock()

// Return the cached value if the block height hasn't changed.
if s.coinSupplyHeight == tipHeight {
return s.coinSupplyTotal, nil
}

for i := 0; int64(i) < tipHeight+1; i++ {
if i == 0 {
continue
Expand Down Expand Up @@ -2874,6 +2883,10 @@ func handleGetCoinSupply(s *rpcServer, cmd interface{}, closeChan <-chan struct{
supply += (work + stake + tax)
}
}

s.coinSupplyHeight = tipHeight
s.coinSupplyTotal = supply

return supply, nil
}

Expand Down Expand Up @@ -4400,6 +4413,11 @@ type rpcServer struct {
templatePool map[chainhash.Hash]*workStateBlockInfo
helpCacher *helpCacher
quit chan int

// coin supply caching values
coinSupplyMtx sync.Mutex
coinSupplyHeight int64
coinSupplyTotal int64
}

// httpStatusLine returns a response Status-Line (RFC 2616 Section 6.1)
Expand Down

0 comments on commit 2edc483

Please sign in to comment.