去中心化 去区块链_使用JavaScript和坚固性在以太坊区块链上构建去中心化应用程序dapp...

本文档翻译自Medium文章,探讨如何利用JavaScript和Solidity在以太坊区块链上构建去中心化应用(DApp)。通过学习,读者可以了解DApp的基本原理及其在区块链技术中的应用。
摘要由CSDN通过智能技术生成

去中心化 去区块链

In this blog, I build a decentralized application, also known as a “dapp”. While there are various definitions of a dapp, in general they are applications that run on a decentralized peer-to-peer network, as opposed to a client-server network, and are governed by all the members, as opposed to a single entity that centrally owns the underlying servers and databases. In Ethereum, a dapp is backed by what is known as a “smart contract” containing the logic of the dapp. This makes a dapp running on the Ethereum blockchain potentially unstoppable as it won’t go down because the logic is replicated across many peers in the network. The smart contract backing our dapp is written in Solidity, an object-oriented, high-level language for implementing smart contracts on blockchain platforms. Our dapp will run locally on Ganache which we will interact with via a Node.js application using the web3.js library, a JavaScript library that provides an API to interact with an Ethereum blockchain.

在此博客中,我构建了一个去中心化应用程序,也称为“ dapp”。 尽管dapp有多种定义,但总的来说,它们是在分散的对等网络(而不是客户端-服务器网络)上运行的应用程序,并且由所有成员管理,而不是单个实体集中拥有基础服务器和数据库。 在以太坊中,dapp由包含该dapp逻辑的所谓“智能合约”支持。 这使得在以太坊区块链上运行的dapp可能无法阻止,因为它不会崩溃,因为逻辑是在网络中的许多对等节点之间复制的。 支持我们dapp的智能合约以Solidity编写,Solidity是一种面向对象的高级语言,用于在区块链平台上实现智能合约。 我们的dapp将在Ganache上本地运行,我们将使用web3.js库通过Node.js应用程序与之交互,该库是一个JavaScript库,提供了与以太坊区块链交互的API。

To follow along, you can find the source code on my GitHub.

接下来,您可以在我的GitHub上找到源代码

We start by setting up our project.

我们从设置项目开始。

$ mkdir ethereum-simple-decentralized-application
$ cd ethereum-simple-decentralized-application
$ echo “{ \”name\”: \”ethereum-simple-decentralized-application\”, \”version\”: \”1.0.0\”, \”description\”: \”A decentralized application (dapp) containing a smart contract deployed to an in-memory blockchain.\” }” >> package.json

We create a directory to hold our project and add a package.json file to it as we will be using the Node package manager for our project’s dependencies.

我们将创建一个目录来保存我们的项目,并向其中添加一个package.json文件,因为我们将使用Node包管理器来获取我们项目的依赖项。

用Ganache启动我们的内存区块链 (Launching our in-memory blockchain with Ganache)

Instead of developing the dapp against a live blockchain, we will use Ganache: a personal, in-memory Ethereum blockchain. The team behind Ganache has also built Ganache CLI which we will use to interact with our in-memory Ethereum blockchain via the command line. Ganache CLI is written in JavaScript making it easy and familiar for us web developers to use.

我们将使用Ganache而不是针对实时区块链开发dapp:个人的内存以太坊区块链。 Ganache背后的团队还构建了Ganache CLI,我们将使用它通过命令行与我们的内存以太坊区块链进行交互。 Ganache CLI用JavaScript编写,使我们的Web开发人员易于使用和熟悉。

The first thing that we need to do is to install the ganache-cli package in our project which we do using a familiar tool: the Node package manager (npm).

我们需要做的第一件事是在我们的项目中安装ganache-cli软件包,我们使用一个熟悉的工具:Node软件包管理器(npm)。

$ nvm use 12 && npm install ganache-cli

Executing this command will create a node_modules folder and a package-lock.json file in our project’s main directory.

执行此命令将在项目的主目录中创建一个node_modules文件夹和一个package-lock.json文件。

$ lsnode_modules package-lock.json package.json

Now let’s launch Ganache CLI to see what has been installed.

现在,让我们启动Ganache CLI,以查看已安装的内容。

