Skip to content

Commit ee087c9

Browse files
committed
chore: add HasAnyLeaseForNodeByProvider method in lease module
1 parent 412ec23 commit ee087c9

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

x/lease/keeper/lease.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ func (k *Keeper) IterateLeasesForNodeByProvider(ctx sdk.Context, nodeAddr base.N
137137
}
138138
}
139139

140+
// HasAnyLeaseForNodeByProvider checks if any lease exists for a given node-provider pair.
141+
func (k *Keeper) HasAnyLeaseForNodeByProvider(ctx sdk.Context, nodeAddr base.NodeAddress, provAddr base.ProvAddress) bool {
142+
exists := false
143+
144+
k.IterateLeasesForNodeByProvider(ctx, nodeAddr, provAddr, func(_ int, _ v1.Lease) bool {
145+
exists = true
146+
147+
// stop iteration after finding the first lease
148+
return true
149+
})
150+
151+
return exists
152+
}
153+
140154
// IterateLeasesForNode iterates over all leases for a specific node and calls the provided function for each lease.
141155
// The iteration stops when the provided function returns 'true' or an error occurs.
142156
func (k *Keeper) IterateLeasesForNode(ctx sdk.Context, addr base.NodeAddress, fn func(int, v1.Lease) (bool, error)) error {

x/lease/keeper/msg_handler.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,7 @@ func (k *Keeper) HandleMsgStartLease(ctx sdk.Context, msg *v1.MsgStartLeaseReque
263263
}
264264

265265
// Check if a lease already exists between this provider and node
266-
leaseExists := false
267-
268-
k.IterateLeasesForNodeByProvider(ctx, nodeAddr, provAddr, func(_ int, _ v1.Lease) bool {
269-
leaseExists = true
270-
271-
return true
272-
})
273-
274-
if leaseExists {
266+
if k.HasAnyLeaseForNodeByProvider(ctx, nodeAddr, provAddr) {
275267
return nil, types.NewErrorDuplicateLease(nodeAddr, provAddr)
276268
}
277269

x/plan/keeper/alias.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func (k *Keeper) GetLease(ctx sdk.Context, id uint64) (leasetypes.Lease, bool) {
1212
return k.lease.GetLease(ctx, id)
1313
}
1414

15-
func (k *Keeper) IterateLeasesForNodeByProvider(ctx sdk.Context, nodeAddr base.NodeAddress, provAddr base.ProvAddress, fn func(index int, item leasetypes.Lease) (stop bool)) {
16-
k.lease.IterateLeasesForNodeByProvider(ctx, nodeAddr, provAddr, fn)
15+
func (k *Keeper) HasAnyLeaseForNodeByProvider(ctx sdk.Context, nodeAddr base.NodeAddress, provAddr base.ProvAddress) bool {
16+
return k.lease.HasAnyLeaseForNodeByProvider(ctx, nodeAddr, provAddr)
1717
}
1818

1919
func (k *Keeper) GetNode(ctx sdk.Context, addr base.NodeAddress) (nodetypes.Node, bool) {

x/plan/keeper/expected.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type BankKeeper interface {
2020

2121
type LeaseKeeper interface {
2222
GetLease(ctx sdk.Context, id uint64) (leasetypes.Lease, bool)
23-
IterateLeasesForNodeByProvider(ctx sdk.Context, nodeAddr base.NodeAddress, provAddr base.ProvAddress, fn func(index int, item leasetypes.Lease) (stop bool))
23+
HasAnyLeaseForNodeByProvider(ctx sdk.Context, nodeAddr base.NodeAddress, provAddr base.ProvAddress) bool
2424
}
2525

2626
type NodeKeeper interface {

x/plan/keeper/msg_handler.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
base "github.com/sentinel-official/sentinelhub/v12/types"
77
v1base "github.com/sentinel-official/sentinelhub/v12/types/v1"
8-
"github.com/sentinel-official/sentinelhub/v12/x/lease/types/v1"
98
"github.com/sentinel-official/sentinelhub/v12/x/plan/types"
109
"github.com/sentinel-official/sentinelhub/v12/x/plan/types/v3"
1110
subscriptiontypes "github.com/sentinel-official/sentinelhub/v12/x/subscription/types/v3"
@@ -99,15 +98,7 @@ func (k *Keeper) HandleMsgLinkNode(ctx sdk.Context, msg *v3.MsgLinkNodeRequest)
9998
}
10099

101100
// Ensure an active lease exists between the node and provider
102-
leaseExists := false
103-
104-
k.IterateLeasesForNodeByProvider(ctx, nodeAddr, provAddr, func(_ int, _ v1.Lease) bool {
105-
leaseExists = true
106-
107-
return true
108-
})
109-
110-
if !leaseExists {
101+
if !k.HasAnyLeaseForNodeByProvider(ctx, nodeAddr, provAddr) {
111102
return nil, types.NewErrorLeaseForNodeByProviderNotFound(nodeAddr, provAddr)
112103
}
113104

0 commit comments

Comments
 (0)