2020-10-28 07:56:14 +00:00
|
|
|
package localnotifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2021-03-31 16:23:45 +00:00
|
|
|
"strings"
|
2020-10-28 07:56:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/transfer"
|
2022-11-29 13:43:18 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2020-10-28 07:56:14 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
"github.com/status-im/status-go/t/utils"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
)
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
func createWalletDb(t *testing.T, db *sql.DB) (*transfer.Database, func()) {
|
|
|
|
return transfer.NewDB(db), func() {
|
2020-10-28 07:56:14 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceStartStop(t *testing.T) {
|
|
|
|
db, stop := setupAppTestDb(t)
|
|
|
|
defer stop()
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
s, err := NewService(db, 1777)
|
|
|
|
require.NoError(t, err)
|
2021-07-12 09:44:26 +00:00
|
|
|
require.NoError(t, s.Start())
|
2020-10-28 07:56:14 +00:00
|
|
|
require.Equal(t, true, s.IsStarted())
|
|
|
|
|
|
|
|
require.NoError(t, s.Stop())
|
|
|
|
require.Equal(t, false, s.IsStarted())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalletSubscription(t *testing.T) {
|
|
|
|
db, stop := setupAppTestDb(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
feed := &event.Feed{}
|
2022-03-23 18:47:00 +00:00
|
|
|
s, err := NewService(db, 1777)
|
|
|
|
require.NoError(t, err)
|
2021-07-12 09:44:26 +00:00
|
|
|
require.NoError(t, s.Start())
|
2020-10-28 07:56:14 +00:00
|
|
|
require.Equal(t, true, s.IsStarted())
|
|
|
|
|
|
|
|
require.NoError(t, s.SubscribeWallet(feed))
|
2020-12-28 15:09:25 +00:00
|
|
|
require.Equal(t, true, s.IsWatchingWallet())
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
s.StartWalletWatcher()
|
|
|
|
require.Equal(t, true, s.IsWatchingWallet())
|
|
|
|
|
|
|
|
s.StopWalletWatcher()
|
|
|
|
require.Equal(t, false, s.IsWatchingWallet())
|
|
|
|
|
|
|
|
require.NoError(t, s.Stop())
|
|
|
|
require.Equal(t, false, s.IsStarted())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransactionNotification(t *testing.T) {
|
|
|
|
db, stop := setupAppTestDb(t)
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
walletDb, stop := createWalletDb(t, db)
|
|
|
|
defer stop()
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
s, err := NewService(db, 1777)
|
|
|
|
require.NoError(t, err)
|
2021-07-12 09:44:26 +00:00
|
|
|
require.NoError(t, s.Start())
|
2020-10-28 07:56:14 +00:00
|
|
|
require.Equal(t, true, s.IsStarted())
|
|
|
|
|
|
|
|
var signalEvent []byte
|
|
|
|
|
|
|
|
signalHandler := signal.MobileSignalHandler(func(s []byte) {
|
|
|
|
signalEvent = s
|
|
|
|
})
|
|
|
|
|
|
|
|
signal.SetMobileSignalHandler(signalHandler)
|
|
|
|
|
|
|
|
feed := &event.Feed{}
|
|
|
|
require.NoError(t, s.SubscribeWallet(feed))
|
2020-12-28 15:09:25 +00:00
|
|
|
s.WatchingEnabled = true
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
s.StartWalletWatcher()
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
header := &transfer.DBHeader{
|
2020-10-28 07:56:14 +00:00
|
|
|
Number: big.NewInt(1),
|
|
|
|
Hash: common.Hash{1},
|
|
|
|
Address: common.Address{1},
|
|
|
|
}
|
|
|
|
tx := types.NewTransaction(1, common.Address{1}, nil, 10, big.NewInt(10), nil)
|
|
|
|
receipt := types.NewReceipt(nil, false, 100)
|
|
|
|
receipt.Logs = []*types.Log{}
|
2021-09-09 14:28:54 +00:00
|
|
|
transfers := []transfer.Transfer{
|
2020-10-28 07:56:14 +00:00
|
|
|
{
|
|
|
|
ID: common.Hash{1},
|
2021-09-09 14:28:54 +00:00
|
|
|
Type: transfer.Type("eth"),
|
2020-10-28 07:56:14 +00:00
|
|
|
BlockHash: header.Hash,
|
|
|
|
BlockNumber: header.Number,
|
|
|
|
Transaction: tx,
|
|
|
|
Receipt: receipt,
|
|
|
|
Address: header.Address,
|
|
|
|
},
|
|
|
|
}
|
2021-02-19 13:34:24 +00:00
|
|
|
nonce := int64(0)
|
2023-05-08 06:02:00 +00:00
|
|
|
lastBlock := &transfer.Block{
|
2021-02-19 13:34:24 +00:00
|
|
|
Number: big.NewInt(1),
|
|
|
|
Balance: big.NewInt(0),
|
|
|
|
Nonce: &nonce,
|
|
|
|
}
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, walletDb.ProcessBlocks(1777, header.Address, big.NewInt(1), lastBlock, []*transfer.DBHeader{header}))
|
2023-02-15 12:28:19 +00:00
|
|
|
require.NoError(t, walletDb.ProcessTransfers(1777, transfers, []*transfer.DBHeader{}))
|
2020-10-28 07:56:14 +00:00
|
|
|
|
2022-11-29 13:43:18 +00:00
|
|
|
feed.Send(walletevent.Event{
|
2021-09-09 14:28:54 +00:00
|
|
|
Type: transfer.EventRecentHistoryReady,
|
2020-11-17 15:54:31 +00:00
|
|
|
BlockNumber: big.NewInt(0),
|
|
|
|
Accounts: []common.Address{header.Address},
|
|
|
|
})
|
|
|
|
|
2022-11-29 13:43:18 +00:00
|
|
|
feed.Send(walletevent.Event{
|
2021-09-09 14:28:54 +00:00
|
|
|
Type: transfer.EventNewTransfers,
|
2020-10-28 07:56:14 +00:00
|
|
|
BlockNumber: header.Number,
|
|
|
|
Accounts: []common.Address{header.Address},
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, utils.Eventually(func() error {
|
|
|
|
if signalEvent == nil {
|
2021-01-26 11:49:37 +00:00
|
|
|
return fmt.Errorf("signal was not handled")
|
2020-10-28 07:56:14 +00:00
|
|
|
}
|
2021-03-31 16:23:45 +00:00
|
|
|
require.True(t, strings.Contains(string(signalEvent), `"type":"local-notifications"`))
|
|
|
|
require.True(t, strings.Contains(string(signalEvent), `"to":"`+header.Address.Hex()))
|
2020-10-28 07:56:14 +00:00
|
|
|
return nil
|
|
|
|
}, 2*time.Second, 100*time.Millisecond))
|
|
|
|
|
|
|
|
require.NoError(t, s.Stop())
|
|
|
|
}
|