From 38cf37c9d30c11ce34ffac30fe8d42818d64524d Mon Sep 17 00:00:00 2001 From: raphaelts3 Date: Thu, 6 Oct 2022 22:04:58 -0300 Subject: [PATCH] Remove duplicated random valid namespace (#838) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes https://github.com/celestiaorg/celestia-app/issues/836 Hey, @rootulp I'm not an expert go, so I don't know if the move I've made is the best possible, but it did its job. That said if you have any suggestions to improve that I'm open to adjusting it. PS: Since the repo doesn't contain the tag `hacktoberfest`, would be possible to label this PR with `hacktoberfest-accepted` so it can be validated there by the rule below? > PR/MRs that also have the “hacktoberfest-accepted” label cannot be marked as spammy via a label. --- app/estimate_square_size_test.go | 3 ++- app/test/block_size_test.go | 14 ++------------ app/test_util.go | 15 ++------------- pkg/namespace/random_ns_generator.go | 22 ++++++++++++++++++++++ pkg/prove/proof_test.go | 14 ++------------ testutil/payment/testutil.go | 15 ++------------- testutil/testnode/full_node_test.go | 4 ++-- testutil/utils.go | 17 ----------------- 8 files changed, 34 insertions(+), 70 deletions(-) create mode 100644 pkg/namespace/random_ns_generator.go diff --git a/app/estimate_square_size_test.go b/app/estimate_square_size_test.go index 9f5d2434..a26f1d07 100644 --- a/app/estimate_square_size_test.go +++ b/app/estimate_square_size_test.go @@ -5,6 +5,7 @@ import ( "github.com/celestiaorg/celestia-app/app/encoding" "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/celestiaorg/celestia-app/pkg/shares" "github.com/celestiaorg/celestia-app/x/payment/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -127,7 +128,7 @@ func Test_overEstimateMalleatedTxSize(t *testing.T) { wpfdTx := generateRawWirePFDTx( t, encConf.TxConfig, - randomValidNamespace(), + namespace.RandomMessageNamespace(), tmrand.Bytes(tt.size), signer, tt.opts..., diff --git a/app/test/block_size_test.go b/app/test/block_size_test.go index 7ba0884c..82697c40 100644 --- a/app/test/block_size_test.go +++ b/app/test/block_size_test.go @@ -1,7 +1,6 @@ package app_test import ( - "bytes" "context" "encoding/hex" "testing" @@ -17,10 +16,10 @@ import ( "github.com/celestiaorg/celestia-app/app" "github.com/celestiaorg/celestia-app/app/encoding" "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/celestiaorg/celestia-app/testutil/network" "github.com/celestiaorg/celestia-app/x/payment" "github.com/celestiaorg/celestia-app/x/payment/types" - "github.com/celestiaorg/nmt/namespace" abci "github.com/tendermint/tendermint/abci/types" tmrand "github.com/tendermint/tendermint/libs/rand" rpctypes "github.com/tendermint/tendermint/rpc/core/types" @@ -265,7 +264,7 @@ func generateSignedWirePayForDataTxs(clientCtx client.Context, txConfig client.T // create a msg msg, err := types.NewWirePayForData( - randomValidNamespace(), + namespace.RandomMessageNamespace(), tmrand.Bytes(thisMessageSize), types.AllSquareSizes(thisMessageSize)..., ) @@ -296,15 +295,6 @@ func generateSignedWirePayForDataTxs(clientCtx client.Context, txConfig client.T return txs, nil } -func randomValidNamespace() namespace.ID { - for { - s := tmrand.Bytes(8) - if bytes.Compare(s, appconsts.MaxReservedNamespace) > 0 { - return s - } - } -} - func queryTx(clientCtx client.Context, hashHexStr string, prove bool) (*rpctypes.ResultTx, error) { hash, err := hex.DecodeString(hashHexStr) if err != nil { diff --git a/app/test_util.go b/app/test_util.go index afebf65a..86d35ea1 100644 --- a/app/test_util.go +++ b/app/test_util.go @@ -1,13 +1,11 @@ package app import ( - "bytes" "testing" "github.com/celestiaorg/celestia-app/app/encoding" - "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/celestiaorg/celestia-app/x/payment/types" - "github.com/celestiaorg/nmt/namespace" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" @@ -68,7 +66,7 @@ func generateManyRawWirePFD(t *testing.T, txConfig client.TxConfig, signer *type wpfdTx := generateRawWirePFDTx( t, txConfig, - randomValidNamespace(), + namespace.RandomMessageNamespace(), tmrand.Bytes(size), signer, opts..., @@ -175,15 +173,6 @@ func generateKeyring(t *testing.T, cdc codec.Codec, accts ...string) keyring.Key return kb } -func randomValidNamespace() namespace.ID { - for { - s := tmrand.Bytes(8) - if bytes.Compare(s, appconsts.MaxReservedNamespace) > 0 { - return s - } - } -} - // generateKeyringSigner creates a types.KeyringSigner with keys generated for // the provided accounts func generateKeyringSigner(t *testing.T, acct string) *types.KeyringSigner { diff --git a/pkg/namespace/random_ns_generator.go b/pkg/namespace/random_ns_generator.go new file mode 100644 index 00000000..231fb96d --- /dev/null +++ b/pkg/namespace/random_ns_generator.go @@ -0,0 +1,22 @@ +package namespace + +import ( + "bytes" + + "github.com/celestiaorg/celestia-app/pkg/appconsts" + nmtnamespace "github.com/celestiaorg/nmt/namespace" + tmrand "github.com/tendermint/tendermint/libs/rand" +) + +func RandomMessageNamespace() nmtnamespace.ID { + for { + ns := tmrand.Bytes(8) + isReservedNS := bytes.Compare(ns, appconsts.MaxReservedNamespace) <= 0 + isParityNS := bytes.Equal(ns, appconsts.ParitySharesNamespaceID) + isTailPaddingNS := bytes.Equal(ns, appconsts.TailPaddingNamespaceID) + if isReservedNS || isParityNS || isTailPaddingNS { + continue + } + return ns + } +} diff --git a/pkg/prove/proof_test.go b/pkg/prove/proof_test.go index f12be088..e8783d3c 100644 --- a/pkg/prove/proof_test.go +++ b/pkg/prove/proof_test.go @@ -1,13 +1,12 @@ package prove import ( - "bytes" "math/rand" "testing" "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/celestiaorg/celestia-app/pkg/shares" - "github.com/celestiaorg/nmt/namespace" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" tmrand "github.com/tendermint/tendermint/libs/rand" @@ -281,17 +280,8 @@ func generateRandomlySizedMessages(count, maxMsgSize int) types.Messages { func generateRandomMessage(size int) types.Message { msg := types.Message{ - NamespaceID: randomValidNamespace(), + NamespaceID: namespace.RandomMessageNamespace(), Data: tmrand.Bytes(size), } return msg } - -func randomValidNamespace() namespace.ID { - for { - s := tmrand.Bytes(8) - if bytes.Compare(s, appconsts.MaxReservedNamespace) > 0 { - return s - } - } -} diff --git a/testutil/payment/testutil.go b/testutil/payment/testutil.go index 55b3af4d..ec96e234 100644 --- a/testutil/payment/testutil.go +++ b/testutil/payment/testutil.go @@ -1,13 +1,11 @@ package paytestutil import ( - "bytes" "testing" "github.com/celestiaorg/celestia-app/app" - "github.com/celestiaorg/celestia-app/pkg/appconsts" + "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/celestiaorg/celestia-app/x/payment/types" - "github.com/celestiaorg/nmt/namespace" "github.com/cosmos/cosmos-sdk/client" sdk "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -43,7 +41,7 @@ func GenerateManyRawWirePFD(t *testing.T, txConfig client.TxConfig, signer *type wpfdTx := generateRawWirePFDTx( t, txConfig, - randomValidNamespace(), + namespace.RandomMessageNamespace(), tmrand.Bytes(size), signer, opts..., @@ -130,12 +128,3 @@ func generateSignedWirePayForData(t *testing.T, ns, message []byte, signer *type const ( TestAccountName = "test-account" ) - -func randomValidNamespace() namespace.ID { - for { - s := tmrand.Bytes(8) - if bytes.Compare(s, appconsts.MaxReservedNamespace) > 0 { - return s - } - } -} diff --git a/testutil/testnode/full_node_test.go b/testutil/testnode/full_node_test.go index c067703b..940aa2e8 100644 --- a/testutil/testnode/full_node_test.go +++ b/testutil/testnode/full_node_test.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "github.com/celestiaorg/celestia-app/testutil" + "github.com/celestiaorg/celestia-app/pkg/namespace" "github.com/stretchr/testify/suite" tmrand "github.com/tendermint/tendermint/libs/rand" coretypes "github.com/tendermint/tendermint/rpc/core/types" @@ -75,7 +75,7 @@ func (s *IntegrationTestSuite) Test_Liveness() { func (s *IntegrationTestSuite) Test_PostData() { require := s.Require() - _, err := s.cctx.PostData(s.accounts[0], testutil.RandomValidNamespace(), tmrand.Bytes(100000)) + _, err := s.cctx.PostData(s.accounts[0], namespace.RandomMessageNamespace(), tmrand.Bytes(100000)) require.NoError(err) } diff --git a/testutil/utils.go b/testutil/utils.go index 54da7a12..c6db4c0c 100644 --- a/testutil/utils.go +++ b/testutil/utils.go @@ -1,30 +1,13 @@ package testutil import ( - "bytes" "context" "encoding/hex" - "github.com/celestiaorg/celestia-app/pkg/appconsts" - "github.com/celestiaorg/nmt/namespace" "github.com/cosmos/cosmos-sdk/client" - tmrand "github.com/tendermint/tendermint/libs/rand" rpctypes "github.com/tendermint/tendermint/rpc/core/types" ) -func RandomValidNamespace() namespace.ID { - for { - ns := tmrand.Bytes(8) - isReservedNS := bytes.Compare(ns, appconsts.MaxReservedNamespace) <= 0 - isParityNS := bytes.Equal(ns, appconsts.ParitySharesNamespaceID) - isTailPaddingNS := bytes.Equal(ns, appconsts.TailPaddingNamespaceID) - if isReservedNS || isParityNS || isTailPaddingNS { - continue - } - return ns - } -} - func QueryWithOutProof(clientCtx client.Context, hashHexStr string) (*rpctypes.ResultTx, error) { hash, err := hex.DecodeString(hashHexStr) if err != nil {