Skip to content

Commit

Permalink
feat(rollapp): state index refactor (#73)
Browse files Browse the repository at this point in the history
* rename StateIndex to StateInfoIndex & LatestStateInfoIndex

* rename files
  • Loading branch information
liorzilp authored Aug 30, 2022
1 parent 7349dc2 commit 4d6ecf1
Show file tree
Hide file tree
Showing 34 changed files with 1,372 additions and 1,418 deletions.
806 changes: 471 additions & 335 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions proto/rollapp/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "gogoproto/gogo.proto";
import "rollapp/params.proto";
import "rollapp/rollapp.proto";
import "rollapp/state_info.proto";
import "rollapp/state_index.proto";
// this line is used by starport scaffolding # genesis/proto/import

option go_package = "github.com/dymensionxyz/dymension/x/rollapp/types";
Expand All @@ -15,6 +14,6 @@ message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated Rollapp rollappList = 2 [(gogoproto.nullable) = false];
repeated StateInfo stateInfoList = 3 [(gogoproto.nullable) = false];
repeated StateIndex stateIndexList = 4 [(gogoproto.nullable) = false];
repeated StateInfoIndex latestStateInfoIndexList = 4 [(gogoproto.nullable) = false];
// this line is used by starport scaffolding # genesis/proto/state
}
29 changes: 14 additions & 15 deletions proto/rollapp/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "cosmos/base/query/v1beta1/pagination.proto";
import "rollapp/params.proto";
import "rollapp/rollapp.proto";
import "rollapp/state_info.proto";
import "rollapp/state_index.proto";
// this line is used by starport scaffolding # 1

