2019-08-20 15:38:40 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/errors"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
2019-08-20 15:38:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
uniqueChatConstraint = "UNIQUE constraint failed: accounts.chat"
|
|
|
|
uniqueWalletConstraint = "UNIQUE constraint failed: accounts.wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Account struct {
|
2019-12-11 13:59:37 +00:00
|
|
|
Address types.Address `json:"address"`
|
2019-08-20 15:38:40 +00:00
|
|
|
Wallet bool `json:"wallet"`
|
|
|
|
Chat bool `json:"chat"`
|
2019-12-16 15:23:36 +00:00
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Storage string `json:"storage,omitempty"`
|
|
|
|
Path string `json:"path,omitempty"`
|
2019-12-11 13:59:37 +00:00
|
|
|
PublicKey types.HexBytes `json:"public-key,omitempty"`
|
2019-08-20 15:38:40 +00:00
|
|
|
Name string `json:"name"`
|
2022-03-11 10:59:15 +00:00
|
|
|
Emoji string `json:"emoji"`
|
2019-08-20 15:38:40 +00:00
|
|
|
Color string `json:"color"`
|
2021-08-10 11:36:06 +00:00
|
|
|
Hidden bool `json:"hidden"`
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 14:03:18 +00:00
|
|
|
const (
|
|
|
|
accountTypeGenerated = "generated"
|
|
|
|
accountTypeKey = "key"
|
|
|
|
accountTypeSeed = "seed"
|
|
|
|
accountTypeWatch = "watch"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsOwnAccount returns true if this is an account we have the private key for
|
|
|
|
// NOTE: Wallet flag can't be used as it actually indicates that it's the default
|
|
|
|
// Wallet
|
|
|
|
func (a *Account) IsOwnAccount() bool {
|
|
|
|
return a.Wallet || a.Type == accountTypeSeed || a.Type == accountTypeGenerated || a.Type == accountTypeKey
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
// Database sql wrapper for operations with browser objects.
|
|
|
|
type Database struct {
|
2022-03-23 18:47:00 +00:00
|
|
|
*settings.Database
|
2019-08-20 15:38:40 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// NewDB returns a new instance of *Database
|
|
|
|
func NewDB(db *sql.DB) (*Database, error) {
|
|
|
|
sDB, err := settings.MakeNewDB(db)
|
2022-01-12 20:04:43 +00:00
|
|
|
if err != nil {
|
2022-03-23 18:47:00 +00:00
|
|
|
return nil, err
|
2022-01-12 20:04:43 +00:00
|
|
|
}
|
2019-08-20 15:38:40 +00:00
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
return &Database{sDB, db}, nil
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// DB Gets db sql.DB
|
|
|
|
func (db Database) DB() *sql.DB {
|
|
|
|
return db.db
|
2019-12-27 09:58:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
// Close closes database.
|
|
|
|
func (db Database) Close() error {
|
|
|
|
return db.db.Close()
|
2019-08-08 07:31:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func (db *Database) GetAccounts() ([]Account, error) {
|
2022-03-11 10:59:15 +00:00
|
|
|
rows, err := db.db.Query("SELECT address, wallet, chat, type, storage, pubkey, path, name, emoji, color, hidden FROM accounts ORDER BY created_at")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer rows.Close()
|
2019-08-20 15:38:40 +00:00
|
|
|
accounts := []Account{}
|
|
|
|
pubkey := []byte{}
|
|
|
|
for rows.Next() {
|
|
|
|
acc := Account{}
|
|
|
|
err := rows.Scan(
|
|
|
|
&acc.Address, &acc.Wallet, &acc.Chat, &acc.Type, &acc.Storage,
|
2022-03-11 10:59:15 +00:00
|
|
|
&pubkey, &acc.Path, &acc.Name, &acc.Emoji, &acc.Color, &acc.Hidden)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if lth := len(pubkey); lth > 0 {
|
2019-12-11 13:59:37 +00:00
|
|
|
acc.PublicKey = make(types.HexBytes, lth)
|
2019-08-20 15:38:40 +00:00
|
|
|
copy(acc.PublicKey, pubkey)
|
|
|
|
}
|
|
|
|
accounts = append(accounts, acc)
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
func (db *Database) GetAccountByAddress(address types.Address) (rst *Account, err error) {
|
2022-03-11 10:59:15 +00:00
|
|
|
row := db.db.QueryRow("SELECT address, wallet, chat, type, storage, pubkey, path, name, emoji, color, hidden FROM accounts WHERE address = ? COLLATE NOCASE", address)
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
acc := &Account{}
|
|
|
|
pubkey := []byte{}
|
|
|
|
err = row.Scan(
|
|
|
|
&acc.Address, &acc.Wallet, &acc.Chat, &acc.Type, &acc.Storage,
|
2022-03-11 10:59:15 +00:00
|
|
|
&pubkey, &acc.Path, &acc.Name, &acc.Emoji, &acc.Color, &acc.Hidden)
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
acc.PublicKey = pubkey
|
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func (db *Database) SaveAccounts(accounts []Account) (err error) {
|
|
|
|
var (
|
|
|
|
tx *sql.Tx
|
|
|
|
insert *sql.Stmt
|
|
|
|
update *sql.Stmt
|
|
|
|
)
|
|
|
|
tx, err = db.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
// NOTE(dshulyak) replace all record values using address (primary key)
|
|
|
|
// can't use `insert or replace` because of the additional constraints (wallet and chat)
|
2019-12-16 15:23:36 +00:00
|
|
|
insert, err = tx.Prepare("INSERT OR IGNORE INTO accounts (address, created_at, updated_at) VALUES (?, datetime('now'), datetime('now'))")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-11 10:59:15 +00:00
|
|
|
update, err = tx.Prepare("UPDATE accounts SET wallet = ?, chat = ?, type = ?, storage = ?, pubkey = ?, path = ?, name = ?, emoji = ?, color = ?, hidden = ?, updated_at = datetime('now') WHERE address = ?")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := range accounts {
|
|
|
|
acc := &accounts[i]
|
|
|
|
_, err = insert.Exec(acc.Address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-03-11 10:59:15 +00:00
|
|
|
_, err = update.Exec(acc.Wallet, acc.Chat, acc.Type, acc.Storage, acc.PublicKey, acc.Path, acc.Name, acc.Emoji, acc.Color, acc.Hidden, acc.Address)
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.Error() {
|
|
|
|
case uniqueChatConstraint:
|
2022-03-23 18:47:00 +00:00
|
|
|
err = errors.ErrChatNotUnique
|
2019-08-20 15:38:40 +00:00
|
|
|
case uniqueWalletConstraint:
|
2022-03-23 18:47:00 +00:00
|
|
|
err = errors.ErrWalletNotUnique
|
2019-08-20 15:38:40 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) DeleteAccount(address types.Address) error {
|
2019-12-16 15:23:36 +00:00
|
|
|
_, err := db.db.Exec("DELETE FROM accounts WHERE address = ?", address)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:48:10 +00:00
|
|
|
func (db *Database) DeleteSeedAndKeyAccounts() error {
|
|
|
|
_, err := db.db.Exec("DELETE FROM accounts WHERE type = ? OR type = ?", accountTypeSeed, accountTypeKey)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) GetWalletAddress() (rst types.Address, err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
err = db.db.QueryRow("SELECT address FROM accounts WHERE wallet = 1").Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
status-im/status-react#9203 Faster tx fetching with less request
*** How it worked before this PR on multiaccount creation:
- On multiacc creation we scanned chain for eth and erc20 transfers. For
each address of a new empty multiaccount this scan required
1. two `eth_getBalance` requests to find out that there is no any
balance change between zero and the last block, for eth transfers
2. and `chain-size/100000` (currently ~100) `eth_getLogs` requests,
for erc20 transfers
- For some reason we scanned an address of the chat account as well, and
also accounts were not deduplicated. So even for an empty multiacc we
scanned chain twice for each chat and main wallet addresses, in result
app had to execute about 400 requests.
- As mentioned above, `eth_getBalance` requests were used to check if
there were any eth transfers, and that caused empty history in case
if user already used all available eth (so that both zero and latest
blocks show 0 eth for an address). There might have been transactions
but we wouldn't fetch/show them.
- There was no upper limit for the number of rpc requests during the
scan, so it could require indefinite number of requests; the scanning
algorithm was written so that we persisted the whole history of
transactions or tried to scan form the beginning again in case of
failure, giving up only after 10 minutes of failures. In result
addresses with sufficient number of transactions would never be fully
scanned and during these 10 minutes app could use gigabytes of
internet data.
- Failures were caused by `eth_getBlockByNumber`/`eth_getBlockByHash`
requests. These requests return significantly bigger responses than
`eth_getBalance`/`eth_transactionsCount` and it is likely that
execution of thousands of them in parallel caused failures for
accounts with hundreds of transactions. Even for an account with 12k
we could successfully determine blocks with transaction in a few
minutes using `eth_getBalance` requests, but `eth_getBlock...`
couldn't be processed for this acc.
- There was no caching for for `eth_getBalance` requests, and this
caused in average 3-4 times more such requests than is needed.
*** How it works now on multiaccount creation:
- On multiacc creation we scan chain for last ~30 eth transactions and
then check erc20 in the range where these eth transactions were found.
For an empty address in multiacc this means:
1. two `eth_getBalance` transactions to determine that there was no
balance change between zero and the last block; two
`eth_transactionsCount` requests to determine there are no outgoing
transactions for this address; total 4 requests for eth transfers
2. 20 `eth_getLogs` for erc20 transfers. This number can be lowered,
but that's not a big deal
- Deduplication of addresses is added and also we don't scan chat
account, so a new multiacc requires ~25 (we also request latest block
number and probably execute a few other calls) request to determine
that multiacc is empty (comparing to ~400 before)
- In case if address contains transactions we:
1. determine the range which contains 20-25 outgoing eth/erc20
transactions. This usually requires up to 10 `eth_transactionCount`
requests
2. then we scan chain for eth transfers using `eth_getBalance` and
`eth_transactionCount` (for double checking zero balances)
3. we make sure that we do not scan db for more than 30 blocks with
transfers. That's important for accounts with mostly incoming
transactions, because the range found on the first step might
contain any number of incoming transfers, but only 20-25 outgoing
transactions
4. when we found ~30 blocks in a given range, we update initial
range `from` block using the oldest found block
5. and now we scan db for erc20transfers using `eth_getLogs`
`oldest-found-eth-block`-`latest-block`, we make not more than 20 calls
6. when all blocks which contain incoming/outgoing transfers for a
given address are found, we save these blocks to db and mark that
transfers from these blocks are still to be fetched
7. Then we select latest ~30 (the number can be adjusted) blocks from
these which were found and fetch transfers, this requires 3-4
requests per transfer.
8. we persist scanned range so that we know were to start next time
9. we dispatch an event which tells client that transactions are found
10. client fetches latest 20 transfers
- when user presses "fetch more" button we check if app's db contains next
20 transfers, if not we scan chain again and return transfers after
small fixes
2019-12-18 11:01:46 +00:00
|
|
|
func (db *Database) GetWalletAddresses() (rst []types.Address, err error) {
|
|
|
|
rows, err := db.db.Query("SELECT address FROM accounts WHERE chat = 0 ORDER BY created_at")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
addr := types.Address{}
|
|
|
|
err = rows.Scan(&addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, addr)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) GetChatAddress() (rst types.Address, err error) {
|
2019-08-20 15:38:40 +00:00
|
|
|
err = db.db.QueryRow("SELECT address FROM accounts WHERE chat = 1").Scan(&rst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) GetAddresses() (rst []types.Address, err error) {
|
2019-12-16 15:23:36 +00:00
|
|
|
rows, err := db.db.Query("SELECT address FROM accounts ORDER BY created_at")
|
2019-08-20 15:38:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-14 10:51:32 +00:00
|
|
|
defer rows.Close()
|
2019-08-20 15:38:40 +00:00
|
|
|
for rows.Next() {
|
2019-12-11 13:59:37 +00:00
|
|
|
addr := types.Address{}
|
2019-08-20 15:38:40 +00:00
|
|
|
err = rows.Scan(&addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, addr)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
2019-08-29 08:06:22 +00:00
|
|
|
|
|
|
|
// AddressExists returns true if given address is stored in database.
|
2019-12-11 13:59:37 +00:00
|
|
|
func (db *Database) AddressExists(address types.Address) (exists bool, err error) {
|
2019-08-29 08:06:22 +00:00
|
|
|
err = db.db.QueryRow("SELECT EXISTS (SELECT 1 FROM accounts WHERE address = ?)", address).Scan(&exists)
|
|
|
|
return exists, err
|
|
|
|
}
|