<p>A transaction that executes on-chain has costs attached to it, irrespective of the validator succeeding or failing. That's why transactions are test-validated off-chain to avoid this from happening.<br />
And as far as I understand it for a transaction to be considered fir for execution there needs to be some amount of lovelace available to cover costs.<br />
The only overhead I can see - and someone please correct if I am wrong - is in filtering the transaction candidates into a list of transactions fit for execution.</p>

CC BY-SA 4.0
2021-08-13T17:40:13.440Z
But if the transaction is invalid, no UTXO is spent and the transaction does not make it into the blockchain. If it does not make it into the blockchain then how is the fee collected? As I understand it, the fee has to come from an UTXO.
Piotr Gorzelany
CC BY-SA 4.0
2021-08-13T17:48:13.800Z
If your transaction includes TxOs that aren't avaliable, you don't pay a fee. To your point, I guess that's obvious--what would you be paying with?
Mitchell Turner
CC BY-SA 4.0
2021-08-13T17:06:52.420Z
2021-08-13T17:06:52.420Z
vcwebco
<p>Generally speaking, you can know whether a transaction will be valid off-chain before submitting it for execution. This is because script validators are deterministic.</p>
<p>There are some exceptions to this however. If you are trying to spend a UTxO that has already been spent, it is possible that your wallet doesn't know that it's spent before it does the off-chain verification. <em>Even in this case you won't have to pay any fees</em>, despite the chain needing to verify something for you.</p>
<p>This is unintuitive to me because this does seem to be a &quot;possible vector&quot; for a DoS attack. However, I don't think the script validator would need to be executed, you could know that it was invalid just by seeing a spent TxO is part of the transaction. Maybe that's the distinction.</p>
<p>I'll try and find the video that explains this and add it here.</p>

CC BY-SA 4.0
2021-08-13T20:03:57.810Z
Thanks that makes sense. I think that in the case were we want to spend a UTxO that was already spent the validator node will not run the contract script but will reject the transaction right away without any heavy computation. And you are right that usually a transaction is validated off-chain **if the user is not malicious**. What if somebody would construct invalid transactions on purpose and make the validator node run a complex script that would consume much resources and fail in the end?
Piotr Gorzelany
CC BY-SA 4.0
2021-08-13T21:10:48.450Z
I'm beginning to suspect that if a script validator or policy runs, you pay a fee. A non-compliant tx will cause it to fail, but you still pay for the effort. I assume things that are on the tx context, e.g. input TxOs and valid time range, can be checked without running a script validator and thus are "free" to invalidate. I don't know how builtin extrinsics like transfers and staking fit in though. Those cost fees.
Mitchell Turner
CC BY-SA 4.0
2021-08-13T17:44:47.167Z
2021-08-13T17:44:47.167Z
Mitchell Turner
<p>Plutus code is broken down into on-chain and off-chain code.</p>
<p>The off-chain code is validated in the wallet before being submitted to the network. This allows the smart contract to check things like user signature or deadlines as well as sufficient funds.</p>
<p>The on-chain code actually submits the transactions. The contract can still fail as UTxO's could have changed between the off-chain validation and the on-chain validation, however it is less likely.</p>

CC BY-SA 4.0
2021-08-15T13:37:21.957Z
2021-08-15T13:37:21.957Z
TheNewBadger
<p>If your transaction is invalid, it won't be broadcasted to the network. So nothing will happen. This is why Cardano is above ETH. It eliminates bad transactions because its network fees are deterministic.</p>

CC BY-SA 4.0
2021-08-13T23:43:44.547Z
2021-08-13T23:43:44.547Z
jnprogrammer
<p>Let's say I construct an invalid transaction, that wants to spend UTXOs sitting at a script address and send it to the network for validation. The script then validates the transaction and fails so the UTXOs are not spent. Are fees still collected for such a validation? If not is it a possible vector of attack on the network, to construct millions of such transaction that would make nodes run complex validation scripts?</p>

<plutus><smart-contract>
Are fees collected for invalid transactions?
# Are fees collected for invalid transactions?

(Asked by: username_0 on 2021-08-13)

Let's say I construct an invalid transaction, that wants to spend UTXOs sitting at a script address and send it to the network for validation. The script then validates the transaction and fails so the UTXOs are not spent. Are fees still collected for such a validation? If not is it a possible vector of attack on the network, to construct millions of such transaction that would make nodes run complex validation scripts?

**Tags:** plutus, smart-contract

---

## 4 answers

### Answer by username_1 on 2021-08-13. Score: 1

A transaction that executes on-chain has costs attached to it, irrespective of the validator succeeding or failing. That's why transactions are test-validated off-chain to avoid this from happening.  
And as far as I understand it for a transaction to be considered fir for execution there needs to be some amount of lovelace available to cover costs.  
The only overhead I can see - and someone please correct if I am wrong - is in filtering the transaction candidates into a list of transactions fit for execution.

> **Comments:**
> **username_0**: But if the transaction is invalid, no UTXO is spent and the transaction does not make it into the blockchain. If it does not make it into the blockchain then how is the fee collected? As I understand it, the fee has to come from an UTXO.  
> **username_2**: If your transaction includes TxOs that aren't avaliable, you don't pay a fee. To your point, I guess that's obvious--what would you be paying with?  

---

### Answer by username_2 on 2021-08-13. Score: 1

Generally speaking, you can know whether a transaction will be valid off-chain before submitting it for execution. This is because script validators are deterministic.

There are some exceptions to this however. If you are trying to spend a UTxO that has already been spent, it is possible that your wallet doesn't know that it's spent before it does the off-chain verification. *Even in this case you won't have to pay any fees*, despite the chain needing to verify something for you.

This is unintuitive to me because this does seem to be a "possible vector" for a DoS attack. However, I don't think the script validator would need to be executed, you could know that it was invalid just by seeing a spent TxO is part of the transaction. Maybe that's the distinction.

I'll try and find the video that explains this and add it here.

> **Comments:**
> **username_0**: Thanks that makes sense. I think that in the case were we want to spend a UTxO that was already spent the validator node will not run the contract script but will reject the transaction right away without any heavy computation. And you are right that usually a transaction is validated off-chain **if the user is not malicious**. What if somebody would construct invalid transactions on purpose and make the validator node run a complex script that would consume much resources and fail in the end?  
> **username_2**: I'm beginning to suspect that if a script validator or policy runs, you pay a fee. A non-compliant tx will cause it to fail, but you still pay for the effort. I assume things that are on the tx context, e.g. input TxOs and valid time range, can be checked without running a script validator and thus are "free" to invalidate. I don't know how builtin extrinsics like transfers and staking fit in though. Those cost fees.  

---

### Answer by username_3 on 2021-08-15. Score: 0

Plutus code is broken down into on-chain and off-chain code.

The off-chain code is validated in the wallet before being submitted to the network. This allows the smart contract to check things like user signature or deadlines as well as sufficient funds.

The on-chain code actually submits the transactions. The contract can still fail as UTxO's could have changed between the off-chain validation and the on-chain validation, however it is less likely.


---

### Answer by username_4 on 2021-08-13. Score: 0

If your transaction is invalid, it won't be broadcasted to the network. So nothing will happen. This is why Cardano is above ETH. It eliminates bad transactions because its network fees are deterministic.


---

