Skip to content

Commit

Permalink
status-im/status-mobile#9203 getBalance caching
Browse files Browse the repository at this point in the history
  • Loading branch information
rasom committed Jan 2, 2020
1 parent 9f60462 commit 59fc472
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions services/wallet/balance_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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
}
Expand Down
12 changes: 6 additions & 6 deletions services/wallet/concurrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ type TransferDownloader interface {
GetTransfersByNumber(context.Context, *big.Int) ([]Transfer, error)
}

func downloadEthConcurrently(c *ConcurrentDownloader, client BalanceReader, cache *balanceCache, downloader TransferDownloader, account common.Address, low, high *big.Int) {
func downloadEthConcurrently(c *ConcurrentDownloader, client BalanceReader, balanceCache *balanceCache, downloader TransferDownloader, account common.Address, low, high *big.Int) {
c.Add(func(ctx context.Context) error {
if low.Cmp(high) >= 0 {
return nil
}
log.Debug("eth transfers comparing blocks", "low", low, "high", high)
lb, err := cache.BalanceAt(client, ctx, account, low)
lb, err := balanceCache.BalanceAt(client, ctx, account, low)
//lb, err := client.BalanceAt(ctx, account, low)
if err != nil {
return err
}
hb, err := cache.BalanceAt(client, ctx, account, high)
hb, err := balanceCache.BalanceAt(client, ctx, account, high)
//hb, err := client.BalanceAt(ctx, account, high)
if err != nil {
return err
Expand Down Expand Up @@ -93,10 +93,10 @@ func downloadEthConcurrently(c *ConcurrentDownloader, client BalanceReader, cach
}
mid := new(big.Int).Add(low, high)
mid = mid.Div(mid, two)
cache.BalanceAt(client, ctx, account, mid)
balanceCache.BalanceAt(client, ctx, account, mid)
log.Debug("balances are not equal. spawn two concurrent downloaders", "low", low, "mid", mid, "high", high)
downloadEthConcurrently(c, client, cache, downloader, account, low, mid)
downloadEthConcurrently(c, client, cache, downloader, account, mid, high)
downloadEthConcurrently(c, client, balanceCache, downloader, account, low, mid)
downloadEthConcurrently(c, client, balanceCache, downloader, account, mid, high)
return nil
})
}
8 changes: 8 additions & 0 deletions services/wallet/concurrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (f balancesFixture) BalanceAt(ctx context.Context, account common.Address,
return f[index], nil
}

func (f balancesFixture) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
index := int(blockNumber.Int64())
if index > len(f)-1 {
return 0, errors.New("nonce unknown")
}
return 100, nil
}

type batchesFixture [][]Transfer

func (f batchesFixture) GetTransfersByNumber(ctx context.Context, number *big.Int) (rst []Transfer, err error) {
Expand Down

0 comments on commit 59fc472

Please sign in to comment.