时间:2024-03-12|浏览:1185
用戶喜愛的交易所
已有账号登陆后会弹出下载
撰文:唐镜,Salus;任一新,红山资本;石灵芝,Salus;王江月,Salus
过去的一年随着生成式AI一次次打破大众的预期,AI创造生产力革命的浪潮席卷加密币圈层。我们看到,许多AI概念项目在二级市场上带来了一波富神话,同时,越来越来越多的开发者开始着手开发自己的「AI+加密」项目。
然而,观察可以发现,这些同质化现象非常严重,且大多数项目只是停留在改善“生产关系”的层面,例如通过去中心化网络来组织计算力,或者创建“去中心化”的拥抱脸”等。很少有项目尝试从底层技术进行真正的融合和创新。我们认为,这种现象的原因在于AI和区块链领域之间存在着一种“领域分歧”。尽管两者的交集广泛,但很少有人能够深入理解这两个领域。例如,AI 开发者很难了解以太坊的技术实现和历史基础构建状态,也更难提出深入的优化方案。
以机器学习(ML)这一线索的AI分支为例,它是一种无需显式编程指令,机器便能通过数据做出决策的技术。机器学习在数据分析和模式识别方面表现出巨大的潜力,并且在web2中已经飞入另外一个。然而由于诞生之初的时代来临,即使是在区块链技术创新的前沿阵地如以太坊,其架构、网络和治理机制也尚未将机器学习作为解决复杂问题的有效的工具。
「伟大的创新往往诞生于交叉领域。」我们撰写本文的最初目的是为了让 AI 开发者更好地理解区块链世界,同时也为以太坊社区的开发者提供新的思路。在文章中,我们首先介绍了以太坊的技术实现,并提出了将机器学习这一基础人工智能算法评估以太坊网络的方案,以提高其安全性、效率和可扩展性。我们希望以这个案例为起点,提出一些和市场上不一样的角度,激发了开发者生态中更多「AI+区块链」的创新交叉组合。
以太坊的技术实现
基础数据结构
区块链本质上是一条连接区块链,用于区分链的关键就是链配置,这也是一条区块链在创世中所必需的部分。对于以太坊来说,链配置用于区分以太坊中不同链,同时标识出其中的一些重要的升级协议以及标志事件。例如 DAOForkBlock 标志了以太坊经历 DAO 攻击的硬分叉高度,以及君士坦丁堡标志了君士坦丁堡升级的区块高度。改进提议的加大升级,会设置特别的字段以对应的区块高度,此外以太坊包含各种测试网络以及主网络,通过ChainID道具地标识了对应的网络生态。
创世区块作为整个区块链的第零个区块,其他区块直接或间接引用到创世区块。因此节点启动之初必须加载正确的创世区块信息,且不得任意修改。创世区块的信息包含了三个的链配置,同时还附加了例如相关挖矿奖励、时间、配置配额和气体设置限制等字段,需要以太关注坊的共识机制已经从工作量证明的挖矿机制转为权益证明。
以太坊账户分为外部账户和合约账户,其中外部账户由私钥唯一控制,而合约账户无私钥控制,只能通过外部账户调用合约代码来操作。它们都包含一个唯一地址。以太坊世界状态是一个以太坊账户树,每个都账户对应一个叶子结点,其中存储了该账户的状态(各种账户信息及代码信息)。
交易:以太坊作为一个去中心化平台,其本质是为了交易和合约,以太坊的区块就是储备的交易,以及附加一些其他相关信息,具体区块分为两部分,即区块头和区块体,其中区块头数据蕴藏了将所有区块连接为链的证据,我们可以理解为前区块链,以及证明整个以太坊以太世界状态的状态根,交易根,回执(收据)根,和其他一些符号索引,统计随机数等额外数据。而区块链体存储了交易列表和叔块头的列表(由于以太坊已经转为权益证明,因此叔块引用不复存在)。
交易回执提供了交易执行后的结果和额外信息,这些是不能通过查看交易本身来直接获得的。具体来说,其中包含的信息可以分为:共识内容,交易信息和区块信息,包含了交易处理是否成功以及交易日志和gas等消耗的信息。通过分析回执中的信息来调试智能合约代码和优化gas消耗。并提供了一个可靠的证据,表明交易已经被网络处理,并且可以查看交易的结果和影响。
在以太坊中,gas费用可以简单的理解为手续费,当你发送Token、执行合约、转移以太币或者区块上所出现的各种操作时,这些交易中的操作都需要gas费,以太币坊计算机在处理近期交易时需要进行消耗网络资源,这样你必须支付gas费才能让计算机为你工作。最终燃料费作为手续费支付给矿工,其具体费用的计算公式可以理解为Fee = Gas使用* Gas Price,其实消耗乘以消耗单价,其中单价由交易的发起者自行设置,其多少往往决定了交易上链的快慢。如果设置过低有可能交易不会被执行,同时还需要设置费用的gas limit消耗上限,避免出现合约中的错误导致不可预期的gas消耗的情况。
交易池
在以太坊中,存在大量的交易,相比于中心化系统,中心化系统的每秒处理交易数去明显黯然神伤。由于大量的交易进入结点,结点需要维护一个交易池并进行正确的管理这些交易。交易的广播是通过 p2p 进行的,具体关注一个节点聚集当前交易广播到其邻节点中,继而邻节点又会广播交易广播到该节点的邻节点中,通过这样的形式,记账交易可以在6秒内扩散至整个以太网络。
在交易池中的交易分为执行交易和非执行交易,执行交易具有较高的优先级会被执行并分配的区块中,而所有刚进入交易池的交易均为非执行交易,之后才会执行。执行交易和非执行交易分别记录在待处理容器中和队列容器中。
另外交易池还会维护一个本地交易列表,本地交易具备多种优势,其优先级更高,不受交易量限制的影响,且在重启结点时可以重新重新加载到交易池中。而本地交易的本地持久化存储就是通过日志来实现的(复活节点时的重新加载),其目的就是为了不丢失未完成的本地交易,并会定期更新。
交易在入队列之前检验交易合法性,具体包括各种类型的检查,例如:防DOS攻击、防负数交易、交易gas上限等等。交易池简单组成可以分为:queue+pending(两个组成)所有交易),在完成合法性检测之后会进行后续的检查,包括检查交易队列是否达到上限,之后判断远程交易(远程交易就是非本地交易)是否是交易池中最低的,替换交易池中最低价格对于执行交易的替换,默认只上调 10% 手续费的交易才允许替换替换已在等待执行的交易,替换后存储为非执行交易。另外在交易池的维护过程中还会删除无效和超上限交易,视觉满足条件的交易进行替换。
意见机制
The early consensus theory of Ethereum was still based on the difficulty value hash calculation method. That is to say, the hash value of the block needs to be calculated to meet the conditions of the target difficulty value before the block is legal. Since the current consensus algorithm of Ethereum has been changed from POW to POS, the theories related to mining will not be discussed in detail. Here is a brief description of the POS algorithm. Ethereum completed the merger of the beacon chain in September 2022 and implemented the POS algorithm. Specifically, for POS-based Ethereum, the block time of each block is stable at 12s, and users pledge their own Ethereum coins to obtain The right to become a validator, and then the users participating in the pledge will be randomly selected to obtain a batch of validators. In each round, validators will be selected from each of the 32 slots, and one of them will be selected for verification. The proposer serves as the proposer, and the proposer implements the block production, and the remaining validators corresponding to the slot serve as the committee to verify the legality of the proposer's block and make a judgment on the legality of the block in the previous cycle. The POS algorithm significantly stabilizes and improves the speed of block generation, while greatly avoiding the waste of computing resources.
Signature algorithm
Ethereum follows Bitcoin's signature algorithm standard and also uses the secp256k1 curve. Its specific signature algorithm uses ECDSA, which means that the calculated signature is calculated based on the hash of the original message. The composition of the entire signature is simple. That's R+S+V. Random numbers are introduced correspondingly in each calculation, where R+S is the original output of ECDSA. The last field V is called the recovery field, which indicates the number of searches required to successfully recover the public key from the content and signature, because there may be multiple coordinate points in the elliptic curve that meet the requirements based on the R value.
The whole process can be simply summarized as follows: the transaction data and signer-related information are hashed after RLP encoding, and signed with the private key through ECDSA to obtain the final signature, and the curve used in ECDSA is the secp256k1 elliptic curve. Finally, by combining the signature data with the transaction data, you can obtain a signed transaction data and broadcast it.
Ethereum's data structure does not just rely on traditional blockchain technology, but also introduces Merkle Patricia trees, also known as Merkle compressed prefix trees, for efficient storage and verification of large amounts of data. MPT combines the cryptographic hashing capabilities of Merkle trees with the key path compression properties of Patricia trees to provide a solution that both guarantees data integrity and supports fast lookups.
Merkle compressed prefix tree
In Ethereum, MPT is used to store all state and transaction data, ensuring that any data changes are reflected in the root hash of the tree. This means that by verifying the root hash, you can prove the integrity and accuracy of the data without having to check the entire database. MPT consists of four types of nodes: leaf nodes, extension nodes, branch nodes and empty nodes, which together form a tree that can adapt to dynamic data changes. Each time the data is updated, MPT reflects these changes by adding, deleting, or modifying nodes, while updating the root hash of the tree. Since each node is encrypted by a hash function, any small changes to the data will lead to huge changes in the root hash, thus ensuring the security and consistency of the data. In addition, MPT's design supports "light client" verification, allowing nodes to verify the existence or status of specific information only by storing the root hash of the tree and the necessary path nodes, greatly reducing the need for data storage and processing.
Through MPT, Ethereum not only achieves efficient management and rapid access to data, but also ensures the security and decentralization of the network, supporting the operation and development of the entire Ethereum network.
state machine
The core architecture of Ethereum incorporates the concept of state machines. The Ethereum Virtual Machine (EVM) is the runtime environment that executes all smart contract codes, and Ethereum itself can be regarded as a globally shared, state transition system. The execution of each block can be viewed as a state transition process, moving from one globally shared state to another. This design not only ensures the consistency and decentralization of the Ethereum network, but also makes the execution results of smart contracts predictable and non-tamperable.
In Ethereum, status refers to the current information of all accounts, including the balance of each account, storage data, and smart contract code. Whenever a transaction occurs, EVM calculates and converts the state based on the transaction content, and this process is recorded efficiently and securely through MPT. Each state transition not only changes the account data, but also results in an update of the MPT, reflected in a change in the root hash of the tree.
The relationship between EVM and MPT is crucial because MPT provides data integrity guarantees for Ethereum’s state transitions. When the EVM executes transactions that change account status, the associated MPT nodes are updated to reflect these changes. Since each node of MPT is linked by a hash, any modification to the state will cause a change in the root hash. This new root hash is then included in the new block, ensuring the stability of the entire Ethereum state. Consistency and security. Next we introduce the EVM virtual machine.
EVM
The EVM virtual machine is the foundation for the entire Ethereum to build smart contracts and execute state transitions. It is thanks to the EVM that Ethereum can be imagined as a world computer in a true sense. The EVM virtual machine is Turing complete, which means that smart contracts on Ethereum can perform any complex logical calculations, and the introduction of the gas mechanism successfully prevents infinite loops in the contract and ensures the stability and security of the network. . On a more technical level, the EVM is a stack-based virtual machine that uses Ethereum-specifi