Skip to content

Commit

Permalink
#4221: added account filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Mar 4, 2022
1 parent f5a092d commit 580e59c
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 46 deletions.
139 changes: 139 additions & 0 deletions services/horizon/internal/ingest/filters/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package filters

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/go/ingest"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/xdr"
)

func TestAccountFilterHasMatch(t *testing.T) {
tt := assert.New(t)
ctx := context.Background()

filterConfig := &history.FilterConfig{
Rules: `{
"account_whitelist": [
"GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"
]
}`,
Enabled: true,
LastModified: 1,
Name: FilterAccountFilterName,
}
filter := NewAccountFilter()
err := filter.RefreshAccountFilter(filterConfig)
tt.NoError(err)

result, err := filter.FilterTransaction(ctx, getAccountTestTx(t,
"GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL",
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"))

tt.NoError(err)
tt.Equal(result, true)
}

func TestAccountFilterEnablesWhenEmptyWhitelist(t *testing.T) {
tt := assert.New(t)
ctx := context.Background()

filterConfig := &history.FilterConfig{
Rules: `{
"account_whitelist": []
}`,
Enabled: true,
LastModified: 1,
Name: FilterAssetFilterName,
}
filter := NewAccountFilter()
err := filter.RefreshAccountFilter(filterConfig)
tt.NoError(err)

result, err := filter.FilterTransaction(ctx, getAccountTestTx(t,
"GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL",
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"))

tt.NoError(err)
tt.Equal(result, true)
}

func TestAccountFilterHasNoMatch(t *testing.T) {
tt := assert.New(t)
ctx := context.Background()

filterConfig := &history.FilterConfig{
Rules: `{
"account_whitelist": [
"GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"
]
}`,

Enabled: true,
LastModified: 1,
Name: FilterAssetFilterName,
}

filter := NewAccountFilter()
err := filter.RefreshAccountFilter(filterConfig)
tt.NoError(err)

result, err := filter.FilterTransaction(ctx, getAccountTestTx(t,
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H",
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H"))

tt.NoError(err)
tt.Equal(result, false)
}

func getAccountTestTx(t *testing.T, accountId string, issuer string) ingest.LedgerTransaction {

var xdrAssetCode [12]byte
var xdrIssuer xdr.AccountId
copy(xdrAssetCode[:], "USDC")
require.NoError(t, xdrIssuer.SetAddress(issuer))

return ingest.LedgerTransaction{
UnsafeMeta: xdr.TransactionMeta{
V: 1,
V1: &xdr.TransactionMetaV1{
Operations: []xdr.OperationMeta{},
},
},
Result: xdr.TransactionResultPair{
Result: xdr.TransactionResult{
Result: xdr.TransactionResultResult{
Code: xdr.TransactionResultCodeTxSuccess,
},
},
},
Envelope: xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
SourceAccount: xdr.MustMuxedAddress(accountId),
Operations: []xdr.Operation{
{Body: xdr.OperationBody{
Type: xdr.OperationTypePayment,
PaymentOp: &xdr.PaymentOp{
Destination: xdr.MustMuxedAddress(accountId),
Asset: xdr.Asset{
Type: xdr.AssetTypeAssetTypeCreditAlphanum12,
AlphaNum12: &xdr.AlphaNum12{
AssetCode: xdrAssetCode,
Issuer: xdrIssuer,
},
},
Amount: 100,
},
}},
},
},
},
},
}
}
4 changes: 4 additions & 0 deletions services/horizon/internal/ingest/filters/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (filter *assetFilter) RefreshAssetFilter(filterConfig *history.FilterConfig

func (f *assetFilter) FilterTransaction(ctx context.Context, transaction ingest.LedgerTransaction) (bool, error) {

if len(f.canonicalAssetsLookup) < 1 {
return true, nil
}

tx, v1Exists := transaction.Envelope.GetV1()
if !v1Exists {
return true, nil
Expand Down
78 changes: 32 additions & 46 deletions services/horizon/internal/ingest/filters/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stellar/go/xdr"
)

func TestFilterHasMatch(t *testing.T) {
func TestAssetFilterHasMatch(t *testing.T) {
tt := assert.New(t)
ctx := context.Background()

Expand All @@ -30,51 +30,35 @@ func TestFilterHasMatch(t *testing.T) {
err := filter.RefreshAssetFilter(filterConfig)
tt.NoError(err)

var xdrAssetCode [12]byte
copy(xdrAssetCode[:], "USDC")
var xdrIssuer xdr.AccountId
require.NoError(t, xdrIssuer.SetAddress("GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"))
result, err := filter.FilterTransaction(ctx, getAssetTestTx(t, "GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"))

ledgerTx := ingest.LedgerTransaction{
Result: xdr.TransactionResultPair{
Result: xdr.TransactionResult{
Result: xdr.TransactionResultResult{
Code: xdr.TransactionResultCodeTxSuccess,
},
},
},
Envelope: xdr.TransactionEnvelope{
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
V1: &xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Operations: []xdr.Operation{
{Body: xdr.OperationBody{
Type: xdr.OperationTypePayment,
PaymentOp: &xdr.PaymentOp{
Destination: xdr.MustMuxedAddress("GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"),
Asset: xdr.Asset{
Type: xdr.AssetTypeAssetTypeCreditAlphanum12,
AlphaNum12: &xdr.AlphaNum12{
AssetCode: xdrAssetCode,
Issuer: xdrIssuer,
},
},
Amount: 100,
},
}},
},
},
},
},
tt.NoError(err)
tt.Equal(result, true)
}

func TestAssetFilterEnablesWhenEmptyWhitelist(t *testing.T) {
tt := assert.New(t)
ctx := context.Background()

filterConfig := &history.FilterConfig{
Rules: `{
"canonical_asset_whitelist": []
}`,
Enabled: true,
LastModified: 1,
Name: FilterAssetFilterName,
}
filter := NewAssetFilter()
err := filter.RefreshAssetFilter(filterConfig)
tt.NoError(err)

result, err := filter.FilterTransaction(ctx, ledgerTx)
result, err := filter.FilterTransaction(ctx, getAssetTestTx(t, "GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"))

tt.NoError(err)
tt.Equal(result, true)
}

func TestFilterHasNoMatch(t *testing.T) {
func TestAssetFilterHasNoMatch(t *testing.T) {
tt := assert.New(t)
ctx := context.Background()

Expand All @@ -94,12 +78,19 @@ func TestFilterHasNoMatch(t *testing.T) {
err := filter.RefreshAssetFilter(filterConfig)
tt.NoError(err)

result, err := filter.FilterTransaction(ctx, getAssetTestTx(t, "GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"))

tt.NoError(err)
tt.Equal(result, false)
}

func getAssetTestTx(t *testing.T, issuer string) ingest.LedgerTransaction {
var xdrAssetCode [12]byte
copy(xdrAssetCode[:], "USDC")
var xdrIssuer xdr.AccountId
require.NoError(t, xdrIssuer.SetAddress("GD6WNNTW664WH7FXC5RUMUTF7P5QSURC2IT36VOQEEGFZ4UWUEQGECAL"))
copy(xdrAssetCode[:], "USDC")
require.NoError(t, xdrIssuer.SetAddress(issuer))

ledgerTx := ingest.LedgerTransaction{
return ingest.LedgerTransaction{
Result: xdr.TransactionResultPair{
Result: xdr.TransactionResult{
Result: xdr.TransactionResultResult{
Expand Down Expand Up @@ -131,9 +122,4 @@ func TestFilterHasNoMatch(t *testing.T) {
},
},
}

result, err := filter.FilterTransaction(ctx, ledgerTx)

tt.NoError(err)
tt.Equal(result, false)
}

0 comments on commit 580e59c

Please sign in to comment.