Exploring the technology of block chain (I.), constructing the Genesis block of the Bitcoin

Source: Internet
Author: User
Tags fread
Exploring the technology of block chain (I.), constructing the Genesis block of the Bitcoin

May one, 2017 0. Understanding block chain technology from a simpler perspective

If we use search engine to search block chain, we see most is go to center, distribute, trust, can't change, can't forge, consensus, work proof, POW, equity proof, POS, public chain, private chain, Alliance chain, Intelligent contract, future, etc. a bunch of similar words.

Now let's get rid of these words from the brain and let's zero in on the block chain.

Traditional center and block chain of miners

In the traditional central application, the center needs to do a lot of complex things, and in the Bitcoin world, the miners do only three things: 1. Verify the transaction 2. Write a transaction 3. Read the transaction, of course, in order to make these three things, miners need to do a lot of other behind-the-scenes work.

in a traditional central application, the center receives your commands and completes tasks for you, while in the Bitcoin world the miners do only three things: 1. Verify the transaction 2. Write a transaction 3. The miners will not accept any of your orders.

in the traditional central application, the center, even without your command, can also for you to complete unexpected tasks, such as your account recharge zero, delete your order, delete your comments, posts and so on, and in the Bitcoin world, the miners do only three things: 1. Verification Transaction 2. Write a transaction 3. Read a transaction Miners cannot create any deals for you, nor can they modify any of your transactions.

In the traditional central application, the center makes the rules, the center wants what kind of rules can have what kind of rules, but in the world of Bitcoin, the rules shape the Buddha, change the Rules Herald Division.

In the traditional centralized application, the center is always trying to get to know you, become very understanding of you, know what you want to do, and in the Bitcoin world, the miners do only three things: 1. Verify the transaction 2. Write a transaction 3. The miners don't care who you are and don't care what you want to do.

In the traditional centralized application, the center often back pot, there have been mistakes and losses, you first thought is to the center of responsibility, and in the Bitcoin world, the miners do only three things: 1. Verify the transaction 2. Write a transaction 3. Read the transaction, all the mistakes you have to be responsible for yourself.

In some kind of traditional central application, for example, you buy a box of toothbrushes on the top of a website, in order to buy the toothbrush, the site center will complete a series of business operations for you, generate a series of business data such as orders, payments, logistics, points and so on, and the site compared to the center, you actually do nothing, Of course, you actually clicked a lot of mouse, dozen words, read a lot of commodity evaluation. 

But in the bitcoin world, you transfer money to a particular address, and you do it yourself, compared to miners, 100% of the money you make, the deal is created, the miners do nothing but validate the transaction, and then write the transaction into the block.

Now we're going to change the top miners to the center, the rest of the same:

Center of Traditional Center and block chain

In the traditional central application, the center needs to do a lot of complex things, and in the Bitcoin world, the center does only three things: 1. Verify the transaction 2. Write a transaction 3. Read the transaction, of course, in order to do the three things, the center needs to do a lot of other behind-the-scenes work.

in a traditional central application, the center receives your commands and completes tasks for you, while in the Bitcoin world, the center does only three things: 1. Verify the transaction 2. Write a transaction 3. Read the transaction, the center will not accept any of your orders.

in the traditional central application, the center, even without your command, can also for you to complete unexpected tasks, such as your account recharge zero, delete your order, delete your comments, posts and so on, and in the Bitcoin World, the center only do three things: 1. Verify the transaction 2. Write a transaction 3. Read a transaction, The center cannot create any transactions for you, nor can it modify any of your transactions.

In the traditional central application, the center makes the rules, the center wants what kind of rules can have what kind of rules, but in the world of Bitcoin, the rules shape the Buddha, change the Rules Herald Division.

In the traditional centralized application, the center is always trying to get to know you, become very understanding of you, know what you want to do, and in the Bitcoin world, the center does only three things: 1. Verify the transaction 2. Write a transaction 3. Read trading, the center doesn't care who you are, and doesn't care what you want to do.

In the traditional centralized application, center often back to the pot, there have been errors and losses, you first thought of is to the center of responsibility, while in the Bitcoin world, the center only do three things: 1. Verify the transaction 2. Write a transaction 3. Read the transaction, all the mistakes you have to be responsible for yourself.

In some kind of traditional central application, for example, you buy a box of toothbrushes on the top of a website, in order to buy the toothbrush, the site center will complete a series of business operations for you, generate a series of business data such as orders, payments, logistics, points and so on, and the site compared to the center, you actually do nothing, Of course, you actually clicked a lot of mouse, dozen words, read a lot of commodity evaluation. 

But in the bitcoin world, you transfer money to an address, and the transfer is actually 100% of your own, compared to the center, and the transaction is created by you, and the center does nothing more than validate the transaction and then write the transaction to the block.

So we can think of block chain technology is: the user to do things, only by the user to do, we can only according to the rules to verify the user do things, and the user himself do things truthfully recorded in a chain, this chain is a block chain. 1. Analysis of Transactions

The Genesis block, the first chunk of the Bitcoin, this block contains only one transaction. We call this transaction the Genesis deal and write it down as Tx0, in order to construct the Genesis block, we first construct the Tx0, in order to construct Tx0 we need to understand the data structure of the transaction in the Bitcoin.

Reference: Https://en.bitcoin.it/wiki/Transaction#general_format_.28inside_a_block.29of_each_output_of_a_transaction-_Txout

General format of the a Bitcoin transaction (inside a block)

