Utxo mode interpretation of bitcoin and script constraint shallow reading

Source: Internet
Author: User
Tags hash

In the Bitcoin blockchain, trading is the core of the content. Through the above, we know that bitcoin through the public private key system based on cryptography, the initiator of the transaction can use their private key to sign the transaction, others can use their public key to verify, which mathematically guarantees the security of the user funds. So how is the transaction structured? Simplified Model

In Nakamoto's white paper, Bitcoin is defined as a chain-like digital signature string. The owner of the coin will complete a transfer by signing a digital signature on the previous transaction and the next owner's public key, and attaching the signature to the transaction. The recipient of the transfer is able to verify that the owner of the chain is the sender by verifying the signature.

The operation diagram of the transaction is as follows:

The problem with this design of the trading system is that it is difficult for the payee to verify whether a previous asset owner made a double payment (double flower). The usual solution is to introduce credible third parties, such as banks, to test each transaction to prevent double payments. If a third-party intermediary is to be excluded, the transaction information should be disclosed, requiring all participants in the entire system to have a unique and recognized history of the transaction sequence. The payee needs to ensure that the majority of the nodes during the transaction agree that the transaction was first seen. the ledger system is not based on "account"

The Bitcoin blockchain is essentially an internet-based, centralized ledger system, and the ledger is a transfer transaction between a Bitcoin address, a specific transaction process as shown above, and how the whole system should be built.


There is often a simplistic argument that the Bitcoin public key is likened to the user's bank card number, which is the user's account. Then the easiest way to think of the trading system is based on account, simplified schematic diagram similar to this:

This kind of bookkeeping is the easiest to think of, but the bitcoin blockchain is not used this way. The important thing to say three times, Bitcoin trading system is not like this, not so, not so. The reason is simple, if the use of this method of accounting, every time a new transaction, the system will have to retrace all the trading history, so as to determine whether the latest transaction is not legal, as shown above, User C has the money. When the system is large and the number of transactions is large, it is undoubtedly cumbersome and inefficient to carry out such tests every time. So how is Bitcoin's trading system built? Utxo is the basic unit of Bitcoin trading

UTXO (unspent Transaction Outputs) is an unpaid trade output, which is a core concept of Bitcoin transaction generation and verification. Trading constitutes a chain structure in which all legitimate Bitcoin transactions can be traced back to the output of one or more transactions, the source of which is mining rewards, and at the end of the transaction output that is not currently spent. All the unused output is the utxo of the entire Bitcoin network.

Bitcoin specifies that each new transaction must be entered as an output that is not spent on a trade, and each input will also require the private key of the previous output to be signed, and each bitcoin node will store the Utxo on the current entire blockchain. Nodes across the network verify the legitimacy of new transactions by Utxo and signature algorithms. In this way, the node does not need to trace the history to verify the legitimacy of the new transaction. input and output of the transaction

Bitcoin trading is not the usual meaning of a hand-delivered transaction, but a transfer of money. Bitcoin transactions are designed to incorporate multiple inputs and outputs in order to make the value easy to combine and split, if each transfer requires the construction of a transaction data. That is, a transaction can be transferred to more than one person. The entire lifecycle of a transaction is a blockchain that is ultimately recorded in bitcoin, from generation to network propagation, through proof of effort, and verification of the entire Network node.

The essence of a transaction is a data structure that contains information about the sender, receiver, asset transfer, etc. of the transaction, with its data structure as follows:

Field

Describe

Size

Version

The rules of the Trade reference

4 bytes

Number of inputs

Number of trade input lists

1-9 bytes

Input list

One or more transaction inputs

Indefinite

Number of outputs

Number of trade output lists

1-9 bytes

Output list

One or more trade outputs

Indefinite

Lock time

Lock time

4 bytes

In terms of the overall structure, the main two-unit field of trading is the input and output of the transaction. The input identifies the sender of the transaction, the output identifies the recipient of the transaction and the change to itself, and the transaction fee is the difference between the sum of the inputs and the sum of the outputs. Since all trading inputs must be the output of a previous trade, the most important field of trading is the output of the trade.

The data structure diagram for a transaction is as follows:

There are three types of Bitcoin trading inputs (txin), standard txin (normal input), spend Coinbase txout (Cost mining bonus), Coinbase/generation (mining bonus generated), The following figure depicts the structure of these three txin:

There are two types of Bitcoin trading output (Txout Script), the standard txout (normal trade output), Coinbase txout (Mining bonus output), and the following diagram describes the structure of the two txout respectively:

Script

Scripting is another important technique in trading. Each output of each transaction does not point to an address in a strict sense, but to a script. A script is like a set of rules that constrain how a receiver can spend a locked asset on that output.

Validation of the legitimacy of the transaction is also dependent on the script. Currently it relies on two types of scripts: Lock script and Unlock script. The locking script is based on a variable pattern, implemented in a script language, and is located in the output of the transaction. The unlock script corresponds to the lock script, only the rules of the locking scripts to solve, in order to spend the corresponding assets on this script, the input of the transaction. The scripting language can express countless conditional variants. This is also a feature of Bitcoin as a "programmable currency". The script is explained by a "virtual machine" similar to the one in our programming domain, which is distributed across every node in the Bitcoin network.

Bitcoin scripts are commonly used in two main categories, one is the ordinary type P2pkh (Pay-to-public-key-hash), that is, the hash is paid to the public key, the address, the receiver only need to use the corresponding private key of the address to sign the output, you can spend the output. The other is P2sh (Pay-to-script-hash), which pays the hash of the script. Take a multi-signature example, it requires that the output must also have N the private key in the M to the private key (M<=n) signed at the same time to spend the asset, it is similar to the real life needs more keys to open at the same time the safe, but more flexible.

For example, in bitcoin, the script rules for P2pkh are as follows:

PubKey script:op_dup op_hash160 <PubKeyHash> op_equalverify Op_checksig

The script rules for P2SH are as follows:

PubKey script:op_hash160 

In the 2 script rules above, the PubKey script represents the lock scripts, and the Signature script represents the unlock scripts. The word op_ begins with the relevant script command and is the one that the virtual machine can parse. These command rules are divided according to the PubKey script, which also determines the rules for unlocking scripts.

The scripting mechanism in Bitcoin is relatively simple, just a stack-based engine that interprets the relevant OP instructions, and the scripting rules that can be parsed are not too many to implement complex logic. But it provides a prototype for blockchain programmability, and subsequent programmable blockchain projects are actually developed based on the principles of scripting, such as Ethereum, which is deeply reinforcing the scripting mechanism, which is no longer simply a simple op instruction, but a scripting language that can be executed through a "virtual machine". Ethereum implements a blockchain platform that supports the Turing complete scripting language.

The mechanism of scripting is very important for blockchain, which is similar to an extension interface provided by Blockchain technology, which can be used by anyone to develop blockchain-based applications, such as the functions of smart contracts. The scripting mechanism also makes it possible for blockchain technology to be an underlying protocol. Many of the future blockchain-based disruptive applications are likely to be done through the scripting language of the chunk chain.

The above describes the transaction process and related important concepts in the Bitcoin blockchain. Confined to space, there are some things that can be expanded here. So far, what we've talked about, including the public private key described in the previous section, is directly related to the user's use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.