2022-11-15 12:14:41 +00:00
|
|
|
package history
|
|
|
|
|
|
|
|
import (
|
2024-03-21 13:00:34 +00:00
|
|
|
"errors"
|
2022-11-15 12:14:41 +00:00
|
|
|
"math/big"
|
2023-10-04 12:00:12 +00:00
|
|
|
"reflect"
|
2024-03-21 13:00:34 +00:00
|
|
|
"sync"
|
2022-11-15 12:14:41 +00:00
|
|
|
"testing"
|
2024-03-21 13:00:34 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-15 12:14:41 +00:00
|
|
|
|
2023-11-29 11:20:18 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-11-15 12:14:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2024-03-21 13:00:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
|
|
|
"github.com/status-im/status-go/appdatabase"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
"github.com/status-im/status-go/services/accounts/accountsevent"
|
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/t/utils"
|
|
|
|
"github.com/status-im/status-go/transactions/fake"
|
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
2022-11-15 12:14:41 +00:00
|
|
|
)
|
|
|
|
|
2023-10-04 12:00:12 +00:00
|
|
|
func Test_entriesToDataPoints(t *testing.T) {
|
|
|
|
type args struct {
|
2023-11-29 11:20:18 +00:00
|
|
|
data []*entry
|
2023-01-25 18:28:51 +00:00
|
|
|
}
|
2023-10-04 12:00:12 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []*DataPoint
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "zeroAllChainsSameTimestamp",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(0),
|
|
|
|
timestamp: 1,
|
|
|
|
block: big.NewInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(0),
|
|
|
|
timestamp: 1,
|
|
|
|
block: big.NewInt(5),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "oneZeroAllChainsDifferentTimestamp",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(0),
|
|
|
|
timestamp: 1,
|
|
|
|
block: big.NewInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 2,
|
|
|
|
block: big.NewInt(2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(2)),
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nonZeroAllChainsDifferentTimestamp",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(1)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
2023-11-29 11:20:18 +00:00
|
|
|
Balance: (*hexutil.Big)(big.NewInt(3)),
|
2023-10-04 12:00:12 +00:00
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sameChainDifferentTimestamp",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 1,
|
|
|
|
block: big.NewInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 2,
|
|
|
|
block: big.NewInt(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(0),
|
|
|
|
timestamp: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(1)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(2)),
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(0)),
|
|
|
|
Timestamp: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sameChainDifferentTimestampOtherChainsEmpty",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 1,
|
|
|
|
block: big.NewInt(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 2,
|
|
|
|
block: big.NewInt(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(0),
|
|
|
|
timestamp: 2,
|
|
|
|
block: big.NewInt(2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(1)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(2)),
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(2)),
|
|
|
|
Timestamp: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "onlyEdgePointsOnManyChainsWithPadding",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
// Left edge - same timestamp
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 3,
|
|
|
|
balance: big.NewInt(3),
|
|
|
|
timestamp: 1,
|
|
|
|
},
|
|
|
|
// Padding
|
|
|
|
{
|
2024-07-16 14:02:52 +00:00
|
|
|
chainID: 3,
|
|
|
|
balance: big.NewInt(3),
|
2023-10-04 12:00:12 +00:00
|
|
|
timestamp: 2,
|
|
|
|
},
|
|
|
|
{
|
2024-07-16 14:02:52 +00:00
|
|
|
chainID: 3,
|
|
|
|
balance: big.NewInt(3),
|
2023-10-04 12:00:12 +00:00
|
|
|
timestamp: 3,
|
|
|
|
},
|
|
|
|
{
|
2024-07-16 14:02:52 +00:00
|
|
|
chainID: 3,
|
|
|
|
balance: big.NewInt(3),
|
2023-10-04 12:00:12 +00:00
|
|
|
timestamp: 4,
|
|
|
|
},
|
|
|
|
// Right edge - same timestamp
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 3,
|
|
|
|
balance: big.NewInt(3),
|
|
|
|
timestamp: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(6)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(6)),
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(6)),
|
|
|
|
Timestamp: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(6)),
|
|
|
|
Timestamp: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(6)),
|
|
|
|
Timestamp: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-11-29 11:20:18 +00:00
|
|
|
{
|
|
|
|
name: "multipleAddresses",
|
|
|
|
args: args{
|
|
|
|
data: []*entry{
|
|
|
|
{
|
|
|
|
chainID: 2,
|
|
|
|
balance: big.NewInt(5),
|
|
|
|
timestamp: 1,
|
|
|
|
address: common.Address{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(6),
|
|
|
|
timestamp: 1,
|
|
|
|
address: common.Address{2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 2,
|
|
|
|
address: common.Address{1},
|
|
|
|
},
|
2024-07-16 14:02:52 +00:00
|
|
|
// padding - duplicate last point, just update timestamp
|
2023-11-29 11:20:18 +00:00
|
|
|
{
|
|
|
|
chainID: 1,
|
2024-07-16 14:02:52 +00:00
|
|
|
balance: big.NewInt(1),
|
2023-11-29 11:20:18 +00:00
|
|
|
timestamp: 3,
|
2024-07-16 14:02:52 +00:00
|
|
|
address: common.Address{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 4,
|
|
|
|
address: common.Address{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 5,
|
|
|
|
address: common.Address{1},
|
|
|
|
},
|
|
|
|
// real points
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
timestamp: 6,
|
2023-11-29 11:20:18 +00:00
|
|
|
address: common.Address{2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
chainID: 1,
|
|
|
|
balance: big.NewInt(4),
|
2024-07-16 14:02:52 +00:00
|
|
|
timestamp: 7,
|
2023-11-29 11:20:18 +00:00
|
|
|
address: common.Address{2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*DataPoint{
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(11)),
|
|
|
|
Timestamp: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(12)),
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
2024-07-16 14:02:52 +00:00
|
|
|
// padding
|
2023-11-29 11:20:18 +00:00
|
|
|
{
|
2024-07-16 14:02:52 +00:00
|
|
|
Balance: (*hexutil.Big)(big.NewInt(12)),
|
2023-11-29 11:20:18 +00:00
|
|
|
Timestamp: 3,
|
|
|
|
},
|
|
|
|
{
|
2024-07-16 14:02:52 +00:00
|
|
|
Balance: (*hexutil.Big)(big.NewInt(12)),
|
2023-11-29 11:20:18 +00:00
|
|
|
Timestamp: 4,
|
|
|
|
},
|
2024-07-16 14:02:52 +00:00
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(12)),
|
|
|
|
Timestamp: 5,
|
|
|
|
},
|
|
|
|
// real points
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(8)),
|
|
|
|
Timestamp: 6,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Balance: (*hexutil.Big)(big.NewInt(10)),
|
|
|
|
Timestamp: 7,
|
|
|
|
},
|
2023-11-29 11:20:18 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-01-25 18:28:51 +00:00
|
|
|
}
|
2022-11-15 12:14:41 +00:00
|
|
|
|
2023-10-04 12:00:12 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-11-29 11:20:18 +00:00
|
|
|
got, err := entriesToDataPoints(tt.args.data)
|
2023-10-04 12:00:12 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2024-07-16 14:02:52 +00:00
|
|
|
t.Errorf("entriesToDataPoints() name: %s, error = %v, wantErr = %v", tt.name, err, tt.wantErr)
|
2023-10-04 12:00:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
2024-07-16 14:02:52 +00:00
|
|
|
t.Errorf("entriesToDataPoints() name: %s, got: %v, want: %v", tt.name, got, tt.want)
|
2023-10-04 12:00:12 +00:00
|
|
|
}
|
2022-11-15 12:14:41 +00:00
|
|
|
})
|
|
|
|
}
|
2023-01-25 18:28:51 +00:00
|
|
|
}
|
2024-03-21 13:00:34 +00:00
|
|
|
|
|
|
|
func Test_removeBalanceHistoryOnEventAccountRemoved(t *testing.T) {
|
|
|
|
appDB, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
walletDB, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
accountsDB, err := accounts.NewDB(appDB)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
address := common.HexToAddress("0x1234")
|
|
|
|
accountFeed := event.Feed{}
|
|
|
|
walletFeed := event.Feed{}
|
|
|
|
chainID := uint64(1)
|
|
|
|
txServiceMockCtrl := gomock.NewController(t)
|
|
|
|
server, _ := fake.NewTestServer(txServiceMockCtrl)
|
|
|
|
client := gethrpc.DialInProc(server)
|
2024-05-13 13:02:22 +00:00
|
|
|
rpcClient, _ := rpc.NewClient(client, chainID, params.UpstreamRPCConfig{}, nil, appDB)
|
2024-03-21 13:00:34 +00:00
|
|
|
rpcClient.UpstreamChainID = chainID
|
|
|
|
|
|
|
|
service := NewService(walletDB, accountsDB, &accountFeed, &walletFeed, rpcClient, nil, nil, nil)
|
|
|
|
|
|
|
|
// Insert balances for address
|
|
|
|
database := service.balance.db
|
|
|
|
err = database.add(&entry{
|
|
|
|
chainID: chainID,
|
|
|
|
address: address,
|
|
|
|
block: big.NewInt(1),
|
|
|
|
balance: big.NewInt(1),
|
|
|
|
timestamp: 1,
|
|
|
|
tokenSymbol: "ETH",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = database.add(&entry{
|
|
|
|
chainID: chainID,
|
|
|
|
address: address,
|
|
|
|
block: big.NewInt(2),
|
|
|
|
balance: big.NewInt(2),
|
|
|
|
tokenSymbol: "ETH",
|
|
|
|
timestamp: 2,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
entries, err := database.getNewerThan(&assetIdentity{chainID, []common.Address{address}, "ETH"}, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 2)
|
|
|
|
|
|
|
|
// Start service
|
|
|
|
service.startAccountWatcher()
|
|
|
|
|
|
|
|
// Watching accounts must start before sending event.
|
|
|
|
// To avoid running goroutine immediately and let the controller subscribe first,
|
|
|
|
// use any delay.
|
|
|
|
group := sync.WaitGroup{}
|
|
|
|
group.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer group.Done()
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
|
|
|
|
accountFeed.Send(accountsevent.Event{
|
|
|
|
Type: accountsevent.EventTypeRemoved,
|
|
|
|
Accounts: []common.Address{address},
|
|
|
|
})
|
|
|
|
|
2024-03-26 12:05:49 +00:00
|
|
|
err := utils.Eventually(func() error {
|
2024-03-21 13:00:34 +00:00
|
|
|
entries, err := database.getNewerThan(&assetIdentity{1, []common.Address{address}, "ETH"}, 0)
|
|
|
|
if err == nil && len(entries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("data is not removed")
|
2024-03-26 12:05:49 +00:00
|
|
|
}, 100*time.Millisecond, 10*time.Millisecond)
|
|
|
|
require.NoError(t, err)
|
2024-03-21 13:00:34 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
group.Wait()
|
|
|
|
|
|
|
|
// Stop service
|
|
|
|
txServiceMockCtrl.Finish()
|
|
|
|
server.Stop()
|
|
|
|
service.stopAccountWatcher()
|
|
|
|
}
|