Blockchain notes (3) data and process of bitcoin transaction

Time:2019-10-20

Can blockchain technology only be used for financial transactions? Maybe we need to know the details of the transaction before we can see whether there are other applications.

1 data model of transaction

1.1 causes

Before that, we talked aboutBitcoin CoreIntroduced a lot, and how to use it as a tool, now we will further study the data model in the blockchain.

Why is block and transaction regarded asdata modelIt’s important to understand?

My answer is: to know how to use data.

When we use blockchain applications to communicate, interact and collaborate with other nodes in the network, we may pay more attention to the protocol. But if you look directly at the protocol, it may not be easy to see through, for example, in the face of some problems: what kind of data is transmitted through the protocol? When developing their own blockchain applications, data is the main role, so how to organize and use it? To be clear, the mountain of data model needs to be overturned.

In addition, when it comes to data, developers can use opcodes(Op-code)In this way, additional data is embedded into the block. At present, the community has two different voices. Take the bitcoin platform as an example, some people think that the bitcoin blockchain thus contains a lot of non-financial data. When the blockchain continues to expand, it will bring a heavy burden on the storage space of those who do not care about the data; others think that these are not The existence of financial data may make blockchain have more application possibilities outside the financial field.

Op-code: some opcodes from bitcoin scripting language used to push data or execute functions in public key scripts and signature scripts.

In fact, it’s interesting to see similar arguments in the community. In the early days, in order to add notes to bitcoin transactions, or other non-financial data unrelated to the transaction itself, people used to burn bitcoin. That is, in different transactions, the verification script in the output was replaced with other data, which would makeUTXOThe data set keeps growing, because doing so will make bitcoin in this transaction unable to be spent any more, and because the whole bitcoin system will put all the transactions that have not been spent for the sake of speed.(UTXO)All of them are stored in memory, which will inevitably make each node of the network contain a lot of redundant information, resulting in high maintenance cost of cross node ledger. Now, with the new improvement scheme has been incorporated into the blockchain and operation code, such asOp-return。 So the agreement is maturing.UTXOData sets are not inflated.

UTXO: that is, unspent transaction outputs, which is a core concept of bitcoin transaction generation and verification. Transactions form a set of chain structure. All legitimate bitcoin transactions can be traced back to the output of one or more transactions. The source of these chains is the mining reward, and the end is the current unused transaction output. What’s more, we can see the account balance in bitcoin wallet, but the concept of account balance is very different from the familiar bank account balance.UTXOFrom the perspective of transaction model, there is no so-called bitcoin one by one, but onlyUTXO。 When we say that Zhang San has 10 bitcoins, we are actually saying that there are several transactions in the current blockchain ledger.UTXOThe payee of the item wrote the address of Zhang San, and theseUTXOThe sum of the items is 10. The account balance seen in the bitcoin wallet is actually that the wallet scans the blockchain and aggregates all the accounts belonging to the user.UTXOCalculated.

Op-returnIn essence, Op “return is a script operation code designed to carry additional transaction information. It’s like our notes in the daily transfer process. The data sent through it will be stored in the block of bitcoin blockchain as we do in bitcoin transactions.

1.2 input and output of transactions

No matter what kind of blockchain application you are facing, transaction is the most important part of the blockchain system. You can think of transactions as atoms that make up the blockchain universe. Just as atoms are the basis of all life, transactions are the units of data blocks. You may have noticed that everything you do on the bitcoin blockchain is to ensure that a transaction can be created, propagated and verified in the network, and finally added to the blockchain. Of course, to get clear about these specific details is to be able to create their own blockchain applications in the future. So it’s still a step-by-step process. First, review how the transaction works and its input and output, which is very important for later discussion of the data model of the transaction.

transactionDescribe a fund from its original owner(input)To upcoming owners(output)Data structure of value transformation

The following transaction details are to view a transaction on the bitcoin test chain using the site we introduced earlier:
Blockchain notes (3) data and process of bitcoin transaction
It is obvious from the figure that there are two inputs of 0.01btc. After participating in a transfer of 0.001btc, they are returned to the original owner of 0.019btc. Based on this, I want to ask: where are these inputs coming from and where are the new outputs generated?

The input of one transaction comes from the unused output of another transaction(UTXO)。

Blockchain notes (3) data and process of bitcoin transaction

