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

RHP4 spec changes #219

Merged
merged 18 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
1 change: 1 addition & 0 deletions consensus/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ func TestApplyRevertBlockV2(t *testing.T) {
v1FC.Filesize = 65
v1FC.FileMerkleRoot = blake2b.SumPair((State{}).StorageProofLeafHash([]byte{1}), (State{}).StorageProofLeafHash([]byte{2}))
v2FC := types.V2FileContract{
Capacity: v1FC.Filesize,
Filesize: v1FC.Filesize,
FileMerkleRoot: v1FC.FileMerkleRoot,
ProofHeight: 20,
Expand Down
6 changes: 6 additions & 0 deletions consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,8 @@ func validateV2FileContracts(ms *MidState, txn types.V2Transaction) error {

validateContract := func(fc types.V2FileContract, renewal bool) error {
switch {
case fc.Filesize > fc.Capacity:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be that we are missing fc.Filesize % SectorSize == 0 checks? Same for capacity?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SectorSize is not a consensus concept, only an RHP one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukechampine what about SegmentSize?

return fmt.Errorf("has filesize (%v) exceeding capacity (%v)", fc.Filesize, fc.Capacity)
case fc.ProofHeight < ms.base.childHeight():
return fmt.Errorf("has proof height (%v) that has already passed", fc.ProofHeight)
case fc.ExpirationHeight <= fc.ProofHeight:
Expand All @@ -737,6 +739,10 @@ func validateV2FileContracts(ms *MidState, txn types.V2Transaction) error {
curOutputSum := cur.RenterOutput.Value.Add(cur.HostOutput.Value)
revOutputSum := rev.RenterOutput.Value.Add(rev.HostOutput.Value)
switch {
case rev.Capacity < cur.Capacity:
return fmt.Errorf("decreases capacity")
case rev.Filesize > rev.Capacity:
return fmt.Errorf("has filesize (%v) exceeding capacity (%v)", rev.Filesize, rev.Capacity)
case cur.ProofHeight < ms.base.childHeight():
return fmt.Errorf("revises contract after its proof window has opened")
case rev.RevisionNumber <= cur.RevisionNumber:
Expand Down
1 change: 1 addition & 0 deletions consensus/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ func TestValidateV2Block(t *testing.T) {
v1GiftFC.Filesize = 65
v1GiftFC.FileMerkleRoot = blake2b.SumPair((State{}).StorageProofLeafHash([]byte{1}), (State{}).StorageProofLeafHash([]byte{2}))
v2GiftFC := types.V2FileContract{
Capacity: v1GiftFC.Filesize,
Filesize: v1GiftFC.Filesize,
FileMerkleRoot: v1GiftFC.FileMerkleRoot,
ProofHeight: 20,
Expand Down
75 changes: 22 additions & 53 deletions rhp/v4/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rhp

import (
"bytes"
"fmt"

"go.sia.tech/core/types"
)
Expand All @@ -26,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.ModifySectorActionPrice).EncodeTo(e)
types.V2Currency(hp.RemoveSectorPrice).EncodeTo(e)
e.WriteUint64(hp.TipHeight)
e.WriteTime(hp.ValidUntil)
hp.Signature.EncodeTo(e)
Expand All @@ -39,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.ModifySectorActionPrice).DecodeFrom(d)
(*types.V2Currency)(&hp.RemoveSectorPrice).DecodeFrom(d)
hp.TipHeight = d.ReadUint64()
hp.ValidUntil = d.ReadTime()
hp.Signature.DecodeFrom(d)
Expand All @@ -54,7 +53,7 @@ func (hs HostSettings) EncodeTo(e *types.Encoder) {
types.V2Currency(hs.MaxCollateral).EncodeTo(e)
e.WriteUint64(hs.MaxContractDuration)
e.WriteUint64(hs.MaxSectorDuration)
e.WriteUint64(hs.MaxModifyActions)
e.WriteUint64(hs.MaxSectorBatchSize)
e.WriteUint64(hs.RemainingStorage)
e.WriteUint64(hs.TotalStorage)
hs.Prices.EncodeTo(e)
Expand All @@ -69,46 +68,12 @@ func (hs *HostSettings) DecodeFrom(d *types.Decoder) {
(*types.V2Currency)(&hs.MaxCollateral).DecodeFrom(d)
hs.MaxContractDuration = d.ReadUint64()
hs.MaxSectorDuration = d.ReadUint64()
hs.MaxModifyActions = d.ReadUint64()
hs.MaxSectorBatchSize = d.ReadUint64()
hs.RemainingStorage = d.ReadUint64()
hs.TotalStorage = d.ReadUint64()
hs.Prices.DecodeFrom(d)
}

// EncodeTo implements types.EncoderTo.
func (m ModifyAction) EncodeTo(e *types.Encoder) {
e.WriteUint8(m.Type)
switch m.Type {
case ActionSwap:
e.WriteUint64(m.A)
e.WriteUint64(m.B)
case ActionTrim:
e.WriteUint64(m.N)
case ActionUpdate:
m.Root.EncodeTo(e)
e.WriteUint64(m.A)
default:
panic("invalid action type")
}
}

// DecodeFrom implements types.DecoderFrom.
func (m *ModifyAction) DecodeFrom(d *types.Decoder) {
m.Type = d.ReadUint8()
switch m.Type {
case ActionSwap:
m.A = d.ReadUint64()
m.B = d.ReadUint64()
case ActionTrim:
m.N = d.ReadUint64()
case ActionUpdate:
m.Root.DecodeFrom(d)
m.A = d.ReadUint64()
default:
d.SetErr(fmt.Errorf("invalid action type (%v)", m.Type))
}
}

// EncodeTo implements types.EncoderTo.
func (a Account) EncodeTo(e *types.Encoder) { e.Write(a[:]) }

Expand Down Expand Up @@ -389,53 +354,57 @@ func (r *RPCRefreshContractThirdResponse) maxLen() int {
return reasonableObjectSize
}

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

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

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

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

Expand Down
5 changes: 4 additions & 1 deletion rhp/v4/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func BuildAppendProof(sectorRoots, appended []types.Hash256) ([]types.Hash256, t
return subtreeRoots, acc.Root()
}

// VerifyAppendProof verifies a Merkle proof produced by BuildAppendProof.
// VerifyAppendSectorsProof verifies a Merkle proof produced by BuildAppendProof.
func VerifyAppendSectorsProof(numSectors uint64, subtreeRoots []types.Hash256, appended []types.Hash256, oldRoot, newRoot types.Hash256) bool {
acc := blake2b.Accumulator{NumLeaves: numSectors}
for i := 0; i < bits.Len64(numSectors); i++ {
Expand Down Expand Up @@ -106,6 +106,8 @@ func VerifySectorRootsProof(proof, sectorRoots []types.Hash256, numSectors, star
return rhp2.VerifySectorRangeProof(proof, sectorRoots, start, end, numSectors, root)
}

/*
TODO: implement for RPC remove
func convertActions(actions []ModifyAction) []rhp2.RPCWriteAction {
rhp2Actions := make([]rhp2.RPCWriteAction, len(actions))
for i, a := range actions {
Expand Down Expand Up @@ -142,3 +144,4 @@ func BuildModifySectorsProof(actions []ModifyAction, sectorRoots []types.Hash256
func VerifyModifySectorsProof(actions []ModifyAction, numSectors uint64, treeHashes, leafHashes []types.Hash256, oldRoot types.Hash256, newRoot types.Hash256) bool {
return rhp2.VerifyDiffProof(convertActions(actions), numSectors, treeHashes, leafHashes, oldRoot, newRoot, nil)
}
*/
Loading
Loading