Skip to content

Commit

Permalink
added a map type which store the state for a block number
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsuryansh committed Jul 2, 2023
1 parent a248b1c commit 916131b
Show file tree
Hide file tree
Showing 9 changed files with 934 additions and 0 deletions.
11 changes: 11 additions & 0 deletions proto/evmstorechain/evmstorechain/blockstoragestate.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";
package evmstorechain.evmstorechain;

option go_package = "EVMStoreChain/x/evmstorechain/types";

message Blockstoragestate {
string blocknumber = 1;
uint64 state = 2;

}

78 changes: 78 additions & 0 deletions x/evmstorechain/client/cli/query_blockstoragestate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

"EVMStoreChain/x/evmstorechain/types"
)

func CmdListBlockstoragestate() *cobra.Command {
cmd := &cobra.Command{
Use: "list-blockstoragestate",
Short: "list all blockstoragestate",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryAllBlockstoragestateRequest{
Pagination: pageReq,
}

res, err := queryClient.BlockstoragestateAll(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdShowBlockstoragestate() *cobra.Command {
cmd := &cobra.Command{
Use: "show-blockstoragestate [blocknumber]",
Short: "shows a blockstoragestate",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

argBlocknumber := args[0]

params := &types.QueryGetBlockstoragestateRequest{
Blocknumber: argBlocknumber,
}

res, err := queryClient.Blockstoragestate(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
160 changes: 160 additions & 0 deletions x/evmstorechain/client/cli/query_blockstoragestate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package cli_test

import (
"fmt"
"strconv"
"testing"

tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"EVMStoreChain/testutil/network"
"EVMStoreChain/testutil/nullify"
"EVMStoreChain/x/evmstorechain/client/cli"
"EVMStoreChain/x/evmstorechain/types"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func networkWithBlockstoragestateObjects(t *testing.T, n int) (*network.Network, []types.Blockstoragestate) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
for i := 0; i < n; i++ {
blockstoragestate := types.Blockstoragestate{
Blocknumber: strconv.Itoa(i),
}
nullify.Fill(&blockstoragestate)
state.BlockstoragestateList = append(state.BlockstoragestateList, blockstoragestate)
}
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), state.BlockstoragestateList
}

func TestShowBlockstoragestate(t *testing.T) {
net, objs := networkWithBlockstoragestateObjects(t, 2)

ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
tests := []struct {
desc string
idBlocknumber string

args []string
err error
obj types.Blockstoragestate
}{
{
desc: "found",
idBlocknumber: objs[0].Blocknumber,

args: common,
obj: objs[0],
},
{
desc: "not found",
idBlocknumber: strconv.Itoa(100000),

args: common,
err: status.Error(codes.NotFound, "not found"),
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
args := []string{
tc.idBlocknumber,
}
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowBlockstoragestate(), args)
if tc.err != nil {
stat, ok := status.FromError(tc.err)
require.True(t, ok)
require.ErrorIs(t, stat.Err(), tc.err)
} else {
require.NoError(t, err)
var resp types.QueryGetBlockstoragestateResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Blockstoragestate)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.Blockstoragestate),
)
}
})
}
}

func TestListBlockstoragestate(t *testing.T) {
net, objs := networkWithBlockstoragestateObjects(t, 5)

ctx := net.Validators[0].ClientCtx
request := func(next []byte, offset, limit uint64, total bool) []string {
args := []string{
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
}
if next == nil {
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset))
} else {
args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next))
}
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit))
if total {
args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal))
}
return args
}
t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(objs); i += step {
args := request(nil, uint64(i), uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListBlockstoragestate(), args)
require.NoError(t, err)
var resp types.QueryAllBlockstoragestateResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Blockstoragestate), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.Blockstoragestate),
)
}
})
t.Run("ByKey", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(objs); i += step {
args := request(next, 0, uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListBlockstoragestate(), args)
require.NoError(t, err)
var resp types.QueryAllBlockstoragestateResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Blockstoragestate), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.Blockstoragestate),
)
next = resp.Pagination.NextKey
}
})
t.Run("Total", func(t *testing.T) {
args := request(nil, 0, uint64(len(objs)), true)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListBlockstoragestate(), args)
require.NoError(t, err)
var resp types.QueryAllBlockstoragestateResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, err)
require.Equal(t, len(objs), int(resp.Pagination.Total))
require.ElementsMatch(t,
nullify.Fill(objs),
nullify.Fill(resp.Blockstoragestate),
)
})
}
63 changes: 63 additions & 0 deletions x/evmstorechain/keeper/blockstoragestate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package keeper