When a transaction occurs, there is a need to obtain the account balance, all of which are related to the wallet address through statistics on the whole blockchain.UTXOThe number of bitcoins on the (transaction output is not spent) to complete. So there is no field to store an account balance, or the address of a bitcoin.

1.3 data model

In this section, we look at how information about transactions is stored in the data model. If the network is required to return an original transaction information to us, the information obtained may be as follows:

0100000001f3f6a909f8521adb57d898d2985834e632374e770fd9e2b98656f1bf1fdfd427010000006b48304502203a776322ebf8eb8b58cc6ced4f2574f4c73aa664edce0b0022690f2f6f47c521022100b82353305988cb0ebd443089a173ceec93fe4dbfe98d74419ecc84a6a698e31d012103c5c1bc61f60ce3d6223a63cedbece03b12ef9f0068f2f3c4a7e7f06c523c3664ffffffff0260e31600000000001976a914977ae6e32349b99b72196cb62b5ef37329ed81b488ac063d1000000000001976a914f76bc4190f3d8e2315e5c11c59cfc8be9df747e388ac00000000

This is a piece of hexadecimal data that has not yet been decoded into a JSON object. Although it’s not easy to understand, in fact, it’s well organized. Take the above information as an example. Starting from the starting position, a transaction generally includes the following contents:

  1. Bitcoin version:01000000
  2. Input count of transaction:01
  3. Input info for the transaction:f3f6a909f8521adb57d898d2985834e632374e770fd9e2b98656f1bf1fdfd427010000006b48304502203a776322ebf8eb8b58cc6ced4f2574f4c73aa664edce0b0022690f2f6f47c521022100b82353305988cb0ebd443089a173ceec93fe4dbfe98d74419ecc84a6a698e31d012103c5c1bc61f60ce3d6223a63cedbece03b12ef9f0068f2f3c4a7e7f06c523c3664ffffffff
  4. Output count of transactions:02
  5. Output info for the transaction:60e31600000000001976a914977ae6e32349b99b72196cb62b5ef37329ed81b488ac063d1000000000001976a914f76bc4190f3d8e2315e5c11c59cfc8be9df747e388ac
  6. Lock time:00000000。 It indicates the earliest block written or the earliest time written after the transaction is confirmed at the earliest time:

    • If the field is non-zero and < 500 million, it indicates the block number of the block where the transaction was first written.
    • If it is more than 500 million, it means the time when the transaction was first written into the block.
    • If it is zero, it means that the transaction is immediately written into the block.

In the input information and output information of the transaction, there is also a small section of instruction script to verify whether the transaction is effective: Unlocking script in the input information and locking script in the output information.

Script here refers to a series of instruction characters recorded in each transaction, which is executed to verify whether the transaction is valid and whether bitcoin can be issued. Bitcoin script statements with similar names
Bitcoin script is a simple and lightweight statement based on stack, which is designed to be used in a series of hardware platforms for related operations. We can store numbers or data constants in the stack and use a series of prefixesOP_The opcode of operates on the data. For example, throughOP_ADDAdd the two data in the stack byOP_EQUALTo check that the two elements at the top of the stack are equal.OP_DUPCopy the data at the top of the stack and so on. There are about 80 instructions in total. See Wikipedia of opcodes for details.

Next, we use a simple arithmetic instruction to observe the three concepts mentioned above: Unlock script, lock script and includeOpcodesBitcoin script statement of the instruction, the arithmetic instruction is as follows:

2 6 OP_ADD 8 OP_EQUAL

