<p>One of the features of Plutus contracts is that fees of all valid Plutus contracts must be able to be determined at compile time. That means that anything for which this cannot be determined is not a valid Plutus contract. Therefore, contracts containing infinite loops are not valid.</p>

CC BY-SA 4.0
2022-12-30T00:07:39.143Z
2022-12-30T00:07:39.143Z
Erik de Castro Lopo
<p>The execution costs of a contract need to be covered by the included transaction fees. Additionally, a collateral needs to be included. The value of the collateral determines the script execution budget. As soon as execution costs exceed the collateral:</p>
<ul>
<li>script execution is aborted,</li>
<li>the transaction is not accepted (i.e. does not make it to the blockchain),</li>
<li>the collateral is collected by the validating node.</li>
</ul>
<p>Because it is impossible to determine <em>in advance</em> if a program/script will succeed or not (halting problem), the script needs to be executed to find out. Because script execution is deterministic on Cardano, the constructor of the transaction can, however, make sure that transaction fees and collateral have the correct values. Execution failure and collateral collection are therefore only to protect against ill intentioned actors, and do not impact well behaving actors.</p>
<p>In this blogpost you can find more about the collateral mechanism on Cardano: <a href="https://iohk.io/en/blog/posts/2021/09/07/no-surprises-transaction-validation-part-2/" rel="nofollow noreferrer">link</a></p>

CC BY-SA 4.0
2022-12-30T18:50:55.587Z
2022-12-30T18:50:55.587Z
Jey
<p>What is the equivalent of gas in preventing DoS attacks? What happens if an attacker writes a recursion that never stops in Plutus? Someone asked <a href="https://www.reddit.com/r/CardanoDevelopers/comments/po7s6g/what_is_cardanos_equivalent_to_gas_metering/" rel="nofollow noreferrer">the question</a> before.</p>

<smart-contract>
What prevents a smart contract from going into an infinite loop?
# What prevents a smart contract from going into an infinite loop?

(Asked by: username_0 on 2022-12-27)

What is the equivalent of gas in preventing DoS attacks? What happens if an attacker writes a recursion that never stops in Plutus? Someone asked [the question](https://www.reddit.com/r/CardanoDevelopers/comments/po7s6g/what_is_cardanos_equivalent_to_gas_metering/) before.

**Tags:** smart-contract

---

## 2 answers

### Answer by username_1 on 2022-12-30. Score: 1

One of the features of Plutus contracts is that fees of all valid Plutus contracts must be able to be determined at compile time. That means that anything for which this cannot be determined is not a valid Plutus contract. Therefore, contracts containing infinite loops are not valid.


---

### Answer by username_2 on 2022-12-30. Score: 1

The execution costs of a contract need to be covered by the included transaction fees. Additionally, a collateral needs to be included. The value of the collateral determines the script execution budget. As soon as execution costs exceed the collateral:

* script execution is aborted,
* the transaction is not accepted (i.e. does not make it to the blockchain),
* the collateral is collected by the validating node.

Because it is impossible to determine *in advance* if a program/script will succeed or not (halting problem), the script needs to be executed to find out. Because script execution is deterministic on Cardano, the constructor of the transaction can, however, make sure that transaction fees and collateral have the correct values. Execution failure and collateral collection are therefore only to protect against ill intentioned actors, and do not impact well behaving actors.

In this blogpost you can find more about the collateral mechanism on Cardano: [link](https://iohk.io/en/blog/posts/2021/09/07/no-surprises-transaction-validation-part-2/)


---

