Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[services][feat] #81: Add test for ingesting path payments #84

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions internal/services/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"testing"

"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"github.com/stellar/wallet-backend/internal/apptracker"
Expand Down Expand Up @@ -215,3 +216,166 @@
assert.Equal(t, payments[0].TransactionHash, "abcd")
})
}

func TestIngestPathPaymentsSend(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this a unit test under the TestIngestPayments function? With a t.Run("op_pat_payment_send", func(t *testing.T) { ...} ? That way you can also reuse the same set up code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, good call. Updated in 90f7d32

dbt := dbtest.Open(t)
defer dbt.Close()

dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
require.NoError(t, err)
defer dbConnectionPool.Close()
models, _ := data.NewModels(dbConnectionPool)
mockAppTracker := apptracker.MockAppTracker{}
mockRPCService := RPCServiceMock{}
mockRouter := tssrouter.MockRouter{}
tssStore, _ := tssstore.NewStore(dbConnectionPool)
ingestService, _ := NewIngestService(models, "ingestionLedger", &mockAppTracker, &mockRPCService, &mockRouter, tssStore)
srcAccount := keypair.MustRandom().Address()
destAccount := keypair.MustRandom().Address()
usdIssuer := keypair.MustRandom().Address()
eurIssuer := keypair.MustRandom().Address()

t.Run("test_op_path_payment_send", func(t *testing.T) {
err := models.Account.Insert(context.Background(), srcAccount)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicking here but I don't really think you require this line. This is not a test to see if models are working. I'd replace it with _ := models.Account.Insert(context.Background(), srcAccount)


path := []txnbuild.Asset{
txnbuild.CreditAsset{Code: "USD", Issuer: usdIssuer},
txnbuild.CreditAsset{Code: "EUR", Issuer: eurIssuer},
}

pathPaymentOp := txnbuild.PathPaymentStrictSend{
SourceAccount: srcAccount,
Destination: destAccount,
DestMin: "9",
SendAmount: "10",
SendAsset: txnbuild.NativeAsset{},
DestAsset: txnbuild.NativeAsset{},
Path: path,
}
transaction, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.MustRandom().Address(),
},
Operations: []txnbuild.Operation{&pathPaymentOp},
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(10)},
})
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above. This can be replaced with:

	transaction, _ := txnbuild.NewTransaction(txnbuild.TransactionParams{
		SourceAccount: &txnbuild.SimpleAccount{
			AccountID: keypair.MustRandom().Address(),
		},
		Operations:    []txnbuild.Operation{&pathPaymentOp},
		Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(10)},
	})


signer := keypair.MustRandom()
errSigner := models.Account.Insert(context.Background(), signer.Address())
require.NoError(t, errSigner)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


signedTx, err := transaction.Sign(network.TestNetworkPassphrase, signer)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


txEnvXDR, err := signedTx.Base64()
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


ledgerTransaction := entities.Transaction{
Status: entities.SuccessStatus,
Hash: "efgh",
ApplicationOrder: 1,
FeeBump: false,
EnvelopeXDR: txEnvXDR,
ResultXDR: "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAANAAAAAAAAAAAAAAAAXF0V022EgeGIo6QNVbMVvHdxvvl2MfZcVZUJpfph+0QAAAAAAAAAAAX14QAAAAAA",
Ledger: 1,
}

ledgerTransactions := []entities.Transaction{ledgerTransaction}

err = ingestService.ingestPayments(context.Background(), ledgerTransactions)

Check failure on line 287 in internal/services/ingest_test.go

View workflow job for this annotation

GitHub Actions / check

ineffectual assignment to err (ineffassign)

payments, _, _, err := models.Payments.GetPaymentsPaginated(context.Background(), srcAccount, "", "", data.ASC, 1)
require.NoError(t, err)
require.NotEmpty(t, payments, "Expected at least one payment")
assert.Equal(t, payments[0].TransactionHash, *&ledgerTransaction.Hash)

Check failure on line 292 in internal/services/ingest_test.go

View workflow job for this annotation

GitHub Actions / check