The execution sequence of bitcoin script statement is from left to right, and is based on stack structure. The execution steps of this statement should be:

  1. Digital 2 in stack;
  2. Digital 6 in stack;
  3. implementOP_ADDAfter the numbers 6 and 2 are out of the stack in turn, the results (8) are added to the stack.
  4. Digital 8 in stack
  5. implementOP_EQUAL: after the numbers 8 and 8 are out of the stack in turn, compare them equally and get the result.(TrueReentry stack

Among them, we can6 OP_ADD 8 OP_EQUALThis part is regarded as a lock script, which needs to meet the requirement that its final result isTrueThe unlocking script (2) of can complete the arithmetic verification. That is to say, if this statement is used to verify the validity of the transaction, all unlocking statements that know that the number 2 can meet the conditions can make it effective.

There are two features of bitcoin scripting language:

  • No process control: the statement is simple, there is no loop and condition control, the advantage is not to worry about blocking errors such as dead loop; the disadvantage is not flexible enough.
  • Stateless: before and after the execution process, do not save any value about the state, the advantage is security, no matter which platform you execute the same statement will get the same answer; the disadvantage is relatively simple.

The characteristics of any implementation method have their advantages and disadvantages. When considering the overall scheme architecture, it should be carefully selected according to the business scenario.

In reality, the bitcoin script statements composed of unlocking script and locking script used to verify the effectiveness of transactions are as follows:

<sig> <pubKey> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

The parts corresponding to unlocking script and locking script are respectively:

  • UnLocking script:<sig> <pubKey>
  • Locking script:OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

Forget to make it clear that verifying the validity of transactions does not use the unlocking script and locking script of the same transaction. Instead, the unlocking script of the current transaction is used to trace back to the input.UTXOThe lock script of the current transaction is used to verify with the unlock script of the future transaction. The specific verification relationship is as follows:
Blockchain notes (3) data and process of bitcoin transaction

In fact, the working principle of transaction validity verification is very simple, that is, asymmetric encryption is used. In the unlocking script, the signature generated by the wallet owner with the private key is included. Because only the wallet owner has the transaction right, can generate the unlocking script to judge the transaction effectively.

The original transaction data above is split as follows:
Blockchain notes (3) data and process of bitcoin transaction

The input information is divided into the following parts:

  • Previous output hash: all inputs can be traced back to an output, i.e. utxo generated by the last transaction.
  • Previous output index: a transaction may contain multiple utxos. This is the index specifying multiple utxos. The first utxo starts from 0.
  • Script Size(bytes): indicates the byte size of the unlock script.
  • scriptSig: Unlocking script mentioned above
  • Sequence: This is currently an abandoned property bit of bitcoin, which is set toffffffff

The output information can also be divided into the following parts:

  • Amount: the number of bitcoin outputs, in satoshis, 10 ^ 8 satoshis = 1 bitcoin.
  • Script Size(bytes): indicates the byte size of the lock script.
  • scriptPubKey: Unlocking script mentioned above.

2 create transaction

Through the GUI tool of bitcoin wallet, although the basic operations in the life cycle of bitcoin blockchain can be completed, there are some limitations. Therefore, in order to understand the details of bitcoin blockchain transactions more deeply, we will use the debugging console to create a transaction. The specific steps are as follows:

  1. View all utxos in bitcoin Wallet
  2. View details of a specific utxo
  3. Create an original transaction
  4. Decode the original transaction
  5. Sign the original transaction
  6. Submit this transaction to the network
  7. Transaction created by txid query

2.1 view utxo

We can view all utxos of this wallet through the help debug window of bitcoin wallet introduced in the previous section. The query command is:listunspent。 It is found that the query result is composed of an array of utxo objects, and one of them is intercepted as follows:

[
  ...
  {
    "Txid": "811ffa0a5c8020a21f115df020b35a00503e4a87523b025390577ee727fbb73f", // transaction ID
    "Vout": 1, // output sequence number
    "Address": "2n1kfmybjzksopo7gpr7l5qwbthuplrekgn", // address
    "Redeemscript": "001462fab42642cbfe84c69e17fcb6c1ae27f63748", // redemption script
    "Scriptpubkey": "a9145883d125a1bb6db07e886bb167d966013f407c4487", // public key script
    "Amount": 0.01898328, // available amount
    "Confirmations": 26738, // number of confirmations
    "Spendable": true, // whether the current wallet has a private key so that it can consume the utxo
    "Solvable": true, // available or not, ignored if the secret key is missing
    "Safe": true // unconfirmed transactions are considered unsafe
  },
  ...
]

2.2 view a utxo detail

In this step, we use the command:gettxoutTo query the details of an unexpended transaction, the command takes three parameters: transaction ID, sequence number of the unexpended output (starting from 0), and an optional Boolean value to control whether to display the unproven output in the memory pool.
The query command to copy the transaction ID in the previous step is as follows:

gettxout 811ffa0a5c8020a21f115df020b35a00503e4a87523b025390577ee727fbb73f 0

The results after operation are as follows:

{
  "bestblock": "00000000000000a88e2e39c56235eb61eaf40fca8273e31d5ce49a4d8577d51f",
  "Confirmations": 26842, // number of verifications
  "Value": 0.00100000, // transaction amount (unit: BTC)
  "Scriptpubkey": {// unlock script
    "ASM": "op ﹣ hash160 c6176d6f78b0205a83bf4bbc516a23dc00a4ca64 op ﹣ equal", // assembly format
    "Hex": "a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca6487", // hex format
    "Reqsigs": 1, // number of signatures required
    "Type": "scripthash", // encryption type
    "Addresses": [// collection address list
      "2NBJdr34cWkdr31rQRRMvcFYAg7kM8wTiNB"
    ]
  },
  "coinbase": false
}

2.3 create an original transaction

Use command:createrawtransaction, creates an unsigned serialized transaction that is not stored in a wallet or transmitted to the network. Two parameters are required: the first is the reference of the previous output; the second is the collection address and collection quantity of P2P KH or p2sh standard. Create the command footprint as follows:

createrawtransaction '[{"txid":"811ffa0a5c8020a21f115df020b35a00503e4a87523b025390577ee727fbb73f","vout": 1}]' '{"2NBn87R8AAwtXUNmmFULvDhmPyeka1X7rRD":0.001, "2NBJdr34cWkdr31rQRRMvcFYAg7kM8wTiNB": 0.001}'

Output after I execute:

02000000013fb7fb27e77e579053023b52874a3e50005ab320f05d111fa220805c0afa1f810100000000ffffffff02a08601000000000017a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787a08601000000000017a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca648700000000

2.4 decoding

The output of the original transaction created in the previous step is a string of hexadecimal strings, which is obviously not readable. To verify the correctness of our creation, we need to decode it into a readable JSON format, using the commanddecoderawtransaction, as follows:

decoderawtransaction 02000000013fb7fb27e77e579053023b52874a3e50005ab320f05d111fa220805c0afa1f810100000000ffffffff02a08601000000000017a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787a08601000000000017a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca648700000000

The output results are as follows:

{
  "txid": "8af75c03ca2e7e84135b2809f73e75d758cfc5b72c1e51ae18b770baef844b54",
  "hash": "8af75c03ca2e7e84135b2809f73e75d758cfc5b72c1e51ae18b770baef844b54",
  "version": 2,
  "size": 115,
  "vsize": 115,
  "weight": 460,
  "locktime": 0,
  "vin": [
    {
      "txid": "811ffa0a5c8020a21f115df020b35a00503e4a87523b025390577ee727fbb73f",
      "vout": 1,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.00100000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 cb4a40c6ccaf652cc9a6459047494359c3ff25d7 OP_EQUAL",
        "hex": "a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2NBn87R8AAwtXUNmmFULvDhmPyeka1X7rRD"
        ]
      }
    },
    {
      "value": 0.00100000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 c6176d6f78b0205a83bf4bbc516a23dc00a4ca64 OP_EQUAL",
        "hex": "a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca6487",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2NBJdr34cWkdr31rQRRMvcFYAg7kM8wTiNB"
        ]
      }
    }
  ]
}

