Skip to content

Commit

Permalink
rhp: add RPCWriteCost
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Aug 30, 2023
1 parent 8f97621 commit b7b4c57
Showing 1 changed file with 51 additions and 37 deletions.
88 changes: 51 additions & 37 deletions rhp/v2/rhp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
package rhp

import (
"crypto/ed25519"
"encoding/json"
"fmt"
"math/bits"
"net"
"strings"
"time"
Expand Down Expand Up @@ -281,51 +279,67 @@ type (
}
)

// RPCSectorRootsCost returns the price of a SectorRoots RPC.
func RPCSectorRootsCost(settings HostSettings, rootOffset, numRoots uint64) types.Currency {
proofSize := RangeProofSize(LeavesPerSector, rootOffset, rootOffset+numRoots)
return settings.BaseRPCPrice.
Add(settings.DownloadBandwidthPrice.Mul64(ed25519.SignatureSize)). // signature
Add(settings.DownloadBandwidthPrice.Mul64(numRoots * 32)). // roots
Add(settings.DownloadBandwidthPrice.Mul64(proofSize * 32)) // proof
}

// RPCReadCost returns the price of a Read RPC.
func RPCReadCost(settings HostSettings, sections []RPCReadRequestSection) types.Currency {
sectorAccessPrice := settings.SectorAccessPrice.Mul64(uint64(len(sections)))
func RPCReadCost(settings HostSettings, sections []RPCReadRequestSection, proof bool) types.Currency {
var bandwidth uint64
for _, sec := range sections {
bandwidth += sec.Length
proofSize := RangeProofSize(LeavesPerSector, sec.Offset/LeafSize, (sec.Offset+sec.Length)/LeafSize)
bandwidth += proofSize * 32 // proof
if proof {
proofSize := RangeProofSize(LeavesPerSector, sec.Offset/LeafSize, (sec.Offset+sec.Length)/LeafSize)
bandwidth += proofSize * 32
}
}
if bandwidth < minMessageSize {
bandwidth = minMessageSize
}
bandwidthPrice := settings.DownloadBandwidthPrice.Mul64(bandwidth)
return settings.BaseRPCPrice.Add(sectorAccessPrice).Add(bandwidthPrice)
}

// RPCAppendCost returns the price and collateral of a Write RPC with a single
// append operation.
func RPCAppendCost(settings HostSettings, storageDuration, numLeaves uint64) (price, collateral types.Currency) {
proofSize := uint64(bits.Len64(numLeaves)) + 1 // hashes + new root
price = settings.BaseRPCPrice.
Add(settings.StoragePrice.Mul64(SectorSize).Mul64(storageDuration)).
Add(settings.UploadBandwidthPrice.Mul64(SectorSize)).
Add(settings.DownloadBandwidthPrice.Mul64(ed25519.SignatureSize)). // signature
Add(settings.DownloadBandwidthPrice.Mul64(proofSize * 32)) // proof
collateral = settings.Collateral.Mul64(SectorSize).Mul64(storageDuration)
// add some leeway to reduce chance of host rejecting
price = price.Mul64(125).Div64(100)
collateral = collateral.Mul64(95).Div64(100)
return
return settings.BaseRPCPrice.
Add(settings.SectorAccessPrice.Mul64(uint64(len(sections)))).
Add(settings.DownloadBandwidthPrice.Mul64(bandwidth))
}

// RPCDeleteCost returns the price of a Write RPC that deletes n sectors.
func RPCDeleteCost(settings HostSettings, actions []RPCWriteAction, numLeaves uint64) types.Currency {
proofSize := DiffProofSize(actions, numLeaves) + 1 // hashes + new root
// RPCSectorRootsCost returns the price of a SectorRoots RPC.
func RPCSectorRootsCost(settings HostSettings, rootOffset, numRoots uint64) types.Currency {
proofSize := RangeProofSize(LeavesPerSector, rootOffset, rootOffset+numRoots)
return settings.BaseRPCPrice.
Add(settings.DownloadBandwidthPrice.Mul64(ed25519.SignatureSize)). // signature
Add(settings.DownloadBandwidthPrice.Mul64(proofSize * 32)) // proof
Add(settings.DownloadBandwidthPrice.Mul64(numRoots * 32)). // roots
Add(settings.DownloadBandwidthPrice.Mul64(proofSize * 32)) // proof
}

// RPCWriteCost returns the price and collateral of a Write RPC.
func RPCWriteCost(settings HostSettings, actions []RPCWriteAction, numLeaves, remainingDuration uint64, proof bool) (price, collateral types.Currency) {
price = settings.BaseRPCPrice

var sectorsAdded, sectorsRemoved uint64
for _, action := range actions {
switch action.Type {
case RPCWriteActionAppend:
sectorsAdded++
case RPCWriteActionTrim:
sectorsRemoved -= action.A
case RPCWriteActionSwap:
default:
panic("unhanbled action type")
}
}

if sectorsAdded > sectorsRemoved {
additionalSectors := sectorsAdded - sectorsRemoved

collateral = collateral.
Add(settings.Collateral.Mul64(SectorSize * additionalSectors * remainingDuration)).
Mul64(95).Div64(100) // leeway

price = price.
Add(settings.StoragePrice.Mul64(SectorSize * additionalSectors * remainingDuration)).
Add(settings.UploadBandwidthPrice.Mul64(SectorSize * additionalSectors)).
Mul64(125).Div64(100) // leeway
}

if proof {
proofSize := DiffProofSize(actions, numLeaves)
price = price.Add(settings.DownloadBandwidthPrice.Mul64(proofSize * 32))
}

return
}

0 comments on commit b7b4c57

Please sign in to comment.