positive integer VI = varint
Field Description Size
Version no currently 1 4 bytes
in-counter positive integer VI = Varint 1-9 bytes
List of inputs the ' transaction ' is also called ' Coinbase ' (its content was Ignored in earlier versions) <in-counter>-many inputs
out-counter 1-9 bytes
List of outputs the outputs Of the the transaction spend the mined bitcoins for the "block <out-counter>-many outputs
lock_time if Non-zero and sequence numbers are < 0xffffffff:block height or timestamp when t Ransaction is final 4 bytes


To make sure that you understand the data structure of a Bitcoin transaction, the best way to do this is to write a parser yourself and then use the parser that you write to parse the contents of the bitcoin transaction data. We can use any familiar programming language to write this parser, but in this article I will most use C language, including C to write this parser, because I think the use of C language Research block chain technology will be more direct and convenient.

The file Tx0.bin used in the following experiment can be downloaded from https://github.com/baya/block7days/blob/master/tx0.bin or downloaded by WEBBTC 1.1 parse Version no

/* parse_tx_version.c

/#include <stdio.h>

int main ()
{
  FILE *fp;
  unsigned int tx_version;

  fp = fopen ("Tx0.bin", "RB");
  Fread (&tx_version, sizeof (tx_version), 1, FP);

  printf ("Tx Version:%u\n", tx_version);
}

By analyzing PARSE_TX_VERSION.C's code, we can see that the data structure for parsing Bitcoin transactions in C is very straightforward: define a unsigned int type tx_version variable, tx_version is also 4 bytes, and then from TX 0.bin start position read 1 times, read byte size is 4 bytes, this 4 by sizeof (tx_version) calculated, read the results stored in the tx_version variable, compile the above code, and execute, we get the following output:

Tx Version:1
1.2 parsing In-counter

Reference: Https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer

The structure of the in-counter is defined as follows:

Variable Length integer

Value Storage Length Format
< 0xFD 1 uint8_t
<= 0xFFFF 3 0xFD followed by the length as uint16_t
<= 0xFFFF FFFF 5 0xFE followed by the length as uint32_t
- 9 0xFF followed by the length as uint64_t


The steps for parsing are as follows:

Reads the 1th byte, assuming its value is V1

If v1 < 0xF, then V1 is the In-counter value.

If V1 = 0xFD, then read 2 bytes, the 2 bytes are stored in little-endian form

If V1 = 0xFE, then read 4 bytes, the 4 bytes are stored in little-endian form

If V1 = 0xFF, then read 8 bytes, the 8 bytes are stored in little-endian form

The parsing program is divided into two parts: the main program PARSE_TX_VINC.C and the program used to process the endian Kyk_endian.h, where the Kyk_endian.h code comes from https://github.com/keeshux/ Basic-blockchain-programming/blob/master/endian.h

/* PARSE_TX_VINC.C/* #include <stdio.h> #include <stdint.h> int main () {FILE *fp;

  
  unsigned int tx_version;
  uint8_t tx_vin1;
  uint16_t tx_vin2;
  uint32_t tx_vin4;
  uint64_t Tx_vin8;

  uint64_t Tx_vin;
  fp = fopen ("Tx0.bin", "RB");
  Fread (&tx_version, sizeof (tx_version), 1, FP);
  Fread (&tx_vin1, sizeof (tx_vin1), 1, FP);
  if (Tx_vin1 < 0xFD) {Tx_vin = tx_vin1;
    else if (tx_vin1 = 0xFD) {fread (&tx_vin2, sizeof (TX_VIN2), 1, FP); Tx_vin = Bbp_eint16 (Bbp_little, tx_vin2);
    /* Process Little-endian form of uint16_t/else if (tx_vin1 = 0xFE) {fread (&tx_vin4, sizeof (TX_VIN4), 1, FP); Tx_vin = Bbp_eint32 (Bbp_little, TX_VIN4);
    /* Process Little-endian form of uint32_t/else if (tx_vin1 = 0xFF) {fread (&tx_vin8, sizeof (TX_VIN8), 1, FP); Tx_vin = Bbp_eint64 (Bbp_little, TX_VIN8);
  /* processing Little-endian form of uint64_t/} else {Tx_vin = 0;
  printf ("Tx Version:%u\n", tx_version); printf ("Tx in-counter:%lld\n ", Tx_vin);

 }

Execution procedure:

$ make Parse_tx_vinc
$./parse_tx_vinc.out

TX version:1
TX in-counter:1
1.3 Parse List of inputs

The list of inputs is composed of a set of Txin, and the txin structure is as follows:

General format (inside a blocks) of each input of a transaction-txin

Field Description Size
Previous Transaction Hash Doubled sha256-hashed of a (previous) to-be-used transaction bytes
Previous Txout-index Non negative integer indexing an output of the to-be-used transaction 4 bytes
Txin-script length Non negative integer VI = varint 1-9 bytes
Txin-script/scriptsig Script <in-script Length>-many bytes
Sequence_no normally 0xFFFFFFFF; Irrelevant unless transaction ' s lock_time is > 0 4 bytes

Reference: Https://en.bitcoin.it/wiki/Transaction#general_format_.28inside_a_block.29of_each_input_of_a_transaction-_Txin

For the sake of simplicity, we only parse the first txin, and we explain the parsing process in terms of parsing Previous Transaction hash for example.

Previous Transaction Hash has a total of 32 bytes, for this we define a length of 32, type uint8_t array to store the hash, this array is defined as uint8_t pre_tx_hash[32];

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.