2.5 signature

From the original transaction information with better readability above, we can see the transaction inputscriptSigThe field is empty because we haven’t signed for it yet, which proves that we have the right to use utxo. Next, use the commandsignrawtransactionwithwalletTo sign:

signrawtransactionwithwallet 02000000013fb7fb27e77e579053023b52874a3e50005ab320f05d111fa220805c0afa1f810100000000ffffffff02a08601000000000017a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787a08601000000000017a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca648700000000

The output result of successful signature is as follows:

{
  "hex": "020000000001013fb7fb27e77e579053023b52874a3e50005ab320f05d111fa220805c0afa1f81010000001716001462fab42642cbfe84c69a9e17fcb6c1ae27f63748ffffffff02a08601000000000017a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787a08601000000000017a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca64870247304402207fbd59f6e806dc1aab5f602b796dc2ecfa96f0e018c7fe4ecc7dcf190e0619f10220168dffa1d5bd5876518530c72fd3bff59337050949c9732dabcfaeef7533de44012103959e3af1e6ddb01d6ac54966cda59464ab27fcaf34b0dca6df02f75d3df7668800000000",
  "complete": true
}

Then, when the signed output is JSON decoded, it will be found that there are more contents in the input part:

{
...
"vin": [{
  "txid": "811ffa0a5c8020a21f115df020b35a00503e4a87523b025390577ee727fbb73f",
  "vout": 1,
  "scriptSig": {
    "asm": "001462fab42642cbfe84c69a9e17fcb6c1ae27f63748",
    "hex": "16001462fab42642cbfe84c69a9e17fcb6c1ae27f63748"
  },
  "txinwitness": ["304402207fbd59f6e806dc1aab5f602b796dc2ecfa96f0e018c7fe4ecc7dcf190e0619f10220168dffa1d5bd5876518530c72fd3bff59337050949c9732dabcfaeef7533de4401", "03959e3af1e6ddb01d6ac54966cda59464ab27fcaf34b0dca6df02f75d3df76688"],
  "sequence": 4294967295
}],
...
}

