-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
status-im/status-mobile#9203 getBalance caching
- Loading branch information
Showing
6 changed files
with
109 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package wallet | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type balanceCache struct { | ||
// cache maps an address to a map of a block number and the balance of this particular address | ||
cache map[common.Address]map[*big.Int]*big.Int | ||
lock sync.RWMutex | ||
} | ||
|
||
func (b *balanceCache) readCachedBalance(account common.Address, blockNumber *big.Int) *big.Int { | ||
b.lock.RLock() | ||
defer b.lock.RUnlock() | ||
|
||
return b.cache[account][blockNumber] | ||
} | ||
|
||
func (b *balanceCache) addBalanceToCache(account common.Address, blockNumber *big.Int, balance *big.Int) { | ||
b.lock.Lock() | ||
defer b.lock.Unlock() | ||
|
||
_, exists := b.cache[account] | ||
if !exists { | ||
b.cache[account] = make(map[*big.Int]*big.Int) | ||
} | ||
b.cache[account][blockNumber] = balance | ||
} | ||
|
||
func (b *balanceCache) BalanceAt(client BalanceReader, ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) { | ||
cachedBalance := b.readCachedBalance(account, blockNumber) | ||
if cachedBalance != nil { | ||
return cachedBalance, nil | ||
} | ||
balance, err := client.BalanceAt(ctx, account, blockNumber) | ||
if err != nil { | ||
return nil, err | ||
} | ||
b.addBalanceToCache(account, blockNumber, balance) | ||
|
||
return balance, nil | ||
} | ||
|
||
func newBalanceCache() *balanceCache { | ||
return &balanceCache{ | ||
cache: make(map[common.Address]map[*big.Int]*big.Int), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters