Make fetching balances multi-thread

pull/1144/head
Rongjian Lan 5 years ago
parent 348977d746
commit fdbe27c4fc
  1. 68
      cmd/client/wallet/main.go

@ -10,6 +10,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"path" "path"
"sync"
"time" "time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@ -785,40 +786,47 @@ func FetchBalance(address common.Address) []*AccountState {
result = append(result, nil) result = append(result, nil)
} }
for shardID := 0; shardID < walletProfile.Shards; shardID++ { var wg sync.WaitGroup
balance := big.NewInt(0) wg.Add(walletProfile.Shards)
var nonce uint64
result[uint32(shardID)] = &AccountState{balance, 0}
LOOP:
for j := 0; j < len(walletProfile.RPCServer[shardID]); j++ {
for retry := 0; retry < rpcRetry; retry++ {
server := walletProfile.RPCServer[shardID][j]
client, err := clientService.NewClient(server.IP, server.Port)
if err != nil {
continue
}
log.Debug("FetchBalance", "server", server) for shardID := 0; shardID < walletProfile.Shards; shardID++ {
response, err := client.GetBalance(address) go func(shardID int) {
if err != nil { defer wg.Done()
log.Info("failed to get balance, retrying ...") balance := big.NewInt(0)
time.Sleep(200 * time.Millisecond) var nonce uint64
continue
} result[uint32(shardID)] = &AccountState{balance, 0}
log.Debug("FetchBalance", "response", response)
respBalance := big.NewInt(0) LOOP:
respBalance.SetBytes(response.Balance) for j := 0; j < len(walletProfile.RPCServer[shardID]); j++ {
if balance.Cmp(respBalance) < 0 { for retry := 0; retry < rpcRetry; retry++ {
balance.SetBytes(response.Balance) server := walletProfile.RPCServer[shardID][j]
nonce = response.Nonce client, err := clientService.NewClient(server.IP, server.Port)
if err != nil {
continue
}
log.Debug("FetchBalance", "server", server)
response, err := client.GetBalance(address)
if err != nil {
log.Info("failed to get balance, retrying ...")
time.Sleep(200 * time.Millisecond)
continue
}
log.Debug("FetchBalance", "response", response)
respBalance := big.NewInt(0)
respBalance.SetBytes(response.Balance)
if balance.Cmp(respBalance) < 0 {
balance.SetBytes(response.Balance)
nonce = response.Nonce
}
break LOOP
} }
break LOOP
} }
} result[shardID] = &AccountState{balance, nonce}
result[shardID] = &AccountState{balance, nonce} }(shardID)
} }
wg.Wait()
return result return result
} }

Loading…
Cancel
Save