Web 3.0应用程序(或“DApps”)的体系结构与Web 2.0应用程序完全不同。
The architecture of the Web 3.0 application (or "DApps") is completely different from that of the Web 2.0 application.
以Medium为例,这是一个简单的博客网站,允许用户发布自己的内容,并与他人的内容进行交互。
In the case of Medium, for example, it is a simple blog site that allows users to publish their own content and interact with the content of others.
作为一个web 2.0应用程序,它听起来可能很简单,但Medium的架构中有很多东西使这一切成为可能:
As a web 2.0 application, it may sound simple, but there's a lot in Medium's structure that makes all this possible:
首先,必须有一个地方存储必要的数据,如用户、帖子、标签、评论、喜欢等等。这需要不断更新数据库。
First, there must be a place where you can store the necessary data, such as users, post, labels, comments, likes, etc. This requires continuous updating of the database.
其次,后端代码(用像Node.js、Java或Python这样的语言编写)必须定义Medium的业务逻辑。例如,当一个新用户注册、发布一个新博客或在其他人的博客上发表评论时,会发生什么?
Second, backend codes (in languages such as Node.js, Java or Python) must define the business logic of Medium. For example, what happens when a new user registers, publishes a new blog or comments on another person’s blog?
第三,前端代码(通常用JavaScript、HTML和CSS编写)必须定义Medium的UI逻辑。例如,站点是什么样子的,当用户与页面上的每个元素交互时会发生什么?
Third, the front end code (usually written by JavaScript, HTML and CSS) must define the UI logic of Medium. For example, what is the site like, and what happens when the user interacts with each element on the page?
当您在Medium上写一篇博客文章时,您将与它的前端交互,前端与后端交互,后端与数据库交互。所有这些代码都驻留在中心化的服务器上,并通过浏览器呈现给用户。这是对当今大多数Web 2.0应用程序如何工作的一个很好的高级总结。
When you write a blog article on Medium, you will interact with the front end, the front end with the back end, and the back end with the database. All these codes are stored on a centralized server and presented to the user through a browser. This is a good summary of how most Web 2.0 applications today work.
但这一切都在改变。
But it's all changing.
区块链技术为Web 3.0应用程序开辟了一个令人兴奋的新方向。在本文中,我们将重点讨论以太坊区块链带来了什么。
Block chain technology opens up an exciting new direction for Web 3.0 applications. In this paper, we will focus on what the Etherm block chain brings.
与像Medium这样的Web 2.0应用程序不同,Web 3.0消除了中间人。没有存储应用程序状态的集中式数据库,也没有后端逻辑驻留的集中式web服务器。
Unlike Web 2.0 applications like Mediam, Web 3.0 eliminates intermediaries. There is no centralized database of application status and no centralized web server with backend logical presence.
相反,您可以利用区块链在分布式的状态机上构建应用程序,该状态机由互联网上的匿名节点维护。
Instead, you can build an application using a block chain on a distributed state machine maintained by an anonymous node on the Internet.
通过“状态机”,我指的是维护某些给定程序状态和该机器上允许的未来状态的机器。区块链是一种状态机,用一些初始状态实例化,并且有非常严格的规则(即共识)来定义该状态如何转换。
By "state machine," I mean a machine that maintains certain given program states and the permitted future state on the machine. Block chains are a state machine that uses some examples of initial state, and there are very strict rules (i.e. consensus) to define how that state is to be converted.
更好的是,没有一个实体控制这个分布式的状态机——它是由网络中的每个人共同维护的。
Even better, no entity controls this distributed state machine — it is maintained by everyone in the network.
那么后端服务器呢? 在Web 3.0中,您可以编写智能合约来定义应用程序的逻辑,并将它们部署到去中心化的状态机上,而不是如何控制Medium的后端。这意味着每个想要构建区块链应用程序的人都会在这个共享状态机上部署他们的代码。
In Web 3.0, you can write an intelligent contract to define the logic of the application and deploy it to the decentralised state machine instead of controlling the end of Medium. This means that every person who wants to build a block chain application will deploy their code on the shared state machine.
前端呢?它几乎保持不变,除了一些例外,我们将在后面讨论。
What about the front end? It's almost the same. With some exceptions, we'll discuss it later.
这个架构是这样的:
The structure is as follows:
现在,让我们更深入地研究一下是什么让这成为可能。
Now, let us look in more depth at what makes that possible.
1)区块链
1) Block Chain
以太坊区块链经常被吹捧为“世界计算机”。这是因为它是一个全局可访问的、由点对点节点网络维护的确定性状态机。此状态机上的状态更改由网络中的对等点遵循的共识规则进行管理。
This is because it is a global, accessible, determinative state machine maintained by the point-to-point network. The state change on this system is regulated by the consensus rules followed by the peer in the network.
换句话说,它被设计成世界上任何人都可以访问和写入的状态机。因此,这台机器不属于任何单一实体,而是由网络中的每个人共同拥有。
In other words, it is designed as a state machine that anyone in the world can access and write. So, this machine does not belong to any single entity, but is owned by everyone in the network.
还有一件事需要知道:数据只能写入以太坊区块链—您永远不能更新已经存在的数据。
There is one more thing to know: data can only be written in the Etheraya block chain — you can never update the data that already exists.
2)智能合约
2) Smart Contract
智能合约是一个运行在以太坊区块链上的程序,它定义了发生在区块链上的状态变化背后的逻辑。智能合约是用高级语言编写的,比如Solidity或Vyper。
A smart contract is a program that runs on the Etherm block chain, which defines the logic behind a change of status that occurs on the block chain. Smart contracts are written in advanced languages, such as Solidity or Vyper.
由于智能合约代码存储在以太坊区块链上,任何人都可以检查网络上所有智能合约的应用逻辑。
Since smart contract codes are stored on the Etherkom block chain, anyone can check the logic of all smart contract applications on the network.
3)以太坊虚拟机(EVM)
3) Etheria Virtual Machine (EVM)
接下来,您将拥有以太坊虚拟机,它执行智能合约中定义的逻辑,并处理在这个全局可访问状态机上发生的状态更改。
Next, you will have the Etheraya virtual machine, which performs the logic defined in the smart contract and handles the state changes that occur on this global accessible status machine.
EVM不理解Solidity和Vyper等用于编写智能合约的高级语言。相反,您必须将高级语言编译为字节码,然后由EVM执行。
EVM does not understand the advanced languages used to write smart contracts, such as Solidity and Vyper. On the contrary, you must compile advanced languages into bytes, which are then executed by EVM.
4)前端
4) Frontend
最后是前端。正如我们前面提到的,它定义了UI逻辑,但前端也与智能合约中定义的应用程序逻辑进行通信。
Finally, the front end. As we mentioned earlier, it defines the UI logic, but the front end also communicates with the application logic defined in the smart contract.
前端和智能合约之间的通信比上图中显示的要复杂一些。让我们接下来仔细看看这个问题。
The communication between the front end and the smart contract is more complicated than shown in the figure above. Let's take a closer look at this question.
我们希望我们的前端与我们的智能合约通信,这样它们就可以调用功能,但请记住,以太坊是一个去中心化的网络。以太坊网络中的每个节点都在以太坊状态机上保存所有状态的副本,包括与每个智能合约相关的代码和数据。
We want our front end to communicate with our smart contracts so that they can call on, but remember, Ether is a decentralised network. Each node in Ethernet keeps a copy of all states, including the code and data associated with each smart contract, on the Taipa state machine.
当我们希望与区块链上的数据和代码交互时,我们需要与这些节点中的一个进行交互。这是因为任何节点都可以广播要在EVM上执行的交易请求。然后,矿工将执行交易,并将结果状态更改广播到网络的其他部分。
When we want to interact with data and codes on the block chain, we need to interact with one of these nodes. This is because any node can broadcast a transaction request to be executed on the EVM. Then the miners will execute the transaction and change the status of the results to the rest of the network.
有两种方式来广播一个新的交易:
There are two ways to broadcast a new deal:
- 启动自己的运行以太坊区块链软件的节点。
- 使用第三方服务提供的节点,如Infura, Alchemy和Quicknode。
如果您使用第三方服务,就不必自己处理运行完整节点的所有麻烦。毕竟,在您自己的服务器上设置一个新的以太坊节点可能需要几天时间。(有很多数据需要同步——它甚至可以占用比普通笔记本电脑更多的带宽和存储空间。)
If you use a third-party service, you do not have to handle all the problems of running the full node yourself. After all, setting up a new ether node on your own server may take a few days. (There are many data that need synchronizing -- it can even take more bandwidth and storage space than a normal laptop.)
此外,存储完整以太坊区块链的成本随着DApp的扩展而增加,您需要添加更多的节点来扩展您的基础设施。这就是为什么当你的基础架构变得更加复杂时,你需要全职的DevOps工程师。它们将帮助您维护基础设施,以确保可靠的正常运行时间和快速的响应时间。
In addition, the cost of storage is increased with the extension of the Dapp block chain. You need to add more nodes to expand your infrastructure. That is why you need a full-time DevOps engineer when your base structure becomes more complex. They will help you maintain the infrastructure to ensure reliable normal operating time and rapid response time.
也就是说,为了避免这些麻烦,许多DApps选择使用像Infura或Alchemy这样的服务来管理它们的节点基础设施。当然,这是有代价的,因为这会创建一个集中的阻塞点。
In other words, in order to avoid these difficulties, many Dapps choose to use services such as Infoura or Alchemy to manage their node infrastructure. Of course, this is costly, because it creates a centralized block.
接下来,让我们谈谈提供者。当您需要与区块链交互时所连接的节点(无论您自己设置它们还是使用来自第三方服务的现有节点)通常被称为“提供者”。
So, let's talk about the provider. When you need to interact with the block chain, the nodes that you connect (whether you set them yourself or use existing nodes from third-party services) are commonly referred to as “providers”.
每个以太坊客户端(即提供者)都实现了一个JSON-RPC规范。这确保了当前端应用程序想要与区块链交互时,有一组统一的方法。如果您需要了解JSON-RPC的基础知识,它是一种无状态的轻量级远程过程调用(RPC)协议,它定义了几种数据结构及其处理规则。它与传输无关,因此可以在同一个进程中、在套接字上、在HTTP上或在许多不同的消息传递环境中使用这些概念。它使用JSON (RFC 4627)作为数据格式。
This ensures that there is a consistent approach to current-end applications that want to interact with block chains. If you need to know the basics of JSON-RPC, it is an unstated light-level remote process call (RPC) protocol that defines several data structures and their handling rules. It has nothing to do with transmission, so that these concepts can be used in the same process, on tweaks, on HTTPs, or in many different messaging environments. It uses JSON (RFC 4627) as a data format.
一旦通过提供程序连接到区块链,就可以读取存储在区块链上的状态。但是,如果您想写入状态,在将交易提交给区块链之前,还需要做一件事—使用您的私钥对交易进行“签名”。
Once you connect to the block chain by providing a program, you can read the state stored on the block chain. However, if you want to write the status, one more thing needs to be done before you submit the transaction to the block chain - signing the transaction using your private key.
例如,假设我们有一个DApp,它允许用户向区块链读取或发布博客文章。在前端可能有一个按钮,允许任何人查询特定用户撰写的博客文章。(回想一下,从区块链读取并不需要用户对交易进行签名。)
Assuming, for example, that we have a DApp that allows users to read or publish blog articles to the block chain. There may be a button at the front that allows anyone to consult a blog post written by a particular user. (Recall, reading from the block chain does not require the user to sign the transaction.)
然而,当用户想要在链上发布一个新的帖子时,我们的DApp会要求用户使用他们的私钥“签名”该交易——只有这样,DApp才会将该交易转发给区块链。否则,节点将不会接受交易。
However, when users want to post a new post on the chain, our Dapp will require users to “sign” the transaction with their private key — so that Dapp will forward the transaction to the block chain. Otherwise, the node will not accept the transaction.
这种交易的“签名”是Metamask通常使用的地方。
The “signature” of such a transaction is the place normally used by Metamask.
Metamask是一个工具,它使应用程序可以轻松地处理密钥管理和交易签名。这非常简单:Metamask将用户的私钥存储在浏览器中,每当前端需要用户签署交易时,它就会调用Metamask。
Metamask is a tool that allows the application to handle key management and transaction signatures easily. This is simple: Metamask stores the user's private key in the browser and calls Metamask whenever the user is required to sign a transaction at the current end.
Metamask还提供了一个到区块链的连接(作为一个“提供者”),因为它已经有一个到Infura提供的节点的连接,又因为需要它来签署交易。通过这种方式,Metamask既是提供者又是签名者。
Metamask also provides a link to the block chain (as a “provider”) because it already has a link to the node provided by Infoura, and because it is needed to sign the transaction. In this way, Metamask is both a provider and a signatory.
当然,如果您正在构建一个应用程序,其中所有的智能合约和数据都完全存在于以太坊区块链上,那么这种架构是有意义的。但任何在以太坊上构建应用程序的人都知道,在区块链上存储所有东西会很快变得非常昂贵。
Of course, if you're building an application with all the smart contracts and data in the Ether section chain, it makes sense. But anyone who builds the application on the Ether section knows that storing everything on the block chain will soon become very expensive.
请记住,在以太坊中,用户每次向区块链添加新数据时都要付费。这是因为向分布式状态机添加一个状态会增加节点维护该状态机的成本。
Remember, in Etheria, users pay fees every time they add new data to the block chain. This is because adding a state to the distributed state machine increases the cost of maintaining the node.
每次交易需要添加新状态时,要求用户为使用DApp支付额外费用并不是最好的用户体验。解决这个问题的一种方法是使用去中心化的链下存储解决方案,如IPFS或Swarm。
When a transaction requires a new state, it is not the best user experience to ask the user to pay extra costs for the use of DApp. One way to solve this problem is to use decentralised chain storage solutions, such as IPFS or Swarm.
IPFS是一种用于存储和访问数据的分布式文件系统。因此,IPFS系统不是将数据存储在中心化的数据库中,而是将数据分布和存储在对等网络中。这使您可以在需要时轻松地获取它。
IPFS is a distributed file system for storing and accessing data. Therefore, the IPFS system does not store data in a centralized database, but rather in a peer network. This allows you to easily access it when you need it.
IPFS还有一个激励层,称为“Filecoin”。这一层激励世界各地的节点存储和检索这些数据。您可以使用像Infura(它为您提供了一个IPFS节点)或Pinata(它提供了一个易于使用的服务,您可以将您的文件“固定”到IPFS,并获取IPFS hash并将其存储在区块链上)这样的提供商。
IPFS also has an incentive layer called Filecoin. This level encourages nodes around the world to store and retrieve these data. You can use a provider like Infra (which provides you with an IPFS node) or Piata (which provides a service that is easy to use, you can “fix” your files to IPFS and get IPFS hash and store them on the block chain).
Swarm的相似之处在于它是一个去中心化的存储网络,但有一个显著的区别。虽然Filecoin是一个单独的系统,但Swarm的激励系统是内置的,并通过以太坊区块链上的智能合约来执行,用于存储和检索数据。
The similarities between Swarm is that it is a decentralised storage network, but there is a significant difference. Although Filecoin is a separate system, Swarm's incentive system is built and implemented through an intelligent contract on the Taiwan block chain for the storage and retrieval of data.
所以现在,有了IPFS或Swarm,我们的应用架构看起来是这样的:
So now, with IPFS or Swarm, our application structure looks like this:
精明的读者可能还注意到在下面的图中,前端代码没有存储在区块链上。我们可以在AWS上托管这些代码,就像我们通常在Web 2.0中所做的那样,但这为您的DApp创建了一个集中化的阻塞点。如果AWS瘫痪了怎么办?如果它审查你的应用呢?
Smart readers may also note that the front-end codes are not stored in the block chain in the graph below. We can host these codes on the AWS, as we usually do in Web 2.0, but this creates a centralized blocking point for your Dapp. What if the AWS is paralysed? What if it reviews your application?
这就是为什么,如果你想构建一个真正去中心化的应用程序,你可能会选择在一个去中心化的存储解决方案上托管你的前端,如IPFS或Swarm。
That's why, if you want to build a truly decentralised application, you might choose to host your front end on a decentralised storage solution, such as IPFS or Swarm.
所以现在你的应用程序架构看起来更像这样:
So now your application structure looks more like this:
到目前为止,我们已经讨论了如何通过签署交易并将它们发送到区块链来写入区块链。但是从区块链上的智能合约读取数据呢?有两种主要的方法:
So far, we have discussed how to write a block chain by signing a transaction and sending it to a block chain. But what about reading data from an intelligent contract on the block chain? There are two main ways:
1)智能合约事件
你可以使用Web3.js库来查询和监听智能合约事件。您可以监听特定的事件,并在每次事件触发时指定回调。例如,如果你有一个智能合约,它在每个区块中发送一个从A到人B的连续支付流,那么你可以在每次向B进行新的支付时发出一个事件。你的前端代码可以侦听由智能合约触发的事件,并基于它执行特定的操作。
1) Smart Contract Event
You can use Web3.js Library to query and listen to smart contract events. You can listen to specific events and specify a callback when each event triggers. For example, if you have a smart contract that sends a continuous payment stream from A to B in each block, you can send an event every time you make a new payment to B. Your front-end code can listen to an event triggered by a smart contract and perform a specific operation based on it.
2)图
上述方法是可行的,但它有一些局限性。例如,如果您部署了一个智能合约,但后来发现需要触发一个最初没有包含的事件,该怎么办?不幸的是,您必须使用该事件和数据重新部署一个新的智能合约。此外,使用回调来处理各种UI逻辑很快就会变得非常复杂。
The above-mentioned approach is feasible, but it has some limitations. For example, if you deploy a smart contract, but later find it necessary to trigger an event that was not originally included. Unfortunately, you have to use the event and data to redeploy a new smart contract. Moreover, the logic of using return calls to deal with various UI logics will soon become very complicated.
这就是“图”发挥作用的地方。
This is where the Map works.
图(Graph)是一个链下索引解决方案,使其更容易查询以太坊区块链上的数据。图允许您定义要索引哪些智能合约,要侦听哪些事件和函数调用,以及如何将传入的事件转换为前端逻辑(或使用API的任何东西)可以使用的实体。它使用GraphQL作为查询语言,许多前端工程师喜欢这种语言,因为与传统REST api相比,它的表达能力很强。
Graph (Graph) is a chain-based indexing solution that makes it easier to search data on the Tetsu block chain. It allows you to define which smart contracts you want to index, which events and functions you want to listen to, and how to convert incoming events into entities that can be used for forward-end logic (or anything from API). It uses GraphQL as a query language, which many front-end engineers like because of its ability to express compared to the traditional REST api.
通过索引区块链数据,Graph允许我们以低延迟查询应用程序逻辑中的链上数据。
By indexing block chain data, Graph allows us to search the chain data in the application logic at a low delay.
现在,你的DApp架构看起来像这样:
Now, your Dapp architecture looks like this:
正如您可能已经听说的,以太坊无法扩展——至少现在还不行。
As you may have heard, the Etheria cannot expand -- at least not yet.
很明显,我们有麻烦了。在以太坊上构建DApp需要支付高额的gas费用以及完整的区块会导致非常糟糕的用户体验。值得庆幸的是,有一些解决方案正在开发中。
Obviously, we're in trouble. The construction of the DApp at the Etheria will cost a lot of gas and the complete blocks will lead to a very bad user experience. Fortunately, some solutions are being developed.
一个流行的扩展解决方案是Polygon,一个L2扩展解决方案。Polygon使用“侧链”来处理和执行交易,而不是在主区块链上执行交易。侧链是与主链交互的二级区块链。每隔一段时间,侧链就会向主链提交最近区块的聚合。
A popular extension solution is Polygon, an L2 extension solution. Polygon uses the "side chain" to handle and execute transactions, rather than performing transactions on the main block chain. The side chain is a secondary block chain that interacts with the main chain. At times, the side chain submits the recent block aggregations to the main chain.
L2解决方案的其他例子是Optimistic Rollups and zkRollups.
。这里的想法是类似的:我们使用“rollup”智能合约来批量处理链下的事务,然后定期将这些事务提交到主链。
Other examples of L2 solutions are .
最重要的想法是:L2解决方案在链下执行交易(即慢的部分),只有交易数据存储在链上。这使我们能够扩展区块链,因为我们不必执行链上的每个交易。这也使得交易更快、更便宜,并且在必要时他们仍然可以与主以太坊区块链通信。
The most important idea is that the L2 solution executes the transaction (i.e., the slow part) under the chain, with only the transaction data stored on the chain. This allows us to expand the block chain, because we do not have to execute every transaction on the chain. It also makes the deal faster and cheaper, and, if necessary, they can still communicate with the chain of the main Ether district.
如果所有这些都让你头晕目眩,你不是一个人。拼凑所有这些工具是复杂的,可能会导致痛苦的开发人员体验。但是别担心——我们已经开始看到新的开发人员框架,它们确实改善了开发人员的体验。
If all this makes you dizzy, you're not alone. Combining all these tools is complicated and can lead to painful developers' experiences. But don't worry -- we've already started to see new developers' frameworks, and they do improve developers' experiences.
例如,Hardhat是一个开发者框架,它使以太坊开发者更容易构建、部署和测试他们的智能合约。Hardhat提供了“Hardhat Network”,开发人员可以使用它将智能合约部署到本地网络上,而不需要处理实时环境。更好的是,它提供了一个很棒的插件生态系统,让开发者的生活更加轻松。为了调试目的,Hardhat还提供了类似于javascript的console.log()功能。
Hardhat, for example, is a developers' framework that makes it easier for the developers to build, deploy and test their smart contracts. Hardhat provides "Hardhat Network," which developers can use to deploy smart contracts to local networks without having to deal with real-time environments. Better yet, it provides a great plug-in ecosystem that makes life easier for developers. Hardhat also provides a console.log function similar to javascript.
翻译自:
https://www.preethikasireddy.com/post/the-architecture-of-a-web-3-0-application
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论