Skip to content

Commit

Permalink
consensus: Add validateSupplement
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Jul 5, 2024
1 parent 49fbb42 commit 77e6556
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions consensus/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ func (acc *ElementAccumulator) containsSpentSiafundElement(sfe types.SiafundElem
return acc.containsLeaf(siafundLeaf(&sfe, true))
}

func (acc *ElementAccumulator) containsUnresolvedFileContractElement(fce types.FileContractElement) bool {
return acc.containsLeaf(fileContractLeaf(&fce, false))
}

func (acc *ElementAccumulator) containsUnresolvedV2FileContractElement(fce types.V2FileContractElement) bool {
return acc.containsLeaf(v2FileContractLeaf(&fce, false))
}
Expand Down
39 changes: 39 additions & 0 deletions consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,43 @@ func ValidateV2Transaction(ms *MidState, txn types.V2Transaction) error {
return nil
}

func validateSupplement(s State, b types.Block, bs V1BlockSupplement) error {
if len(bs.Transactions) != len(b.Transactions) {
return errors.New("incorrect number of transactions")
}
for _, txn := range bs.Transactions {
for _, sce := range txn.SiacoinInputs {
if !s.Elements.containsUnspentSiacoinElement(sce) {
return fmt.Errorf("siacoin element %v is not present in the accumulator", sce.ID)
}
}
for _, sfe := range txn.SiafundInputs {
if !s.Elements.containsUnspentSiafundElement(sfe) {
return fmt.Errorf("siafund element %v is not present in the accumulator", sfe.ID)
}
}
for _, fce := range txn.RevisedFileContracts {
if !s.Elements.containsUnresolvedFileContractElement(fce) {
return fmt.Errorf("revised file contract %v is not present in the accumulator", fce.ID)
}
}
for _, fce := range txn.ValidFileContracts {
if !s.Elements.containsUnresolvedFileContractElement(fce) {
return fmt.Errorf("valid file contract %v is not present in the accumulator", fce.ID)
}
}
if len(txn.StorageProofBlockIDs) != len(txn.ValidFileContracts) {
return errors.New("incorrect number of storage proof block IDs")
}
}
for _, fce := range bs.ExpiringFileContracts {
if !s.Elements.containsUnresolvedFileContractElement(fce) {
return fmt.Errorf("expiring file contract %v is not present in the accumulator", fce.ID)
}
}
return nil
}

// ValidateBlock validates b in the context of s.
//
// This function does not check whether the header's timestamp is too far in the
Expand All @@ -904,6 +941,8 @@ func ValidateV2Transaction(ms *MidState, txn types.V2Transaction) error {
func ValidateBlock(s State, b types.Block, bs V1BlockSupplement) error {
if err := ValidateOrphan(s, b); err != nil {
return err
} else if err := validateSupplement(s, b, bs); err != nil {
return fmt.Errorf("block supplement is invalid: %w", err)
}
if b.V2 != nil {
if b.V2.Commitment != s.Commitment(s.TransactionsCommitment(b.Transactions, b.V2Transactions()), b.MinerPayouts[0].Address) {
Expand Down
12 changes: 12 additions & 0 deletions consensus/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ type consensusDB struct {
}

func (db *consensusDB) applyBlock(au ApplyUpdate) {
for id, sce := range db.sces {
au.UpdateElementProof(&sce.StateElement)
db.sces[id] = sce
}
for id, sfe := range db.sfes {
au.UpdateElementProof(&sfe.StateElement)
db.sfes[id] = sfe
}
for id, fce := range db.fces {
au.UpdateElementProof(&fce.StateElement)
db.fces[id] = fce
}
au.ForEachSiacoinElement(func(sce types.SiacoinElement, created, spent bool) {
if spent {
delete(db.sces, types.SiacoinOutputID(sce.ID))
Expand Down

0 comments on commit 77e6556

Please sign in to comment.