2019-06-14 10:16:30 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2019-08-28 07:49:03 +00:00
|
|
|
"context"
|
2019-06-14 10:16:30 +00:00
|
|
|
"math/big"
|
2021-02-11 15:20:06 +00:00
|
|
|
"time"
|
2019-06-14 10:16:30 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-08-28 07:49:03 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2019-06-14 10:16:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewService initializes service instance.
|
2019-08-28 07:49:03 +00:00
|
|
|
func NewService(db *Database, accountsFeed *event.Feed) *Service {
|
2019-06-14 10:16:30 +00:00
|
|
|
feed := &event.Feed{}
|
|
|
|
return &Service{
|
2020-10-28 07:56:14 +00:00
|
|
|
db: db,
|
|
|
|
feed: feed,
|
|
|
|
signals: &SignalsTransmitter{
|
|
|
|
publisher: feed,
|
|
|
|
},
|
2019-08-28 07:49:03 +00:00
|
|
|
accountsFeed: accountsFeed,
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service is a wallet service.
|
|
|
|
type Service struct {
|
2021-01-26 13:00:32 +00:00
|
|
|
feed *event.Feed
|
|
|
|
db *Database
|
|
|
|
reactor *Reactor
|
|
|
|
signals *SignalsTransmitter
|
2021-03-08 11:18:43 +00:00
|
|
|
client *walletClient
|
2021-01-26 13:00:32 +00:00
|
|
|
cryptoOnRampManager *CryptoOnRampManager
|
|
|
|
started bool
|
2019-08-28 07:49:03 +00:00
|
|
|
|
|
|
|
group *Group
|
|
|
|
accountsFeed *event.Feed
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals transmitter.
|
|
|
|
func (s *Service) Start(*p2p.Server) error {
|
2019-08-28 07:49:03 +00:00
|
|
|
s.group = NewGroup(context.Background())
|
2019-06-14 10:16:30 +00:00
|
|
|
return s.signals.Start()
|
|
|
|
}
|
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
// GetFeed returns signals feed.
|
|
|
|
func (s *Service) GetFeed() *event.Feed {
|
|
|
|
return s.feed
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:58:17 +00:00
|
|
|
// SetClient sets ethclient
|
|
|
|
func (s *Service) SetClient(client *ethclient.Client) {
|
2021-03-08 11:18:43 +00:00
|
|
|
s.client = &walletClient{client: client}
|
2021-01-28 15:58:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 14:20:26 +00:00
|
|
|
// MergeBlocksRanges merge old blocks ranges if possible
|
|
|
|
func (s *Service) MergeBlocksRanges(accounts []common.Address, chain uint64) error {
|
|
|
|
for _, account := range accounts {
|
|
|
|
err := s.db.mergeRanges(account, chain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:16:30 +00:00
|
|
|
// StartReactor separately because it requires known ethereum address, which will become available only after login.
|
2021-04-01 09:04:47 +00:00
|
|
|
func (s *Service) StartReactor(client *ethclient.Client, accounts []common.Address, chain *big.Int) error {
|
|
|
|
reactor := NewReactor(s.db, s.feed, client, chain)
|
2019-08-28 07:49:03 +00:00
|
|
|
err := reactor.Start(accounts)
|
2019-06-14 10:16:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.reactor = reactor
|
2021-03-08 11:18:43 +00:00
|
|
|
s.SetClient(client)
|
2019-08-28 07:49:03 +00:00
|
|
|
s.group.Add(func(ctx context.Context) error {
|
|
|
|
return WatchAccountsChanges(ctx, s.accountsFeed, accounts, reactor)
|
|
|
|
})
|
2020-08-18 08:44:56 +00:00
|
|
|
s.started = true
|
2019-06-14 10:16:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopReactor stops reactor and closes database.
|
|
|
|
func (s *Service) StopReactor() error {
|
|
|
|
if s.reactor == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.reactor.Stop()
|
2020-07-29 09:13:47 +00:00
|
|
|
if s.group != nil {
|
|
|
|
s.group.Stop()
|
|
|
|
s.group.Wait()
|
|
|
|
}
|
2020-08-18 08:44:56 +00:00
|
|
|
s.started = false
|
2019-07-25 05:35:09 +00:00
|
|
|
return nil
|
2019-06-14 10:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop reactor, signals transmitter and close db.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
log.Info("wallet will be stopped")
|
|
|
|
err := s.StopReactor()
|
|
|
|
s.signals.Stop()
|
2019-08-28 07:49:03 +00:00
|
|
|
if s.group != nil {
|
|
|
|
s.group.Stop()
|
|
|
|
s.group.Wait()
|
|
|
|
s.group = nil
|
|
|
|
}
|
2019-06-14 10:16:30 +00:00
|
|
|
log.Info("wallet stopped")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns list of available RPC APIs.
|
|
|
|
func (s *Service) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "wallet",
|
|
|
|
Version: "0.1.0",
|
|
|
|
Service: NewAPI(s),
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocols returns list of p2p protocols.
|
|
|
|
func (s *Service) Protocols() []p2p.Protocol {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-28 07:49:03 +00:00
|
|
|
|
|
|
|
// WatchAccountsChanges subsribes to a feed and watches for changes in accounts list. If there are new or removed accounts
|
|
|
|
// reactor will be restarted.
|
|
|
|
func WatchAccountsChanges(ctx context.Context, feed *event.Feed, initial []common.Address, reactor *Reactor) error {
|
|
|
|
accounts := make(chan []accounts.Account, 1) // it may block if the rate of updates will be significantly higher
|
|
|
|
sub := feed.Subscribe(accounts)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
listen := make(map[common.Address]struct{}, len(initial))
|
|
|
|
for _, address := range initial {
|
|
|
|
listen[address] = struct{}{}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case err := <-sub.Err():
|
|
|
|
if err != nil {
|
|
|
|
log.Error("accounts watcher subscription failed", "error", err)
|
|
|
|
}
|
|
|
|
case n := <-accounts:
|
2019-10-28 13:50:33 +00:00
|
|
|
log.Debug("wallet received updated list of accounts", "accounts", n)
|
2019-08-28 07:49:03 +00:00
|
|
|
restart := false
|
|
|
|
for _, acc := range n {
|
2019-12-11 13:59:37 +00:00
|
|
|
_, exist := listen[common.Address(acc.Address)]
|
2019-08-28 07:49:03 +00:00
|
|
|
if !exist {
|
2019-12-11 13:59:37 +00:00
|
|
|
listen[common.Address(acc.Address)] = struct{}{}
|
2019-08-28 07:49:03 +00:00
|
|
|
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)
|
|
|
|
reactor.Stop()
|
|
|
|
err := reactor.Start(listenList) // error is raised only if reactor is already running
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to restart reactor with new accounts", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapToList(m map[common.Address]struct{}) []common.Address {
|
|
|
|
rst := make([]common.Address, 0, len(m))
|
|
|
|
for address := range m {
|
|
|
|
rst = append(rst, address)
|
|
|
|
}
|
|
|
|
return rst
|
|
|
|
}
|
2020-02-28 16:05:37 +00:00
|
|
|
|
|
|
|
func (s *Service) IsStarted() bool {
|
2020-08-18 08:44:56 +00:00
|
|
|
return s.started
|
2020-02-28 16:05:37 +00:00
|
|
|
}
|
2021-02-11 15:20:06 +00:00
|
|
|
|
|
|
|
func (s *Service) SetInitialBlocksRange(network uint64) error {
|
|
|
|
accountsDB := accounts.NewDB(s.db.db)
|
|
|
|
watchAddress, err := accountsDB.GetWalletAddress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
from := big.NewInt(0)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
header, err := s.client.HeaderByNumber(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-19 13:34:24 +00:00
|
|
|
err = s.db.UpsertRange(common.Address(watchAddress), network, from, header.Number, big.NewInt(0), 0)
|
2021-02-11 15:20:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|