Skip to content

Commit

Permalink
Merge pull request #219 from SiaFoundation/nate/rhp4-rpc-changes
Browse files Browse the repository at this point in the history
RHP4 spec changes
  • Loading branch information
n8maninger authored Oct 23, 2024
2 parents efc26ef + 2652ab2 commit 6769c7c
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 201 deletions.
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:
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.FreeSectorPrice).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.FreeSectorPrice).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 *RPCFreeSectorsRequest) 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 *RPCFreeSectorsRequest) 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 *RPCFreeSectorsRequest) maxLen() int {
return reasonableObjectSize
}

func (r *RPCModifySectorsResponse) 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 *RPCModifySectorsResponse) 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 *RPCModifySectorsResponse) maxLen() int {
func (r *RPCFreeSectorsResponse) maxLen() int {
return reasonableObjectSize
}

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

func (r *RPCModifySectorsThirdResponse) encodeTo(e *types.Encoder) {
func (r *RPCFreeSectorsThirdResponse) encodeTo(e *types.Encoder) {
r.HostSignature.EncodeTo(e)
}
func (r *RPCModifySectorsThirdResponse) decodeFrom(d *types.Decoder) {
func (r *RPCFreeSectorsThirdResponse) decodeFrom(d *types.Decoder) {
r.HostSignature.DecodeFrom(d)
}
func (r *RPCModifySectorsThirdResponse) maxLen() int {
func (r *RPCFreeSectorsThirdResponse) 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

0 comments on commit 6769c7c

Please sign in to comment.