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 a6f3bbb commit a248b1c
Show file tree
Hide file tree
Showing 10 changed files with 1,318 additions and 145 deletions.
8 changes: 5 additions & 3 deletions proto/evmstorechain/evmstorechain/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package evmstorechain.evmstorechain;
import "gogoproto/gogo.proto";
import "evmstorechain/evmstorechain/params.proto";
import "evmstorechain/evmstorechain/vote.proto";
import "evmstorechain/evmstorechain/blockstoragestate.proto";

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

// GenesisState defines the evmstorechain module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated Vote voteList = 2 [(gogoproto.nullable) = false];
uint64 voteCount = 3;
Params params = 1 [(gogoproto.nullable) = false];
repeated Vote voteList = 2 [(gogoproto.nullable) = false];
uint64 voteCount = 3;
repeated Blockstoragestate blockstoragestateList = 4 [(gogoproto.nullable) = false];
}

28 changes: 28 additions & 0 deletions proto/evmstorechain/evmstorechain/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "evmstorechain/evmstorechain/params.proto";
import "evmstorechain/evmstorechain/vote.proto";
import "evmstorechain/evmstorechain/blockstoragestate.proto";

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

Expand All @@ -28,6 +29,16 @@ service Query {
option (google.api.http).get = "/EVMStoreChain/evmstorechain/vote";

}

// Queries a list of Blockstoragestate items.
rpc Blockstoragestate (QueryGetBlockstoragestateRequest) returns (QueryGetBlockstoragestateResponse) {
option (google.api.http).get = "/EVMStoreChain/evmstorechain/blockstoragestate/{blocknumber}";

}
rpc BlockstoragestateAll (QueryAllBlockstoragestateRequest) returns (QueryAllBlockstoragestateResponse) {
option (google.api.http).get = "/EVMStoreChain/evmstorechain/blockstoragestate";

}
}
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}
Expand Down Expand Up @@ -56,3 +67,20 @@ message QueryAllVoteResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryGetBlockstoragestateRequest {
string blocknumber = 1;
}

message QueryGetBlockstoragestateResponse {
Blockstoragestate blockstoragestate = 1 [(gogoproto.nullable) = false];
}

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

message QueryAllBlockstoragestateResponse {
repeated Blockstoragestate blockstoragestate = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

2 changes: 2 additions & 0 deletions x/evmstorechain/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdListVote())
cmd.AddCommand(CmdShowVote())
cmd.AddCommand(CmdListBlockstoragestate())
cmd.AddCommand(CmdShowBlockstoragestate())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
5 changes: 5 additions & 0 deletions x/evmstorechain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)

// Set vote count
k.SetVoteCount(ctx, genState.VoteCount)
// Set all the blockstoragestate
for _, elem := range genState.BlockstoragestateList {
k.SetBlockstoragestate(ctx, elem)
}
// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
}
Expand All @@ -26,6 +30,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {

genesis.VoteList = k.GetAllVote(ctx)
genesis.VoteCount = k.GetVoteCount(ctx)
genesis.BlockstoragestateList = k.GetAllBlockstoragestate(ctx)
// this line is used by starport scaffolding # genesis/module/export

return genesis
Expand Down
9 changes: 9 additions & 0 deletions x/evmstorechain/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func TestGenesis(t *testing.T) {
},
},
VoteCount: 2,
BlockstoragestateList: []types.Blockstoragestate{
{
Blocknumber: "0",
},
{
Blocknumber: "1",
},
},
// this line is used by starport scaffolding # genesis/test/state
}

Expand All @@ -36,5 +44,6 @@ func TestGenesis(t *testing.T) {

require.ElementsMatch(t, genesisState.VoteList, got.VoteList)
require.Equal(t, genesisState.VoteCount, got.VoteCount)
require.ElementsMatch(t, genesisState.BlockstoragestateList, got.BlockstoragestateList)
// this line is used by starport scaffolding # genesis/test/assert
}
13 changes: 12 additions & 1 deletion x/evmstorechain/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const DefaultIndex uint64 = 1
// DefaultGenesis returns the default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
VoteList: []Vote{},
VoteList: []Vote{},
BlockstoragestateList: []Blockstoragestate{},
// this line is used by starport scaffolding # genesis/types/default
Params: DefaultParams(),
}
Expand All @@ -31,6 +32,16 @@ func (gs GenesisState) Validate() error {
}
voteIdMap[elem.Id] = true
}
// Check for duplicated index in blockstoragestate
blockstoragestateIndexMap := make(map[string]struct{})

for _, elem := range gs.BlockstoragestateList {
index := string(BlockstoragestateKey(elem.Blocknumber))
if _, ok := blockstoragestateIndexMap[index]; ok {
return fmt.Errorf("duplicated index for blockstoragestate")
}
blockstoragestateIndexMap[index] = struct{}{}
}
// this line is used by starport scaffolding # genesis/types/validate

return gs.Params.Validate()
Expand Down
92 changes: 78 additions & 14 deletions x/evmstorechain/types/genesis.pb.go

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

22 changes: 22 additions & 0 deletions x/evmstorechain/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func TestGenesisState_Validate(t *testing.T) {
},
},
VoteCount: 2,
BlockstoragestateList: []types.Blockstoragestate{
{
Blocknumber: "0",
},
{
Blocknumber: "1",
},
},
// this line is used by starport scaffolding # types/genesis/validField
},
valid: true,
Expand Down Expand Up @@ -61,6 +69,20 @@ func TestGenesisState_Validate(t *testing.T) {
},
valid: false,
},
{
desc: "duplicated blockstoragestate",
genState: &types.GenesisState{
BlockstoragestateList: []types.Blockstoragestate{
{
Blocknumber: "0",
},
{
Blocknumber: "0",
},
},
},
valid: false,
},
// this line is used by starport scaffolding # types/genesis/testcase
}
for _, tc := range tests {
Expand Down
Loading

0 comments on commit a248b1c

Please sign in to comment.