-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
175 additions
and
46 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
services/horizon/internal/ingest/filters/account_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters