Bit Coin Development reference

Source: Internet
Author: User

BYTE block chain block header Merkle Tree target difficulty nbits transaction opcode address conversion original transaction format txin a transaction output-coinbase outpoint specific output of specific parts txout Transaction Output Example Coinbase input a block in the first Trading compactsize unsigned integer wallet deterministic wallet format type 1 single chain wallet type 2 tiered deterministic HD Wallet Peer-to-peer Network constants and default protocol version information header data chunks get chunks get data get block head head INV memory pool Merkle block not found TX Control Information Address Alert cost filter address filter filterclear filterload get address Ping Pong reject Send header verack version bit currency core API hash byte order Remote procedure call RPCs Quick View RPCs Ntransaction addmultisigaddress HTTP REST Quick Requests Get block

bit coins block Chain Block Header

byte name Data Type Description
4 Veesion int32_t The version number indicates the rules followed
32 Preblockheaderhash CHAR[32] SHA256 (SHA256 ()) a hash of the front head
32 Merkleroothash CHAR[32] Hash of all transactions in this block
4 Time uint32_t Unix time stamp
4 Nbits uint32_t Difficulty coefficient
4 Nonce uint32_t Random number
Merkle Tree

Merkle Root is generated based on the txid of all transactions in this block: Coinbase transactions are ranked first. Any input within that block can also appear in the output (assuming it is cost effective). However, the output corresponding to the TXID must be placed in the input corresponding to the TXID
A point before that. Ensured that it was entered before the output. In other words, if you want to spend money, you have to turn it over before you spend it, and then someone will give you a turn.

If a block has only one coinbase transaction, the Txid of the Coinbase is used as the root hash of the Merkle.

If a block has a coinbase transaction and one other transaction, then the order is 64 bytes and then the SHA256 (SHA256 ()) is formed Merkle Genhachy.

If there are more than 3 transactions in a block, the middle Merkle tree is formed. The Txid are arranged in sequence and paired, starting with the Txid of the Coinbase. Each pair is spliced together and then SHA256 (SHA256 ()) to form a second row of hashes. If there is a non-even (odd) number of Txid, the last txid copies itself and then SHA256 (SHA256 ()). If there are more than 2 hashed in the second row, repeat the previous action. Until only 2 hashed are left, then stitching and SHA256 (SHA256 ()) Form the roots of the Merkle tree.

Target Difficulty Nbits

The target threshold is an unsigned 256-bit integer that must be less than or equal to this value to become a valid part of the block chain.
Serialization Block
According to the current consensus rule, blocks larger than 1MB are not valid. The size described below is all serialized.

byte name Data Type Description
80 Block header Block_header The structure described above
Varies Txn_count Compactsizeunit Number of transactions, including Coinbase
Varies Txns Raw Trasaction Each transaction in this block, one after the other.

The first transaction in the block must be a Coinbase transaction to collect the transaction costs in this block.

All blocks with a height of no more than 6,930,000 are entitled to a reward for creating blocks. (Block awards start with 50 Bitcoin, half every 210,000 pieces, about once every four years, as of November 2017, for 12.5 bitcoin.) ) Trading

The following subsections briefly describe the details of the core transaction. Operation Code

Public script opcode in standard transactions: Various data manipulation codes, from 0x00 to 0x4e (1-78). Op_true/op_1 (0x51) and op_2 to Op_16 (0x52-0x60) indicate that the 1-16 is pressed into the stack. Op_checksig consumes a signature and a public key that is pressed to true if the transaction data specified by the SIGHASH flag is converted to a signature that uses the same ECDSA private key that generated the public key, otherwise it presses false onto the stack. Op_dup the top element of the stack and presses it into the stack. op_hash160 consumes the top element of the stack, calculates the RIPEMD160 (SHA256 ()), and presses the calculated value into the stack. Op_equal consumption stack top 2 elements, compare, the same pressure into the true, otherwise pressed into the false. Op_verify the top element of the consumer stack and terminates the script if it is 0. Op_equalverify run op_equal and op_verify sequentially.
See the wiki and its implementation script for more opcode. Address Translation

