Skip to content

Commit

Permalink
CM-274 implemented MsgRenewLease
Browse files Browse the repository at this point in the history
  • Loading branch information
rnistuk committed Mar 31, 2020
1 parent 20538d5 commit da9644b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
37 changes: 37 additions & 0 deletions x/crud/internal/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,40 @@ func (msg MsgGetNShortestLease) GetSignBytes() []byte {
func (msg MsgGetNShortestLease) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// RenewLease
type MsgRenewLease struct {
UUID string
Key string
Lease int64
Owner sdk.AccAddress
}

func (msg MsgRenewLease) Route() string { return RouterKey }

func (msg MsgRenewLease) Type() string { return "renewlease" }

func (msg MsgRenewLease) ValidateBasic() error {
if msg.Owner.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Owner.String())
}

if len(msg.UUID) == 0 || len(msg.Key) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "UUID or Key empty")
}

if msg.Lease < 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Lease negative")
}

return nil
}

func (msg MsgRenewLease) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}

func (msg MsgRenewLease) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
45 changes: 45 additions & 0 deletions x/crud/internal/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,48 @@ func TestMsgGetNShortestLease_GetSigners(t *testing.T) {
}
Equal(t, sut.GetSigners(), []sdk.AccAddress{sut.Owner})
}

/////////////////////////////////////////////////////////////////////////////////
func TestMsgRenewLease_Route(t *testing.T) {
Equal(t, "crud", MsgRenewLease{}.Route())
}

func TestMsgRenewLease_Type(t *testing.T) {
Equal(t, "renewlease", MsgRenewLease{}.Type())
}

func TestMsgRenewLease_ValidateBasic(t *testing.T) {
owner := []byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23")
sut := MsgRenewLease{UUID: "uuid", Key: "key", Lease: int64(100), Owner: owner}

Nil(t, sut.ValidateBasic())

sut.Owner = nil
NotNil(t, sut.ValidateBasic())

sut.Owner = owner
sut.UUID = ""
Equal(t, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "UUID or Key empty").Error(), sut.ValidateBasic().Error())

sut.UUID = "uuid"
sut.Key = ""
Equal(t, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "UUID or Key empty").Error(), sut.ValidateBasic().Error())

sut.Key = "key"
sut.Lease = -5
Equal(t, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "Lease negative").Error(), sut.ValidateBasic().Error())
}

func TestMsgRenewLease_GetSignBytes(t *testing.T) {
owner := []byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23")
sut := MsgRenewLease{UUID: "uuid", Key: "key", Lease: int64(100), Owner: owner}
Equal(t,
"{\"type\":\"crud/renewlease\",\"value\":{\"Key\":\"key\",\"Lease\":\"0\",\"Owner\":\"cosmos1vfk827n9d3kx2vt5xpuhwardwfj82mryvcmxsdrhw9exumns09crjamjxekxzaejw56k5ampxgeslhg4h3\",\"UUID\":\"uuid\",\"Lease\":\"100\"}}",
string(sut.GetSignBytes()))
}

func TestMsgRenewLease_GetSigners(t *testing.T) {
owner := []byte("bluzelle1t0ywtmrduldf6h4wqrnnpyp9wr6law2u5jwa23")
sut := MsgRenewLease{UUID: "uuid", Key: "key", Lease: int64(100), Owner: owner}
Equal(t, sut.GetSigners(), []sdk.AccAddress{sut.Owner})
}

0 comments on commit da9644b

Please sign in to comment.