diff --git a/testsuite/storjscan/backfill_test.go b/testsuite/storjscan/backfill_test.go new file mode 100644 index 000000000..a30e1ad94 --- /dev/null +++ b/testsuite/storjscan/backfill_test.go @@ -0,0 +1,122 @@ +// Copyright (C) 2022 Storj Labs, Inc. +// See LICENSE for copying information. + +package storjscan + +import ( + "math/big" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "storj.io/common/currency" + "storj.io/common/testcontext" + blockchain2 "storj.io/storj/private/blockchain" + "storj.io/storj/private/testplanet" + "storj.io/storj/satellite/payments" + "storj.io/storj/testsuite/storjscan/storjscantest" + "storj.io/storjscan/blockchain" + "storj.io/storjscan/private/testeth/testtoken" +) + +func TestBackfillPayments(t *testing.T) { + storjscantest.Run(t, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, stack *storjscantest.Stack) { + receiver, _ := blockchain.AddressFromHex("0x27e3d303B0B70B1b17f14525b48Ae7c45D34666f") + err := stack.App.Wallets.Service.Register(ctx, "eu", map[blockchain.Address]string{ + receiver: "test", + }) + require.NoError(t, err) + + sat := planet.Satellites[0] + sat.Core.Payments.StorjscanChore.TransactionCycle.Pause() + + // claim wallet + _, err = sat.API.Payments.StorjscanClient.ClaimNewEthAddress(ctx) + require.NoError(t, err) + + client := stack.Network.Dial() + defer client.Close() + accs := stack.Network.Accounts() + + tk, err := testtoken.NewTestToken(blockchain.Address(stack.Token), client) + require.NoError(t, err) + + opts := stack.Network.TransactOptions(ctx, accs[0], 1) + tx, err := tk.Transfer(opts, receiver, big.NewInt(10000)) + require.NoError(t, err) + rcpt, err := stack.Network.WaitForTx(ctx, tx.Hash()) + require.NoError(t, err) + + block, err := client.BlockByNumber(ctx, rcpt.BlockNumber) + require.NoError(t, err) + blockTime := time.Unix(int64(block.Time()), 0) + + sat.Core.Payments.StorjscanChore.TransactionCycle.TriggerWait() + + pmnts, err := sat.API.Payments.StorjscanService.Payments(ctx, blockchain2.Address(receiver), 1, 0) + require.NoError(t, err) + require.Len(t, pmnts, 1) + + expected := payments.WalletPayment{ + From: blockchain2.Address(accs[0].Address), + To: blockchain2.Address(receiver), + TokenValue: currency.AmountFromBaseUnits(10000, currency.StorjToken), + USDValue: currency.AmountFromBaseUnits(100, currency.USDollarsMicro), + Status: payments.PaymentStatusPending, + BlockHash: blockchain2.Hash(block.Hash()), + BlockNumber: block.Number().Int64(), + Transaction: blockchain2.Hash(rcpt.TxHash), + LogIndex: 0, + Timestamp: blockTime.UTC(), + } + require.Equal(t, expected, pmnts[0]) + + // disable + err = stack.CloseApp() + require.NoError(t, err) + + // send 2nd tx + opts = stack.Network.TransactOptions(ctx, accs[0], 2) + tx, err = tk.Transfer(opts, receiver, big.NewInt(10000)) + require.NoError(t, err) + rcpt, err = stack.Network.WaitForTx(ctx, tx.Hash()) + require.NoError(t, err) + + block, err = client.BlockByNumber(ctx, rcpt.BlockNumber) + require.NoError(t, err) + blockTime = time.Unix(int64(block.Time()), 0) + + sat.Core.Payments.StorjscanChore.TransactionCycle.TriggerWait() + + // ensure there is still only one pmnt in the db + pmnts, err = sat.API.Payments.StorjscanService.Payments(ctx, blockchain2.Address(receiver), 2, 0) + require.NoError(t, err) + require.Len(t, pmnts, 1) + require.Equal(t, expected, pmnts[0]) + + // Start storjscan + err = stack.StartApp() + require.NoError(t, err) + + sat.Core.Payments.StorjscanChore.TransactionCycle.TriggerWait() + + pmnts, err = sat.API.Payments.StorjscanService.Payments(ctx, blockchain2.Address(receiver), 2, 0) + require.NoError(t, err) + require.Len(t, pmnts, 2) + + expected = payments.WalletPayment{ + From: blockchain2.Address(accs[0].Address), + To: blockchain2.Address(receiver), + TokenValue: currency.AmountFromBaseUnits(10000, currency.StorjToken), + USDValue: currency.AmountFromBaseUnits(100, currency.USDollarsMicro), + Status: payments.PaymentStatusPending, + BlockHash: blockchain2.Hash(block.Hash()), + BlockNumber: block.Number().Int64(), + Transaction: blockchain2.Hash(rcpt.TxHash), + LogIndex: 0, + Timestamp: blockTime.UTC(), + } + require.Equal(t, expected, pmnts[0]) + }) +} diff --git a/testsuite/storjscan/go.mod b/testsuite/storjscan/go.mod index 6c9b00662..aed2be8b1 100644 --- a/testsuite/storjscan/go.mod +++ b/testsuite/storjscan/go.mod @@ -13,7 +13,7 @@ require ( storj.io/common v0.0.0-20220912074536-0fff01212055 storj.io/private v0.0.0-20220915132359-957cab776577 storj.io/storj v1.63.1 - storj.io/storjscan v0.0.0-20220909003402-28d145ea1272 + storj.io/storjscan v0.0.0-20220926140643-1623c3b391b0 storj.io/uplink v1.9.1-0.20220811092921-37dccde06f31 ) diff --git a/testsuite/storjscan/go.sum b/testsuite/storjscan/go.sum index 92b6af660..6173e4f92 100644 --- a/testsuite/storjscan/go.sum +++ b/testsuite/storjscan/go.sum @@ -1266,7 +1266,7 @@ storj.io/monkit-jaeger v0.0.0-20220726162929-c3a9898b5bca/go.mod h1:iK+dmHZZXQlW storj.io/private v0.0.0-20220823161836-79135887c9be/go.mod h1:GbCItLcLLzsrMClBAa2aaj8xlawtHXWrqhr3D1I3NPM= storj.io/private v0.0.0-20220915132359-957cab776577 h1:Oy9maD0fOrtoUkhpkOMBtLH0QyQMNVNT6NtFg+ONM5w= storj.io/private v0.0.0-20220915132359-957cab776577/go.mod h1:GbCItLcLLzsrMClBAa2aaj8xlawtHXWrqhr3D1I3NPM= -storj.io/storjscan v0.0.0-20220909003402-28d145ea1272 h1:ZUsmoEwpTo9w2h82K61IgNQUyKOAEE6P8TduRyUknAM= -storj.io/storjscan v0.0.0-20220909003402-28d145ea1272/go.mod h1:5nLgAOl1KTDVyqORAhvrp+167PtShEuS1L3pJgXPjwo= +storj.io/storjscan v0.0.0-20220926140643-1623c3b391b0 h1:pSfGf9E9OlUd17W7LSpL4tTONIyFji6dz8I2iTDd8BY= +storj.io/storjscan v0.0.0-20220926140643-1623c3b391b0/go.mod h1:5nLgAOl1KTDVyqORAhvrp+167PtShEuS1L3pJgXPjwo= storj.io/uplink v1.9.1-0.20220811092921-37dccde06f31 h1:wRpjo1lgzigTRWEFNj50VW8wRigetEpHvQZ/5aO1O20= storj.io/uplink v1.9.1-0.20220811092921-37dccde06f31/go.mod h1:YHtYuVqGUtczViQPlPqfXz3+3Hgyzh91UcV5SlJV27w= diff --git a/testsuite/storjscan/storjscantest/run.go b/testsuite/storjscan/storjscantest/run.go index 296b3dfcc..399539179 100644 --- a/testsuite/storjscan/storjscantest/run.go +++ b/testsuite/storjscan/storjscantest/run.go @@ -33,10 +33,12 @@ var mon = monkit.Package() // Stack contains references to storjscan app and eth test network. type Stack struct { - Log *zap.Logger - App *storjscan.App - Network *testeth.Network - Token blockchain.Address + Log *zap.Logger + App *storjscan.App + StartApp func() error + CloseApp func() error + Network *testeth.Network + Token blockchain.Address } // Test defines common services for storjscan tests. @@ -132,18 +134,38 @@ func Run(t *testing.T, test Test) { var run errgroup.Group runCtx, runCancel := context.WithCancel(ctx) - run.Go(func() error { - err := stack.App.Run(runCtx) - return err - }) - defer ctx.Check(func() error { + stack.StartApp = func() error { + storjscanConfig.API.Address = stack.App.API.Listener.Addr().String() + + stack.App, err = storjscan.NewApp(stack.Log.Named("app"), storjscanConfig, storjscanDB) + if err != nil { + return err + } + + runCtx, runCancel = context.WithCancel(ctx) + + run = errgroup.Group{} + run.Go(func() error { + err := stack.App.Run(runCtx) + return err + }) + + return nil + } + stack.CloseApp = func() error { runCancel() var errlist errs.Group errlist.Add(run.Wait()) errlist.Add(stack.App.Close()) return errlist.Err() + } + + run.Go(func() error { + err := stack.App.Run(runCtx) + return err }) + defer ctx.Check(stack.CloseApp) // ------------ planetConfig.Reconfigure = testplanet.Reconfigure{Satellite: func(log *zap.Logger, index int, config *satellite.Config) {