Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: Checking Vesting Coins with AmountOf #3486

Merged
merged 4 commits into from
Feb 4, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 6 additions & 52 deletions x/auth/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,27 +221,11 @@ func (bva BaseVestingAccount) spendableCoins(vestingCoins sdk.Coins) sdk.Coins {
var spendableCoins sdk.Coins
bc := bva.GetCoins()

j, k := 0, 0
for _, coin := range bc {
// zip/lineup all coins by their denomination to provide O(n) time
for j < len(vestingCoins) && vestingCoins[j].Denom != coin.Denom {
j++
}
for k < len(bva.DelegatedVesting) && bva.DelegatedVesting[k].Denom != coin.Denom {
k++
}

baseAmt := coin.Amount

vestingAmt := sdk.ZeroInt()
if len(vestingCoins) > 0 {
vestingAmt = vestingCoins[j].Amount
}

delVestingAmt := sdk.ZeroInt()
if len(bva.DelegatedVesting) > 0 {
delVestingAmt = bva.DelegatedVesting[k].Amount
}
vestingAmt := vestingCoins.AmountOf(coin.Denom)
delVestingAmt := bva.DelegatedVesting.AmountOf(coin.Denom)

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

i, j, k := 0, 0, 0
for _, coin := range amount {
// zip/lineup all coins by their denomination to provide O(n) time
for i < len(bc) && bc[i].Denom != coin.Denom {
i++
}
for j < len(vestingCoins) && vestingCoins[j].Denom != coin.Denom {
j++
}
for k < len(bva.DelegatedVesting) && bva.DelegatedVesting[k].Denom != coin.Denom {
k++
}

baseAmt := sdk.ZeroInt()
if len(bc) > 0 {
baseAmt = bc[i].Amount
}

vestingAmt := sdk.ZeroInt()
if len(vestingCoins) > 0 {
vestingAmt = vestingCoins[j].Amount
}

delVestingAmt := sdk.ZeroInt()
if len(bva.DelegatedVesting) > 0 {
delVestingAmt = bva.DelegatedVesting[k].Amount
}
baseAmt := bc.AmountOf(coin.Denom)
vestingAmt := vestingCoins.AmountOf(coin.Denom)
delVestingAmt := bva.DelegatedVesting.AmountOf(coin.Denom)

// Panic if the delegation amount is zero or if the base coins does not
// exceed the desired delegation amount.
Expand Down Expand Up @@ -325,21 +288,12 @@ func (bva *BaseVestingAccount) trackDelegation(vestingCoins, amount sdk.Coins) {
//
// CONTRACT: The account's coins and undelegation coins must be sorted.
func (bva *BaseVestingAccount) TrackUndelegation(amount sdk.Coins) {
i := 0
for _, coin := range amount {
// panic if the undelegation amount is zero
if coin.Amount.IsZero() {
panic("undelegation attempt with zero coins")
}

for i < len(bva.DelegatedFree) && bva.DelegatedFree[i].Denom != coin.Denom {
i++
}

delegatedFree := sdk.ZeroInt()
if len(bva.DelegatedFree) > 0 {
delegatedFree = bva.DelegatedFree[i].Amount
}
delegatedFree := bva.DelegatedFree.AmountOf(coin.Denom)

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