Skip to content

Commit c256b10

Browse files
Zaki ManianZaki Manian
Zaki Manian
authored and
Zaki Manian
committed
Remove a performance optimization for checking the state of vesting coins for a more expensive but correct call to AmountOf
1 parent 857a65d commit c256b10

File tree

1 file changed

+6
-52
lines changed

1 file changed

+6
-52
lines changed

x/auth/account.go

+6-52
Original file line numberDiff line numberDiff line change
@@ -221,27 +221,11 @@ func (bva BaseVestingAccount) spendableCoins(vestingCoins sdk.Coins) sdk.Coins {
221221
var spendableCoins sdk.Coins
222222
bc := bva.GetCoins()
223223

224-
j, k := 0, 0
225224
for _, coin := range bc {
226225
// zip/lineup all coins by their denomination to provide O(n) time
227-
for j < len(vestingCoins) && vestingCoins[j].Denom != coin.Denom {
228-
j++
229-
}
230-
for k < len(bva.DelegatedVesting) && bva.DelegatedVesting[k].Denom != coin.Denom {
231-
k++
232-
}
233-
234226
baseAmt := coin.Amount
235-
236-
vestingAmt := sdk.ZeroInt()
237-
if len(vestingCoins) > 0 {
238-
vestingAmt = vestingCoins[j].Amount
239-
}
240-
241-
delVestingAmt := sdk.ZeroInt()
242-
if len(bva.DelegatedVesting) > 0 {
243-
delVestingAmt = bva.DelegatedVesting[k].Amount
244-
}
227+
vestingAmt := vestingCoins.AmountOf(coin.Denom)
228+
delVestingAmt := bva.DelegatedVesting.AmountOf(coin.Denom)
245229

246230
// compute min((BC + DV) - V, BC) per the specification
247231
min := sdk.MinInt(baseAmt.Add(delVestingAmt).Sub(vestingAmt), baseAmt)
@@ -264,33 +248,12 @@ func (bva BaseVestingAccount) spendableCoins(vestingCoins sdk.Coins) sdk.Coins {
264248
func (bva *BaseVestingAccount) trackDelegation(vestingCoins, amount sdk.Coins) {
265249
bc := bva.GetCoins()
266250

267-
i, j, k := 0, 0, 0
268251
for _, coin := range amount {
269252
// zip/lineup all coins by their denomination to provide O(n) time
270-
for i < len(bc) && bc[i].Denom != coin.Denom {
271-
i++
272-
}
273-
for j < len(vestingCoins) && vestingCoins[j].Denom != coin.Denom {
274-
j++
275-
}
276-
for k < len(bva.DelegatedVesting) && bva.DelegatedVesting[k].Denom != coin.Denom {
277-
k++
278-
}
279253

280-
baseAmt := sdk.ZeroInt()
281-
if len(bc) > 0 {
282-
baseAmt = bc[i].Amount
283-
}
284-
285-
vestingAmt := sdk.ZeroInt()
286-
if len(vestingCoins) > 0 {
287-
vestingAmt = vestingCoins[j].Amount
288-
}
289-
290-
delVestingAmt := sdk.ZeroInt()
291-
if len(bva.DelegatedVesting) > 0 {
292-
delVestingAmt = bva.DelegatedVesting[k].Amount
293-
}
254+
baseAmt := bc.AmountOf(coin.Denom)
255+
vestingAmt := vestingCoins.AmountOf(coin.Denom)
256+
delVestingAmt := bva.DelegatedVesting.AmountOf(coin.Denom)
294257

295258
// Panic if the delegation amount is zero or if the base coins does not
296259
// exceed the desired delegation amount.
@@ -325,21 +288,12 @@ func (bva *BaseVestingAccount) trackDelegation(vestingCoins, amount sdk.Coins) {
325288
//
326289
// CONTRACT: The account's coins and undelegation coins must be sorted.
327290
func (bva *BaseVestingAccount) TrackUndelegation(amount sdk.Coins) {
328-
i := 0
329291
for _, coin := range amount {
330292
// panic if the undelegation amount is zero
331293
if coin.Amount.IsZero() {
332294
panic("undelegation attempt with zero coins")
333295
}
334-
335-
for i < len(bva.DelegatedFree) && bva.DelegatedFree[i].Denom != coin.Denom {
336-
i++
337-
}
338-
339-
delegatedFree := sdk.ZeroInt()
340-
if len(bva.DelegatedFree) > 0 {
341-
delegatedFree = bva.DelegatedFree[i].Amount
342-
}
296+
delegatedFree := bva.DelegatedFree.AmountOf(coin.Denom)
343297

344298
// compute x and y per the specification, where:
345299
// X := min(DF, D)

0 commit comments

Comments
 (0)