Skip to content

Commit

Permalink
rhp4: RPC remove -> RPC free
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Oct 22, 2024
1 parent 5672e6c commit 2652ab2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
28 changes: 14 additions & 14 deletions rhp/v4/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (hp HostPrices) EncodeTo(e *types.Encoder) {
types.V2Currency(hp.StoragePrice).EncodeTo(e)
types.V2Currency(hp.IngressPrice).EncodeTo(e)
types.V2Currency(hp.EgressPrice).EncodeTo(e)
types.V2Currency(hp.RemoveSectorPrice).EncodeTo(e)
types.V2Currency(hp.FreeSectorPrice).EncodeTo(e)
e.WriteUint64(hp.TipHeight)
e.WriteTime(hp.ValidUntil)
hp.Signature.EncodeTo(e)
Expand All @@ -38,7 +38,7 @@ func (hp *HostPrices) DecodeFrom(d *types.Decoder) {
(*types.V2Currency)(&hp.StoragePrice).DecodeFrom(d)
(*types.V2Currency)(&hp.IngressPrice).DecodeFrom(d)
(*types.V2Currency)(&hp.EgressPrice).DecodeFrom(d)
(*types.V2Currency)(&hp.RemoveSectorPrice).DecodeFrom(d)
(*types.V2Currency)(&hp.FreeSectorPrice).DecodeFrom(d)
hp.TipHeight = d.ReadUint64()
hp.ValidUntil = d.ReadTime()
hp.Signature.DecodeFrom(d)
Expand Down Expand Up @@ -354,57 +354,57 @@ func (r *RPCRefreshContractThirdResponse) maxLen() int {
return reasonableObjectSize
}

func (r *RPCRemoveSectorsRequest) encodeTo(e *types.Encoder) {
func (r *RPCFreeSectorsRequest) encodeTo(e *types.Encoder) {
r.ContractID.EncodeTo(e)
r.Prices.EncodeTo(e)
types.EncodeSliceFn(e, r.Indices, func(e *types.Encoder, v uint64) {
e.WriteUint64(v)
})
r.ChallengeSignature.EncodeTo(e)
}
func (r *RPCRemoveSectorsRequest) decodeFrom(d *types.Decoder) {
func (r *RPCFreeSectorsRequest) decodeFrom(d *types.Decoder) {
r.ContractID.DecodeFrom(d)
r.Prices.DecodeFrom(d)
types.DecodeSliceFn(d, &r.Indices, func(d *types.Decoder) uint64 {
return d.ReadUint64()
})
r.ChallengeSignature.DecodeFrom(d)
}
func (r *RPCRemoveSectorsRequest) maxLen() int {
func (r *RPCFreeSectorsRequest) maxLen() int {
return reasonableObjectSize
}

func (r *RPCRemoveSectorsResponse) encodeTo(e *types.Encoder) {
func (r *RPCFreeSectorsResponse) encodeTo(e *types.Encoder) {
types.EncodeSlice(e, r.OldSubtreeHashes)
types.EncodeSlice(e, r.OldLeafHashes)
r.NewMerkleRoot.EncodeTo(e)
}
func (r *RPCRemoveSectorsResponse) decodeFrom(d *types.Decoder) {
func (r *RPCFreeSectorsResponse) decodeFrom(d *types.Decoder) {
types.DecodeSlice(d, &r.OldSubtreeHashes)
types.DecodeSlice(d, &r.OldLeafHashes)
r.NewMerkleRoot.DecodeFrom(d)
}
func (r *RPCRemoveSectorsResponse) maxLen() int {
func (r *RPCFreeSectorsResponse) maxLen() int {
return reasonableObjectSize
}

func (r *RPCRemoveSectorsSecondResponse) encodeTo(e *types.Encoder) {
func (r *RPCFreeSectorsSecondResponse) encodeTo(e *types.Encoder) {
r.RenterSignature.EncodeTo(e)
}
func (r *RPCRemoveSectorsSecondResponse) decodeFrom(d *types.Decoder) {
func (r *RPCFreeSectorsSecondResponse) decodeFrom(d *types.Decoder) {
r.RenterSignature.DecodeFrom(d)
}
func (r *RPCRemoveSectorsSecondResponse) maxLen() int {
func (r *RPCFreeSectorsSecondResponse) maxLen() int {
return sizeofSignature
}

func (r *RPCRemoveSectorsThirdResponse) encodeTo(e *types.Encoder) {
func (r *RPCFreeSectorsThirdResponse) encodeTo(e *types.Encoder) {
r.HostSignature.EncodeTo(e)
}
func (r *RPCRemoveSectorsThirdResponse) decodeFrom(d *types.Decoder) {
func (r *RPCFreeSectorsThirdResponse) decodeFrom(d *types.Decoder) {
r.HostSignature.DecodeFrom(d)
}
func (r *RPCRemoveSectorsThirdResponse) maxLen() int {
func (r *RPCFreeSectorsThirdResponse) maxLen() int {
return sizeofSignature
}

Expand Down
68 changes: 34 additions & 34 deletions rhp/v4/rhp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
RPCFundAccountsID = types.NewSpecifier("FundAccounts")
RPCLatestRevisionID = types.NewSpecifier("LatestRevision")
RPCAppendSectorsID = types.NewSpecifier("AppendSectors")
RPCRemoveSectorsID = types.NewSpecifier("RemoveSectors")
RPCFreeSectorsID = types.NewSpecifier("FreeSectors")
RPCReadSectorID = types.NewSpecifier("ReadSector")
RPCRenewContractID = types.NewSpecifier("RenewContract")
RPCRefreshContractID = types.NewSpecifier("RefreshContract")
Expand Down Expand Up @@ -74,14 +74,14 @@ func (u Usage) Add(b Usage) Usage {
// HostPrices specify a time-bound set of parameters used to calculate the cost
// of various RPCs.
type HostPrices struct {
ContractPrice types.Currency `json:"contractPrice"`
Collateral types.Currency `json:"collateral"`
StoragePrice types.Currency `json:"storagePrice"`
IngressPrice types.Currency `json:"ingressPrice"`
EgressPrice types.Currency `json:"egressPrice"`
RemoveSectorPrice types.Currency `json:"removeSectorPrice"`
TipHeight uint64 `json:"tipHeight"`
ValidUntil time.Time `json:"validUntil"`
ContractPrice types.Currency `json:"contractPrice"`
Collateral types.Currency `json:"collateral"`
StoragePrice types.Currency `json:"storagePrice"`
IngressPrice types.Currency `json:"ingressPrice"`
EgressPrice types.Currency `json:"egressPrice"`
FreeSectorPrice types.Currency `json:"freeSectorPrice"`
TipHeight uint64 `json:"tipHeight"`
ValidUntil time.Time `json:"validUntil"`

// covers above fields
Signature types.Signature `json:"signature"`
Expand Down Expand Up @@ -125,10 +125,10 @@ func (hp HostPrices) RPCVerifySectorCost() Usage {
}
}

// RPCRemoveSectorsCost returns the cost of removing sectors from a contract.
func (hp HostPrices) RPCRemoveSectorsCost(sectors int) Usage {
// RPCFreeSectorsCost returns the cost of removing sectors from a contract.
func (hp HostPrices) RPCFreeSectorsCost(sectors int) Usage {
return Usage{
RPC: hp.RemoveSectorPrice.Mul64(uint64(sectors)),
RPC: hp.FreeSectorPrice.Mul64(uint64(sectors)),
}
}

Expand All @@ -149,7 +149,7 @@ func (hp HostPrices) SigHash() types.Hash256 {
types.V2Currency(hp.StoragePrice).EncodeTo(h.E)
types.V2Currency(hp.IngressPrice).EncodeTo(h.E)
types.V2Currency(hp.EgressPrice).EncodeTo(h.E)
types.V2Currency(hp.RemoveSectorPrice).EncodeTo(h.E)
types.V2Currency(hp.FreeSectorPrice).EncodeTo(h.E)
h.E.WriteUint64(hp.TipHeight)
h.E.WriteTime(hp.ValidUntil)
return h.Sum()
Expand Down Expand Up @@ -349,26 +349,26 @@ type (
TransactionSet []types.V2Transaction `json:"transactionSet"`
}

// RPCRemoveSectorsRequest implements Object.
RPCRemoveSectorsRequest struct {
// RPCFreeSectorsRequest implements Object.
RPCFreeSectorsRequest struct {
ContractID types.FileContractID `json:"contractID"`
Prices HostPrices `json:"prices"`
Indices []uint64 `json:"indices"`
// A ChallengeSignature proves the renter can modify the contract.
ChallengeSignature types.Signature `json:"challengeSignature"`
}
// RPCRemoveSectorsResponse implements Object.
RPCRemoveSectorsResponse struct {
// RPCFreeSectorsResponse implements Object.
RPCFreeSectorsResponse struct {
OldSubtreeHashes []types.Hash256 `json:"oldSubtreeHashes"`
OldLeafHashes []types.Hash256 `json:"oldLeafHashes"`
NewMerkleRoot types.Hash256 `json:"newMerkleRoot"`
}
// RPCRemoveSectorsSecondResponse implements Object.
RPCRemoveSectorsSecondResponse struct {
// RPCFreeSectorsSecondResponse implements Object.
RPCFreeSectorsSecondResponse struct {
RenterSignature types.Signature `json:"renterSignature"`
}
// RPCRemoveSectorsThirdResponse implements Object.
RPCRemoveSectorsThirdResponse struct {
// RPCFreeSectorsThirdResponse implements Object.
RPCFreeSectorsThirdResponse struct {
HostSignature types.Signature `json:"hostSignature"`
}

Expand Down Expand Up @@ -504,15 +504,15 @@ type (

// ChallengeSigHash returns the hash of the challenge signature used for
// signing.
func (r *RPCRemoveSectorsRequest) ChallengeSigHash(revisionNumber uint64) types.Hash256 {
func (r *RPCFreeSectorsRequest) ChallengeSigHash(revisionNumber uint64) types.Hash256 {
h := types.NewHasher()
r.ContractID.EncodeTo(h.E)
h.E.WriteUint64(revisionNumber)
return h.Sum()
}

// ValidChallengeSignature checks the challenge signature for validity.
func (r *RPCRemoveSectorsRequest) ValidChallengeSignature(fc types.V2FileContract) bool {
func (r *RPCFreeSectorsRequest) ValidChallengeSignature(fc types.V2FileContract) bool {
return fc.RenterPublicKey.VerifyHash(r.ChallengeSigHash(fc.RevisionNumber+1), r.ChallengeSignature)
}

Expand Down Expand Up @@ -625,19 +625,18 @@ func PayWithContract(fc *types.V2FileContract, usage Usage) error {
return nil
}

// ReviseForRemoveSectors creates a contract revision from a modify sectors request
// and response.
func ReviseForRemoveSectors(fc types.V2FileContract, prices HostPrices, newRoot types.Hash256, deletions int) (types.V2FileContract, Usage, error) {
// ReviseForFreeSectors creates a contract revision for the free sectors RPC
func ReviseForFreeSectors(fc types.V2FileContract, prices HostPrices, newRoot types.Hash256, deletions int) (types.V2FileContract, Usage, error) {
fc.Filesize -= SectorSize * uint64(deletions)
usage := prices.RPCRemoveSectorsCost(deletions)
usage := prices.RPCFreeSectorsCost(deletions)
if err := PayWithContract(&fc, usage); err != nil {
return fc, Usage{}, err
}
fc.FileMerkleRoot = newRoot
return fc, usage, nil
}

// ReviseForAppendSectors creates a contract revision from an append sectors request
// ReviseForAppendSectors creates a contract revision for the append sectors RPC
func ReviseForAppendSectors(fc types.V2FileContract, prices HostPrices, root types.Hash256, appended uint64) (types.V2FileContract, Usage, error) {
sectors := fc.Filesize / SectorSize
capacity := fc.Capacity / SectorSize
Expand All @@ -651,14 +650,14 @@ func ReviseForAppendSectors(fc types.V2FileContract, prices HostPrices, root typ
return fc, usage, nil
}

// ReviseForSectorRoots creates a contract revision from a sector roots request
// ReviseForSectorRoots creates a contract revision for the sector roots RPC
func ReviseForSectorRoots(fc types.V2FileContract, prices HostPrices, numRoots uint64) (types.V2FileContract, Usage, error) {
usage := prices.RPCSectorRootsCost(numRoots)
err := PayWithContract(&fc, usage)
return fc, usage, err
}

// ReviseForFundAccounts creates a contract revision from a fund account request.
// ReviseForFundAccounts creates a contract revision for the fund accounts RPC
func ReviseForFundAccounts(fc types.V2FileContract, amount types.Currency) (types.V2FileContract, Usage, error) {
usage := Usage{AccountFunding: amount}
err := PayWithContract(&fc, usage)
Expand All @@ -672,7 +671,7 @@ func MinRenterAllowance(hp HostPrices, duration uint64, collateral types.Currenc
return hp.StoragePrice.Mul64(duration).Mul(maxCollateralBytes)
}

// RenewContract creates a contract renewal from an existing contract revision
// RenewContract creates a contract renewal for the renew RPC
func RenewContract(fc types.V2FileContract, prices HostPrices, rp RPCRenewContractParams) (types.V2FileContractRenewal, Usage) {
var renewal types.V2FileContractRenewal
// clear the old contract
Expand Down Expand Up @@ -704,8 +703,9 @@ func RenewContract(fc types.V2FileContract, prices HostPrices, rp RPCRenewContra
// missed host value should only include the new collateral value
renewal.NewContract.MissedHostValue = rp.Collateral

// storage cost is the difference between the new and old contract since the old contract
// already paid for the storage up to the current expiration height.
// storage cost is the difference between the new and old contract since the
// old contract already paid for the storage up to the current expiration
// height.
storageCost := prices.StoragePrice.Mul64(fc.Filesize).Mul64(renewal.NewContract.ExpirationHeight - fc.ExpirationHeight)

// host output value includes the locked + risked collateral, the additional
Expand Down Expand Up @@ -737,7 +737,7 @@ func RenewContract(fc types.V2FileContract, prices HostPrices, rp RPCRenewContra
}
}

// RefreshContract creates a new contract renewal from an existing contract revision
// RefreshContract creates a contract renewal for the refresh RPC.
func RefreshContract(fc types.V2FileContract, prices HostPrices, rp RPCRefreshContractParams) (types.V2FileContractRenewal, Usage) {
var renewal types.V2FileContractRenewal

Expand Down
2 changes: 1 addition & 1 deletion rhp/v4/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (req *RPCWriteSectorStreamingRequest) Validate(pk types.PublicKey, maxDurat
}

// Validate validates a modify sectors request. Signatures are not validated.
func (req *RPCRemoveSectorsRequest) Validate(pk types.PublicKey, fc types.V2FileContract, maxActions uint64) error {
func (req *RPCFreeSectorsRequest) Validate(pk types.PublicKey, fc types.V2FileContract, maxActions uint64) error {
if err := req.Prices.Validate(pk); err != nil {
return fmt.Errorf("prices are invalid: %w", err)
} else if uint64(len(req.Indices)) > maxActions {
Expand Down

0 comments on commit 2652ab2

Please sign in to comment.