Take you step by step to understand the blockchain! (second class)

Take you step by step to understand the blockchain! (second class)

·

6 min read

Table of contents

No heading

No headings in the article.

  1. Basic structure of blockchain

The basic structure of the blockchain is composed of multiple blocks, and each block contains a certain amount of transaction data, block identification, time stamp and hash value of the previous block and other information. Blocks are linked through hash pointers to form an ever-expanding chain data structure. Blockchain also includes functions such as consensus mechanism, network protocol, and smart contract to realize decentralized distributed applications.

(1) Block ID:

Usually refers to the tags or symbols used to define a specific block in the code, such as

in HTML or colon (:) and indentation in Python. Commonly used block identifiers in HTML include:

  • : Used to define blocks in the document, usually used for grouping and style settings.

  • : Used to define sections in documents, usually used to organize content and identify topics.

  • : used to define the article in the document, usually used to contain independent and complete article content.

  • : Used to define the title of the document or block, usually used to contain titles, navigation, etc.

  • : Footnotes used to define documents or blocks, usually used to include copyright, contact information, etc.

  • : Used to define the navigation block of a document or block, usually used to contain navigation links and other content.

  • : Used to define the sidebar of a document or block, usually used to contain supplementary information related to the main content.

(These tags can be used to organize and identify blocks or content in an HTML document, helping to achieve better layout and content structure.)

Commonly used block identifiers in Python include:

  • Colon (:): Used to define the beginning of multi-line statements such as conditional judgment, loop and function definition.

  • Indentation: Used to distinguish program code blocks, that is, to indent statements at the same level with the same space or tab, usually 4 spaces for an indentation level.

  • Braces ({}): Used to define data structures such as dictionaries and collections, as well as blocks in formatted strings.

  • Brackets ([]): Used to define data structures such as lists and tuples, and to access elements in sequences. (These block identifiers can be used to distinguish program code blocks and define data structures, which help the readability and maintainability of the code.)

(2) Timestamp:

Timestamp (Timestamp) refers to a specific date and time, usually the number of seconds or milliseconds elapsed from a specific starting time (such as January 1, 1970 00:00:00 UTC), used for Perform time comparison and exchange of timestamps on different systems. In computer programming, time stamps are often used to record the time when events occur, the execution time of scheduled tasks, timing and other scenarios. In Python, you can use the built-in modules datetime and time to get, convert, and compute timestamps. For example, use the now() method of the datetime module to obtain a datetime object at the current time, and then use the timestamp() method to convert it to a timestamp (in seconds):

import datetime

now = datetime.datetime.now() timestamp = now. timestamp() print(timestamp)

In Python, a timestamp is usually a floating-point number representing the number of seconds or milliseconds elapsed since January 1, 1970 00:00:00 UTC, which can be added, subtracted, and compared.

(3) Hash value, hash pointer:

A hash value refers to compressing a message of arbitrary length (string, binary string, file, etc.) into a fixed-length number, usually an integer. Hash values ​​can be used to verify data integrity, identify data, encrypt, etc. In Python, the hash value of an object can be calculated using the built-in function hash(). For the same object, its hash value is fixed; different objects may have the same hash value, which is called hash collision (Hash Collision). In order to reduce the occurrence of hash collisions, built-in types in Python (such as numbers, strings, tuples, etc.) have implemented the hash() method to calculate the hash value of the object.

A hash pointer refers to a pointer to a data block, and its pointer value is usually a hash value calculated based on the content of the data block. Hash pointers are often used to implement data structures, such as hash tables, hash linked lists, etc., for operations such as searching, inserting, and deleting elements in a data set, and have the characteristics of fast search and modification. In Python, hashes can be calculated using the built-in hashlib module, for example:

1import hashlib 2 3m = hashlib. sha256() 4m. update(b"hello") 5hash_value = m.digest() 6print(hash_value)

Among them, the sha256() method creates a SHA-256 hash object, the update() method is used to add a byte string to the hash object, and the digest() method returns the calculated hash value

(4) Consensus mechanism There are mainly the following types:

  • Proof of work: By solving complex mathematical problems to generate new blocks, consume a large amount of computing resources, thereby protecting the security of the blockchain.

  • Proof of rights and interests: By holding a certain amount of cryptocurrency to participate in the verification and generation of the blockchain, improve the efficiency of block generation.

  • Delegated Proof of Stake (DPoS): The nodes elected by the stake holders in PoS represent the nodes, responsible for verifying transactions and generating new blocks.

  • Proof of Elapsed Time (PoET): By choosing a random waiting time on the node to determine which node can generate the next block.

  • Proof of Authority (PoA): It is authorized by specific nodes or organizations to verify and generate new blocks, which has high efficiency and scalability. (Different consensus mechanisms differ in terms of security, efficiency, energy consumption, etc., and need to be selected and optimized according to actual needs.)

    (5) Network protocol

Network Protocol (Network Protocol) refers to the standard specifications and agreements used to communicate and exchange data between different computers and devices in the Internet (Internet). The purpose of network protocols is to ensure that data can be transmitted correctly, reliably, and efficiently, and to achieve interoperability in different network environments. Common network protocols include TCP/IP protocol, HTTP protocol, FTP protocol, SMTP protocol, POP3 protocol, etc. Among them, the TCP/IP protocol is the basic protocol of the Internet, which is used to realize data transmission and routing on the Internet; the HTTP protocol is the most widely used protocol on the Web, and is used to transmit Web pages and data between clients and servers. ; The FTP protocol is a protocol for file transfer between different computers; the SMTP protocol and the POP3 protocol are used to send and receive emails respectively. In Python, you can use the socket module in the standard library to implement related operations of the network protocol, such as establishing a Socket connection, sending and receiving data, etc. You can also use third-party libraries, such as requests, urllib, etc. to implement HTTP communication.

(6) Smart contract

Smart Contract refers to an automated contract based on blockchain technology, which can implement the terms of the contract on the blockchain in the form of program code. Smart contracts can automatically execute, monitor and enforce contract regulations without the intervention of third-party agencies, thereby realizing decentralized transactions and agreements. Smart contracts are usually written in languages ​​such as Solidity, and can implement applications such as asset management, voting, e-commerce, supply chain management, etc. The execution process of the smart contract is executed through the nodes in the blockchain network. Each node can check whether the contract conforms to the specification, and record the running process of the contract on the blockchain to ensure the reliability and immutability of the contract sex. On blockchain platforms such as Ethereum, smart contracts have been widely used and developed. In Python, third-party libraries, such as web3.py, can be used to implement smart contract-related operations, such as connecting blockchain nodes, deploying smart contracts, executing contract methods, receiving and parsing events, etc.

Did you find this article valuable?

Support ZPD by becoming a sponsor. Any amount is appreciated!