Making Blocks And Transactions Simple
Cryptocurrencies appeared a relatively short time ago. This is a new type of money backed up with mathematical complexity and the credibility of participants. The technical part of cryptocurrencies is based on two terms — blockchain and distribution.
Blockchain as a technology is a special type of databases. It is intended to store information with increased reliability and coherence. This means that it is impossible to change data in blockchain without significant efforts. These properties are supported by complex cryptography algorithms.
Structurally, blockchain consists of blocks. Every block is a solid data unit. Blocks are placed in a strict order and each subsequent block is strongly tied to the previous one. If any of the blocks are changed, a user will find out about this. A cryptographic check shows an intrusion point.
Basically, a block sequence does not need to be duplicated but cryptocurrencies require transparency. Full access to blockchain can be supported only through decentralization. This means that there is no central authority in the network. The participants store a full blockchain copy on their computers.
As an example, let’s consider Bitcoin, the blockchain of the most popular cryptocurrency.
Each block as a blockchain unit consists of the following components:
- “Magic number”, a bit sequence that uniquely defines the beginning of a block. For Bitcoin, it is 0xD9B4BEF9.
- A block size shows how much space the block takes.
- A block header contains six fields:
- block version shows what rules are applied during the block composition;
- hash gives reference to the previous block;
- Merkle tree root gives a reference value to check the useful content of the block;
- block creation timestamp;
- calculation complexity level;
- Transaction counter.
- All transactions.
In computer technologies, hashing is an operation that uses input data of any size and always gives out a same-length result. It is important to note, that even if a single bit in the input data is changed, we get an absolutely different result. Since each block contains the hash of a previous block, a user can check whether blockchain was modified.
A Merkle tree root is a special hash received through multiple hashing operations. In case of Bitcoin, the Merkle tree shows multiple hashing of transactions within a block. Calculation is quite simple. All block transactions are calculated and get hashed. The received results are arranged by pairs and get hashed again. If the number of hashes is odd, the last hash is processed twice. The calculations are repeated until a single hash-result is received. This result is called the Merkle tree root.
The complexity level defines conditions that the hash of a block must satisfy so that the block could be added to blockchain as a valid one. According to the Bitcoin network rules, the complexity can be changed.
In most cases, a valid block hash cannot be found instantaneously. At the same time, a hashing operation gives the same output for the unchanged input. In order to find a correct hash, information in the block must be changed. That is what a nonce is used for. After each failed hashing operation the number is increased by one. If a correct hash is not found yet, the block creation timestamp is updated and the nonce resets.
Now let’s talk about transactions, the main component of blocks. Transactions are created by blockchain users who send assets to each other. Each transaction shows the address of a sender and the address of a recipient who gets the right to own the transferred assets. Roughly speaking, a single transaction can transfer the right to manage several assets to several different addresses. This is possible due to the transaction structure.
Each transaction has the following fields:
- Version number defines the rules applied while a transaction is composed.
- Special flag “0001” shows that the SegWit technology is applied (optional).
- List of inputs.
- List of outputs.
- Witnesses. This is where SegWit data is placed if a correspondent flag is applied.
- Lock time defines the time when the transaction can be executed.
Key components of a transaction are inputs and outputs. It is important to understand that every input of a new transaction refers to the output of the previous transaction. The output defines the address that is allowed to spend the funds, a closing code and an assets value. The input shows an output address and an opening code. If addresses are chosen correctly and codes fit each other, the transaction output is considered to be valid.
Each transaction in blockchain uses inputs as the sources of funds. Basically, a single input is a reference to the output of the previous transaction added to the block that had already been accepted by blockchain. In order to make this reference valid, other transactions must not refer to the same output. Such an output is called UTXO (Unspent Transaction Output). If at least one of the input’s reference is wrong, the whole transaction will be rejected.
The SegWit technology application allows switching data components inside of the transaction without losing validity. It does not change the transaction purpose but displace the positions of input and output keys a little.
A lock time is used to delay the transaction placement into the block. It may reach up to several days.
The composition of a transaction has some peculiarities as well:
- An output transaction sum must be less or equal to an input transaction sum.
- Difference between input and output sums goes to a miner’s account as a fee.
- The fee is calculated automatically. If a user wants to keep some change apart from funds transmission and fee payments, this must be specified as an additional output.
Basically, funds management with the help of transactions is automated. Users do not have to carry out any additional calculations, as crypto wallets can propose the fee amount, calculate the change and manage available funds.
Sergey Borsuk, Blockspoint