Infrastructure as Code Best Practices with Terraform for DevOps
João Victor Alhadas | Dec 17, 2024
Blockchain transactions form the backbone of cryptocurrency networks, determining how value is transferred between users.
Whether you’re dealing with Bitcoin, Ethereum, or Cardano, understanding the mechanisms behind these transactions is essential for anyone exploring blockchain technology.
In this post, we’ll dive into two key models of transaction recording — UTxO and account-based systems — and explore how they work, their differences, and why these models are crucial to the security and functionality of blockchain networks.
There are two main record-keeping models for blockchain payments: UTxO-based and account-based.
The account-based model is one of the primary approaches used in blockchain networks to keep track of users’ balances and transactions.
In this model, each user has a unique account, identified by an address, which stores their current balance. Transactions modify these balances directly, just like a bank updates an account after a deposit or withdrawal.
The account-based model’s strength lies in its simplicity and straightforward approach to balance management. It’s similar to a bank updating an account after a deposit or withdrawal, making it a familiar and efficient system for users.
Ethereum is a notable example of a blockchain that uses the account-based model, allowing for faster processing of transactions and smart contracts by focusing on the state of accounts.
In the UTxO model, each transaction consumes inputs (which are unspent transaction outputs from previous transactions) and generates new outputs.
Each input must be used as a whole and will produce transaction outputs that have not yet been spent, called UTxOs. Each UTxO represents an amount of Bitcoin that can be used as an input for future transactions. Once the original UTxO is used as an input, it becomes “spent” and cannot be used in future transactions.
The UTxO model, with its concept of Unspent Transaction Output, was first introduced by Bitcoin to manage how transactions are recorded on its blockchain.
This model ensures higher security and privacy, significantly reducing the risks of certain types of attacks and providing a secure and protected environment for transactions.
Let’s see how this works with a few examples. The classic example of buying a pizza with Bitcoin is often used to explain how the UTxO (Unspent Transaction Output) model works.
In this case, just like paying for a pizza with physical cash, you use outputs from previous transactions to pay for the pizza, receiving change in the form of a new output if necessary.
So, if Gus has 10 tokens as a single UTxO and wants to buy a pizza from John for 7 tokens, here’s how it would work.
Since the UTxO must be spent in full, the 10-token UTxO will be used as an input, and two UTxOs will be generated as outputs. After the transaction, John will receive a UTxO worth 7 tokens as payment, and Gus will receive one worth 3 tokens as change.
The same logic applies when using multiple UTxOs as inputs. If one UTxO is not enough to cover the transaction value, multiple ones can be used and will produce outputs in the same way.
One last detail to consider is transaction fees. Simply put, they are the difference between the transaction’s inputs and outputs.
If a given transaction has 10 tokens as inputs and 9 tokens as outputs, the remaining 1 token will be deducted as a fee. Users need to be careful about avoiding high fees by not fully utilizing their inputs.
Fees are used to incentivize miners to process transactions and secure the network. They also prevent users from overloading the network with worthless transactions.
Within the transaction, an address is specified to create the output. The right to use the given UTxO in a future transaction depends on this address.
It acts as a lock that can only be unlocked by the signature of the owner’s private key. The blockchain tracks all of a user’s UTxOs and adds them together to determine the user’s balance.
Several key differences emerge when comparing the UTxO model to the account-based model.
The UTxO model, used by Bitcoin, treats every transaction as the spending of previously received outputs, resulting in new outputs spent in future transactions.
This model is similar to dealing with physical cash, where each coin or note represents a discrete unit that must be accounted for.
On the other hand, the account-based model, used in networks like Ethereum, tracks balances in accounts like a traditional ledger.
While the UTxO model offers better traceability and parallelization in transaction verification, the account-based model tends to be simpler and more efficient for systems that require frequent state changes, like those involving smart contracts.
Both models have their advantages, depending on the use case and network architecture.
One of the advantages of the account-based model introduced by Ethereum is the use of smart contracts. A smart contract is a self-executing program stored on a blockchain that runs when predetermined conditions are met.
However, running smart contracts on Ethereum modifies the ledger while only carrying modifications without a reference to the previous state of the blockchain.
The issue is that a contract may not even be applicable by the time it reaches the block producer. This is not a deterministic model, making it unpredictable in practice.
Cardano, a third-generation blockchain platform, introduced the concept of the Extended Unspent Transaction Output model (eUTxO) to solve this issue.
The new model builds on the foundation of the original UTxO, improving it to handle smart contracts while preserving determinism.
One of the key differences between traditional smart contracts and the eUTxO model is the moment of execution. While Ethereum-like contracts are executed when a transaction is sent to them, the eUTxOs are executed when someone tries to spend from the UTxO that has a smart contract attached.
The smart contract script in Cardano is a validator that verifies transactions and either authorizes or rejects a spend. Applications then require an off-chain infrastructure to monitor on-chain activity and trigger the correct actions.
This new model’s main advantage is that the transaction’s success depends only on the transaction itself and its inputs.
Unlike account-based smart contracts, it does not rely on the global state of the network. As a result, the validity of a transaction can be checked off-chain, and if it fails, it isn’t broadcasted to the network.
The eUTxO adds two new, crucial elements to the mix to make this possible: the datum and the redeemer. These components enable more advanced transaction logic, especially when interacting with smart contracts.
The datum represents additional data attached to an output, providing context or information about the transaction.
This data can be anything relevant to the smart contract, such as details about ownership, conditions for spending, or information necessary to execute the contract logic.
The redeemer, on the other hand, is a piece of data provided by the transaction input that unlocks or “redeems” the UTxO, allowing the transaction to proceed. The redeemer can include parameters the smart contract will check to ensure the transaction meets the required conditions.
Let’s look at an example of how the datum and redeemer work in eUTxO transactions.
When a participant buys a lottery ticket, the smart contract attaches a datum to the UTxO representing the ticket.
The datum could include the participant’s address and ticket number. This information is critical for determining the winner once the draw is complete.
When it’s time to draw a winner, a separate transaction is initiated to pick the winning ticket. The smart contract requires a redeemer input to verify that the correct conditions are met (for example, the draw deadline has passed, and a valid entry exists).
The redeemer might include the lottery’s random number generator result, which is used to identify the winning ticket. Once the winning ticket is identified, the UTxO representing the prize money is released to the winner’s address.
In this example, the datum carries the relevant ticket information, while the redeemer provides the condition (the winning number) necessary to unlock the lottery funds.
This interaction between the datum and redeemer ensures the transaction is secure and only executed when the predefined conditions are met.
By using datum and redeemer, the eUTxO model allows for greater flexibility and security in complex transactions, like lotteries, auctions, or conditional payments.
Let’s look at a few eUTxO examples.
Imagine a decentralized crowdfunding campaign. Each contribution would be represented as a separate UTxO, and once the funding goal is met, the smart contract would automatically distribute the funds based on predefined rules.
This allows for traceable and conditional transactions that can only be executed if certain criteria are met.
Take, for example, a smart contract handling a loan. When a user deposits collateral, the smart contract holds the UTxO, representing the loan amount.
If the loan is paid back on time, the collateral is returned. If not, the smart contract automatically transfers ownership of the collateral to the lender. This conditional nature of transactions showcases the flexibility of the eUTxO model in handling more complex scenarios with higher reliability and security.
Based on these examples, it’s clear that the eUTxO model enhances the capabilities of the traditional UTxO model by enabling more dynamic and programmable transaction logic, especially in environments requiring smart contracts.
Let’s look at a breakdown of UTxO versus eUTxO versus account-based models:
UTxO | eUTxO | Account-Based | |
---|---|---|---|
Primary Use | Bitcoin | Cardano | Ethereum |
Transaction Model | Based on unspent transaction outputs | Extended UTxO with data and logic | Based on balances |
Basic Unit | UTxO (transaction output) | UTxO with datum and redeemer | Account with balance |
Smart Contracts | Limited | Native and advanced support | Native support |
Flexibility | Less flexible for contracts | High flexibility with programmable logic | High flexibility |
Complexity | Simple, each transaction spends specific outputs | More complex due to additional information | Simple, focusing on balances |
Privacy | Higher due to UTxO fragmentation | High, because each UTxO is handled individually | Lower, as accounts are directly updated |
Parallelization | High, since independent transactions can be processed in parallel | High, due to UTxO and contract independence | Low, all accounts share the same state |
For regular UTxOs, the address in the output refers to public keys. For eUTxOs, the address can also refer to scripts.
Each script defines arbitrary logic that must be met to spend the output. Upon execution, each script uses its datum, redeemer, and transaction elements to determine if the funds can be unlocked.
We can think of the script as a mathematical equation. The parameters represent the datum and arguments represent the redeemer. This equation is expected to return a “True” or “False” decision on whether the funds can be unlocked.
By adopting eUTxO, Cardano offers more robust and secure smart contract functionality than blockchains using the account/balance model.
The eUTxO model allows Cardano to support complex financial applications and decentralized apps with enhanced security and predictability, as each transaction is processed independently.
This approach also aids in maintaining high levels of scalability, a key component of Cardano’s mission to provide a sustainable and efficient blockchain platform.
Blockchain transactions are not one-size-fits-all. While account-based models like Ethereum’s offer simplicity and speed, UTxO and its extended version (eUTxO) in Cardano bring heightened security and programmability, especially for smart contracts.
Each model has its strengths, tailored to different use cases, from basic transfers of value to complex decentralized applications. As blockchain technology continues to evolve, understanding these models will help you appreciate and understand the innovations shaping the future of decentralized systems.
If you want to learn more about blockchain transactions and how they work, the Cheesecake Labs blog has you covered.
With posts about the basics of blockchain and blockchain payments plus more detailed looks at the world of blockchain transactions, including Soroban fees and our Stellar Lumens (XLM) cost estimator, there’s plenty to explore.
And if your business wants to learn more about using blockchain transactions, get in touch! We’d love to chat about your ideas.
Software engineer, passionate about learning new languages and tools. I am a forward-thinking professional, eager to develop new projects and to innovate.