-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into evan/data-square-layout-specs-part-1
- Loading branch information
Showing
6 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
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,102 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/celestiaorg/celestia-app/x/qgb/types" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
cdctypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
"github.com/cosmos/cosmos-sdk/std" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// GetQueryCmd returns the CLI query commands for this module | ||
func GetQueryCmd() *cobra.Command { | ||
// Group qgb queries under a subcommand | ||
cmd := &cobra.Command{ | ||
Use: types.ModuleName, | ||
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName), | ||
DisableFlagParsing: true, | ||
SuggestionsMinimumDistance: 2, | ||
RunE: client.ValidateCmd, | ||
} | ||
|
||
cmd.AddCommand(CmdQueryAttestationByNonce()) | ||
|
||
return cmd | ||
} | ||
|
||
func CmdQueryAttestationByNonce() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "attestation <nonce>", | ||
Aliases: []string{"att"}, | ||
Short: "query an attestation by nonce", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx := client.GetClientContextFromCmd(cmd) | ||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
nonce, err := strconv.ParseUint(args[0], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
res, err := queryClient.AttestationRequestByNonce( | ||
cmd.Context(), | ||
&types.QueryAttestationRequestByNonceRequest{Nonce: nonce}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
if res.Attestation == nil { | ||
return types.ErrNilAttestation | ||
} | ||
att, err := unmarshallAttestation(res.Attestation) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch att.(type) { | ||
case *types.Valset, *types.DataCommitment: | ||
jsonDC, err := json.Marshal(att) | ||
if err != nil { | ||
return err | ||
} | ||
return clientCtx.PrintString(string(jsonDC)) | ||
default: | ||
return types.ErrUnknownAttestationType | ||
} | ||
}, | ||
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
// unmarshallAttestation unmarshal a wrapper protobuf `Any` type to an `AttestationRequestI`. | ||
func unmarshallAttestation(attestation *cdctypes.Any) (types.AttestationRequestI, error) { | ||
var unmarshalledAttestation types.AttestationRequestI | ||
err := makeInterfaceRegistry().UnpackAny(attestation, &unmarshalledAttestation) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return unmarshalledAttestation, nil | ||
} | ||
|
||
// makeInterfaceRegistry creates the interface registry containing the QGB interfaces | ||
func makeInterfaceRegistry() codectypes.InterfaceRegistry { | ||
// create the codec | ||
interfaceRegistry := codectypes.NewInterfaceRegistry() | ||
|
||
// register the standard types from the sdk | ||
std.RegisterInterfaces(interfaceRegistry) | ||
|
||
// register the qgb module interfaces | ||
types.RegisterInterfaces(interfaceRegistry) | ||
|
||
return interfaceRegistry | ||
} |
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,59 @@ | ||
package client_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/x/qgb/client" | ||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" | ||
) | ||
|
||
func (s *CLITestSuite) TestQueryAttestationByNonce() { | ||
_, err := s.network.WaitForHeight(402) | ||
s.Require().NoError(err) | ||
val := s.network.Validators[0] | ||
testCases := []struct { | ||
name string | ||
nonce string | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "query the first valset that's created on chain startup", | ||
nonce: "1", | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "query the first data commitment", | ||
nonce: "2", | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "negative attestation nonce", | ||
nonce: "-1", | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "zero attestation nonce", | ||
nonce: "0", | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "higher attestation nonce than latest attestation nonce", | ||
nonce: "100", | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
s.T().Run(tc.name, func(t *testing.T) { | ||
cmd := client.CmdQueryAttestationByNonce() | ||
clientCtx := val.ClientCtx | ||
|
||
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{tc.nonce}) | ||
if tc.expectErr { | ||
s.Assert().Error(err) | ||
} else { | ||
s.Assert().NoError(err) | ||
} | ||
}) | ||
} | ||
} |
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,55 @@ | ||
package client_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/celestiaorg/celestia-app/test/util/network" | ||
"github.com/cosmos/cosmos-sdk/crypto/keyring" | ||
cosmosnet "github.com/cosmos/cosmos-sdk/testutil/network" | ||
"github.com/stretchr/testify/suite" | ||
tmrand "github.com/tendermint/tendermint/libs/rand" | ||
) | ||
|
||
type CLITestSuite struct { | ||
suite.Suite | ||
cfg cosmosnet.Config | ||
network *cosmosnet.Network | ||
kr keyring.Keyring | ||
} | ||
|
||
func (s *CLITestSuite) SetupSuite() { | ||
if testing.Short() { | ||
s.T().Skip("skipping QGB CLI tests in short mode.") | ||
} | ||
s.T().Log("setting up QGB CLI test suite") | ||
|
||
cfg := network.DefaultConfig() | ||
cfg.EnableTMLogging = false | ||
cfg.MinGasPrices = "0utia" | ||
cfg.NumValidators = 1 | ||
cfg.TargetHeightDuration = time.Millisecond | ||
s.cfg = cfg | ||
|
||
numAccounts := 120 | ||
accounts := make([]string, numAccounts) | ||
for i := 0; i < numAccounts; i++ { | ||
accounts[i] = tmrand.Str(20) | ||
} | ||
|
||
net := network.New(s.T(), cfg, accounts...) | ||
|
||
s.network = net | ||
s.kr = net.Validators[0].ClientCtx.Keyring | ||
_, err := s.network.WaitForHeight(2) | ||
s.Require().NoError(err) | ||
} | ||
|
||
func (s *CLITestSuite) TearDownSuite() { | ||
s.T().Log("tearing down QGB CLI test suite") | ||
s.network.Cleanup() | ||
} | ||
|
||
func TestQGBCLI(t *testing.T) { | ||
suite.Run(t, new(CLITestSuite)) | ||
} |
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
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