Bit-currency addresses are typically hashed using P2PKH and P2SH.
First, get your hash value. For P2pkh, use the RIPEMD-160 (SHA256 ()) hash to the public key corresponding to the ECDSA private key (random number) of your 256-bit. For P2sh, use the RIPEMD-160 (SHA256 ()) to obtain a hash of the serialized original transaction through a transformation script (as described in the following). Get hash Result:
1. Add a byte version before the hash value the P2PKH address on the primary network (mainnet) is the P2pkh address on the 0x00 bit currency test network (testnet) 0x6f the P2SH address on the main network 0x05 the P2SH address on the network

2. Create a version and hash-worthy copy, and then perform a 2-time hash SHA256 (SHA256 (version. hash))
3. Extracts the first four bytes from a double hash copy. These are used as checksums to ensure that the hash transfer is correct.
4. Officers transferred Guevara and append to the version and hash, and encode it as a base58 string: BASE58 (version. Hash. Checksum)

The base58 encoding of bitcoin is called Base58check and is not the same as other general implementations. Wiki

code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZABCDEFGHIJKMNOPQRSTUVWXYZ"
x = Convert_bytes_to_big_integer ( Hash_result)

output_string = "" While

(x > 0) 
   {
       (x, remainder) = Divide (x,
       output_) String.append (Code_string[remainder])
   }

repeat (Number_of_leading_zero_bytes_in_hash)
   {
   Output_ String.append (Code_string[0]);

Output_string.reverse ();

To convert the address back to the hash value, first base58 the decoder, extract the checksum, repeat these steps to create the checksum, compare it to the extracted checksum, and then delete the version byte. Original Transaction Format

Bitcoin transactions are advertised between point-to-point points in serialized byte formats, SHA256 (SHA256 ()) transactions to create TXID, and ultimately the Mekle root of the block that contains the transaction.

The original transaction has the following format:

bytes name data type description
4 version uint32_t Transaction version number, now the version number is 1; transactions created with updated consensus rule rules may have A higher version number.
varies tx_in count compactsize UINT Number of transactions entered
varies tx_in txin transaction input. See the txin below for a description. The number of
varies tx_out count compactsize UINT transaction output.
varies tx_out txout transaction output. See the txout below for a description.
4 local_time unit32_t Unix time or block numbers see Locktime parsing rules.

A transaction may have multiple inputs and outputs, so the txin and txout structures may recur in one structure. A compactsize unsigned integer is a form of variable long integers; They are described in the Compactsize section. txin: One transaction output (non-coinbase)

Each Coinbase input costs a outpoint from the previous transaction. (Coinbase input is described separately after the following example section.) )

byte name Data Type Description
36 Previous_output Outpoint Please refer to the outpoint description below for the outpoint that was previously consumed.
Varies Script bytes Compactsize UINT The number of bytes in the signature script. The maximum value is 10,000 bytes.
Varies Signature Script Char[] A scripting language that satisfies the conditions in the Outpoint PubKey script. Should only contain data push; See Signature script modification warning.
4 Sequence uint32_t Serial number. The default value for the Bitcoin core and almost all other programs is 0xFFFFFFFF.
outpoint: Specific parts of a specific output

Since a single transaction can contain multiple outputs, the outpoint structure consists of a txid and an output index number pointing to a specific output.

byte name Data Type Description
32 Hash CHAR[32] The cost of the TXID output of the transaction, TXID is the hash provided in internal byte order.
4 Index uint32_t The specific output index number of the specific cost of the transaction. The first output is 0x00000000
txout: Transaction Output

Each output costs a certain amount of Cong, and any person who can provide satisfying PubKey script can use it.

byte name Data Type Description
8 Value int64_t The amount of money that is spent. may be 0; the sum of all outputs must not exceed the sum of the Cong supplied by the input section previously. (Exception: Coinbase Transactions cost block rewards and transaction fees charged.) )
1 + Pk_script bytes Compactsize UINT The number of bytes in the PubKey script. The maximum value is 10,000 bytes.
Varies Pk_script Char[] Define the conditions that must be met to spend this output.
Example

An example of a transaction.

