区块链的原理

Coin Demo - Cryptocurrency Demo Blockchain Demo blockchaindemo

区块链是由一系列区块组成,每个区块包括如下几部分信息:

A blockchain has a list of blocks.

It starts with a single block, called the genesis block. Each block stores the following information: Index、Timestamp、Hash、Previous hash、Data、Nonce

组成

Index

The index is the position of the block in the chain. The genesis block has an index of 0. The next block will have an index of 1.

Timestamp

A record of when the block was created. The timestamp helps to keep the blockchain in order.

Hash

A hash looks like a bunch of random numbers and letters. It is a alphanumeric value that uniquely identifies data, or the "digital fingerprint" of data.

Properties of a hash

  1. Hash has a fixed length.
  2. Same data always maps to same hash.
  3. Different data always maps to a different hash (within practical limitations).
  4. Is easy to compute.
  5. Is infeasible to convert hash to data.
  6. A small change in data leads to a large change in hash.

Valid Hash

A valid hash for a blockchain is a hash that meets a certain requirement. For this blockchain, having three zeros at the beginning of the hash is the requirement for a valid hash. The number of leading zeros required is the difficulty.

Block Hash Calculation

A hashing function takes data as input, and returns a unique hash.

f ( data ) = hash

Since the hash is a "digital fingerprint" of the entire block, the data is the combination of index, timestamp, previous hash, block data, and nonce.

f ( index + previous hash + timestamp + data + nonce ) = hash

Replace the values for our genesis block, we get:

f ( 0 + "0" + 1508270000000 + "Welcome to Blockchain Demo 2.0!" + 604 ) = 000dc75a315c77a1f9c98fb6247d03dd18ac52632d7dc6a9920261d8109b37cf

Previous hash

The previous hash is the hash of the previous block.

The genesis block's previous hash is "0" because there is no previous block.

Data

Each block can store data against it. In cryptocurrencies such as Bitcoin, the data would include money transactions.

nonce

The nonce is the number used to find a valid hash.

To find a valid hash, we need to find a nonce value that will produce a valid hash when used with the rest of the information from that block.

Mining a block

The process of determining this nonce is called mining.

We start with a nonce of 0 and keep incrementing it by 1 until we find a valid hash.

mining a block

As difficulty increases, the number of possible valid hashes decreases. With fewer possible valid hashes, it takes more processing power to find a valid hash.

If the hash on the block is invalid, click on the tool button to mine the genesis block!

Data mutation

Edit the "Welcome to Blockchain Demo 2.0!" input!

Since data is an input variable for the hash, changing the data will change the hash.

The new hash will not have three leading zeros, and therefore becomes invalid.

MUTATION EFFECT

Subsequent blocks will also be invalid.

A hash change will cause a mutation in the previous hash of subsequent blocks. Since previous hash is used to calculate the hash, subsequent hashes will also change.

This will lead to a cascading invalidation of blocks.

Try it yourself:

Add 3 blocks, then mutate the genesis block input.

ADDING A NEW BLOCK

To mine another block to the blockchain, fill out the data input and click the button.

ADDING VALID BLOCKS

When adding a new block to the blockchain, the new block needs to meet these requirements.

Block index one greater than latest block index. Block previous hash equal to latest block hash. Block hash meets difficulty requirement. Block hash is correctly calculated. Other peers on the network will be adding blocks to the blockchain, so new blocks need to be validated.

PEER-TO-PEER NETWORK

A global network of computers work together to keep the blockchain secure, correct, and consistent.

ADD PEER

Click the button to add a peer to the network.

PEER STATUS

Peers have three states:

  1. Currently Active
  2. Connected
  3. Disconnected

To change peer: Click on the desired peer.

To connect to a peer: On a red peer, click .

To see history between peer: On a green peer, click .

To disconnect with peer: On a green peer, click .

PEER MESSAGES

Peers ask for each other's blocks to determine who has the most up-to-date blockchain.

PEER Communication

peer communication

P2P Tour PT.1

part1
  1. Start with the genesis block.
  2. Add a peer & connect.
  3. Add a block.
  4. Check history between peer.

P2P Tour PT.2

longest rule

The longer valid chain takes precedent over a shorter chain. Also called longest chain rule.

P2P Tour PT.3

part3

After receiving the longest valid chain, it will broadcast its latest block to its peers.

Eventually, all the peers on the network will have the longest valid chain.

Immutability

If a block is mutated, the block, and subsequent blocks become invalid.

Invalid blocks are rejected by the peers on the network. They need to be re-mined to be valid.

Earlier blocks will be harder to corrupt because there are more subsequent invalid blocks to re-mine.

Because peers on the network are always adding new valid blocks, the hacker would have to outmine the network, which requires majority processing power.

51% Attack

If a participant has more than 51% of the network, he could out-mine the network and hack the blockchain.

When there are more miners in the network, the processing power becomes more distributed and no one has majority power. This leads to a more secure blockchain.