From 66c58005d684b6d316197dfa0d236b7ca069c5f6 Mon Sep 17 00:00:00 2001 From: PJ Date: Tue, 3 Oct 2023 10:00:37 +0200 Subject: [PATCH 1/3] types,rhpv2: add RPCCost --- rhp/v2/merkle.go | 23 +++++++ rhp/v2/rhp.go | 160 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 150 insertions(+), 33 deletions(-) diff --git a/rhp/v2/merkle.go b/rhp/v2/merkle.go index ba026283..bfdbc46c 100644 --- a/rhp/v2/merkle.go +++ b/rhp/v2/merkle.go @@ -506,6 +506,29 @@ func VerifyDiffProof(actions []RPCWriteAction, numLeaves uint64, treeHashes, lea return verifyMulti(newProofIndices, treeHashes, newLeafHashes, numLeaves, newRoot) } +// DiffProofSize returns the size of a Merkle diff proof for the specified +// actions within a tree containing numLeaves leaves. +func DiffProofSize(actions []RPCWriteAction, numLeaves uint64) (numHashes uint64) { + indices := sectorsChanged(actions, numLeaves) + numHashes += uint64(len(indices)) + + buildRange := func(i, j uint64) { + for i < j { + subtreeSize := nextSubtreeSize(i, j) + numHashes++ + i += subtreeSize + } + } + + var start uint64 + for _, end := range indices { + buildRange(start, end) + start = end + 1 + } + buildRange(start, numLeaves) + return +} + func sectorsChanged(actions []RPCWriteAction, numSectors uint64) []uint64 { newNumSectors := numSectors sectorsChanged := make(map[uint64]struct{}) diff --git a/rhp/v2/rhp.go b/rhp/v2/rhp.go index 5cbd9a4f..4e757e35 100644 --- a/rhp/v2/rhp.go +++ b/rhp/v2/rhp.go @@ -3,8 +3,8 @@ package rhp import ( "encoding/json" + "errors" "fmt" - "math/bits" "net" "strings" "time" @@ -141,6 +141,29 @@ var ( RPCReadStop = types.NewSpecifier("ReadStop") ) +// RPC read/write errors +var ( + // ErrOffsetOutOfBounds is returned when the offset exceeds and length + // exceed the sector size. + ErrOffsetOutOfBounds = errors.New("update section is out of bounds") + + // ErrInvalidSectorLength is returned when a sector is not the correct + // length. + ErrInvalidSectorLength = errors.New("length of sector data must be exactly 4MiB") + // ErrSwapOutOfBounds is returned when one of the swap indices exceeds the + // total number of sectors + ErrSwapOutOfBounds = errors.New("swap index is out of bounds") + // ErrTrimOutOfBounds is returned when a trim operation exceeds the total + // number of sectors + ErrTrimOutOfBounds = errors.New("trim size exceeds number of sectors") + // ErrUpdateOutOfBounds is returned when the update index exceeds the total + // number of sectors + ErrUpdateOutOfBounds = errors.New("update index is out of bounds") + // ErrUpdateProofSize is returned when a proof is requested for an update + // operation that is not a multiple of 64 bytes. + ErrUpdateProofSize = errors.New("update section is not a multiple of the segment size") +) + // RPC request/response objects type ( // RPCFormContractRequest contains the request parameters for the @@ -280,45 +303,116 @@ type ( } ) -// RPCSectorRootsCost returns the price of a SectorRoots RPC. -func RPCSectorRootsCost(settings HostSettings, n uint64) types.Currency { - return settings.BaseRPCPrice. - Add(settings.DownloadBandwidthPrice.Mul64(n * 32)). // roots - Add(settings.DownloadBandwidthPrice.Mul64(128 * 32)) // proof +type RPCCost struct { + Base types.Currency + Storage types.Currency + Ingress types.Currency + Egress types.Currency + Collateral types.Currency +} + +func (c RPCCost) Add(o RPCCost) RPCCost { + return RPCCost{ + Base: c.Base.Add(o.Base), + Storage: c.Storage.Add(o.Storage), + Ingress: c.Ingress.Add(o.Ingress), + Egress: c.Egress.Add(o.Egress), + Collateral: c.Collateral.Add(o.Collateral), + } +} + +func (c RPCCost) Total() (cost, collateral types.Currency) { + return c.Base.Add(c.Storage).Add(c.Ingress).Add(c.Egress), c.Collateral } -// RPCReadCost returns the price of a Read RPC. -func RPCReadCost(settings HostSettings, sections []RPCReadRequestSection) types.Currency { - sectorAccessPrice := settings.SectorAccessPrice.Mul64(uint64(len(sections))) +// RPCReadCost returns the cost of a Read RPC. +func RPCReadCost(sections []RPCReadRequestSection, proof bool, settings HostSettings) (RPCCost, error) { + // validate the request sections and calculate the cost var bandwidth uint64 for _, sec := range sections { - bandwidth += sec.Length - bandwidth += 2 * uint64(bits.Len64(LeavesPerSector)) * 32 // proof + switch { + case uint64(sec.Offset)+uint64(sec.Length) > SectorSize: + return RPCCost{}, ErrOffsetOutOfBounds + case sec.Length == 0: + return RPCCost{}, errors.New("length cannot be zero") + case proof && (sec.Offset%LeafSize != 0 || sec.Length%LeafSize != 0): + return RPCCost{}, errors.New("offset and length must be multiples of SegmentSize when requesting a Merkle proof") + } + + bandwidth += uint64(sec.Length) + if proof { + start := sec.Offset / LeafSize + end := (sec.Offset + sec.Length) / LeafSize + proofSize := RangeProofSize(LeavesPerSector, start, end) + bandwidth += proofSize * 32 + } } - if bandwidth < minMessageSize { - bandwidth = minMessageSize - } - bandwidthPrice := settings.DownloadBandwidthPrice.Mul64(bandwidth) - return settings.BaseRPCPrice.Add(sectorAccessPrice).Add(bandwidthPrice) + + return RPCCost{ + Base: settings.BaseRPCPrice.Add(settings.SectorAccessPrice.Mul64(uint64(len(sections)))), + Egress: settings.DownloadBandwidthPrice.Mul64(bandwidth), + }, nil } -// RPCAppendCost returns the price and collateral of a Write RPC with a single -// append operation. -func RPCAppendCost(settings HostSettings, storageDuration uint64) (price, collateral types.Currency) { - price = settings.BaseRPCPrice. - Add(settings.StoragePrice.Mul64(SectorSize).Mul64(storageDuration)). - Add(settings.UploadBandwidthPrice.Mul64(SectorSize)). - Add(settings.DownloadBandwidthPrice.Mul64(128 * 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 +// RPCSectorRootsCost returns the cost of a SectorRoots RPC. +func RPCSectorRootsCost(rootOffset, numRoots uint64, settings HostSettings) RPCCost { + proofSize := RangeProofSize(LeavesPerSector, rootOffset, rootOffset+numRoots) + return RPCCost{ + Base: settings.BaseRPCPrice, + Egress: settings.DownloadBandwidthPrice.Mul64((numRoots + proofSize) * 32), + } } -// RPCDeleteCost returns the price of a Write RPC that deletes n sectors. -func RPCDeleteCost(settings HostSettings, n int) types.Currency { - price := settings.BaseRPCPrice. - Add(settings.DownloadBandwidthPrice.Mul64(128 * 32)) // proof - return price.Mul64(105).Div64(100) +// RPCWriteCost returns the cost of a Write RPC. +func RPCWriteCost(actions []RPCWriteAction, oldSectors, remainingDuration uint64, proof bool, settings HostSettings) (RPCCost, error) { + var uploadBytes uint64 + newSectors := oldSectors + for _, action := range actions { + switch action.Type { + case RPCWriteActionAppend: + if len(action.Data) != SectorSize { + return RPCCost{}, fmt.Errorf("invalid sector size: %v: %w", len(action.Data), ErrInvalidSectorLength) + } + newSectors++ + uploadBytes += SectorSize + case RPCWriteActionTrim: + if action.A > newSectors { + return RPCCost{}, ErrTrimOutOfBounds + } + newSectors -= action.A + case RPCWriteActionSwap: + if action.A >= newSectors || action.B >= newSectors { + return RPCCost{}, ErrSwapOutOfBounds + } + case RPCWriteActionUpdate: + idx, offset := action.A, action.B + if idx >= newSectors { + return RPCCost{}, ErrUpdateOutOfBounds + } else if offset+uint64(len(action.Data)) > SectorSize { + return RPCCost{}, ErrOffsetOutOfBounds + } else if proof && (offset%LeafSize != 0) || len(action.Data)%LeafSize != 0 { + return RPCCost{}, ErrUpdateProofSize + } + default: + return RPCCost{}, fmt.Errorf("unknown write action type '%v'", action.Type) + } + } + + cost := RPCCost{ + Base: settings.BaseRPCPrice, // base cost of the RPC + Ingress: settings.UploadBandwidthPrice.Mul64(uploadBytes), // cost of uploading the new sectors + } + + if newSectors > oldSectors { + additionalSectors := (newSectors - oldSectors) + cost.Storage = settings.StoragePrice.Mul64(SectorSize * additionalSectors * remainingDuration) // cost of storing the new sectors + cost.Collateral = settings.Collateral.Mul64(SectorSize * additionalSectors * remainingDuration) // collateral for the new sectors + } + + if proof { + // estimate cost of Merkle proof + proofSize := DiffProofSize(actions, oldSectors) + cost.Egress = settings.DownloadBandwidthPrice.Mul64(proofSize * 32) + } + return cost, nil } From 8affaf3c42f40181b0b2b33381e955198fbbf232 Mon Sep 17 00:00:00 2001 From: PJ Date: Tue, 3 Oct 2023 17:02:58 +0200 Subject: [PATCH 2/3] rhpv2: make RPC cost functions methods on HostSettings --- rhp/v2/rhp.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rhp/v2/rhp.go b/rhp/v2/rhp.go index 4e757e35..3f594b40 100644 --- a/rhp/v2/rhp.go +++ b/rhp/v2/rhp.go @@ -326,7 +326,7 @@ func (c RPCCost) Total() (cost, collateral types.Currency) { } // RPCReadCost returns the cost of a Read RPC. -func RPCReadCost(sections []RPCReadRequestSection, proof bool, settings HostSettings) (RPCCost, error) { +func (hs *HostSettings) RPCReadCost(sections []RPCReadRequestSection, proof bool) (RPCCost, error) { // validate the request sections and calculate the cost var bandwidth uint64 for _, sec := range sections { @@ -349,22 +349,22 @@ func RPCReadCost(sections []RPCReadRequestSection, proof bool, settings HostSett } return RPCCost{ - Base: settings.BaseRPCPrice.Add(settings.SectorAccessPrice.Mul64(uint64(len(sections)))), - Egress: settings.DownloadBandwidthPrice.Mul64(bandwidth), + Base: hs.BaseRPCPrice.Add(hs.SectorAccessPrice.Mul64(uint64(len(sections)))), + Egress: hs.DownloadBandwidthPrice.Mul64(bandwidth), }, nil } // RPCSectorRootsCost returns the cost of a SectorRoots RPC. -func RPCSectorRootsCost(rootOffset, numRoots uint64, settings HostSettings) RPCCost { +func (hs *HostSettings) RPCSectorRootsCost(rootOffset, numRoots uint64) RPCCost { proofSize := RangeProofSize(LeavesPerSector, rootOffset, rootOffset+numRoots) return RPCCost{ - Base: settings.BaseRPCPrice, - Egress: settings.DownloadBandwidthPrice.Mul64((numRoots + proofSize) * 32), + Base: hs.BaseRPCPrice, + Egress: hs.DownloadBandwidthPrice.Mul64((numRoots + proofSize) * 32), } } // RPCWriteCost returns the cost of a Write RPC. -func RPCWriteCost(actions []RPCWriteAction, oldSectors, remainingDuration uint64, proof bool, settings HostSettings) (RPCCost, error) { +func (hs *HostSettings) RPCWriteCost(actions []RPCWriteAction, oldSectors, remainingDuration uint64, proof bool) (RPCCost, error) { var uploadBytes uint64 newSectors := oldSectors for _, action := range actions { @@ -399,20 +399,20 @@ func RPCWriteCost(actions []RPCWriteAction, oldSectors, remainingDuration uint64 } cost := RPCCost{ - Base: settings.BaseRPCPrice, // base cost of the RPC - Ingress: settings.UploadBandwidthPrice.Mul64(uploadBytes), // cost of uploading the new sectors + Base: hs.BaseRPCPrice, // base cost of the RPC + Ingress: hs.UploadBandwidthPrice.Mul64(uploadBytes), // cost of uploading the new sectors } if newSectors > oldSectors { additionalSectors := (newSectors - oldSectors) - cost.Storage = settings.StoragePrice.Mul64(SectorSize * additionalSectors * remainingDuration) // cost of storing the new sectors - cost.Collateral = settings.Collateral.Mul64(SectorSize * additionalSectors * remainingDuration) // collateral for the new sectors + cost.Storage = hs.StoragePrice.Mul64(SectorSize * additionalSectors * remainingDuration) // cost of storing the new sectors + cost.Collateral = hs.Collateral.Mul64(SectorSize * additionalSectors * remainingDuration) // collateral for the new sectors } if proof { // estimate cost of Merkle proof proofSize := DiffProofSize(actions, oldSectors) - cost.Egress = settings.DownloadBandwidthPrice.Mul64(proofSize * 32) + cost.Egress = hs.DownloadBandwidthPrice.Mul64(proofSize * 32) } return cost, nil } From 422566c1acdadef184ecfb2e749cee21a6f07290 Mon Sep 17 00:00:00 2001 From: PJ Date: Wed, 4 Oct 2023 11:06:16 +0200 Subject: [PATCH 3/3] rhp: update copy --- rhp/v2/rhp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rhp/v2/rhp.go b/rhp/v2/rhp.go index 3f594b40..d8a9cb82 100644 --- a/rhp/v2/rhp.go +++ b/rhp/v2/rhp.go @@ -336,7 +336,7 @@ func (hs *HostSettings) RPCReadCost(sections []RPCReadRequestSection, proof bool case sec.Length == 0: return RPCCost{}, errors.New("length cannot be zero") case proof && (sec.Offset%LeafSize != 0 || sec.Length%LeafSize != 0): - return RPCCost{}, errors.New("offset and length must be multiples of SegmentSize when requesting a Merkle proof") + return RPCCost{}, errors.New("offset and length must be multiples of LeafSize when requesting a Merkle proof") } bandwidth += uint64(sec.Length)