Skip to content

Commit

Permalink
refactor(batching): --with-unsigned flag for query of batches
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Nov 26, 2024
1 parent 54d9e95 commit 00a6060
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 62 deletions.
3 changes: 3 additions & 0 deletions proto/sedachain/batching/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ message QueryBatchForHeightResponse {
message QueryBatchesRequest {
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 1;
// with_unsigned indicates whether to return batches without
// signatures or not.
bool with_unsigned = 2;
}

// The response message for QueryBatches RPC.
Expand Down
17 changes: 15 additions & 2 deletions x/batching/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/sedaprotocol/seda-chain/x/batching/types"
)

const flagWithUnsigned = "with-unsigned"

// GetQueryCmd returns the CLI query commands for batching module.
func GetQueryCmd(_ string) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -104,7 +106,7 @@ func GetCmdQueryBatchByHeight() *cobra.Command {
func GetCmdQueryBatches() *cobra.Command {
cmd := &cobra.Command{
Use: "batches",
Short: "List all batches in the store",
Short: "Query all batches",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
Expand All @@ -117,14 +119,25 @@ func GetCmdQueryBatches() *cobra.Command {
if err != nil {
return err
}
res, err := queryClient.Batches(cmd.Context(), &types.QueryBatchesRequest{Pagination: pageReq})

withUnsigned, err := cmd.Flags().GetBool(flagWithUnsigned)
if err != nil {
return err
}

res, err := queryClient.Batches(cmd.Context(),
&types.QueryBatchesRequest{
Pagination: pageReq,
WithUnsigned: withUnsigned,
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

cmd.Flags().Bool(flagWithUnsigned, false, "include batches without signatures")
flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "batches")
return cmd
Expand Down
6 changes: 3 additions & 3 deletions x/batching/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ func (k Keeper) GetTreeEntriesForBatch(ctx context.Context, batchNum uint64) (ty
if err != nil {
return types.BatchTreeEntries{}, err
}
var valEntries []types.ValidatorTreeEntry
for _, kv := range valKvs {
valEntries = append(valEntries, kv.Value)
valEntries := make([]types.ValidatorTreeEntry, len(valKvs))
for i, kv := range valKvs {
valEntries[i] = kv.Value
}

return types.BatchTreeEntries{
Expand Down
6 changes: 3 additions & 3 deletions x/batching/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
if err != nil {
panic(err)
}
var entries []types.BatchTreeEntries
for _, batch := range batches {
entries := make([]types.BatchTreeEntries, len(batches))
for i, batch := range batches {
batchEntries, err := k.GetTreeEntriesForBatch(ctx, batch.BatchNumber)
if err != nil {
panic(err)
}
entries = append(entries, batchEntries)
entries[i] = batchEntries
}
params, err := k.GetParams(ctx)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions x/batching/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"

storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
Expand All @@ -21,8 +22,6 @@ import (
batchingtypes "github.com/sedaprotocol/seda-chain/x/batching/types"
)

var mockedByteArray = []byte("82a9dda829eb7f8ffe9fbe49e45d47d2dad9664fbb7adf72492e3c81ebd3e29134d9bc12212bf83c6840f10e8246b9db54a4859b7ccd0123d86e5872c1e5082")

type KeeperTestSuite struct {
suite.Suite
ctx sdk.Context
Expand Down
3 changes: 2 additions & 1 deletion x/batching/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

"cosmossdk.io/collections"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"

Expand Down Expand Up @@ -68,7 +69,7 @@ func (q Querier) Batches(c context.Context, req *types.QueryBatchesRequest) (*ty
batches, pageRes, err := query.CollectionFilteredPaginate(
ctx, q.batches, req.Pagination,
func(_ int64, value types.Batch) (bool, error) {
if value.BlockHeight > ctx.BlockHeight()+abci.BlockOffsetCollectPhase {
if !req.WithUnsigned && value.BlockHeight > ctx.BlockHeight()+abci.BlockOffsetCollectPhase {
return false, nil
}
return true, nil
Expand Down
4 changes: 2 additions & 2 deletions x/batching/types/batching.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 94 additions & 49 deletions x/batching/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00a6060

Please sign in to comment.