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

Fix ContractRenewalCollateral #135

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
28 changes: 13 additions & 15 deletions rhp/v2/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,27 @@ func ContractRenewalCollateral(fc types.FileContract, expectedNewStorage uint64,
if endHeight < blockHeight {
panic("current blockHeight should be lower than the endHeight")
}
duration := endHeight - blockHeight

// calculate the base collateral - if it exceeds MaxCollateral we can't add more collateral
// calculate the base collateral
baseCollateral := host.Collateral.Mul64(fc.Filesize).Mul64(extension)

// if it exceeds MaxCollateral we can't add more
if baseCollateral.Cmp(host.MaxCollateral) >= 0 {
return types.ZeroCurrency
}

// calculate the new collateral
newCollateral := host.Collateral.Mul64(expectedNewStorage).Mul64(duration)
peterjan marked this conversation as resolved.
Show resolved Hide resolved
// calculate the renewal collateral
duration := endHeight - blockHeight
renewalCollateral := host.Collateral.Mul64(expectedNewStorage).Mul64(duration)

// if the total collateral is more than the MaxCollateral subtract the
// delta.
totalCollateral := baseCollateral.Add(newCollateral)
if totalCollateral.Cmp(host.MaxCollateral) > 0 {
delta := totalCollateral.Sub(host.MaxCollateral)
if delta.Cmp(newCollateral) > 0 {
newCollateral = types.ZeroCurrency
} else {
newCollateral = newCollateral.Sub(delta)
}
// if the new total exceeds MaxCollateral, the renewal collateral must equal
// the delta to ensure we don't exceed it
totalCollateral := baseCollateral.Add(renewalCollateral)
if totalCollateral.Cmp(host.MaxCollateral) >= 0 {
renewalCollateral = host.MaxCollateral.Sub(baseCollateral)
peterjan marked this conversation as resolved.
Show resolved Hide resolved
}
return newCollateral

return renewalCollateral
}

// PrepareContractRenewal constructs a contract renewal transaction.
Expand Down