option go_package = "github.com/dymensionxyz/dymension/x/rollapp/types";
Expand All @@ -30,22 +29,22 @@ service Query {

// Queries a StateInfo by index.
rpc StateInfo(QueryGetStateInfoRequest) returns (QueryGetStateInfoResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info/{rollappId}/{stateIndex}";
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info/{rollappId}/{index}";
}

// Queries a list of StateInfo items.
rpc StateInfoAll(QueryAllStateInfoRequest) returns (QueryAllStateInfoResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_info";
}

// Queries a StateIndex by index.
rpc StateIndex(QueryGetStateIndexRequest) returns (QueryGetStateIndexResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_index/{rollappId}";
// Queries a LatestStateInfoIndex by index.
rpc LatestStateInfoIndex(QueryGetLatestStateInfoIndexRequest) returns (QueryGetLatestStateInfoIndexResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/latest_state_info_index/{rollappId}";
}

// Queries a list of StateIndex items.
rpc StateIndexAll(QueryAllStateIndexRequest) returns (QueryAllStateIndexResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/state_index";
// Queries a list of LatestStateInfoIndex items.
rpc LatestStateInfoIndexAll(QueryAllLatestStateInfoIndexRequest) returns (QueryAllLatestStateInfoIndexResponse) {
option (google.api.http).get = "/dymensionxyz/dymension/rollapp/latest_state_info_index";
}

// this line is used by starport scaffolding # 2
Expand Down Expand Up @@ -80,7 +79,7 @@ message QueryAllRollappResponse {

message QueryGetStateInfoRequest {
string rollappId = 1;
uint64 stateIndex = 2;
uint64 index = 2;
}

message QueryGetStateInfoResponse {
Expand All @@ -96,21 +95,21 @@ message QueryAllStateInfoResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryGetStateIndexRequest {
message QueryGetLatestStateInfoIndexRequest {
string rollappId = 1;

}

message QueryGetStateIndexResponse {
StateIndex stateIndex = 1 [(gogoproto.nullable) = false];
message QueryGetLatestStateInfoIndexResponse {
StateInfoIndex latestStateInfoIndex = 1 [(gogoproto.nullable) = false];
}

message QueryAllStateIndexRequest {
message QueryAllLatestStateInfoIndexRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllStateIndexResponse {
repeated StateIndex stateIndex = 1 [(gogoproto.nullable) = false];
message QueryAllLatestStateInfoIndexResponse {
repeated StateInfoIndex latestStateInfoIndex = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

Expand Down
19 changes: 0 additions & 19 deletions proto/rollapp/state_index.proto

This file was deleted.

38 changes: 25 additions & 13 deletions proto/rollapp/state_info.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,41 @@ import "gogoproto/gogo.proto";
import "rollapp/block_descriptor.proto";
import "rollapp/state_status.proto";

// StateInfo defines a rollapps' state.
message StateInfo {
// StateInfoIndex is the data used for indexing and retrieving a StateInfo
// it updated and saved with every UpdateState in StateInfo.
// We use the this structure also for LatestStateInfoIndex which defines the rollapps' current (latest)
// index of the last UpdateState
message StateInfoIndex {
// rollappId is the rollapp that the sequencer belongs to and asking to update
// it used to identify the what rollapp a StateInfo belongs
// The rollappId follows the same standard as cosmos chain_id
string rollappId = 1;
// stateIndex is a sequential increasing number, updating on each
string rollappId = 1;
// latestStateInfoIndex is a sequential increasing number, updating on each
// state update used for indexing to a specific state info
uint64 stateIndex = 2;
uint64 index = 2;

}

// StateInfo defines a rollapps' state.
message StateInfo {
// stateInfoIndex defines what rollapp the state belongs to
// and in which index it can be referenced
StateInfoIndex stateInfoIndex = 1 [(gogoproto.nullable) = false];
// sequencer is the bech32-encoded address of the sequencer sent the update
string sequencer = 3;
string sequencer = 2;
// startHeight is the block height of the first block in the batch
uint64 startHeight = 4;
uint64 startHeight = 3;
// numBlocks is the number of blocks included in this batch update
uint64 numBlocks = 5;
uint64 numBlocks = 4;
// DAPath is the description of the location on the DA layer
string DAPath = 6;
string DAPath = 5;
// version is the version of the rollapp
uint64 version = 7;
uint64 version = 6;
// creationHeight is the height at which the UpdateState took place
uint64 creationHeight = 8;
uint64 creationHeight = 7;
// status is the status of the state update
StateStatus status = 9;
StateStatus status = 8;
// BDs is a list of block description objects (one per block)
// the list must be ordered by height, starting from startHeight to startHeight+numBlocks-1
BlockDescriptors BDs = 10 [(gogoproto.nullable) = false];
BlockDescriptors BDs = 9 [(gogoproto.nullable) = false];
}
4 changes: 2 additions & 2 deletions x/rollapp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdShowRollapp())
cmd.AddCommand(CmdListStateInfo())
cmd.AddCommand(CmdShowStateInfo())
cmd.AddCommand(CmdListStateIndex())
cmd.AddCommand(CmdShowStateIndex())
cmd.AddCommand(CmdListLatestStateInfoIndex())
cmd.AddCommand(CmdShowLatestStateInfoIndex())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/spf13/cobra"
)

func CmdListStateIndex() *cobra.Command {
func CmdListLatestStateInfoIndex() *cobra.Command {
cmd := &cobra.Command{
Use: "list-state-index",
Short: "list all state_index",
Short: "list all latest_state_info_index",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

Expand All @@ -23,11 +23,11 @@ func CmdListStateIndex() *cobra.Command {

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryAllStateIndexRequest{
params := &types.QueryAllLatestStateInfoIndexRequest{
Pagination: pageReq,
}

res, err := queryClient.StateIndexAll(context.Background(), params)
res, err := queryClient.LatestStateInfoIndexAll(context.Background(), params)
if err != nil {
return err
}
Expand All @@ -42,10 +42,10 @@ func CmdListStateIndex() *cobra.Command {
return cmd
}

func CmdShowStateIndex() *cobra.Command {
func CmdShowLatestStateInfoIndex() *cobra.Command {
cmd := &cobra.Command{
Use: "show-state-index [rollapp-id]",
Short: "shows a state_index",
Short: "shows a latest_state_info_index",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
Expand All @@ -54,11 +54,11 @@ func CmdShowStateIndex() *cobra.Command {

argRollappId := args[0]

params := &types.QueryGetStateIndexRequest{
params := &types.QueryGetLatestStateInfoIndexRequest{
RollappId: argRollappId,
}

res, err := queryClient.StateIndex(context.Background(), params)
res, err := queryClient.LatestStateInfoIndex(context.Background(), params)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ import (
// Prevent strconv unused error
var _ = strconv.IntSize

func networkWithStateIndexObjects(t *testing.T, n int) (*network.Network, []types.StateIndex) {
func networkWithLatestStateInfoIndexObjects(t *testing.T, n int) (*network.Network, []types.StateInfoIndex) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))

for i := 0; i < n; i++ {
stateIndex := types.StateIndex{
latestStateInfoIndex := types.StateInfoIndex{
RollappId: strconv.Itoa(i),
}
nullify.Fill(&stateIndex)
state.StateIndexList = append(state.StateIndexList, stateIndex)
nullify.Fill(&latestStateInfoIndex)
state.LatestStateInfoIndexList = append(state.LatestStateInfoIndexList, latestStateInfoIndex)
}
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), state.StateIndexList
return network.New(t, cfg), state.LatestStateInfoIndexList
}

func TestShowStateIndex(t *testing.T) {
net, objs := networkWithStateIndexObjects(t, 2)
func TestShowLatestStateInfoIndex(t *testing.T) {
net, objs := networkWithLatestStateInfoIndexObjects(t, 2)

ctx := net.Validators[0].ClientCtx
common := []string{
Expand All @@ -53,7 +53,7 @@ func TestShowStateIndex(t *testing.T) {

args []string
err error
obj types.StateIndex
obj types.StateInfoIndex
}{
{
desc: "found",
Expand All @@ -75,27 +75,27 @@ func TestShowStateIndex(t *testing.T) {
tc.idRollappId,
}
args = append(args, tc.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowStateIndex(), args)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowLatestStateInfoIndex(), 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.QueryGetStateIndexResponse
var resp types.QueryGetLatestStateInfoIndexResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.StateIndex)
require.NotNil(t, resp.LatestStateInfoIndex)
require.Equal(t,
nullify.Fill(&tc.obj),
nullify.Fill(&resp.StateIndex),
nullify.Fill(&resp.LatestStateInfoIndex),
)
}
})
}
}

func TestListStateIndex(t *testing.T) {
net, objs := networkWithStateIndexObjects(t, 5)
func TestListLatestStateInfoIndex(t *testing.T) {
net, objs := networkWithLatestStateInfoIndexObjects(t, 5)

ctx := net.Validators[0].ClientCtx
request := func(next []byte, offset, limit uint64, total bool) []string {
Expand All @@ -117,14 +117,14 @@ func TestListStateIndex(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.CmdListStateIndex(), args)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListLatestStateInfoIndex(), args)
require.NoError(t, err)
var resp types.QueryAllStateIndexResponse
var resp types.QueryAllLatestStateInfoIndexResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.StateIndex), step)
require.LessOrEqual(t, len(resp.LatestStateInfoIndex), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.StateIndex),
nullify.Fill(resp.LatestStateInfoIndex),
)
}
})
Expand All @@ -133,29 +133,29 @@ func TestListStateIndex(t *testing.T) {
var next []byte
for i := 0; i < len(objs); i += step {
args := request(next, 0, uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListStateIndex(), args)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListLatestStateInfoIndex(), args)
require.NoError(t, err)
var resp types.QueryAllStateIndexResponse
var resp types.QueryAllLatestStateInfoIndexResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.StateIndex), step)
require.LessOrEqual(t, len(resp.LatestStateInfoIndex), step)
require.Subset(t,
nullify.Fill(objs),
nullify.Fill(resp.StateIndex),
nullify.Fill(resp.LatestStateInfoIndex),
)
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.CmdListStateIndex(), args)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListLatestStateInfoIndex(), args)
require.NoError(t, err)
var resp types.QueryAllStateIndexResponse
var resp types.QueryAllLatestStateInfoIndexResponse
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.StateIndex),
nullify.Fill(resp.LatestStateInfoIndex),
)
})
}
7 changes: 2 additions & 5 deletions x/rollapp/client/cli/query_state_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ func CmdShowStateInfo() *cobra.Command {
queryClient := types.NewQueryClient(clientCtx)

argRollappId := args[0]
argStateIndex, err := cast.ToUint64E(args[1])
argIndex, err := cast.ToUint64E(args[1])
if err != nil {
return err
}

params := &types.QueryGetStateInfoRequest{
RollappId: argRollappId,
StateIndex: argStateIndex,
}
params := &types.QueryGetStateInfoRequest{RollappId: argRollappId, Index: argIndex}

res, err := queryClient.StateInfo(context.Background(), params)
if err != nil {
Expand Down
Loading

0 comments on commit 4d6ecf1

Please sign in to comment.