$ node_modules/.bin/ganache-cliGanache CLI v6.10.1 (ganache-core: 2.11.2)
Available Accounts
==================
(0) 0x496B1E7794494E1ff44D036925728186Af03dDb6 (100 ETH)
(1) 0x14aCF29C8B33034e9E459218126cafC1a1E7ed93 (100 ETH)
(2) 0x00D59D87d9DFd7a89FA541715e2d998349f10364 (100 ETH)
(3) 0xaDd0A44f0E7aE577d177Ed23dA5449301E8401F6 (100 ETH)
(4) 0xc0a30F28697BBCf1ED569012D9cF9Dddb1b6444F (100 ETH)
(5) 0xa360C829083D5dFc381A913e13B9f4A6756B6a8f (100 ETH)
(6) 0x0B86E07440f8AE6Fbb325FD043cc9b0585c53FD7 (100 ETH)
(7) 0x4fb9f2ceF1B85b5774509FCE4025396D3886BC4d (100 ETH)
(8) 0x51b4d3BB8ECc292b71c8435D648Be9dDaf2A36c8 (100 ETH)
(9) 0x3f90e9DBafB8E1Ab1fa250A9eb7cF126955C34ec (100 ETH)Private Keys
==================
(0) 0x8a24d3f9313c02046999dc9191c29372e3aea28de03b5a01be11f559a38d4e73
(1) 0x8bf1573b7e5fa11eb4b2a3134667b317f53852580062fc264bf1863160b8390a
(2) 0xdbff66e07fbae8ddca541eb114cf8879f1e9cbc95334deb40b6105033d172fe5
(3) 0xf7306aadef15ea421113f7e95d200e8b33ccfaef0745127ed1e553066b20beac
(4) 0x1734b721539638b191e981b621337864bb934d6927ab847b6e609f6c4fa9a702
(5) 0xd545c1fcb2d29371c0c447529ead316af7ed86f50b97592f1b78a7b2fd35c127
(6) 0x90b597304632a200debe347c2aeeddc5012789fa626052e25ab6bf1d3dd697f3
(7) 0x89159cde33199b0da9b8a317e3e92f243efe38fc76045556e60e56baced2ba95
(8) 0xdb1f97689085844ed68fd69ca9d844a86f2e027104432a84ce0443fab4597ee8
(9) 0x6f4b0937d948c11d70d81331a86421866542c2e47054fa0bc0e2b0c1fb815f20HD Wallet
==================
Mnemonic: picture imitate fan snow tray fabric grass fish glance rare remove nuclear
Base HD Path: m/44'/60'/0'/0/{account_index}Gas Price
==================
20000000000Gas Limit
==================
6721975Call Gas Limit
==================
9007199254740991Listening on 127.0.0.1:8545

Launching Ganache CLI automatically creates ten test accounts and preloads them with 100 units of fake ether (ETH), by default, however this amount can be changed using the --defaultBalanceEther option. Ether is the currency for transactions performed on the Ethereum blockchain so each of these accounts can be thought of as bank accounts, or “wallets” in blockchain terminology.

启动Ganache CLI会自动创建十个测试帐户,并在默认情况下预加载100个单位的假以太( ETH ),但是可以使用--defaultBalanceEther选项更改此数量。 以太是在以太坊区块链上进行交易的货币,因此每个账户都可以被视为银行账户,或区块链术语中的“钱包”。

In addition to units of fake ether, the Ganache instance also defines a “Gas Price” (gasPrice), “Gas Limit” (gasLimit), and “Call Gas Limit” (callGasLimit). The concept of “gas” in Ethereum is a measurement of the computational effort that is needed in order to commit the transaction to the Ethereum network. This differentiates computational cost from cryptocurrency cost much like we differentiate the fuel cost to run a car from the currency cost of purchasing that fuel.

除了单位的假醚外,Ganache实例还定义了一个“汽油价格”( gasPrice ),“汽油极限”( gasLimit )和“ callGasLimit极限”( callGasLimit )。 以太坊中的“ gas”概念是对将交易提交到以太坊网络所需的计算工作量的一种度量。 这将计算成本与加密货币成本区分开来,就像我们将驾驶汽车的燃料成本与购买该燃料的货币成本区分开来一样。

The smart contract that we will write in this blog consists of a series of operations that make up a transaction that will become a new block in the blockchain. Ethereum miners provide the processing power to process and verify our transactions and ultimately add the new blocks by quickly solving complex hashing algorithms. Miners are incentivized to provide their computational services through rewards in the form of a transaction fee which is the product of the gas price and gas usage. Having a transaction fee ensures that the network does not get misused because each operation that it performs comes at a cost.

我们将在此博客中编写的智能合约包括一系列构成交易的操作,这些交易将成为区块链中的新块。 以太坊矿工提供处理能力来处理和验证我们的交易,并通过快速解决复杂的哈希算法来最终添加新的区块。 激励矿工通过以交易费的形式提供奖励来提供计算服务,交易费是天然气价格和天然气使用量的乘积。 收取交易费用可确保网络不会被滥用,因为它执行的每个操作都需要付出一定的代价。

The Gas Price that we see as the output of launching Ganache CLI is the price per gas unit, in units of wei, which is used to calculate the total transaction fee if a transaction does not set its own gas price. By default, the value of Gas Price is set to 20000000000 wei, however this amount can be changed using the --gasPrice option when launching Ganache CLI. Wei is the smallest unit of ether; wei is to ether like a penny is to a dollar. In real-terms, it takes 10¹⁸ wei to equal 1 ether.

我们看到的启动Ganache CLI的输出的天然气价格是每天然气单位的价格,以wei为单位,如果交易未设置其自身的天然气价格,则用于计算总交易费用。 默认情况下,Gas Price的值设置为20000000000 wei,但是在启动Ganache CLI时可以使用--gasPrice选项更改此--gasPrice 。 魏是醚的最小单位。 魏对以太就像一分钱对一美元。 实际上,等于1个以太需要10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值