Skip to content

Commit

Permalink
feat: add start_at in lease
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Jan 8, 2025
1 parent 12d12c0 commit b2deb3f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 123 deletions.
6 changes: 1 addition & 5 deletions proto/sentinel/lease/v1/lease.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ message Lease {
int64 hours = 5;
int64 max_hours = 6;
sentinel.types.v1.RenewalPricePolicy renewal_price_policy = 7;
google.protobuf.Timestamp inactive_at = 8 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true
];
google.protobuf.Timestamp payout_at = 9 [
google.protobuf.Timestamp start_at = 8 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true
];
Expand Down
2 changes: 1 addition & 1 deletion types/v1/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (p Price) Copy() Price {
}

func (p Price) String() string {
return fmt.Sprintf("%s;%s;%s", p.BaseValue.String(), p.QuoteValue.String(), p.Denom)
return fmt.Sprintf("%s;%s;%s", p.BaseValue, p.QuoteValue, p.Denom)
}

func (p Price) IsEqual(v Price) bool {
Expand Down
14 changes: 4 additions & 10 deletions x/lease/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -87,19 +86,14 @@ func (k *Keeper) handleLeasePayouts(ctx sdk.Context) {
)

// Remove the lease from the payout queue as it has been processed
k.DeleteLeaseForPayoutAt(ctx, item.PayoutAt, item.ID)
k.DeleteLeaseForPayoutAt(ctx, item.PayoutAt(), item.ID)

// Update lease hours and payout time if applicable
// Update lease hours
item.Hours = item.Hours + 1
if item.Hours < item.MaxHours {
item.PayoutAt = item.PayoutAt.Add(time.Hour)
} else {
item.PayoutAt = time.Time{} // Set PayoutAt to zero value if max hours reached
}

// Update the lease in the store with new details
k.SetLease(ctx, item)
k.SetLeaseForPayoutAt(ctx, item.PayoutAt, item.ID)
k.SetLeaseForPayoutAt(ctx, item.PayoutAt(), item.ID)

// Emit an event for the updated lease details
ctx.EventManager().EmitTypedEvent(
Expand All @@ -108,7 +102,7 @@ func (k *Keeper) handleLeasePayouts(ctx sdk.Context) {
NodeAddress: item.NodeAddress,
ProvAddress: item.ProvAddress,
Hours: item.Hours,
PayoutAt: item.PayoutAt.String(),
PayoutAt: item.PayoutAt().String(),
},
)

Expand Down
22 changes: 10 additions & 12 deletions x/lease/keeper/msg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (k *Keeper) HandleMsgEndLease(ctx sdk.Context, msg *v1.MsgEndLeaseRequest)
k.DeleteLease(ctx, lease.ID)
k.DeleteLeaseForNodeByProvider(ctx, nodeAddr, provAddr, lease.ID)
k.DeleteLeaseForProvider(ctx, provAddr, lease.ID)
k.DeleteLeaseForInactiveAt(ctx, lease.InactiveAt, lease.ID)
k.DeleteLeaseForPayoutAt(ctx, lease.PayoutAt, lease.ID)
k.DeleteLeaseForInactiveAt(ctx, lease.InactiveAt(), lease.ID)
k.DeleteLeaseForPayoutAt(ctx, lease.PayoutAt(), lease.ID)
k.DeleteLeaseForRenewalAt(ctx, lease.RenewalAt(), lease.ID)

ctx.EventManager().EmitTypedEvent(
Expand Down Expand Up @@ -130,8 +130,8 @@ func (k *Keeper) HandleMsgRenewLease(ctx sdk.Context, msg *v1.MsgRenewLeaseReque
},
)

k.DeleteLeaseForInactiveAt(ctx, lease.InactiveAt, lease.ID)
k.DeleteLeaseForPayoutAt(ctx, lease.PayoutAt, lease.ID)
k.DeleteLeaseForInactiveAt(ctx, lease.InactiveAt(), lease.ID)
k.DeleteLeaseForPayoutAt(ctx, lease.PayoutAt(), lease.ID)
k.DeleteLeaseForRenewalAt(ctx, lease.RenewalAt(), lease.ID)

lease = v1.Lease{
Expand All @@ -142,8 +142,7 @@ func (k *Keeper) HandleMsgRenewLease(ctx sdk.Context, msg *v1.MsgRenewLeaseReque
Hours: 0,
MaxHours: msg.Hours,
RenewalPricePolicy: lease.RenewalPricePolicy,
InactiveAt: ctx.BlockTime().Add(msg.GetHours()),
PayoutAt: ctx.BlockTime(),
StartAt: ctx.BlockTime(),
}

deposit := lease.DepositAmount()
Expand All @@ -152,8 +151,8 @@ func (k *Keeper) HandleMsgRenewLease(ctx sdk.Context, msg *v1.MsgRenewLeaseReque
}

k.SetLease(ctx, lease)
k.SetLeaseForInactiveAt(ctx, lease.InactiveAt, lease.ID)
k.SetLeaseForPayoutAt(ctx, lease.PayoutAt, lease.ID)
k.SetLeaseForInactiveAt(ctx, lease.InactiveAt(), lease.ID)
k.SetLeaseForPayoutAt(ctx, lease.PayoutAt(), lease.ID)
k.SetLeaseForRenewalAt(ctx, lease.RenewalAt(), lease.ID)

ctx.EventManager().EmitTypedEvent(
Expand Down Expand Up @@ -229,8 +228,7 @@ func (k *Keeper) HandleMsgStartLease(ctx sdk.Context, msg *v1.MsgStartLeaseReque
Hours: 0,
MaxHours: msg.Hours,
RenewalPricePolicy: msg.RenewalPricePolicy,
InactiveAt: ctx.BlockTime().Add(msg.GetHours()),
PayoutAt: ctx.BlockTime(),
StartAt: ctx.BlockTime(),
}

deposit := lease.DepositAmount()
Expand All @@ -242,8 +240,8 @@ func (k *Keeper) HandleMsgStartLease(ctx sdk.Context, msg *v1.MsgStartLeaseReque
k.SetLease(ctx, lease)
k.SetLeaseForNodeByProvider(ctx, nodeAddr, provAddr, lease.ID)
k.SetLeaseForProvider(ctx, provAddr, lease.ID)
k.SetLeaseForInactiveAt(ctx, lease.InactiveAt, lease.ID)
k.SetLeaseForPayoutAt(ctx, lease.PayoutAt, lease.ID)
k.SetLeaseForInactiveAt(ctx, lease.InactiveAt(), lease.ID)
k.SetLeaseForPayoutAt(ctx, lease.PayoutAt(), lease.ID)
k.SetLeaseForRenewalAt(ctx, lease.RenewalAt(), lease.ID)

ctx.EventManager().EmitTypedEvent(
Expand Down
28 changes: 21 additions & 7 deletions x/lease/types/v1/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ func (m *Lease) DepositAmount() sdk.Coin {
return sdk.Coin{Denom: m.Price.Denom, Amount: amount}
}

func (m *Lease) GetHours() time.Duration {
return time.Duration(m.Hours) * time.Hour
}

func (m *Lease) GetMaxHours() time.Duration {
return time.Duration(m.MaxHours) * time.Hour
}

func (m *Lease) InactiveAt() time.Time {
return m.StartAt.Add(m.GetMaxHours())
}

func (m *Lease) PayoutAt() time.Time {
if m.Hours < m.MaxHours {
return m.StartAt.Add(m.GetHours())
}

return time.Time{}
}

func (m *Lease) RefundAmount() sdk.Coin {
amount := m.Price.QuoteValue.MulRaw(m.MaxHours - m.Hours)
return sdk.Coin{Denom: m.Price.Denom, Amount: amount}
Expand All @@ -26,7 +46,7 @@ func (m *Lease) RenewalAt() time.Time {
return time.Time{}
}

return m.InactiveAt
return m.InactiveAt()
}

func (m *Lease) ValidateRenewalPolicies(price v1base.Price) error {
Expand Down Expand Up @@ -65,12 +85,6 @@ func (m *Lease) Validate() error {
if m.MaxHours < m.Hours {
return fmt.Errorf("max_hours cannot be less than hours")
}
if m.PayoutAt.IsZero() {
return fmt.Errorf("payout_at cannot be zero")
}
if m.InactiveAt.IsZero() {
return fmt.Errorf("inactive_at cannot be zero")
}

return nil
}
111 changes: 33 additions & 78 deletions x/lease/types/v1/lease.pb.go

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

10 changes: 0 additions & 10 deletions x/lease/types/v1/msg.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package v1

import (
"time"

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

Expand Down Expand Up @@ -58,10 +56,6 @@ func NewMsgRenewLeaseRequest(from base.ProvAddress, id uint64, hours int64, deno
}
}

func (m *MsgRenewLeaseRequest) GetHours() time.Duration {
return time.Duration(m.Hours) * time.Hour
}

func (m *MsgRenewLeaseRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(types.ErrInvalidMessage, "from cannot be empty")
Expand Down Expand Up @@ -106,10 +100,6 @@ func NewMsgStartLeaseRequest(from base.ProvAddress, nodeAddr base.NodeAddress, h
}
}

func (m *MsgStartLeaseRequest) GetHours() time.Duration {
return time.Duration(m.Hours) * time.Hour
}

func (m *MsgStartLeaseRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(types.ErrInvalidMessage, "from cannot be empty")
Expand Down

0 comments on commit b2deb3f

Please sign in to comment.