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?
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.
3But 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. Aug 13, 2021 at 17:40
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? Aug 13, 2021 at 17:48
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.
1Thanks 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? Aug 13, 2021 at 20:03
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. Aug 13, 2021 at 21:10
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.
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.