Skip to content

Commit

Permalink
_
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed Dec 18, 2019
1 parent 241b201 commit 49171d1
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions services/wallet/concurrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ type TransferDownloader interface {
GetTransfersByNumber(context.Context, *big.Int) ([]Transfer, error)
}

var balancesCache = map[common.Address]map[*big.Int]*big.Int{}
var balanaceMapLock = sync.RWMutex{}
var (
balancesCache = map[common.Address]map[*big.Int]*big.Int{}
balanaceMapLock = sync.RWMutex{}
)

func readCachedBalance(account common.Address, blockNumber *big.Int) (*big.Int, bool) {
balanaceMapLock.RLock()
Expand All @@ -56,13 +58,13 @@ func readCachedBalance(account common.Address, blockNumber *big.Int) (*big.Int,
return balance, exists
}

func writeCachedBalance(account common.Address, blockNumber *big.Int, balance *big.Int) {
func addBalanceToCache(account common.Address, blockNumber *big.Int, balance *big.Int) {
balanaceMapLock.Lock()
defer balanaceMapLock.Unlock()

_, exists := balancesCache[account]
if !exists {
balancesCache[account] = map[*big.Int]*big.Int{}
balancesCache[account] = make(map[*big.Int]*big.Int)
}
balancesCache[account][blockNumber] = balance
}
Expand All @@ -76,7 +78,7 @@ func getBalanceAt(client BalanceReader, ctx context.Context, account common.Addr
if err != nil {
return nil, err
}
writeCachedBalance(account, blockNumber, balance)
addBalanceToCache(account, blockNumber, balance)

return balance, nil
}
Expand Down

0 comments on commit 49171d1

Please sign in to comment.