2.6 push the signed transaction to the network

Use the command sendrawtransaction to push the signed transaction to the network.

sendrawtransaction 020000000001013fb7fb27e77e579053023b52874a3e50005ab320f05d111fa220805c0afa1f81010000001716001462fab42642cbfe84c69a9e17fcb6c1ae27f63748ffffffff02a08601000000000017a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787a08601000000000017a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca64870247304402207fbd59f6e806dc1aab5f602b796dc2ecfa96f0e018c7fe4ecc7dcf190e0619f10220168dffa1d5bd5876518530c72fd3bff59337050949c9732dabcfaeef7533de44012103959e3af1e6ddb01d6ac54966cda59464ab27fcaf34b0dca6df02f75d3df7668800000000

The result returned after execution is the hexadecimal value of the transaction ID:

24cd5619a366ad6a3a34a29766fd5f82c39657bc15dcfdcd4d7363a65f401c8b

2.7 view transaction details

At this point, the declaration cycle of the whole transaction is completed. We can view the details of the transaction through gettransaction.

gettransaction 24cd5619a366ad6a3a34a29766fd5f82c39657bc15dcfdcd4d7363a65f401c8b

The detailed results are as follows:

{
  "amount": -0.00200000,
  "fee": -0.01698328,
  "confirmations": 1,
  "blockhash": "000000000000006715d295c34b2896d0c28f67a092869610200684e45fdd3ad9",
  "blockindex": 1,
  "blocktime": 1558762656,
  "txid": "24cd5619a366ad6a3a34a29766fd5f82c39657bc15dcfdcd4d7363a65f401c8b",
  "walletconflicts": [
  ],
  "time": 1558762586,
  "timereceived": 1558762586,
  "bip125-replaceable": "no",
  "details": [
    {
      "address": "2NBn87R8AAwtXUNmmFULvDhmPyeka1X7rRD",
      "category": "send",
      "amount": -0.00100000,
      "vout": 0,
      "fee": -0.01698328,
      "abandoned": false
    },
    {
      "address": "2NBJdr34cWkdr31rQRRMvcFYAg7kM8wTiNB",
      "category": "send",
      "amount": -0.00100000,
      "label": "like you",
      "vout": 1,
      "fee": -0.01698328,
      "abandoned": false
    }
  ],
  "hex": "020000000001013fb7fb27e77e579053023b52874a3e50005ab320f05d111fa220805c0afa1f81010000001716001462fab42642cbfe84c69a9e17fcb6c1ae27f63748ffffffff02a08601000000000017a914cb4a40c6ccaf652cc9a6459047494359c3ff25d787a08601000000000017a914c6176d6f78b0205a83bf4bbc516a23dc00a4ca64870247304402207fbd59f6e806dc1aab5f602b796dc2ecfa96f0e018c7fe4ecc7dcf190e0619f10220168dffa1d5bd5876518530c72fd3bff59337050949c9732dabcfaeef7533de44012103959e3af1e6ddb01d6ac54966cda59464ab27fcaf34b0dca6df02f75d3df7668800000000"
}

Recommended Today

Single and multiple buttons are styled with pictures

I’ve always seen people asking how to style and enlarge the buttons of radio buttons and multi buttons? Let’s share an example I did. 1. First make the button into a picture  2.html page Copy code The code is as follows: <!DOCTYPE HTML> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> <script type=”text/javascript” src=”jquery-1.10.2.min.js”></script> <script type=”text/javascript”> […]