fix(wallet): added `remove` event for accounts. Stop loading

transfers for removed account

Updates #10246
This commit is contained in:
Ivan Belyakov 2023-05-31 21:45:11 +02:00 committed by IvanBelyakoff
parent f82818ff93
commit ad1b8b6d43
3 changed files with 52 additions and 11 deletions

View File

@ -15,6 +15,7 @@ import (
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/services/accounts/accountsevent"
)
func NewAccountsAPI(manager *account.GethManager, config *params.NodeConfig, db *accounts.Database, feed *event.Feed, messenger **protocol.Messenger) *API {
@ -43,7 +44,11 @@ func (api *API) SaveAccount(ctx context.Context, account *accounts.Account) erro
if err != nil {
return err
}
api.feed.Send([]*accounts.Account{account})
api.feed.Send(accountsevent.Event{
Type: accountsevent.EventTypeAdded,
Accounts: []common.Address{common.Address(account.Address)},
})
return nil
}
@ -54,9 +59,16 @@ func (api *API) SaveKeypair(ctx context.Context, keypair *accounts.Keypair) erro
if err != nil {
return err
}
commonAddresses := []common.Address{}
for _, acc := range keypair.Accounts {
api.feed.Send([]*accounts.Account{acc})
commonAddresses = append(commonAddresses, common.Address(acc.Address))
}
api.feed.Send(accountsevent.Event{
Type: accountsevent.EventTypeAdded,
Accounts: commonAddresses,
})
return nil
}
@ -140,6 +152,11 @@ func (api *API) DeleteAccount(ctx context.Context, address types.Address) error
}
}
api.feed.Send(accountsevent.Event{
Type: accountsevent.EventTypeRemoved,
Accounts: []common.Address{common.Address(address)},
})
return (*api.messenger).DeleteAccount(address)
}

View File

@ -0,0 +1,19 @@
package accountsevent
import "github.com/ethereum/go-ethereum/common"
// EventType type for event types.
type EventType string
// Event is a type for accounts events.
type Event struct {
Type EventType `json:"type"`
Accounts []common.Address `json:"accounts"`
}
const (
// EventTypeAdded is emitted when a new account is added.
EventTypeAdded EventType = "added"
// EventTypeRemoved is emitted when an account is removed.
EventTypeRemoved EventType = "removed"
)

View File

@ -10,9 +10,9 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/rpc/chain"
"github.com/status-im/status-go/services/accounts/accountsevent"
"github.com/status-im/status-go/services/wallet/async"
)
@ -131,8 +131,8 @@ func (c *Controller) CheckRecentHistory(chainIDs []uint64, accounts []common.Add
func watchAccountsChanges(ctx context.Context, accountFeed *event.Feed, reactor *Reactor,
chainClients map[uint64]*chain.ClientWithFallback, initial []common.Address, fetchStrategyType FetchStrategyType) error {
accounts := make(chan []*accounts.Account, 1) // it may block if the rate of updates will be significantly higher
sub := accountFeed.Subscribe(accounts)
ch := make(chan accountsevent.Event, 1) // it may block if the rate of updates will be significantly higher
sub := accountFeed.Subscribe(ch)
defer sub.Unsubscribe()
listen := make(map[common.Address]struct{}, len(initial))
for _, address := range initial {
@ -146,19 +146,24 @@ func watchAccountsChanges(ctx context.Context, accountFeed *event.Feed, reactor
if err != nil {
log.Error("accounts watcher subscription failed", "error", err)
}
case n := <-accounts:
log.Debug("wallet received updated list of accounts", "accounts", n)
case ev := <-ch:
restart := false
for _, acc := range n {
_, exist := listen[common.Address(acc.Address)]
if !exist {
listen[common.Address(acc.Address)] = struct{}{}
for _, address := range ev.Accounts {
_, exist := listen[address]
if ev.Type == accountsevent.EventTypeAdded && !exist {
listen[address] = struct{}{}
restart = true
} else if ev.Type == accountsevent.EventTypeRemoved && exist {
delete(listen, address)
restart = true
}
}
if !restart {
continue
}
listenList := mapToList(listen)
log.Debug("list of accounts was changed from a previous version. reactor will be restarted", "new", listenList)