import (
"EVMStoreChain/x/evmstorechain/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// SetBlockstoragestate set a specific blockstoragestate in the store from its index
func (k Keeper) SetBlockstoragestate(ctx sdk.Context, blockstoragestate types.Blockstoragestate) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BlockstoragestateKeyPrefix))
b := k.cdc.MustMarshal(&blockstoragestate)
store.Set(types.BlockstoragestateKey(
blockstoragestate.Blocknumber,
), b)
}

// GetBlockstoragestate returns a blockstoragestate from its index
func (k Keeper) GetBlockstoragestate(
ctx sdk.Context,
blocknumber string,

) (val types.Blockstoragestate, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BlockstoragestateKeyPrefix))

b := store.Get(types.BlockstoragestateKey(
blocknumber,
))
if b == nil {
return val, false
}

k.cdc.MustUnmarshal(b, &val)
return val, true
}

// RemoveBlockstoragestate removes a blockstoragestate from the store
func (k Keeper) RemoveBlockstoragestate(
ctx sdk.Context,
blocknumber string,

) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BlockstoragestateKeyPrefix))
store.Delete(types.BlockstoragestateKey(
blocknumber,
))
}

// GetAllBlockstoragestate returns all blockstoragestate
func (k Keeper) GetAllBlockstoragestate(ctx sdk.Context) (list []types.Blockstoragestate) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BlockstoragestateKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.Blockstoragestate
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}
63 changes: 63 additions & 0 deletions x/evmstorechain/keeper/blockstoragestate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package keeper_test

import (
"strconv"
"testing"

keepertest "EVMStoreChain/testutil/keeper"
"EVMStoreChain/testutil/nullify"
"EVMStoreChain/x/evmstorechain/keeper"
"EVMStoreChain/x/evmstorechain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func createNBlockstoragestate(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Blockstoragestate {
items := make([]types.Blockstoragestate, n)
for i := range items {
items[i].Blocknumber = strconv.Itoa(i)

keeper.SetBlockstoragestate(ctx, items[i])
}
return items
}

func TestBlockstoragestateGet(t *testing.T) {
keeper, ctx := keepertest.EvmstorechainKeeper(t)
items := createNBlockstoragestate(keeper, ctx, 10)
for _, item := range items {
rst, found := keeper.GetBlockstoragestate(ctx,
item.Blocknumber,
)
require.True(t, found)
require.Equal(t,
nullify.Fill(&item),
nullify.Fill(&rst),
)
}
}
func TestBlockstoragestateRemove(t *testing.T) {
keeper, ctx := keepertest.EvmstorechainKeeper(t)
items := createNBlockstoragestate(keeper, ctx, 10)
for _, item := range items {
keeper.RemoveBlockstoragestate(ctx,
item.Blocknumber,
)
_, found := keeper.GetBlockstoragestate(ctx,
item.Blocknumber,
)
require.False(t, found)
}
}

func TestBlockstoragestateGetAll(t *testing.T) {
keeper, ctx := keepertest.EvmstorechainKeeper(t)
items := createNBlockstoragestate(keeper, ctx, 10)
require.ElementsMatch(t,
nullify.Fill(items),
nullify.Fill(keeper.GetAllBlockstoragestate(ctx)),
)
}
Loading

0 comments on commit 916131b

Please sign in to comment.