2020-10-28 07:56:14 +00:00
|
|
|
package localnotifications
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
|
2023-05-19 15:31:45 +00:00
|
|
|
w_common "github.com/status-im/status-go/services/wallet/common"
|
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"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2020-10-28 07:56:14 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
2020-10-28 07:56:14 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
)
|
|
|
|
|
2023-08-11 11:28:45 +00:00
|
|
|
func createWalletDb(t *testing.T) (*transfer.Database, func()) {
|
|
|
|
db, cleanup, err := helpers.SetupTestSQLDB(walletdatabase.DbInitializer{}, "local-notifications-tests-wallet-")
|
|
|
|
require.NoError(t, err)
|
2021-09-09 14:28:54 +00:00
|
|
|
return transfer.NewDB(db), func() {
|
2023-08-11 11:28:45 +00:00
|
|
|
require.NoError(t, cleanup())
|
2020-10-28 07:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceStartStop(t *testing.T) {
|
|
|
|
db, stop := setupAppTestDb(t)
|
|
|
|
defer stop()
|
|
|
|
|
2023-08-11 11:28:45 +00:00
|
|
|
walletDb, walletStop := createWalletDb(t)
|
|
|
|
defer walletStop()
|
|
|
|
|
|
|
|
s, err := NewService(db, walletDb, 1777)
|
2022-03-23 18:47:00 +00:00
|
|
|
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()
|
|
|
|
|
2023-08-11 11:28:45 +00:00
|
|
|
walletDb, walletStop := createWalletDb(t)
|
|
|
|
defer walletStop()
|
|
|
|
|
2020-10-28 07:56:14 +00:00
|
|
|
feed := &event.Feed{}
|
2023-08-11 11:28:45 +00:00
|
|
|
s, err := NewService(db, walletDb, 1777)
|
2022-03-23 18:47:00 +00:00
|
|
|
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()
|
|
|
|
|
2023-08-11 11:28:45 +00:00
|
|
|
walletDb, walletStop := createWalletDb(t)
|
|
|
|
defer walletStop()
|
2020-10-28 07:56:14 +00:00
|
|
|
|
2023-08-11 11:28:45 +00:00
|
|
|
s, err := NewService(db, walletDb, 1777)
|
2022-03-23 18:47:00 +00:00
|
|
|
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},
|
2023-05-19 15:31:45 +00:00
|
|
|
Type: w_common.Type("eth"),
|
2020-10-28 07:56:14 +00:00
|
|
|
BlockHash: header.Hash,
|
|
|
|
BlockNumber: header.Number,
|
|
|
|
Transaction: tx,
|
|
|
|
Receipt: receipt,
|
|
|
|
Address: header.Address,
|
|
|
|
},
|
|
|
|
}
|
2023-11-27 10:08:17 +00:00
|
|
|
require.NoError(t, walletDb.SaveBlocks(1777, []*transfer.DBHeader{header}))
|
|
|
|
require.NoError(t, transfer.SaveTransfersMarkBlocksLoaded(walletDb, 1777, header.Address, transfers, []*big.Int{header.Number}))
|
2020-10-28 07:56:14 +00:00
|
|
|
|
2022-11-29 13:43:18 +00:00
|
|
|
feed.Send(walletevent.Event{
|
2023-12-07 07:11:22 +00:00
|
|
|
Type: transfer.EventRecentHistoryReady,
|
|
|
|
Accounts: []common.Address{header.Address},
|
2020-11-17 15:54:31 +00:00
|
|
|
})
|
|
|
|
|
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())
|
|
|
|
}
|