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

Take storage cost into account in RenewalCost #251

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
default: patch
---

# Have RenewalCost take into account cost of existing storage
7 changes: 4 additions & 3 deletions rhp/v4/rhp.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,10 @@ func ContractCost(cs consensus.State, p HostPrices, fc types.V2FileContract, min
}

// RenewalCost calculates the cost to the host and renter for renewing a contract.
func RenewalCost(cs consensus.State, p HostPrices, r types.V2FileContractRenewal, minerFee types.Currency) (renter, host types.Currency) {
renter = r.NewContract.RenterOutput.Value.Add(p.ContractPrice).Add(minerFee).Add(cs.V2FileContractTax(r.NewContract)).Sub(r.RenterRollover)
host = r.NewContract.TotalCollateral.Sub(r.HostRollover)
func RenewalCost(cs consensus.State, p HostPrices, r types.V2FileContractRenewal, minerFee types.Currency, prevExpirationHeight uint64) (renter, host types.Currency) {
storageCost := p.StoragePrice.Mul64(r.NewContract.Filesize).Mul64(r.NewContract.ExpirationHeight - prevExpirationHeight)
renter = r.NewContract.RenterOutput.Value.Add(storageCost).Add(p.ContractPrice).Add(minerFee).Add(cs.V2FileContractTax(r.NewContract)).Sub(r.RenterRollover)
host = r.NewContract.HostOutput.Value.Add(r.NewContract.TotalCollateral).Sub(r.HostRollover)
return
}

Expand Down
117 changes: 117 additions & 0 deletions rhp/v4/rhp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"testing"

"go.sia.tech/core/consensus"
"go.sia.tech/core/types"
)

Expand All @@ -19,3 +20,119 @@
t.Fatalf("expected %v, got %v", expected, minAllowance)
}
}

func TestRenewalCost(t *testing.T) {
// contract for renewal
contract := types.V2FileContract{
Capacity: 1000 * SectorSize,
Filesize: 900 * SectorSize,
FileMerkleRoot: types.Hash256{1, 1, 1},
ProofHeight: 500000, // block 500k
ExpirationHeight: 500000 + 1000, // 1000 block window
RenterOutput: types.SiacoinOutput{Value: types.Siacoins(300)},
HostOutput: types.SiacoinOutput{Value: types.Siacoins(400)},
MissedHostValue: types.Siacoins(700),
TotalCollateral: types.Siacoins(100),
RevisionNumber: 99999999,
}

cs := consensus.State{}
prices := HostPrices{
TipHeight: 40000,
ContractPrice: types.NewCurrency64(100),
Collateral: types.NewCurrency64(200),
StoragePrice: types.NewCurrency64(300),
IngressPrice: types.NewCurrency64(400),
EgressPrice: types.NewCurrency64(500),
FreeSectorPrice: types.NewCurrency64(600),
}

// renew contract
renewal, _ := RenewContract(contract, prices, RPCRenewContractParams{
Allowance: contract.RenterOutput.Value.Add(types.Siacoins(20)), // 20 SC more than renter output
Collateral: contract.MissedHostValue.Add(types.Siacoins(10)), // 10 SC more than before
ProofHeight: contract.ExpirationHeight + 1000,
})

minerFee := types.NewCurrency64(700)
prevExpirationHeight := uint64(900) // 100 blocks before

// assert expected results
renter, host := RenewalCost(cs, prices, renewal, minerFee, prevExpirationHeight)
expectedRenter := types.NewCurrency(12531552842177053476, 3317658)
expectedHost := types.NewCurrency(7783457943256563812, 71557343)
if !renter.Equals(expectedRenter) {
t.Fatalf("expected %v, got %v", expectedRenter, renter)
} else if !host.Equals(expectedHost) {
t.Fatalf("expected %v, got %v", expectedHost, host)
}

// make sure the sums match
fc := renewal.NewContract
inputSum := renter.Add(host)
outputSum := fc.RenterOutput.Value.
Add(fc.HostOutput.Value).
Add(cs.V2FileContractTax(fc)).
Add(minerFee)
if !inputSum.Equals(outputSum) {
t.Fatalf("expected %v, got %v", inputSum, outputSum)

Check failure on line 78 in rhp/v4/rhp_test.go

View workflow job for this annotation

GitHub Actions / test / test (1.23, ubuntu-latest)

Test go.sia.tech/core/rhp/v4/TestRenewalCost failed in 0s

rhp_test.go:78: expected 1.381200001280757536391168904 KS, got 1.071200000364210676563968804 KS
}
}

func TestRefreshCost(t *testing.T) {
// contract for renewal
contract := types.V2FileContract{
Capacity: 1000 * SectorSize,
Filesize: 900 * SectorSize,
FileMerkleRoot: types.Hash256{1, 1, 1},
ProofHeight: 500000, // block 500k
ExpirationHeight: 500000 + 1000, // 1000 block window
RenterOutput: types.SiacoinOutput{Value: types.Siacoins(300)},
HostOutput: types.SiacoinOutput{Value: types.Siacoins(400)},
MissedHostValue: types.Siacoins(700),
TotalCollateral: types.Siacoins(1000),
RevisionNumber: 99999999,
}

cs := consensus.State{}
prices := HostPrices{
TipHeight: 40000,
ContractPrice: types.NewCurrency64(100),
Collateral: types.NewCurrency64(200),
StoragePrice: types.NewCurrency64(300),
IngressPrice: types.NewCurrency64(400),
EgressPrice: types.NewCurrency64(500),
FreeSectorPrice: types.NewCurrency64(600),
}

// renew contract
renewal, _ := RefreshContract(contract, prices, RPCRefreshContractParams{
Allowance: contract.RenterOutput.Value.Add(types.Siacoins(20)), // 20 SC more than renter output
Collateral: contract.TotalCollateral.Add(types.Siacoins(10)),
})

minerFee := types.NewCurrency64(700)

// assert expected results
renter, host := RefreshCost(cs, prices, renewal, minerFee)
expectedRenter := types.NewCurrency(10700203959496213284, 21749095)
expectedHost := types.NewCurrency(13106743224624480256, 54752209)
if !renter.Equals(expectedRenter) {
t.Fatalf("expected %v, got %v", expectedRenter, renter)
} else if !host.Equals(expectedHost) {
t.Fatalf("expected %v, got %v", expectedHost, host)
}

// make sure the sums match
fc := renewal.NewContract
inputSum := renter.Add(host)
outputSum := fc.RenterOutput.Value.
Add(fc.HostOutput.Value).
Add(cs.V2FileContractTax(fc)).
Add(minerFee).
Sub(renewal.RenterRollover).
Sub(renewal.HostRollover)
if !inputSum.Equals(outputSum) {
t.Fatalf("expected %v, got %v", inputSum, outputSum)
}
}
Loading