Skip to content

Commit

Permalink
consensus: Move v2 transaction restriction checks into ValidateTransa…
Browse files Browse the repository at this point in the history
…ction

This isn't just a cosmetic refactor: it means that the txpool will reject
v2 txns before allow height.
  • Loading branch information
lukechampine committed Sep 15, 2023
1 parent 313f01f commit 57e896a
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions consensus/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ func ValidateOrphan(s State, b types.Block) error {
} else if err := validateHeader(s, b.ParentID, b.Timestamp, b.Nonce, b.ID()); err != nil {
return err
}

if b.V2 != nil {
if b.V2.Height != s.Index.Height+1 {
return errors.New("block height does not increment parent height")
} else if b.V2.Commitment != s.Commitment(s.TransactionsCommitment(b.Transactions, b.V2Transactions()), b.MinerPayouts[0].Address) {
return errors.New("commitment hash does not match header")
return errors.New("commitment hash mismatch")
}
}
return nil
Expand Down Expand Up @@ -496,7 +495,9 @@ func validateSignatures(ms *MidState, txn types.Transaction) error {

// ValidateTransaction validates txn within the context of ms and store.
func ValidateTransaction(ms *MidState, txn types.Transaction, ts V1TransactionSupplement) error {
if err := validateCurrencyOverflow(ms, txn); err != nil {
if ms.base.childHeight() >= ms.base.Network.HardforkV2.RequireHeight {
return errors.New("v1 transactions are not allowed after v2 hardfork is complete")
} else if err := validateCurrencyOverflow(ms, txn); err != nil {
return err
} else if err := validateMinimumValues(ms, txn); err != nil {
return err
Expand Down Expand Up @@ -871,7 +872,9 @@ func validateFoundationUpdate(ms *MidState, txn types.V2Transaction) error {

// ValidateV2Transaction validates txn within the context of ms.
func ValidateV2Transaction(ms *MidState, txn types.V2Transaction) error {
if err := validateV2CurrencyValues(ms, txn); err != nil {
if ms.base.childHeight() < ms.base.Network.HardforkV2.AllowHeight {
return errors.New("v2 transactions are not allowed until v2 hardfork begins")
} else if err := validateV2CurrencyValues(ms, txn); err != nil {
return err
} else if err := validateV2Siacoins(ms, txn); err != nil {
return err
Expand All @@ -897,27 +900,17 @@ func ValidateBlock(s State, b types.Block, bs V1BlockSupplement) error {
return err
}
ms := NewMidState(s)
if len(b.Transactions) > 0 {
if s.childHeight() >= ms.base.Network.HardforkV2.RequireHeight {
return errors.New("v1 transactions are not allowed after v2 hardfork is complete")
}
for i, txn := range b.Transactions {
if err := ValidateTransaction(ms, txn, bs.Transactions[i]); err != nil {
return fmt.Errorf("transaction %v is invalid: %w", i, err)
}
ms.ApplyTransaction(txn, bs.Transactions[i])
for i, txn := range b.Transactions {
if err := ValidateTransaction(ms, txn, bs.Transactions[i]); err != nil {
return fmt.Errorf("transaction %v is invalid: %w", i, err)
}
ms.ApplyTransaction(txn, bs.Transactions[i])
}
if b.V2 != nil {
if s.childHeight() < ms.base.Network.HardforkV2.AllowHeight {
return errors.New("v2 transactions are not allowed until v2 hardfork begins")
}
for i, txn := range b.V2.Transactions {
if err := ValidateV2Transaction(ms, txn); err != nil {
return fmt.Errorf("v2 transaction %v is invalid: %w", i, err)
}
ms.ApplyV2Transaction(txn)
for i, txn := range b.V2Transactions() {
if err := ValidateV2Transaction(ms, txn); err != nil {
return fmt.Errorf("v2 transaction %v is invalid: %w", i, err)
}
ms.ApplyV2Transaction(txn)
}
return nil
}

0 comments on commit 57e896a

Please sign in to comment.