SA4001: *&x will be simplified to x. It will not copy x. (staticcheck)
assert.Equal(t, payments[0].SrcAmount, int64(100000000))
assert.Equal(t, payments[0].SrcAssetType, xdr.AssetTypeAssetTypeNative.String())
assert.Equal(t, payments[0].ToAddress, destAccount)
assert.Equal(t, payments[0].FromAddress, srcAccount)
assert.Equal(t, payments[0].SrcAssetCode, "XLM")
assert.Equal(t, payments[0].DestAssetCode, "XLM")
})
}

func TestIngestPathPaymentsReceive(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Can you make this a unit test under the TestIngestPayments function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in 90f7d32

dbt := dbtest.Open(t)
defer dbt.Close()

dbConnectionPool, err := db.OpenDBConnectionPool(dbt.DSN)
require.NoError(t, err)
defer dbConnectionPool.Close()
models, _ := data.NewModels(dbConnectionPool)
mockAppTracker := apptracker.MockAppTracker{}
mockRPCService := RPCServiceMock{}
mockRouter := tssrouter.MockRouter{}
tssStore, _ := tssstore.NewStore(dbConnectionPool)
ingestService, _ := NewIngestService(models, "ingestionLedger", &mockAppTracker, &mockRPCService, &mockRouter, tssStore)
srcAccount := keypair.MustRandom().Address()
destAccount := keypair.MustRandom().Address()
usdIssuer := keypair.MustRandom().Address()
eurIssuer := keypair.MustRandom().Address()

t.Run("test_op_path_payment_receive", func(t *testing.T) {
err := models.Account.Insert(context.Background(), srcAccount)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


path := []txnbuild.Asset{
txnbuild.CreditAsset{Code: "USD", Issuer: usdIssuer},
txnbuild.CreditAsset{Code: "EUR", Issuer: eurIssuer},
}

pathPaymentOp := txnbuild.PathPaymentStrictReceive{
SourceAccount: srcAccount,
Destination: destAccount,
SendMax: "11",
DestAmount: "10",
SendAsset: txnbuild.NativeAsset{},
DestAsset: txnbuild.NativeAsset{},
Path: path,
}
transaction, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.MustRandom().Address(),
},
Operations: []txnbuild.Operation{&pathPaymentOp},
Preconditions: txnbuild.Preconditions{TimeBounds: txnbuild.NewTimeout(10)},
})
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


signer := keypair.MustRandom()
errSigner := models.Account.Insert(context.Background(), signer.Address())
require.NoError(t, errSigner)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


signedTx, err := transaction.Sign(network.TestNetworkPassphrase, signer)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


txEnvXDR, err := signedTx.Base64()
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


ledgerTransaction := entities.Transaction{
Status: entities.SuccessStatus,
Hash: "efgh",
ApplicationOrder: 1,
FeeBump: false,
EnvelopeXDR: txEnvXDR,
ResultXDR: "AAAAAAAAAGQAAAAAAAAAAQAAAAAAAAACAAAAAAAAAAAAAAAAjOiEfRh4kaFVQDu/CSTZLMtnyg0DbNowZ/G2nLES3KwAAAAAAAAAAAVdSoAAAAAA",
Ledger: 1,
}

ledgerTransactions := []entities.Transaction{ledgerTransaction}

err = ingestService.ingestPayments(context.Background(), ledgerTransactions)

Check failure on line 369 in internal/services/ingest_test.go

View workflow job for this annotation

GitHub Actions / check

ineffectual assignment to err (ineffassign)

payments, _, _, err := models.Payments.GetPaymentsPaginated(context.Background(), srcAccount, "", "", data.ASC, 1)
require.NoError(t, err)
require.NotEmpty(t, payments, "Expected at least one payment")
assert.Equal(t, payments[0].TransactionHash, *&ledgerTransaction.Hash)

Check failure on line 374 in internal/services/ingest_test.go

View workflow job for this annotation

GitHub Actions / check

SA4001: *&x will be simplified to x. It will not copy x. (staticcheck)
assert.Equal(t, payments[0].SrcAssetType, xdr.AssetTypeAssetTypeNative.String())
assert.Equal(t, payments[0].ToAddress, destAccount)
assert.Equal(t, payments[0].FromAddress, srcAccount)
assert.Equal(t, payments[0].SrcAssetCode, "XLM")
assert.Equal(t, payments[0].DestAssetCode, "XLM")
})
}
Loading