Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/stdevAntiD2ta_t295_Slow_load_tim…
Browse files Browse the repository at this point in the history
…es' into stdevAntiD2ta_t295_Slow_load_times
  • Loading branch information
mauricio1802 committed Feb 14, 2020
2 parents b00cfd2 + 08c2450 commit 9f767b3
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 39 deletions.
69 changes: 40 additions & 29 deletions src/coin/skycoin/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ func (wlt *RemoteWallet) ListPendingTransactions() (core.TransactionIterator, er
return NewSkycoinTransactionIterator(txns), nil
}

func (wlt *LocalWallet) GetBalance(ticker string) (uint64, error) {
func (wlt *LocalWallet) updateBalances() error {
walletName := filepath.Join(wlt.WalletDir, wlt.Id)
log.WithField("walletName", walletName).Info("Calling wallet.Load(walletName)")
walletLoaded, err := wallet.Load(walletName)
if err != nil {
log.WithError(err).Error("wallet.Load(walletName) failed")
return 0, err
return err
}
var addrs []string
addresses := walletLoaded.GetAddresses()
Expand All @@ -213,49 +213,60 @@ func (wlt *LocalWallet) GetBalance(ticker string) (uint64, error) {
c, err := NewSkycoinApiClient(PoolSection)
if err != nil {
log.WithError(err).Error("Couldn't get API client")
return 0, err
return err
}
defer ReturnSkycoinClient(c)
log.Info("POST /api/v1/outputs?addrs=xxx")
outs, err := c.OutputsForAddresses(addrs)
if err != nil {
log.WithError(err).WithField("Length of addrs", len(addrs)).Error("Couldn't POST /api/v1/outputs?addrs=xxx")
return 0, err
return err
}

bl, err := getBalanceOfAddresses(outs, addrs)
if err != nil {
log.WithError(err).Warn("getBalanceOfAddresses(outs, addrs) failed")
return 0, err
return err
}

if ticker == Sky {
flSky, err := strconv.ParseFloat(bl.Confirmed.Coins, 64)
if err != nil {
log.WithError(err).WithField("bl.Confirmed.Coins", bl.Confirmed.Coins).Error("strconv.ParseFloat(bl.Confirmed.Coins, 64) failed")
return 0, err
}
accuracy, err2 := util.AltcoinQuotient(Sky)
if err2 != nil {
log.WithError(err2).WithField("Sky", Sky).Error("util.AltcoinQuotient(Sky) failed")
return 0, err2
}
return uint64(flSky * float64(accuracy)), nil
} else if ticker == CoinHour {
coinHours, err := strconv.ParseFloat(bl.Confirmed.Hours, 64)
if err != nil {
log.WithError(err).WithField("bl.Confirmed.Hours", bl.Confirmed.Hours).Error("strconv.ParseFloat(bl.Confirmed.Hours, 64) failed")
flSky, err := strconv.ParseFloat(bl.Confirmed.Coins, 64)
if err != nil {
log.WithError(err).WithField("bl.Confirmed.Coins", bl.Confirmed.Coins).Error("strconv.ParseFloat(bl.Confirmed.Coins, 64) failed")
return err
}
accuracy, err2 := util.AltcoinQuotient(Sky)
if err2 != nil {
log.WithError(err2).WithField("Sky", Sky).Error("util.AltcoinQuotient(Sky) failed")
return err2
}
wlt.balance.SetCoins(Sky, uint64(flSky*float64(accuracy)))
coinHours, err := strconv.ParseFloat(bl.Confirmed.Hours, 64)
if err != nil {
log.WithError(err).WithField("bl.Confirmed.Hours", bl.Confirmed.Hours).Error("strconv.ParseFloat(bl.Confirmed.Hours, 64) failed")
return err
}
accuracy, err2 = util.AltcoinQuotient(CoinHour)
if err2 != nil {
log.WithError(err2).WithField("CoinHour", CoinHour).Error("util.AltcoinQuotient(CoinHour) failed")
return err2
}
wlt.balance.SetCoins(CoinHour, uint64(coinHours*float64(accuracy)))
return nil
}

func (wlt *LocalWallet) GetBalance(ticker string) (uint64, error) {
if wlt.balance == nil {
wlt.balance = util.NewBalanceSnapshot(0)
}
if !wlt.balance.IsUpdated() {
if err := wlt.updateBalances(); err != nil {
return 0, err
}
accuracy, err2 := util.AltcoinQuotient(CoinHour)
if err2 != nil {
log.WithError(err2).WithField("CoinHour", CoinHour).Error("util.AltcoinQuotient(CoinHour) failed")
return 0, err2
}
return uint64(coinHours * float64(accuracy)), nil
} else {
return 0, errorTickerInvalid{ticker}
}
if coins, err := wlt.balance.GetCoins(ticker); err == nil {
return coins, nil
}
return 0, errorTickerInvalid{ticker}
}

func (wlt *LocalWallet) ListAssets() []string {
Expand Down
3 changes: 2 additions & 1 deletion src/coin/skycoin/models/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func (p *SkyFiberPlugin) LoadTransactionAPI(netType string) (core.BlockchainTran
if netType != "MainNet" {
return nil, errors.ErrInvalidNetworkType
}
return NewSkycoinBlockchain(config.GetDataRefreshTimeout()), nil
refreshTimeOut := config.GetDataRefreshTimeout()
return NewSkycoinBlockchain(refreshTimeOut*(1000000000)), nil
}

// LoadSignService sign service entry point
Expand Down
3 changes: 2 additions & 1 deletion src/coin/skycoin/models/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func (spex *SkycoinPEX) GetTxnPool() (core.TransactionIterator, error) {
}
skycoinTxns := make([]core.Transaction, 0)
for _, txn := range txns {
skycoinTxns = append(skycoinTxns, &SkycoinPendingTransaction{Transaction: &txn})
t := txn
skycoinTxns = append(skycoinTxns, &SkycoinPendingTransaction{Transaction: &t})
}
return NewSkycoinTransactionIterator(skycoinTxns), nil
}
Expand Down
1 change: 1 addition & 0 deletions src/coin/skycoin/models/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ type LocalWallet struct {
Encrypted bool
Type string
WalletDir string
balance *util.BalanceSnapshot
}

func (wlt *LocalWallet) Sign(txn core.Transaction, signer core.TxnSigner, pwd core.PasswordReader, index []string) (signedTxn core.Transaction, err error) {
Expand Down
2 changes: 2 additions & 0 deletions src/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ var (
ErrWalletCantSign = errors.New("Wallet does not support transaction signing")
// ErrNotImplemented feature not implemented
ErrNotImplemented = errors.New("Feature not implemented")
// ErrKeyNotFound unknown key
ErrKeyNotFound = errors.New("Key not found")
)
2 changes: 1 addition & 1 deletion src/models/history/historyManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (hm *HistoryManager) init() {
hm.txnForAddresses = make(map[string][]core.Transaction, 0)
hm.newTxn = make(map[string][]core.Transaction, 0)
updateTime := config.GetDataUpdateTime()
uptimeTicker := time.NewTicker(time.Duration(updateTime) * time.Microsecond * 2)
uptimeTicker := time.NewTicker(time.Duration(updateTime) * time.Second)
historyManager = hm
hm.txnFinded = make(map[string]struct{}, 0)
go func() {
Expand Down
2 changes: 1 addition & 1 deletion src/models/modelWallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (m *ModelWallets) init() {

m.WalletEnv = walletsEnvs[0]
go func() {
uptimeTicker := time.NewTicker(time.Duration(config.GetDataUpdateTime()) * time.Microsecond)
uptimeTicker := time.NewTicker(time.Duration(config.GetDataUpdateTime()) * time.Second)

for range uptimeTicker.C {
go m.loadModel()
Expand Down
6 changes: 4 additions & 2 deletions src/models/pending/PendingModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (model *PendingTransactionList) getAll() {
}
ptModels := make([]*PendingTransaction, 0)
for txns.Next() {
ptModel := TransactionToPendingTransaction(txns.Value())
t := txns.Value()
ptModel := TransactionToPendingTransaction(t)
ptModel.SetMine(0)
ptModels = append(ptModels, ptModel)
}
Expand All @@ -96,7 +97,8 @@ func (model *PendingTransactionList) getMine() {
}
ptModels := make([]*PendingTransaction, 0)
for wallets.Next() {
account := wallets.Value().GetCryptoAccount()
wlt := wallets.Value()
account := wlt.GetCryptoAccount()
txns, err := account.ListPendingTransactions()
if err != nil {
//display an error in qml app when Mine is selected
Expand Down
18 changes: 16 additions & 2 deletions src/models/walletsManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type WalletManager struct {
transactionAPI core.BlockchainTransactionAPI
walletsIterator core.WalletIterator
updaterChannel chan *updateWalletInfo
timerUpdate chan time.Duration

_ func() `slot:"updateWalletEnvs"`
_ func(wltId, address string) `slot:"updateOutputs"`
Expand Down Expand Up @@ -152,10 +153,20 @@ func (walletM *WalletManager) init() {
logWalletManager.Debug("Finish wallets")
walletM.wallets = qWallets
go func() {
logWalletManager.Debug("Update time is :=> ", time.Duration(config.GetDataUpdateTime())*time.Microsecond)
uptimeTicker := time.NewTicker(time.Duration(config.GetDataUpdateTime()) * time.Microsecond)
updateTime := config.GetDataUpdateTime()
logWalletManager.Debug("Update time is :=> ", time.Duration(updateTime)*time.Second)
uptimeTicker := time.NewTicker(time.Duration(updateTime) * time.Second)

for {
select {
case <-uptimeTicker.C:
go walletM.updateWallets()
walletManager = walletM
break
case t := <-walletM.timerUpdate:
uptimeTicker = time.NewTicker(t)
break
}
<-uptimeTicker.C
go walletM.updateWallets()
walletManager = walletM
Expand Down Expand Up @@ -185,6 +196,9 @@ func (walletM *WalletManager) updateAll() {
walletM.updateSigner()
walletM.updateWalletEnvs()
skycoin.UpdateAltcoin()
updateTime := config.GetDataUpdateTime()
logWalletManager.Debug("Update time is :=> ", time.Duration(updateTime)*time.Second)
walletM.timerUpdate <- time.Duration(updateTime) * time.Second
}

func GetWalletEnv() core.WalletEnv {
Expand Down
4 changes: 2 additions & 2 deletions src/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package params

const (
// Default refresh timeout for API data
DataRefreshTimeout = 1000000 //FIXME: set correct value
DataUpdateTime = 5000000
DataRefreshTimeout = 10
DataUpdateTime = 5
OrganizationName = "Simelo.Tech"
OrganizationDomain = "simelo.tech"
ApplicationName = "FiberCryptoWallet"
Expand Down
45 changes: 45 additions & 0 deletions src/util/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package util

import (
"github.com/fibercrypto/fibercryptowallet/src/errors"
"time"
)

// BalanceSnapshot act as account balances cache
type BalanceSnapshot struct {
cache map[string]uint64
lastUpdate int64
updateInterval int64
}

const defaultUpdateInterval = 10000000

// NewBalanceSnapshot new balance snapshot with validity timeout
func NewBalanceSnapshot(updInterval int64) *BalanceSnapshot {
if updInterval <= 0 {
updInterval = defaultUpdateInterval
}
return &BalanceSnapshot{
cache: make(map[string]uint64),
lastUpdate: 0,
updateInterval: updInterval,
}
}

// SetCoins update balance for a given coin token
func (bs *BalanceSnapshot) SetCoins(ticker string, coins uint64) {
bs.cache[ticker] = coins
bs.lastUpdate = time.Now().UnixNano()
}

// GetCoins retrieve balance for a given coin ticker
func (bs *BalanceSnapshot) GetCoins(ticker string) (uint64, error) {
if coins, hasCoins := bs.cache[ticker]; hasCoins {
return coins, nil
}
return 0, errors.ErrKeyNotFound
}

func (bs *BalanceSnapshot) IsUpdated() bool {
return time.Now().UnixNano()-bs.lastUpdate < bs.updateInterval
}

0 comments on commit 9f767b3

Please sign in to comment.