-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a map type which store the state for a block number
- Loading branch information
1 parent
a248b1c
commit 916131b
Showing
9 changed files
with
934 additions
and
0 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,11 @@ | ||
syntax = "proto3"; | ||
package evmstorechain.evmstorechain; | ||
|
||
option go_package = "EVMStoreChain/x/evmstorechain/types"; | ||
|
||
message Blockstoragestate { | ||
string blocknumber = 1; | ||
uint64 state = 2; | ||
|
||
} | ||
|
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,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
160
x/evmstorechain/client/cli/query_blockstoragestate_test.go
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,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), | ||
) | ||
}) | ||
} |
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,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 | ||
} |
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,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)), | ||
) | ||
} |
Oops, something went wrong.