所谓区块链,实际上就是一种数据结构,是一种链式结构(可以理解为具有指向前面节点指针的链表),每个节点表示一个区块,区块实际上是用来存储数据的数据包,里面存储交易信息等数据。
The so-called block chain is, in fact, a data structure, a chain structure (which can be understood as a chain table with a pointer pointer) and each node represents a block that is actually a data package used to store data, in which data such as transaction information are stored.
在【web3】01. 学习环境搭建及测试中讲到,以以太坊为例,我们可以使用 Geth 启动一个以太坊的节点,实际上全球有千千万万个节点,这些节点可能会存储部分区块,也可能会存储全量区块信息,所以区块存储于这些节点上。
我们知道,区块链是没有主服务器的,或者说千千万万个节点组成了主服务器(分布式服务器),没有任何一台服务器可以直接操控数据,而是要大量的服务器一起合作操控数据,这也是区块链最主要的特性。
We know that the block chain does not have a primary server, or that tens of millions of nodes form the main server (distributed server), and that no single server can directly manipulate data, but rather that a large number of servers work together to manipulate data, which is the most important feature of the block chain.
区块里面存储的信息主要包括区块头和区块体,区块头包含区块的元数据信息,比如当前区块的 hash,上一个区块的指针等;区块体中包含区块的交易信息。
The information stored in blocks consists mainly of block heads and blocks, the head of which contains metadata information on blocks, such as the current hash of blocks, the pointer of the previous block, etc.; the block contains transaction information on blocks.
所谓交易,实际上就是区块链上的数据发生变更的过程,比如由账户 A 转账 1 个以太币给账户 B,那么则称为发生了一次交易。当然交易不限于转账,还有比如一些其他的 token 转换等,比如我们常说的 NFT 的交易、智能合约的执行。
The transaction is actually the process of changing the data on the block chain, as in the case of account A transfers 1 to account B, which is called a transaction. Of course, the transaction is not limited to transfers, but also to other token conversions, such as the NFT transaction, which we often call the operation of smart contracts.
区块实际上就是将一个个的交易打包成数据包。打包过程是按照分布式一致性算法再各个节点之间达成共识,认定某个客户端打包的区块是合法的过程,具体打包的过程以及计算和共识算法将在下面介绍。
Blocks are actually packaged into data packages. The packing process is a process by which there is consensus among the various nodes, according to the distributed consistency algorithm, that the blocks that a client packs are legitimate, and the specific packing process and calculation and consensus algorithms are described below.
所谓账户,实际上就是一个公钥/私钥对,我们知道,区块链主要依赖于非对称加密算法,非对称加密算法中,公钥是可以公开的,而私钥是不能公开的,公钥加密的数据只能通过私钥解密,从而实现了安全性。
The account is actually a public key/private key pair, and we know that the block chain relies mainly on asymmetric encryption algorithms, in which the public key is public, whereas the private key is not public, and the public key encryption data can only be declassified by private key, thus achieving security.
而区块链中的账户,以以太坊为例,账户地址就是你的公钥通过 hash 得到的值,私钥只能自己持有,不能透露给其他任何一个人,否则别人拿到你的私钥就可以对你的账户进行任何操作。
In the block chain, for example, the account address is the value that your public key receives through hash, and the private key can only be held on its own and cannot be disclosed to any other person, otherwise others can do anything about your account if they have your private key.
如果你自己不主动透露私钥,那么别人在持有你公钥地址的情况下,在当前计算机算力的情况下,想枚举你的私钥几乎是一件不可能完成的事情(当然如果后续算力有突破,比如量子计算机有重大突破,也许能够猜出来你的私钥)。
If you do not voluntarily disclose your private key, then someone else, with your public key address, and under the current computing power, trying to list your private key is almost impossible (and, of course, if there is a breakthrough in the subsequent calculation, such as a major breakthrough in the quantum computer, it may be possible to guess your private key).
以太坊中的账户分为「外部账户」与「合约账户」,这两种账户都会存储一些 token 数据(比如你的以太坊余额、你的 NFT 等),一般外部账户是你用于存储资产的账户,合约账户是用来执行只智能合约的账户,合约账户会关联智能合约代码,可以执行智能合约,其他的这两种账户区别不大。
The accounts in Etheria are divided into “external” and “contractual” accounts, both of which store some token data (e.g. your Etherton balance, your NFT, etc.), the general external accounts are those you use to store assets, the contractual accounts are accounts used to execute only smart contracts, contract accounts are linked to smart contract codes, smart contracts can be executed, and the other two accounts are not very different.
挖矿就是将大家的交易打包成区块的过程,挖矿首先有矿工,矿工有一个账户,矿工将最新的交易信息进行收集和打包,并通过计算最终证明自己打包的区块合法,并得到其他节点认可,这个时候这个区块就会被纳入整个区块链的链中,成为最新的一个链,而矿工打包区块后也可以获得一定的 token 收益。
Mining is the process of wrapping people's transactions into blocks, where miners first have miners, where miners have an account, where miners collect and pack up-to-date transaction information and, by calculating the blocks that ultimately prove that they have packed, are recognized by other nodes, when the blocks are integrated into the entire chain of blocks and become the latest chain, and when the miners pack the blocks, they can also generate some token revenues.
假设你有两个账户 A 和 B,你的朋友有两个账户 C 和 D,余额如下:
Assuming you have two accounts A and B, your friends have two accounts C and D, the balance is as follows:
- A 有 5 eth,B 有 5 eth
- C 有 1 eth,D 有 1 eth
那么,发出以下交易:账户 A 向账户 C 发送 1 eth,账户 B 向账户 D 发送 1 eth。这时候会有矿工将这两笔交易进行计算,打包到一个(或者多个)区块中,打包完成得到确认后,相当于这两笔交易得到了确认,这个时候 C 和 D 账户分别会多出来 1 eth 的余额,而 A 和 B 账户则会少 1 eth 余额。
The following transactions are sent: Account A sends 1 Eth to Account C, Account B sends 1 Eth to Account D. At this point the miners will calculate the two transactions, wrap them in one or more blocks, and when the packing is confirmed, the equivalent of the two transactions will be confirmed, at which time the balances of 1 Eth will be added to the accounts of C and D, respectively, while the balances of A and B will be reduced by 1 Eth.
在实际交易过程中,需要支付一定的费用(gas),所以 A 和 B 账户会减少的余额会大于 1 eth。
In the course of the actual transaction, a certain fee (gas) would have to be paid, so accounts A and B would have reduced the balance by more than 1 eth.
完成交易后,你们的账户余额和交易信息将永久存储于区块中,并可以通过各种手段查询。
Upon completion of the transaction, your account balances and transaction information will be permanently stored in blocks and can be consulted by various means.
本节有些名词不熟悉也没关系,后面会解释。
Some of the terms in this section are not familiar or relevant and will be explained later.
我们尝试使用环境搭建中使用的 Ganache 来生成一个区块。首先打开 Ganache,可以看到 Ganache 上显示的比如我的 RPC SERVER 为 http:127.0.0.1:7545 ,那么我们打开 Postman 来使用 json rpc 进行一次交易:
We try to create a block using the Ganache used in the environment setup. Start with Ganache, you can see, for example, that my RPC SERVER on Ganache is http:126.0.0.1:7545, so we open Postman to make a deal with json rpc:
上述的 http 请求表示从 from 地址发送 1 个以太币到 to 地址,点击发送后即可看到两个地址之间以太币的变化。
The above-mentioned http request indicates that a change between two addresses can be seen in the same currency when you click on the address from the address to the address.
完成以后,点击 Ganache 上面的 Blocks 即可看到区块信息,我们点击一个有交易信息的区块,比如:
When finished, click on Blocks above Ganache to see block information. We click on a block with trade information, for example:
可以看到,区块信息里面包含区块的基本信息和交易信息(TX),区块基本信息里面包含区块的 Hash、Gas 信息等,交易信息里面包括该区块内的各个交易(实际上真正的区块里面会包含很多个交易),可以点进去下面的交易信息详细查看交易。
As can be seen, block information contains basic block information and transaction information (TX), block basic information contains information on block Hash, Gas, etc. The transaction information includes transactions within the block (in fact, the real block contains many transactions), and details of the transaction can be identified below.
随后,我们还可以使用 json rpc 发送请求查询区块的详细信息:
Then we can also use json rpc to send details of the request for a search block:
params 里面的 hash 即为区块的 hash 值,上述请求得到如下返回结果:
The hash in the paraams is the hash of the block, and the above request was returned as follows:
可以看到,实际上,每个区块包含区块头和区块体,区块头中包含区块的基本信息,而区块体中包含区块的交易信息(transactions)。线上的实时区块信息也可以在线查看,比如这个区块:etherscan.io/block/16317…
As you can see, in fact, each block contains a block head and block body, and the block head contains the basic information on the block, while the block contains the transaction information on the block (transactions). Real-time block information on the line can also be viewed online, for example: otherscan.io/block/163117...
我们挨个解释返回值信息。
We explained the return value information one by one.
- number: 区块号,比如我的区块编号是 75,就是 16 进制的 0x4b;
- hash:当前区块的 hash 值;
- parentHash:父区块的 hash 值,比如这里表示的是区块编号为 74 的区块 hash 值,还记得前面说的区块是个链式结构吗?parentHash 就是指向前面区块的指针;
- mixHash:用于工作量证明机制的 hash 值(答案),与 nonce 一起用于工作量证明,后面工作量证明机制会讲到,此处由于是 Ganache 模拟的,所以该 hash 为 0;
- nonce:与 mixHash 一起用于工作量证明;
- sha3Uncles:32 字节的叔块 sha3 的 hash 值,叔块的概念下面会介绍;
- logsBloom:交易的日志 Bloom 过滤器,可以用于快速过滤交易日志(暂时不用关心);
- transactionsRoot:可以参考这里讲的比较详细,总体来说,是区块中交易默克尔树的根 hash 值,用于证明区块中交易完整性;
- stateRoot:表示执行完该区块内所有交易后,整个区块链的状态 hash,暂时先不用关心,想了解可以参考这里;
- receiptsRoot:表示交易回执的 hash,暂时先不用关心;
- miner:挖出该区块的矿工地址(或者叫 Coinbase);
- difficulty:表示区块挖矿的难度,可以简单理解为计算区块是一个根据问题猜答案的过程,difficulty 是「问题」,而 mixHash 和 nonce 是「答案」。需要注意,这里是区块的 nonce,交易中也存在一个 nonce,与此处不一样;
- totalDifficulty:表示从创世区块开始,到该区块截止的总体难度之和;
- extraData:区块附加数据,一般由矿工设置;
- size:区块大小;
- gasLimit:gas 就是执行交易、合约等所需的费用,gasLimit 就是该区块内所有交易所能花费的 gas 上限(多余的未使用完的 gas 会退回);
- gasUsed:区块内所有交易已经使用了的 gas 费用; timeStamp:区块的时间戳。
交易信息包含在区块的 transactions 中,里面是一个交易列表,列表中的每个交易包括,公链上交易举例 ,下面讲解的还是上述示例上的交易:
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论