From 213745b201d7fc31cc76fbb69be0862b69dbc4eb Mon Sep 17 00:00:00 2001 From: Abel Armoa <30988000+aarmoa@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:04:50 -0300 Subject: [PATCH 1/3] (fix) Changed ofac.json file path to make it relative for compiled apps to be able to read/download the file --- client/chain/ofac.go | 4 ++-- client/metadata/ofac.json => ofac.json | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename client/metadata/ofac.json => ofac.json (100%) diff --git a/client/chain/ofac.go b/client/chain/ofac.go index eccd14fc..499a38d9 100644 --- a/client/chain/ofac.go +++ b/client/chain/ofac.go @@ -25,7 +25,7 @@ type OfacChecker struct { func NewOfacChecker() (*OfacChecker, error) { checker := &OfacChecker{ - ofacListPath: getOfacListPath(), + ofacListPath: ofacListFilename, } if _, err := os.Stat(checker.ofacListPath); os.IsNotExist(err) { if err := DownloadOfacList(); err != nil { @@ -53,7 +53,7 @@ func DownloadOfacList() error { return fmt.Errorf("failed to download OFAC list, status code: %d", resp.StatusCode) } - outFile, err := os.Create(getOfacListPath()) + outFile, err := os.Create(ofacListFilename) if err != nil { return err } diff --git a/client/metadata/ofac.json b/ofac.json similarity index 100% rename from client/metadata/ofac.json rename to ofac.json From c722ea37c3c3c76d9d4a74c6ff6f0642f24d3cf1 Mon Sep 17 00:00:00 2001 From: Abel Armoa <30988000+aarmoa@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:10:16 -0300 Subject: [PATCH 2/3] (fix) Refactored OFAC tests moving them to their own file. Also improved the test to use a temp test directory --- client/chain/chain_test.go | 89 +++---------------- .../chain/injective_data/ofac.json | 0 client/chain/ofac.go | 19 ++-- client/chain/ofac_test.go | 83 +++++++++++++++++ injective_data/ofac.json | 48 ++++++++++ 5 files changed, 152 insertions(+), 87 deletions(-) rename ofac.json => client/chain/injective_data/ofac.json (100%) create mode 100644 client/chain/ofac_test.go create mode 100644 injective_data/ofac.json diff --git a/client/chain/chain_test.go b/client/chain/chain_test.go index 57466e42..4a0c79a1 100644 --- a/client/chain/chain_test.go +++ b/client/chain/chain_test.go @@ -1,23 +1,20 @@ -package chain +package chain_test import ( - "encoding/json" - "io" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + cosmtypes "github.com/cosmos/cosmos-sdk/types" + eth "github.com/ethereum/go-ethereum/common" "os" "testing" - "github.com/stretchr/testify/suite" - "github.com/InjectiveLabs/sdk-go/client" + "github.com/InjectiveLabs/sdk-go/client/chain" "github.com/InjectiveLabs/sdk-go/client/common" - rpchttp "github.com/cometbft/cometbft/rpc/client/http" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - cosmtypes "github.com/cosmos/cosmos-sdk/types" - eth "github.com/ethereum/go-ethereum/common" ) func accountForTests() (cosmtypes.AccAddress, keyring.Keyring, error) { - senderAddress, cosmosKeyring, err := InitCosmosKeyring( + senderAddress, cosmosKeyring, err := chain.InitCosmosKeyring( os.Getenv("HOME")+"/.injectived", "injectived", "file", @@ -30,9 +27,9 @@ func accountForTests() (cosmtypes.AccAddress, keyring.Keyring, error) { return senderAddress, cosmosKeyring, err } -func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyring, network common.Network) (ChainClient, error) { +func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyring, network common.Network) (chain.ChainClient, error) { tmClient, _ := rpchttp.New(network.TmEndpoint, "/websocket") - clientCtx, err := NewClientContext( + clientCtx, err := chain.NewClientContext( network.ChainId, senderAddress.String(), cosmosKeyring, @@ -46,7 +43,7 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr // configure Keyring as nil to avoid the account initialization request when running unit tests clientCtx.Keyring = nil - chainClient, err := NewChainClient( + chainClient, err := chain.NewChainClient( clientCtx, network, common.OptionGasPrices(client.DefaultGasPriceWithDenom), @@ -55,72 +52,6 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr return chainClient, err } -type OfacTestSuite struct { - suite.Suite - network common.Network - tmClient *rpchttp.HTTP - senderAddress cosmtypes.AccAddress - cosmosKeyring keyring.Keyring -} - -func (suite *OfacTestSuite) SetupTest() { - var err error - suite.network = common.LoadNetwork("testnet", "lb") - suite.tmClient, err = rpchttp.New(suite.network.TmEndpoint, "/websocket") - suite.NoError(err) - - suite.senderAddress, suite.cosmosKeyring, err = accountForTests() - suite.NoError(err) - - // Prepare OFAC list file - testList := []string{ - suite.senderAddress.String(), - } - jsonData, err := json.Marshal(testList) - suite.NoError(err) - - ofacListFilename = "ofac_test.json" - file, err := os.Create(getOfacListPath()) - suite.NoError(err) - - _, err = io.WriteString(file, string(jsonData)) - suite.NoError(err) - - err = file.Close() - suite.NoError(err) -} - -func (suite *OfacTestSuite) TearDownTest() { - err := os.Remove(getOfacListPath()) - suite.NoError(err) - ofacListFilename = defaultofacListFilename -} - -func (suite *OfacTestSuite) TestOfacList() { - clientCtx, err := NewClientContext( - suite.network.ChainId, - suite.senderAddress.String(), - suite.cosmosKeyring, - ) - suite.NoError(err) - - clientCtx = clientCtx.WithNodeURI(suite.network.TmEndpoint).WithClient(suite.tmClient) - testChecker, err := NewOfacChecker() - suite.NoError(err) - suite.Equal(1, len(testChecker.ofacList)) - - _, err = NewChainClient( - clientCtx, - suite.network, - common.OptionGasPrices(client.DefaultGasPriceWithDenom), - ) - suite.Error(err) -} - -func TestOfacTestSuite(t *testing.T) { - suite.Run(t, new(OfacTestSuite)) -} - func TestDefaultSubaccount(t *testing.T) { network := common.LoadNetwork("devnet", "lb") senderAddress, cosmosKeyring, err := accountForTests() diff --git a/ofac.json b/client/chain/injective_data/ofac.json similarity index 100% rename from ofac.json rename to client/chain/injective_data/ofac.json diff --git a/client/chain/ofac.go b/client/chain/ofac.go index 499a38d9..32b5fa66 100644 --- a/client/chain/ofac.go +++ b/client/chain/ofac.go @@ -10,12 +10,12 @@ import ( ) const ( - defaultOfacListURL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/master/wallets/ofac.json" - defaultofacListFilename = "ofac.json" + DefaultOfacListURL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/master/wallets/ofac.json" ) var ( - ofacListFilename = defaultofacListFilename + OfacListPath = "injective_data" + OfacListFilename = "ofac.json" ) type OfacChecker struct { @@ -25,7 +25,7 @@ type OfacChecker struct { func NewOfacChecker() (*OfacChecker, error) { checker := &OfacChecker{ - ofacListPath: ofacListFilename, + ofacListPath: GetOfacListPath(), } if _, err := os.Stat(checker.ofacListPath); os.IsNotExist(err) { if err := DownloadOfacList(); err != nil { @@ -38,12 +38,12 @@ func NewOfacChecker() (*OfacChecker, error) { return checker, nil } -func getOfacListPath() string { - return getFileAbsPath(path.Join("..", "metadata", ofacListFilename)) +func GetOfacListPath() string { + return path.Join(OfacListPath, OfacListFilename) } func DownloadOfacList() error { - resp, err := http.Get(defaultOfacListURL) + resp, err := http.Get(DefaultOfacListURL) if err != nil { return err } @@ -53,7 +53,10 @@ func DownloadOfacList() error { return fmt.Errorf("failed to download OFAC list, status code: %d", resp.StatusCode) } - outFile, err := os.Create(ofacListFilename) + if err := os.MkdirAll(OfacListPath, 0755); err != nil { + return err + } + outFile, err := os.Create(GetOfacListPath()) if err != nil { return err } diff --git a/client/chain/ofac_test.go b/client/chain/ofac_test.go new file mode 100644 index 00000000..ed950a77 --- /dev/null +++ b/client/chain/ofac_test.go @@ -0,0 +1,83 @@ +package chain_test + +import ( + "encoding/json" + "github.com/InjectiveLabs/sdk-go/client" + "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + cosmtypes "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/suite" + "io" + "os" + "testing" +) + +type OfacTestSuite struct { + suite.Suite + network common.Network + tmClient *rpchttp.HTTP + senderAddress cosmtypes.AccAddress + cosmosKeyring keyring.Keyring + originalOfacPath string +} + +func (suite *OfacTestSuite) SetupTest() { + var err error + suite.network = common.LoadNetwork("testnet", "lb") + suite.tmClient, err = rpchttp.New(suite.network.TmEndpoint, "/websocket") + suite.NoError(err) + + suite.senderAddress, suite.cosmosKeyring, err = accountForTests() + suite.NoError(err) + + // Prepare OFAC list file + testList := []string{ + suite.senderAddress.String(), + } + jsonData, err := json.Marshal(testList) + suite.NoError(err) + + suite.originalOfacPath = chain.OfacListPath + chain.OfacListPath = suite.T().TempDir() + suite.NoError(err) + file, err := os.Create(chain.GetOfacListPath()) + suite.NoError(err) + + _, err = io.WriteString(file, string(jsonData)) + suite.NoError(err) + + err = file.Close() + suite.NoError(err) +} + +func (suite *OfacTestSuite) TearDownTest() { + chain.OfacListPath = suite.originalOfacPath +} + +func (suite *OfacTestSuite) TestOfacList() { + clientCtx, err := chain.NewClientContext( + suite.network.ChainId, + suite.senderAddress.String(), + suite.cosmosKeyring, + ) + suite.NoError(err) + + clientCtx = clientCtx.WithNodeURI(suite.network.TmEndpoint).WithClient(suite.tmClient) + testChecker, err := chain.NewOfacChecker() + suite.NoError(err) + suite.True(testChecker.IsBlacklisted(suite.senderAddress.String())) + suite.False(testChecker.IsBlacklisted("inj1")) + + _, err = chain.NewChainClient( + clientCtx, + suite.network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + suite.Error(err) +} + +func TestOfacTestSuite(t *testing.T) { + suite.Run(t, new(OfacTestSuite)) +} diff --git a/injective_data/ofac.json b/injective_data/ofac.json new file mode 100644 index 00000000..59b4160e --- /dev/null +++ b/injective_data/ofac.json @@ -0,0 +1,48 @@ +[ + "0x179f48c78f57a3a78f0608cc9197b8972921d1d2", + "0x1967d8af5bd86a497fb3dd7899a020e47560daaf", + "0x19aa5fe80d33a56d56c78e82ea5e50e5d80b4dff", + "0x19aa5fe80d33a56d56c78e82ea5e50e5d80b4dff", + "0x1da5821544e25c636c1417ba96ade4cf6d2f9b5a", + "0x2f389ce8bd8ff92de3402ffce4691d17fc4f6535", + "0x2f389ce8bd8ff92de3402ffce4691d17fc4f6535", + "0x2f50508a8a3d323b91336fa3ea6ae50e55f32185", + "0x308ed4b7b49797e1a98d3818bff6fe5385410370", + "0x3cbded43efdaf0fc77b9c55f6fc9988fcc9b757d", + "0x3efa30704d2b8bbac821307230376556cf8cc39e", + "0x48549a34ae37b12f6a30566245176994e17c6b4a", + "0x4f47bc496083c727c5fbe3ce9cdf2b0f6496270c", + "0x4f47bc496083c727c5fbe3ce9cdf2b0f6496270c", + "0x4f47bc496083c727c5fbe3ce9cdf2b0f6496270c", + "0x530a64c0ce595026a4a556b703644228179e2d57", + "0x5512d943ed1f7c8a43f3435c85f7ab68b30121b0", + "0x5a7a51bfb49f190e5a6060a5bc6052ac14a3b59f", + "0x5f48c2a71b2cc96e3f0ccae4e39318ff0dc375b2", + "0x6be0ae71e6c41f2f9d0d1a3b8d0f75e6f6a0b46e", + "0x6f1ca141a28907f78ebaa64fb83a9088b02a8352", + "0x746aebc06d2ae31b71ac51429a19d54e797878e9", + "0x77777feddddffc19ff86db637967013e6c6a116c", + "0x797d7ae72ebddcdea2a346c1834e04d1f8df102b", + "0x8576acc5c05d6ce88f4e49bf65bdf0c62f91353c", + "0x901bb9583b24d97e995513c6778dc6888ab6870e", + "0x961c5be54a2ffc17cf4cb021d863c42dacd47fc1", + "0x97b1043abd9e6fc31681635166d430a458d14f9c", + "0x9c2bc757b66f24d60f016b6237f8cdd414a879fa", + "0x9f4cda013e354b8fc285bf4b9a60460cee7f7ea9", + "0xa7e5d5a720f06526557c513402f2e6b5fa20b008", + "0xb6f5ec1a0a9cd1526536d3f0426c429529471f40", + "0xb6f5ec1a0a9cd1526536d3f0426c429529471f40", + "0xb6f5ec1a0a9cd1526536d3f0426c429529471f40", + "0xc455f7fd3e0e12afd51fba5c106909934d8a0e4a", + "0xca0840578f57fe71599d29375e16783424023357", + "0xd0975b32cea532eadddfc9c60481976e39db3472", + "0xd882cfc20f52f2599d84b8e8d58c7fb62cfe344b", + "0xd882cfc20f52f2599d84b8e8d58c7fb62cfe344b", + "0xe1d865c3d669dcc8c57c8d023140cb204e672ee4", + "0xe7aa314c77f4233c18c6cc84384a9247c0cf367b", + "0xed6e0a7e4ac94d976eebfb82ccf777a3c6bad921", + "0xf3701f445b6bdafedbca97d1e477357839e4120d", + "0xfac583c0cf07ea434052c49115a4682172ab6b4f", + "0xfec8a60023265364d066a1212fde3930f6ae8da7", + "0xffbac21a641dcfe4552920138d90f3638b3c9fba" +] From 722a2d3c3c48ed735dc5be811026bdfa8c5c00a6 Mon Sep 17 00:00:00 2001 From: Abel Armoa <30988000+aarmoa@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:27:43 -0300 Subject: [PATCH 3/3] (fix) Fixed pre-commit check issue --- client/chain/chain_test.go | 5 +++-- client/chain/ofac.go | 2 +- client/chain/ofac_test.go | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/client/chain/chain_test.go b/client/chain/chain_test.go index 4a0c79a1..48b24378 100644 --- a/client/chain/chain_test.go +++ b/client/chain/chain_test.go @@ -1,12 +1,13 @@ package chain_test import ( + "os" + "testing" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" "github.com/cosmos/cosmos-sdk/crypto/keyring" cosmtypes "github.com/cosmos/cosmos-sdk/types" eth "github.com/ethereum/go-ethereum/common" - "os" - "testing" "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/chain" diff --git a/client/chain/ofac.go b/client/chain/ofac.go index 32b5fa66..6ae77c16 100644 --- a/client/chain/ofac.go +++ b/client/chain/ofac.go @@ -53,7 +53,7 @@ func DownloadOfacList() error { return fmt.Errorf("failed to download OFAC list, status code: %d", resp.StatusCode) } - if err := os.MkdirAll(OfacListPath, 0755); err != nil { + if err := os.MkdirAll(OfacListPath, 0755); err != nil { // nolint:gocritic // 0755 is the correct permission return err } outFile, err := os.Create(GetOfacListPath()) diff --git a/client/chain/ofac_test.go b/client/chain/ofac_test.go index ed950a77..cc02c8e0 100644 --- a/client/chain/ofac_test.go +++ b/client/chain/ofac_test.go @@ -2,6 +2,10 @@ package chain_test import ( "encoding/json" + "io" + "os" + "testing" + "github.com/InjectiveLabs/sdk-go/client" "github.com/InjectiveLabs/sdk-go/client/chain" "github.com/InjectiveLabs/sdk-go/client/common" @@ -9,9 +13,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" cosmtypes "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/suite" - "io" - "os" - "testing" ) type OfacTestSuite struct {