Skip to content

Commit

Permalink
Merge pull request #172 from SiaFoundation/v2-storage-proof
Browse files Browse the repository at this point in the history
types: Fix (*V2Transaction).DeepCopy mutating V2StorageProof
  • Loading branch information
n8maninger committed Jun 21, 2024
2 parents 2c5f229 + f7470bf commit 5054a28
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func validateV2FileContracts(ms *MidState, txn types.V2Transaction) error {
} else if sp.ProofIndex.ChainIndex.Height != fc.ProofHeight {
// see note on this field in types.StorageProof
return fmt.Errorf("file contract storage proof %v has ProofIndex height (%v) that does not match contract ProofHeight (%v)", i, sp.ProofIndex.ChainIndex.Height, fc.ProofHeight)
} else if ms.base.Elements.containsChainIndex(sp.ProofIndex) {
} else if !ms.base.Elements.containsChainIndex(sp.ProofIndex) {
return fmt.Errorf("file contract storage proof %v has invalid history proof", i)
}
leafIndex := ms.base.StorageProofLeafIndex(fc.Filesize, sp.ProofIndex.ChainIndex.ID, types.FileContractID(fcr.Parent.ID))
Expand Down
12 changes: 8 additions & 4 deletions consensus/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func TestValidateBlock(t *testing.T) {
}
}

func updateProofs(au ApplyUpdate, sces []types.SiacoinElement, sfes []types.SiafundElement, fces []types.V2FileContractElement) {
func updateProofs(au ApplyUpdate, sces []types.SiacoinElement, sfes []types.SiafundElement, fces []types.V2FileContractElement, cies []types.ChainIndexElement) {
for i := range sces {
au.UpdateElementProof(&sces[i].StateElement)
}
Expand All @@ -698,6 +698,9 @@ func updateProofs(au ApplyUpdate, sces []types.SiacoinElement, sfes []types.Siaf
for i := range fces {
au.UpdateElementProof(&fces[i].StateElement)
}
for i := range cies {
au.UpdateElementProof(&cies[i].StateElement)
}
}

func TestValidateV2Block(t *testing.T) {
Expand Down Expand Up @@ -1171,7 +1174,7 @@ func TestValidateV2Block(t *testing.T) {

cs, testAU := ApplyBlock(cs, validBlock, db.supplementTipBlock(validBlock), time.Now())
db.applyBlock(testAU)
updateProofs(testAU, sces, sfes, fces)
updateProofs(testAU, sces, sfes, fces, cies)

var testSces []types.SiacoinElement
testAU.ForEachSiacoinElement(func(sce types.SiacoinElement, spent bool) {
Expand Down Expand Up @@ -1209,8 +1212,8 @@ func TestValidateV2Block(t *testing.T) {
}
cs, au = ApplyBlock(cs, b, db.supplementTipBlock(validBlock), time.Now())
db.applyBlock(au)
updateProofs(au, sces, sfes, fces)
updateProofs(au, testSces, testSfes, testFces)
updateProofs(au, sces, sfes, fces, cies)
updateProofs(au, testSces, testSfes, testFces, nil)
cies = append(cies, au.ChainIndexElement())

blockID = b.ID()
Expand Down Expand Up @@ -1246,6 +1249,7 @@ func TestValidateV2Block(t *testing.T) {
Proof: []types.Hash256{cs.StorageProofLeafHash([]byte{1})},
}
}

signTxn(cs, &b.V2.Transactions[0])
b.V2.Commitment = cs.Commitment(cs.TransactionsCommitment(b.Transactions, b.V2Transactions()), b.MinerPayouts[0].Address)
findBlockNonce(cs, &validBlock)
Expand Down
4 changes: 3 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,11 @@ func (txn *V2Transaction) DeepCopy() V2Transaction {
c.FileContractResolutions = append([]V2FileContractResolution(nil), c.FileContractResolutions...)
for i := range c.FileContractResolutions {
c.FileContractResolutions[i].Parent.MerkleProof = append([]Hash256(nil), c.FileContractResolutions[i].Parent.MerkleProof...)
if sp, ok := c.FileContractResolutions[i].Resolution.(*V2StorageProof); ok {
if res, ok := c.FileContractResolutions[i].Resolution.(*V2StorageProof); ok {
sp := *res
sp.ProofIndex.MerkleProof = append([]Hash256(nil), sp.ProofIndex.MerkleProof...)
sp.Proof = append([]Hash256(nil), sp.Proof...)
c.FileContractResolutions[i].Resolution = &sp
}
}
c.Attestations = append([]Attestation(nil), c.Attestations...)
Expand Down

0 comments on commit 5054a28

Please sign in to comment.