Skip to content

Commit

Permalink
scaffold tx set protocol version
Browse files Browse the repository at this point in the history
  • Loading branch information
oren-lava authored and Yarom Swisa committed Feb 11, 2024
1 parent 70a0732 commit 44362a1
Show file tree
Hide file tree
Showing 14 changed files with 663 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ func New(
keys[protocolmoduletypes.StoreKey],
keys[protocolmoduletypes.MemStoreKey],
app.GetSubspace(protocolmoduletypes.ModuleName),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
protocolModule := protocolmodule.NewAppModule(appCodec, app.ProtocolKeeper)

Expand Down
10 changes: 9 additions & 1 deletion proto/lavanet/lava/protocol/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ syntax = "proto3";
package lavanet.lava.protocol;

// this line is used by starport scaffolding # proto/tx/import

import "lavanet/lava/protocol/params.proto";
option go_package = "github.com/lavanet/lava/x/protocol/types";

// Msg defines the Msg service.
service Msg {
rpc SetVersion(MsgSetVersion) returns (MsgSetVersionResponse);
// this line is used by starport scaffolding # proto/tx/rpc
}

// this line is used by starport scaffolding # proto/tx/message
message MsgSetVersion {
string authority = 1;
Version version = 2;
}

message MsgSetVersionResponse {
}
2 changes: 1 addition & 1 deletion testutil/keeper/keepers_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func InitAllKeepers(t testing.TB) (*Servers, *Keepers, context.Context) {
ks.SlashingKeeper = slashingkeeper.NewKeeper(cdc, legacyCdc, slashingStoreKey, ks.StakingKeeper, authtypes.NewModuleAddress(govtypes.ModuleName).String())
ks.Plans = *planskeeper.NewKeeper(cdc, plansStoreKey, plansMemStoreKey, plansparamsSubspace, ks.Epochstorage, ks.Spec, ks.FixationStoreKeeper, ks.StakingKeeper)
ks.Projects = *projectskeeper.NewKeeper(cdc, projectsStoreKey, projectsMemStoreKey, projectsparamsSubspace, ks.Epochstorage, ks.FixationStoreKeeper)
ks.Protocol = *protocolkeeper.NewKeeper(cdc, protocolStoreKey, protocolMemStoreKey, protocolparamsSubspace)
ks.Protocol = *protocolkeeper.NewKeeper(cdc, protocolStoreKey, protocolMemStoreKey, protocolparamsSubspace, authtypes.NewModuleAddress(govtypes.ModuleName).String())
ks.Downtime = downtimekeeper.NewKeeper(cdc, downtimeKey, downtimeParamsSubspace, ks.Epochstorage)
ks.Rewards = *rewardskeeper.NewKeeper(cdc, rewardsStoreKey, rewardsMemStoreKey, rewardsparamsSubspace, ks.BankKeeper, ks.AccountKeeper, ks.Spec, ks.Epochstorage, ks.Downtime, ks.StakingKeeper, ks.Dualstaking, ks.Distribution, authtypes.FeeCollectorName, ks.TimerStoreKeeper)
ks.Subscription = *subscriptionkeeper.NewKeeper(cdc, subscriptionStoreKey, subscriptionMemStoreKey, subscriptionparamsSubspace, &ks.BankKeeper, &ks.AccountKeeper, &ks.Epochstorage, ks.Projects, ks.Plans, ks.Dualstaking, ks.Rewards, ks.FixationStoreKeeper, ks.TimerStoreKeeper, ks.StakingKeeper)
Expand Down
3 changes: 3 additions & 0 deletions testutil/keeper/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/lavanet/lava/x/protocol/keeper"
"github.com/lavanet/lava/x/protocol/types"
Expand Down Expand Up @@ -41,6 +43,7 @@ func ProtocolKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey,
memStoreKey,
paramsSubspace,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
Expand Down
6 changes: 6 additions & 0 deletions x/protocol/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type (
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace

// the address capable of executing a MsgSetIprpcData message. Typically, this
// should be the x/gov module account.
authority string
}
)

Expand All @@ -27,6 +31,7 @@ func NewKeeper(
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,
authority string,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
Expand All @@ -38,6 +43,7 @@ func NewKeeper(
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
authority: authority,
}
}

Expand Down
28 changes: 28 additions & 0 deletions x/protocol/keeper/msg_server_set_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
"context"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/lavanet/lava/x/protocol/types"
)

func (k msgServer) SetVersion(goCtx context.Context, msg *types.MsgSetVersion) (*types.MsgSetVersionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

if msg.Authority != k.authority {
sdkerrors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

params := k.GetParams(ctx)
params.Version = *msg.Version
if err := params.Validate(); err != nil {
return &types.MsgSetVersionResponse{}, err
}

k.SetParams(ctx, params)

return &types.MsgSetVersionResponse{}, nil
}
2 changes: 1 addition & 1 deletion x/protocol/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (am AppModule) Name() string {
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))

migrator := keeper.NewMigrator(am.keeper)

Expand Down
7 changes: 3 additions & 4 deletions x/protocol/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ package types
import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"

sdk "github.com/cosmos/cosmos-sdk/types"
// this line is used by starport scaffolding # 1
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSetVersion{}, "protocol/MsgSetVersion", nil)
// this line is used by starport scaffolding # 2
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSetVersion{})
}

var (
Expand Down
2 changes: 1 addition & 1 deletion x/protocol/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func DefaultGenesis() *GenesisState {
func (gs GenesisState) Validate() error {
// this line is used by starport scaffolding # genesis/types/validate

return gs.Params.Validate(true)
return gs.Params.Validate()
}
43 changes: 43 additions & 0 deletions x/protocol/types/message_set_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package types

import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const TypeMsgSetVersion = "set_version"

var _ sdk.Msg = &MsgSetVersion{}

func NewMsgSetVersion(authority string, version Version) *MsgSetVersion {
return &MsgSetVersion{
Authority: authority,
Version: &version,
}
}

func (msg *MsgSetVersion) Route() string {
return RouterKey
}

func (msg *MsgSetVersion) Type() string {
return TypeMsgSetVersion
}

func (msg *MsgSetVersion) GetSigners() []sdk.AccAddress {
authority, _ := sdk.AccAddressFromBech32(msg.Authority)
return []sdk.AccAddress{authority}
}

func (msg *MsgSetVersion) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgSetVersion) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return sdkerrors.Wrap(err, "invalid authority address")
}

return nil
}
48 changes: 48 additions & 0 deletions x/protocol/types/message_set_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package types

import (
"testing"

"github.com/lavanet/lava/testutil/sample"
"github.com/stretchr/testify/require"
)

func TestSetIprpcData_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgSetVersion
valid bool
}{
{
name: "invalid authority address",
msg: MsgSetVersion{
Authority: "invalid_address",
},
valid: false,
},
{
name: "invalid subscription address",
msg: MsgSetVersion{
Authority: sample.AccAddress(),
},
valid: false,
},
{
name: "valid message",
msg: MsgSetVersion{
Authority: sample.AccAddress(),
},
valid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.valid {
require.NoError(t, err)
return
}
require.Error(t, err)
})
}
}
2 changes: 1 addition & 1 deletion x/protocol/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func versionToInteger(v string) (int, error) {
}

// Validate validates the set of params
func (p Params) Validate(genesis bool) error {
func (p Params) Validate() error {
return validateVersion(p.Version)
}

Expand Down
Loading

0 comments on commit 44362a1

Please sign in to comment.