Skip to content

Commit

Permalink
consensus: Split (MidState).storageProof into two methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Dec 18, 2024
1 parent 6db4ba8 commit 3be7f60
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
6 changes: 3 additions & 3 deletions consensus/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,12 @@ func (ms *MidState) ApplyTransaction(txn types.Transaction, ts V1TransactionSupp
ms.reviseFileContractElement(fce, fcr.FileContract)
}
for _, sp := range txn.StorageProofs {
sps, ok := ms.storageProof(ts, sp.ParentID)
fce, ok := ms.fileContractElement(ts, sp.ParentID)
if !ok {
panic("missing V1StorageProofSupplement")
}
ms.resolveFileContractElement(sps.FileContract, true, txid)
for i, sco := range sps.FileContract.FileContract.ValidProofOutputs {
ms.resolveFileContractElement(fce, true, txid)
for i, sco := range fce.FileContract.ValidProofOutputs {
ms.addImmatureSiacoinElement(sp.ParentID.ValidOutputID(i), sco)
}
}
Expand Down
17 changes: 10 additions & 7 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,18 +623,21 @@ func (ms *MidState) siafundElement(ts V1TransactionSupplement, id types.SiafundO
func (ms *MidState) fileContractElement(ts V1TransactionSupplement, id types.FileContractID) (types.FileContractElement, bool) {
if rev, ok := ms.revs[id]; ok {
return *rev, true
}
if i, ok := ms.created[id]; ok {
} else if i, ok := ms.created[id]; ok {
return ms.fces[i], true
} else if rev, ok := ts.revision(id); ok {
return rev, true
}
return ts.revision(id)
sps, ok := ts.storageProof(id)
return sps.FileContract, ok
}

func (ms *MidState) storageProof(ts V1TransactionSupplement, id types.FileContractID) (V1StorageProofSupplement, bool) {
if i, ok := ms.created[id]; ok {
return V1StorageProofSupplement{FileContract: ms.fces[i], WindowID: ms.base.Index.ID}, true
func (ms *MidState) storageProofWindowID(ts V1TransactionSupplement, id types.FileContractID) (types.BlockID, bool) {
if i, ok := ms.created[id]; ok && ms.fces[i].FileContract.WindowStart == ms.base.childHeight() {
return ms.base.Index.ID, true
}
return ts.storageProof(id)
sps, ok := ts.storageProof(id)
return sps.WindowID, ok
}

func (ms *MidState) spent(id types.ElementID) (types.TransactionID, bool) {
Expand Down
9 changes: 5 additions & 4 deletions consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,16 @@ func validateFileContracts(ms *MidState, txn types.Transaction, ts V1Transaction
if txid, ok := ms.spent(sp.ParentID); ok {
return fmt.Errorf("storage proof %v conflicts with previous proof (in %v)", i, txid)
}
sps, ok := ms.storageProof(ts, sp.ParentID)
fce, ok := ms.fileContractElement(ts, sp.ParentID)
if !ok {
return fmt.Errorf("storage proof %v references nonexistent file contract", i)
}
fc := sps.FileContract.FileContract
if ms.base.childHeight() < fc.WindowStart {
fc := fce.FileContract
windowID, ok := ms.storageProofWindowID(ts, sp.ParentID)
if !ok {
return fmt.Errorf("storage proof %v cannot be submitted until after window start (%v)", i, fc.WindowStart)
}
leafIndex := ms.base.StorageProofLeafIndex(fc.Filesize, sps.WindowID, sp.ParentID)
leafIndex := ms.base.StorageProofLeafIndex(fc.Filesize, windowID, sp.ParentID)
leaf := storageProofLeaf(leafIndex, fc.Filesize, sp.Leaf)
if leaf == nil {
continue
Expand Down
13 changes: 13 additions & 0 deletions consensus/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,19 @@ func TestValidateBlock(t *testing.T) {
b.Transactions[1].StorageProofs[0].ParentID = b.Transactions[0].FileContractID(0)
},
},
{
"file contract revision 0 conflicts with previous proof or revision",
func(b *types.Block) {
rev := revision
rev.RevisionNumber++
b.Transactions = append(b.Transactions, types.Transaction{
FileContractRevisions: []types.FileContractRevision{{
ParentID: b.Transactions[1].StorageProofs[0].ParentID,
FileContract: rev,
}},
})
},
},
{
fmt.Sprintf("storage proof 1 resolves contract (%v) already resolved by storage proof 0", b.Transactions[0].FileContractID(0)),
func(b *types.Block) {
Expand Down

0 comments on commit 3be7f60

Please sign in to comment.