Skip to content

Commit

Permalink
rhp/v2,rhp/v3: use WindowEnd for duration calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed May 22, 2023
1 parent a982d45 commit 8521a5f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions rhp/v2/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ func (sh *SessionHandler) rpcRenewAndClearContract(s *session, log *zap.Logger)
// not increase, base costs are zero since the storage is already paid for.
baseRevenue := settings.ContractPrice
var baseCollateral types.Currency
if renewedContract.WindowStart > existingRevision.WindowStart {
extension := uint64(renewedContract.WindowStart - existingRevision.WindowStart)
if renewedContract.WindowEnd > existingRevision.WindowEnd {
extension := uint64(renewedContract.WindowEnd - existingRevision.WindowEnd)
baseRevenue = baseRevenue.Add(settings.StoragePrice.Mul64(renewedContract.Filesize).Mul64(extension))
baseCollateral = settings.Collateral.Mul64(renewedContract.Filesize).Mul64(extension)
}
Expand Down Expand Up @@ -530,7 +530,7 @@ func (sh *SessionHandler) rpcWrite(s *session, log *zap.Logger) error {
return fmt.Errorf("failed to read write request: %w", err)
}

remainingDuration := uint64(s.contract.Revision.WindowStart) - currentHeight
remainingDuration := uint64(s.contract.Revision.WindowEnd) - currentHeight
// validate the requested actions
oldSectors := s.contract.Revision.Filesize / rhpv2.SectorSize
costs, err := validateWriteActions(req.Actions, oldSectors, req.MerkleProof, remainingDuration, settings)
Expand Down
14 changes: 7 additions & 7 deletions rhp/v2/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestUploadDownload(t *testing.T) {

// calculate the remaining duration of the contract
var remainingDuration uint64
contractExpiration := uint64(session.Revision().Revision.WindowStart)
contractExpiration := uint64(session.Revision().Revision.WindowEnd)
currentHeight := renter.TipState().Index.Height
if contractExpiration < currentHeight {
t.Fatal("contract expired")
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestRenew(t *testing.T) {
}
time.Sleep(100 * time.Millisecond)

renewHeight := origin.Revision.WindowStart + 10
renewHeight := origin.Revision.WindowEnd + 10
settings := session.Settings()
current := session.Revision().Revision
additionalCollateral := rhpv2.ContractRenewalCollateral(current.FileContract, 1<<22, settings, renter.TipState().Index.Height, renewHeight)
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestRenew(t *testing.T) {

// calculate the remaining duration of the contract
var remainingDuration uint64
contractExpiration := uint64(session.Revision().Revision.WindowStart)
contractExpiration := uint64(session.Revision().Revision.WindowEnd)
currentHeight := renter.TipState().Index.Height
if contractExpiration < currentHeight {
t.Fatal("contract expired")
Expand All @@ -259,7 +259,7 @@ func TestRenew(t *testing.T) {
time.Sleep(100 * time.Millisecond)

settings := session.Settings()
renewHeight := origin.Revision.WindowStart + 10
renewHeight := origin.Revision.WindowEnd + 10
current := session.Revision().Revision
additionalCollateral := rhpv2.ContractRenewalCollateral(current.FileContract, 1<<22, settings, renter.TipState().Index.Height, renewHeight)
renewed, basePrice := rhpv2.PrepareContractRenewal(session.Revision().Revision, renter.WalletAddress(), renter.PrivateKey(), types.Siacoins(10), additionalCollateral, host.PublicKey(), settings, renewHeight)
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestRenew(t *testing.T) {

// calculate the remaining duration of the contract
var remainingDuration uint64
contractExpiration := uint64(session.Revision().Revision.WindowStart)
contractExpiration := uint64(session.Revision().Revision.WindowEnd)
currentHeight := renter.TipState().Index.Height
if contractExpiration < currentHeight {
t.Fatal("contract expired")
Expand All @@ -370,7 +370,7 @@ func TestRenew(t *testing.T) {
time.Sleep(100 * time.Millisecond)

settings := session.Settings()
renewHeight := origin.Revision.WindowStart + 10
renewHeight := origin.Revision.WindowEnd + 10
current := session.Revision().Revision
additionalCollateral := rhpv2.ContractRenewalCollateral(current.FileContract, 1<<22, settings, renter.TipState().Index.Height, renewHeight)
renewed, basePrice := rhpv2.PrepareContractRenewal(session.Revision().Revision, renter.WalletAddress(), renter.PrivateKey(), types.Siacoins(10), additionalCollateral, host.PublicKey(), settings, renewHeight)
Expand Down Expand Up @@ -448,7 +448,7 @@ func BenchmarkUpload(b *testing.B) {

// calculate the remaining duration of the contract
var remainingDuration uint64
contractExpiration := uint64(session.Revision().Revision.WindowStart)
contractExpiration := uint64(session.Revision().Revision.WindowEnd)
currentHeight := renter.TipState().Index.Height
if contractExpiration < currentHeight {
b.Fatal("contract expired")
Expand Down
2 changes: 1 addition & 1 deletion rhp/v3/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ func (sh *SessionHandler) newExecutor(instructions []rhpv3.Instruction, data []b
}

if finalize {
ex.remainingDuration = revision.Revision.WindowStart - pt.HostBlockHeight
ex.remainingDuration = revision.Revision.WindowEnd - pt.HostBlockHeight
updater, err := sh.contracts.ReviseContract(revision.Revision.ParentID)
if err != nil {
return nil, fmt.Errorf("failed to create contract updater: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions rhp/v3/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ func (sh *SessionHandler) handleRPCRenew(s *rhpv3.Stream, log *zap.Logger) error
// not increase, base costs are zero since the storage is already paid for.
baseRevenue := pt.RenewContractCost
var baseCollateral types.Currency
if renewal.WindowStart > existing.Revision.WindowStart {
extension := uint64(renewal.WindowStart - existing.Revision.WindowStart)
if renewal.WindowEnd > existing.Revision.WindowEnd {
extension := uint64(renewal.WindowEnd - existing.Revision.WindowEnd)
baseRevenue = baseRevenue.Add(pt.WriteStoreCost.Mul64(renewal.Filesize).Mul64(extension))
baseCollateral = pt.CollateralCost.Mul64(renewal.Filesize).Mul64(extension)
}
Expand Down
8 changes: 4 additions & 4 deletions rhp/v3/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestAppendSector(t *testing.T) {
// upload a sector
accountSession := contractSession.WithAccountPayment(account, renter.PrivateKey())
// calculate the cost of the upload
cost, _ := pt.BaseCost().Add(pt.AppendSectorCost(revision.Revision.WindowStart - renter.TipState().Index.Height)).Total()
cost, _ := pt.BaseCost().Add(pt.AppendSectorCost(revision.Revision.WindowEnd - renter.TipState().Index.Height)).Total()
if cost.IsZero() {
t.Fatal("cost is zero")
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestRenew(t *testing.T) {
}

state = renter.TipState()
renewHeight := origin.Revision.WindowStart + 10
renewHeight := origin.Revision.WindowEnd + 10
renterFunds := types.Siacoins(10)
additionalCollateral := types.Siacoins(20)
renewal, _, err := session.RenewContract(&origin, settings.Address, renter.PrivateKey(), renterFunds, additionalCollateral, renewHeight)
Expand Down Expand Up @@ -346,7 +346,7 @@ func TestRenew(t *testing.T) {

// calculate the remaining duration of the contract
var remainingDuration uint64
contractExpiration := uint64(origin.Revision.WindowStart)
contractExpiration := uint64(origin.Revision.WindowEnd)
currentHeight := renter.TipState().Index.Height
if contractExpiration < currentHeight {
t.Fatal("contract expired")
Expand All @@ -368,7 +368,7 @@ func TestRenew(t *testing.T) {
time.Sleep(100 * time.Millisecond)

state = renter.TipState()
renewHeight := origin.Revision.WindowStart + 10
renewHeight := origin.Revision.WindowEnd + 10
renterFunds := types.Siacoins(10)
additionalCollateral := types.Siacoins(20)
renewal, _, err := session.RenewContract(&origin, settings.Address, renter.PrivateKey(), renterFunds, additionalCollateral, renewHeight)
Expand Down

0 comments on commit 8521a5f

Please sign in to comment.