From aef0242f030711d343838d5f071a52c98cba7b45 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 21 Oct 2024 14:16:57 -0400 Subject: [PATCH] types,consensus: Add Capacity field to V2FileContract --- consensus/update_test.go | 1 + consensus/validation.go | 6 ++++++ consensus/validation_test.go | 1 + types/encoding.go | 2 ++ types/types.go | 1 + 5 files changed, 11 insertions(+) diff --git a/consensus/update_test.go b/consensus/update_test.go index 097ad351..62aedac1 100644 --- a/consensus/update_test.go +++ b/consensus/update_test.go @@ -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, diff --git a/consensus/validation.go b/consensus/validation.go index 60ac38bd..32bb1523 100644 --- a/consensus/validation.go +++ b/consensus/validation.go @@ -725,6 +725,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: @@ -747,6 +749,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: diff --git a/consensus/validation_test.go b/consensus/validation_test.go index 223059c7..96185832 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -863,6 +863,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, diff --git a/types/encoding.go b/types/encoding.go index d1a8477c..2586abcf 100644 --- a/types/encoding.go +++ b/types/encoding.go @@ -651,6 +651,7 @@ func (sfe SiafundElement) EncodeTo(e *Encoder) { // EncodeTo implements types.EncoderTo. func (fc V2FileContract) EncodeTo(e *Encoder) { + e.WriteUint64(fc.Capacity) e.WriteUint64(fc.Filesize) fc.FileMerkleRoot.EncodeTo(e) e.WriteUint64(fc.ProofHeight) @@ -1246,6 +1247,7 @@ func (sfe *SiafundElement) DecodeFrom(d *Decoder) { // DecodeFrom implements types.DecoderFrom. func (fc *V2FileContract) DecodeFrom(d *Decoder) { + fc.Capacity = d.ReadUint64() fc.Filesize = d.ReadUint64() fc.FileMerkleRoot.DecodeFrom(d) fc.ProofHeight = d.ReadUint64() diff --git a/types/types.go b/types/types.go index 2b58f9e4..bdee4ed5 100644 --- a/types/types.go +++ b/types/types.go @@ -455,6 +455,7 @@ func (txn *Transaction) TotalFees() Currency { // or "missed" depending on whether a valid StorageProof is submitted for the // contract. type V2FileContract struct { + Capacity uint64 `json:"capacity"` Filesize uint64 `json:"filesize"` FileMerkleRoot Hash256 `json:"fileMerkleRoot"` ProofHeight uint64 `json:"proofHeight"`