Skip to content

Commit

Permalink
Remove duplicated random valid namespace (#838)
Browse files Browse the repository at this point in the history
Closes celestiaorg/celestia-app#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.
  • Loading branch information
raphaelts3 authored Oct 7, 2022
1 parent bafe683 commit 38cf37c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 70 deletions.
3 changes: 2 additions & 1 deletion app/estimate_square_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...,
Expand Down
14 changes: 2 additions & 12 deletions app/test/block_size_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app_test

import (
"bytes"
"context"
"encoding/hex"
"testing"
Expand All @@ -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"
Expand Down Expand Up @@ -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)...,
)
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 2 additions & 13 deletions app/test_util.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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...,
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions pkg/namespace/random_ns_generator.go
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 2 additions & 12 deletions pkg/prove/proof_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
}
}
15 changes: 2 additions & 13 deletions testutil/payment/testutil.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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...,
Expand Down Expand Up @@ -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
}
}
}
4 changes: 2 additions & 2 deletions testutil/testnode/full_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
17 changes: 0 additions & 17 deletions testutil/utils.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 38cf37c

Please sign in to comment.