Ethereum Architecture : 以太坊架构

2022-05-13 15:10:19 浏览数 (1)

Ethereum Architecture

去中心化背后的想法是不依赖单一/集中式服务器.

以太坊区块链到底是什么?区块链有两个主要组成部分:

  1. 数据库:网络中的每笔交易都存储在区块链中。当您部署应用程序时,它被视为一个事务。例如,如果您有一个允许任何人为候选人投票的投票应用程序,那么对候选人的投票将被视为交易。所有这些交易都是公开的,任何人都可以看到并验证。这些数据永远不会被篡改。为了确保网络中的所有节点都拥有相同的数据副本并确保没有无效数据被写入该数据库,以太坊使用称为工作证明的算法来保护网络。(http://ethereum.stackexchange.com/questions/14/what-proof-of-work-function-does-ethereum-use)
  2. 代码:区块链的数据库方面只存储交易。但是投票给候选人的所有逻辑在哪里,检索总票数等。在以太坊世界中,你用一种称为 Solidity的语言编写逻辑/应用程序代码(称为合约) 。然后,您使用solidity 编译器将其编译为以太坊字节码,然后将该字节码部署到区块链(几乎没有其他语言可以用来编写合约,但solidity 是迄今为止最流行且相对容易的选择)。因此,以太坊区块链不仅存储交易,还存储和执行合约代码。

所以基本上,区块链存储你的数据、存储代码并在 EVM(以太坊虚拟机)中运行代码。您将在以后的部分中了解有关 ByteCode 和 EVM 的更多信息。

为了构建基于 Web 的 Dapp,以太坊提供了一个方便的 JavaScript 库,称为 web3.js,它连接到您的区块链节点。因此,您可以将这个库包含在您著名的 js 框架中,如 reactjs、angularjs 等,然后开始构建。

If you notice the diagram , every client (browser) communicates with its own instance of the application. There is no central server to which all clients connect to. This means, in an ideal decentralized world, every person who wants to interact with a dapp (Decentralized Application) will need a full copy of the blockchain running on their computer/phone etc. That means, before you can use an application, you have to download the entire blockchain and then start using the application.

We don't live in an ideal world and it is unreasonable to expect everyone to run a blockchain server to use these apps. But the idea behind decentralization is to not rely on a single/centralized server. So, the community has come up with solutions (hosted blockchain servers, metamask etc.) where you don't have to spend lot of your hard disk and RAM downloading and running a full copy of the blockchain but also not compromise on the decentralized aspect. We will evaluate those options in the future lessons.

Now, what exactly is in the Ethereum blockchain? The blockchain has 2 main components:

  1. Database: Every transaction in the network is stored in the blockchain. When you deploy your application, it is considered as a transaction. If you have for example a Voting application that allows anyone to vote for candidates, a vote for a candidate would be considered a transaction. All these transactions are public and any one can see this and verify. This data can never be tampered with. To make sure all the nodes in the network have same copy of the data and to insure no invalid data gets written to this database, Ethereum uses an algorithm called Proof of Work to secure the network. (http://ethereum.stackexchange.com/questions/14/what-proof-of-work-function-does-ethereum-use)
  2. Code: The database aspect of blockchain just stores the transactions. But where is all the logic to vote for candidate, retrieve the total votes etc. In Ethereum world, you write the logic/application code (called contract) in a language called Solidity. You then use the solidity compiler to compile it to Ethereum Byte Code and then deploy that byte code to the blockchain (There are few other languages you could use to write contracts but solidity is by far the most popular and relatively easier option). So, not only does Ethereum blockchain store the transactions, it also stores and executes the contract code.

So basically, the blockchain stores your data, stores the code and also runs the code in the EVM (Ethereum Virtual Machine). You will learn more about ByteCode and EVM in the future sections.

To build web based Dapps, Ethereum comes with a handy javascript library called web3.js which connects to your blockchain node. So you can just include this library in your famous js framework like reactjs, angularjs etc and start building.

Webapp Architecture

One of the best ways to understand Ethereum is by comparing it with a traditional client/server architecture. If you are a web developer, the diagram on the right should make sense (You can still follow along if you are not aware of the web architecture). It is a high level client/server architecture of a simple web application.

A typical web application consists of server side code which is usually written in a programming language like Java, C#, Ruby, Python etc. The frontend code is implemented using HTML/CSS/Javascript. This entire application is then hosted on a hosting provider like AWS, Microsoft Azure, Google Cloud Platform, Heroku or a VPS.

Users interact with the web application using a client such as web browser, curl/wget (command line) or through an API. Note that there is one web application which is centralized and all the clients interact with this one application. When a client makes a request to the server, the server processes the request, interacts with the database and/or cache, reads/writes/updates the database and returns a response to the client.

EVM

The Ethereum Virtual Machine (EVM) is a simple but powerful, Turing complete 256bit Virtual Machine that allows anyone to execute arbitrary EVM Byte Code. The EVM is part of the Ethereum Protocol and plays a crucial role in the consensus engine of the Ethereum system. It allows anyone to execute arbitrary code in a trust-less environment in which the outcome of an execution can be guaranteed and is fully deterministic.

When you install and start the geth, parity or any other client, the EVM is started and it starts syncing, validating and executing transactions.

参考资料

https://www.zastrin.com/courses/ethereum-primer/lessons/1-1

0 人点赞