01000000. ............. ....... ....... ... Version 01 ... ....... ...... ...... ..... ...... ....... ... Number of Inputs | | 7b1eabe0209b1fe794124575ef807057 | C77ada2138ae4fa8d6c4de0398a14f3f ..... Outpoint Txid | 00000000. ............... ......... ... Outpoint Index number | | 49. .......... ....... ...... ....... ..... Bytes in sig. script:73 | | 48. ......... ........ ...... ........ ... Push bytes as Data | | | 30450221008949f0cb400094ad2b5eb3 | | | 99d59d01c14d73d8fe6e96df1a7150de | | | b388ab8935022079656090d7f6bac4c9 | | | a94e0aad311a4268e082a725f8aeae05 | | | 73fb12ff866a5f01 .............. SECP256K1 Signature | | Ffffffff ........ ........ ....... ...... ... Sequence Number:uint32_max 01 ............ ...... ...... ...... ...... ... Number of outputs | f0ca052a01000000 ................. Satoshis (49.99990000 BTC) | | 19. .......... ....... ...... ....... ..... Bytes in PubKey script:25 | | 76. ......... ........ ...... ........ ... Op_dup | | A9 ............................... op_hash160 | | 14. ......... ........ ...... ........ ... Push bytes as Data | | | cbc20a7664f2f69e5355aa427045bc15 | | | e7c6c772 ................ ...... PubKey Hash | | 88. ......... ........ ...... ........ ... op_equalverify | | AC ... ....... ...... ...... ...... ..... ..... ... Op_checksig 00000000 ... locktime:0 (a block height)--------------------
Coinbase Input: The first transaction in a block

The first transaction in the block, called the Coinbase transaction, has and only one input, called Coinbase. The format of the Coinbase input is as follows:

byte name Data Type Description
32 Hash (NULL) CHAR[32] A 32-byte null, as Coinbase without previous outpoint.
4 Index (UINT32_MAX) uint32_t 0xFFFFFFFF, Coinbase has no previous outpoint.
Varies (4) Script bytes Compactsize UINT The number of bytes in the Coinbase script, with a maximum of 100 bytes.
Varies Height Script Block height. Scripting language: Starting with the data compression stack opcode, this opcode indicates how many bytes are pushed to the stack and then the block height as a small-ended unsigned integer. The script must be as short as possible, or it may be rejected. The data push opcode will be 0x03, with a size of four bytes to 16,777,216 blocks from now about 300 years.
Varies Coinbase Script None Coinbase field: Arbitrary data, no more than 100 bytes minus (4) Height byte. Miners usually place an extra random number in this field to update the block Merkle root during hashing.
4 Sequence uint32_t Serial number

Most, but not all, blocks before block height 227,836 use version 1.0, which does not require a height parameter to be prefixed with a currency base script. The block height parameter is now required.

A Coinbase trade show:

01000000. .............. ........ Version 01 ... ........ ....... ...

... ...... ...... Number of Inputs
| 00000000000000000000000000000000 |
00000000000000000000000000000000 ...  Previous outpoint txid
| ffffffff .......... ........, ... Previous outpoint Index
| | 29 ..... ....... ....... ...
... ..... ... Bytes in Coinbase | | | | |-03 ....... ....... ...
..... ... Bytes in height
| | | | 4e0105. ............. ... height:328014 | | | | | 062f503253482f0472d35454085fffed | | |
6c74682021 ................ arbitrary data | 00000000 ............. ........
Sequence 01 ......... ....... ....... ...

... ..... Output Count | 2c37449500000000 ............
Satoshis (25.04275756 BTC)
| 1976a914a09be8040cbf399926aeb1f4
| 70c37d1341f3b46588ac ........ P2pkh Script
| 00000000 .......... ...... ....... Locktime
compactsize unsigned integer

Transaction format and partial point-to-point network messages use a variable length integer to indicate the number of bytes in the data. The bitcoin core code and this document refer to these variable integers as compactsize.
For numbers from 0 to 252, compactsize unsigned integers look like plain unsigned integers. For other numbers above 0xffffffffffffffff, a byte is prefixed with a number to indicate its length, and the remainder of the number looks like a regular unsigned integer in the order of the small end.

value byte use format
>= 0 && <= 252

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.