区块链dapp_使用以太坊构建DApp:应用规则和区块链设置

区块链dapp

This is the first part of a series on building decentralized applications using the Ethereum blockchain.

这是使用以太坊区块链构建去中心化应用程序系列的第一部分。

We’re going to build 3 things:

我们将构建3件事:

  1. a custom token

    自定义令牌
  2. a DAO which uses the tokens as votes

    使用令牌作为投票的DAO
  3. a prototype web UI for interacting with the smart contracts

    用于与智能合约进行交互的原型Web UI

After following the instructions in this course, you’ll be able to build your own custom Ethereum token with or without an accompanying DAO and app.

按照本课程中的说明进行操作之后,无论有没有附带的DAO和应用程序,您都可以构建自己的自定义以太坊令牌。

我们将要建设的 (What We’ll Be Building)

The project we’re building is called The Neverending Story (TNS). The full project can be found at storydao.bitfalls.com. Its full code is on GitHub.

我们正在构建的项目称为永无止境的故事 (TNS)。 完整的项目可以在storydao.bitfalls.com上找到。 其完整代码在GitHub上 。

The Neverending Story is a web application with input fields (text and image) where users who have MetaMask installed can send in a paragraph of text or an image, depending on certain conditions. (The image only appears once every 50 paragraphs and can only be a URL.)

永无止境的故事是一个具有输入字段(文本和图像)的Web应用程序,安装了MetaMask的用户可以在其中输入文本或图像的段落,具体取决于某些条件。 (该图像每50段仅出现一次,并且只能是一个URL。)

The full story is rendered next to the input fields. The more entries that exist in the story, the more expensive it is to submit an entry. Submitting an entry is an action available to everyone who pays the fee, is whitelisted, and has at least one TNS token.

整个故事显示在输入字段旁边。 故事中存在的条目越多,提交条目的成本就越高。 提交条目是一项向所有支付费用,被列入白名单并且具有至少一个TNS令牌的人可用的操作。

Action types:

动作类型:

  1. Submit entry: requires the sender to have at least 1 TNS token and be in the whitelist (see below). Costs 0.0001 ether * number of entries in current chapter (see the “Chapters” section below). This can be image only once per 50 text entries and can only be a URL. Otherwise it’s text. No code, plain text only. In either case, text field size limit is 256 characters.

    提交条目 :要求发件人至少具有1个TNS令牌,并且必须在白名单中(请参阅下文)。 费用为0.0001 ether * number of entries in current chapter (请参见下面的“章节”部分)。 每50个文本条目只能显示一次图像,并且只能是URL。 否则为文字。 没有代码,仅纯文本。 在任何一种情况下,文本字段的大小限制均为256个字符。

  2. Transfer ownership [Owner only]: can transfer ownership to a new address.

    转移所有权[仅限所有者] :可以将所有权转移到新地址。

  3. Reduce fee [Owner only]: the owner can make the fee to submit entries (see the “Fees” section below) lower without asking for a vote.

    降低费用[仅限所有者] :所有者可以降低提交条目的费用(请参阅下面的“费用”部分),而无需征集表决权。

  4. Buy TNS Tokens: send ether to the DAO to get a proportionate amount of TNS tokens automatically sent back. If there aren’t enough TNS tokens in the DAO, you must obtain them from an exchange instead.

    购买TNS代币将以发送到DAO,以获取一定比例的TNS代币自动发送回去。 如果DAO中没有足够的TNS令牌,则必须改为从交易所获取。

  5. Check token balance: a read-only action which checks how many TNS tokens there are in the DAO.

    检查令牌余额 :一种只读操作,用于检查DAO中有多少个TNS令牌。

  6. Whitelist: this function is auto-called when ether is sent to the DAO’s address. It notes the sender in a whitelist array for future reference so that people who acquire a lot of TNS can’t just distribute them to various accounts.

    白名单 :当以太币发送到DAO的地址时,此功能会自动调用。 它以白名单列表的形式记录发件人,以备将来参考,以便获得大量TNS的人不能只是将其分发到各种帐户中。

  7. End Chapter: triggers the chapter end process for distributing Ether dividends and resetting the entry fee.

    结束章 :触发​​本章结束过程,以分配以太币股息并重置入场费。

  8. Withdraw: called when stakeholders want to withdraw their dividend.

    提现 :当利益相关者要提取其股息时调用。

  9. Owner Withdraw [Owner only]: called by Owner to withdraw fee earnings.

    所有者提款[仅限所有者] :由所有者调用以提取费用收入。

Stakeholders (TNS token holders) will be able to vote on proposals, and any proposal reaching more “YES” votes than “NO” votes will get approved. The number of votes does not matter; the ratio matters. Proposals need to be open for votes for a given period of time, depending on type of proposal.

利益相关者(TNS令牌持有者)将能够对提案进行投票,任何获得“是”票数超过“否”票的提案都将获得批准。 票数无关紧要; 比率很重要。 根据提案类型的不同,提案需要在给定的时间内公开投票。

Proposal types:

提案类型:

  1. Remove entry: when confirmed by vote, the target entry is removed. Voting time: 48 hours.

    删除条目 :经投票确认后,将删除目标条目。 投票时间: 48小时

  2. Emergency remove entry [Only Owner]: can only be triggered by Owner. When confirmed by vote, the target entry is removed. Voting time: 12 hours.

    紧急删除条目[仅所有者] :只能由所有者触发。 经投票确认后,将删除目标条目。 投票时间: 12小时

  3. Emergency remove image [Only Owner]: only applies to image entries. Can only be triggered by Owner. When confirmed by vote, the target entry is removed. Voting time: 4 hours.

    紧急删除图像[仅所有者] :仅适用于图像条目。 只能由所有者触发。 经投票确认后,将删除目标条目。 投票时间: 4小时

Later on we can add other types of proposals using the same method.

稍后,我们可以使用相同的方法添加其他类型的投标。

The proposals all need to be clearly listed somewhere in the UI of course, so that people know their votes are needed. A voter needs only one TNS token to be able to vote, but the more tokens they have the more their vote is worth.

当然,所有提案都必须在用户界面中清楚地列出,以便人们知道需要他们的投票。 投票者只需要一个TNS令牌即可投票,但是投票者拥有的令牌越多,其投票价值就越高。

Important: a user’s tokens are locked during a vote. That prevents a whale TNS holder from overpowering every active vote. The voters will have to distribute their voting power on votes that matter to them.

重要提示:用户的令牌在投票期间被锁定 。 这可以防止鲸鱼TNS持有人压倒每一个活跃的投票。 选民必须分配对他们重要的选票的投票权。

章节 (Chapters)

After 1000 entries or three weeks of time, a chapter’s end can be called by any user. The DAO will then distribute collected ether to all TNS holders, proportionate to their balance. A holder does not need to be whitelisted in the DAO to get dividend ether. They only need to hold TNS tokens at the moment of distribution.

在输入1000次或三周后,任何用户都可以调用章节的结尾。 然后,DAO将根据其余额向所有TNS持有者分配所收集的以太币。 持有人无需在DAO中列入白名单即可获得以太币。 他们只需要在分发时持有TNS令牌即可。

The distribution is a pull-mechanism: the holders need to call the withdraw function of the DAO to get their ether. It will not be automatically called. The withdrawal window is 72 hours. 50% of the unclaimed ether goes to the owner, while 50% goes into the next chapter’s dividend.

该分布是拉机制:持有人需要调用DAO的退出函数来获取他们的醚。 它不会被自动调用。 退出窗口为72小时。 无人认领的以太币的50%归所有者,而下一章的股息则占50%。

费用 (Fees)

1% of every submitted entry fee goes to the current Owner. The rest is put into a pool and distributed to all TNS holders after every chapter ends, proportionate to their TNS holdings.

每位提交的报名费的1%用于当前的所有者。 其余各部分放入库中,并在每一章结束后分配给所有TNS持有者,并与其持有的TNS成比例。

The Owner’s fees go into a separate balance from which they can periodically withdraw at will.

所有者的费用进入一个单独的余额,可以随意从中定期提取。

To get whitelisted, the user must send 0.01 ether to the DAO. All future purchases of tokens are at a much lower price. The 0.01 ether entry fee is there to prevent Sybil attacks. If the user sends more than 0.01 in the first go, the 0.01 go towards the whitelisting, while the leftovers will be used to calculate how much more TNS the user gets. All tokens will be sent back to the user in one go.

要进入白名单,用户必须向DAO发送0.01乙醚。 所有未来购买的代币价格都低得多。 那里有0.01的以太网进入费以防止Sybil攻击 。 如果用户在第一次发送中发送的分数超过0.01,则0.01进入白名单,而剩余部分将用于计算用户获得了多少TNS。 所有令牌将一次性发送回用户。

In a nutshell, think of this project as a crowdsourced choose-your-own-adventure story with crowd curation and community censorship. A story-DAO.

简而言之,可以将这个项目看作是一个由群众策展和社区审查的众包自选冒险故事。 一个故事,DAO。

引导程序:PoA私有区块链 (Bootstrapping: PoA Private Blockchain)

We’ll do two bootstrapping procedures: this one will focus on running our own real Ethereum blockchain locally. It will use Proof of Authority as a consensus mechanism, and function as any real Ethereum testnet would.

我们将执行两个引导过程:这一过程将专注于在本地运行我们自己的真实以太坊区块链。 它将使用权威证明作为共识机制,并像任何真正的以太坊测试网一样发挥作用 。

  • Download and install Virtualbox.

    下载并安装Virtualbox 。

  • Download and install Vagrant. If you’re not sure what virtual machines are, please see this post.

    下载并安装Vagrant 。 如果您不确定什么是虚拟机,请参阅这篇文章 。

  • Download Geth from here. Be sure to scroll down and download “Geth & Tools”, not just “Geth”. If you’re on macOS, you can also use Homebrew and install it with brew install ethereum.

    从此处下载Geth。 确保向下滚动并下载“ Geth&Tools”,而不仅仅是“ Geth”。 如果您使用的是macOS,还可以使用Homebrew并通过brew install ethereum进行brew install ethereum

  • Mist can be downloaded here. Be sure to download “Mist” and not “Ethereum Wallet”. Mist is the Ethereum “browser” which can open various DApps. Ethereum Wallet is the same thing, but locked into wallet-mode, so you can’t open other apps with it. This is to prevent non-technical users from visiting malicious DApps.

    薄雾可以在这里下载。 确保下载“ Mist”而不是“ Ethereum Wallet”。 Mist是以太坊的“浏览器”,可以打开各种DApp。 以太坊钱包是一回事,但被锁定为钱包模式,因此您无法使用它打开其他应用程序。 这是为了防止非技术用户访问恶意DApp。

If you’re curious, learn more about Mist and Geth here.

如果您好奇,请在此处了解有关Mist和Geth的更多信息。

We need VirtualBox and Vagrant because we’ll be using this approach to launch two virtual machines on which our nodes will be running, simulating two computers running an Ethereum node.

我们需要VirtualBox和Vagrant,因为我们将使用此方法来启动将在其上运行我们的节点的两个虚拟机,以模拟运行以太坊节点的两台计算机。

Next, follow the instructions from this post to get a PoA private blockchain set up.

接下来,按照这篇文章中的说明进行操作以建立PoA私有区块链。

You’ll end up with two running nodes. Each will be mining to its own address. Make a note of those addresses. Mine were:

您将最终得到两个正在运行的节点。 每个人都将挖掘自己的地址。 记下这些地址。 我的是:

  • Node1: 0x4b61dc81fe382068e459444e8beed1aab9940e3b

    0x4b61dc81fe382068e459444e8beed1aab9940e3b 1: 0x4b61dc81fe382068e459444e8beed1aab9940e3b

  • Node2: 0x97e01610f1c4f4367c326ed1e9c41896b4378458

    0x97e01610f1c4f4367c326ed1e9c41896b4378458 2: 0x97e01610f1c4f4367c326ed1e9c41896b4378458

引导程序:Ganache CLI (Bootstrapping: Ganache CLI)

The second type of bootstrapping we can approach, and this is particularly useful for testing our contracts, is Ganache CLI, previously known as Testrpc.

我们可以采用的第二种引导方式,对于测试我们的合同特别有用,是Ganache CLI,以前称为Testrpc。

Ganache is a JavaScript-based blockchain simulation running locally. It re-runs the blockchain from scratch every time we run tests or relaunch our app. This helps us test edge cases quickly but doesn’t lend itself well to long-term testing of long-lasting contracts.

Ganache是在本地运行的基于JavaScript的区块链模拟。 每当我们运行测试或重新启动我们的应用程序时,它将从头开始重新运行区块链。 这有助于我们快速测试边缘案例,但不能很好地用于长期合同的长期测试。

First, install Ganache with npm install -g ganache-cli.

首先,使用npm install -g ganache-cli安装Ganache。

Then, simply run ganache-cli and you’re ready. You should see output similar to this one:

然后,只需运行ganache-cli ,您就可以准备就绪。 您应该看到类似于以下内容的输出:

$ ganache-cli
Ganache CLI v6.1.0 (ganache-core: 2.1.0)
(node:40584) ExperimentalWarning: The fs.promises API is experimental

Available Accounts
==================
(0) 0xa0b7139a36ecda5ffda66b9cf97cb9de36e63f2f
(1) 0x1f5546797a0ff7efe42ecafaeebd5c932f1a0143
(2) 0x0eacbad38a642db2204574ad01b2b51c82ff7080
(3) 0x77f40a8add69b0e0806c0c506208e5783b89076d
(4) 0x1ea41547984ecb949c2b2df311bffe0fdeae4632
(5) 0xa1ad154fd5fd11ebe5410c992e5e97b461c516a2
(6) 0x34da52fd90c015a41bcc383ba3d804f7cebbc84e
(7) 0xddd5232788c1f1192d6ac5e82e74ca80945e119e
(8) 0x7ebc838124a676eac57f9b6275cd29b1a1c63d4d
(9) 0x6feed7913319ffb1b01f767960dd843ea7f96181

Private Keys
==================
(0) 62727ad35a288eb34f268cffb1ce620ef3ee80910138aed0e81f24d912fd8a49
(1) a6c76b382c655dcc66dd92428e3e0a0f14b7458162ad8e5cbbbcc64d3362d28c
(2) eef05f81fd995329c80d8875d5cb62b81f8f28c39951665b4b15688dc48b4c47
(3) 5ae06fc34da5d47a64a814ee088f7c6f0d1cae3c63d7ad2d6b71b8128bce1764
(4) 8cc43f28054f90243dea496263bd9a45f33db44ea3956ab8d0e8704e15d0787e
(5) dcf37436237105ea2f5b1be608b6aa1fe6fb7ca80b8b23ce01ff96930a2a3045
(6) f896b6f0ee11ea272c1567ec1950f7ff610df79193cbb7b668ae0ea11f6ec825
(7) 877b5868dca9a2f5c7d9546647171c9825f1b02922442f18dd4e90d108b9e54d
(8) 7f1f3515d71d348a78ae85a755e02df49be4e0b374447b822abe5a6481fe0c58
(9) 20d50b28c8b051406edc6aa61becc3443e430d7d68925a108958f8abecd55ed3

HD Wallet
==================
Mnemonic:      soda tower talk dynamic swim tattoo edit cook pair bid glance beauty
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

What Ganache is doing here is starting a private blockchain with virtually no mining time and no nodes. It executes transactions as soon as they come in. It’s also generating 10 addresses pre-loaded with a lot of virtual ether, and spitting out their private keys so you can import them into various tools like MetaMask, MyEtherWallet, or the previously downloaded Mist.

Ganache在这里所做的工作是建立一个没有采矿时间,没有节点的私有区块链。 它一进入交易就执行交易。它还生成10个预加载了许多虚拟以太币的地址,并吐出它们的私钥,以便您可以将它们导入各种工具,例如MetaMask , MyEtherWallet或先前下载的Mist。

We’ll be using a mix of this method and the method above, depending on our needs, so make sure you set up both.

根据我们的需要,我们将混合使用此方法和上述方法,因此请确保同时设置两者。

Note: preferably and alternatively, use the Ganache UI tool to have a visual interface for managing your Ganache blockchain.

注意:最好或者可替代地,使用Ganache UI工具具有用于管理Ganache区块链的可视界面。

结论 (Conclusion)

Use this guide to connect to the private blockchain (either version) with tools like MetaMask, MyEtherWallet, or the previously downloaded Mist. Both bootstrapping options, when running, run on localhost:8545 by default, so the process of connecting to them is identical.

使用本指南通过MetaMask , MyEtherWallet或以前下载的Mist之类的工具连接到私有区块链(任一版本)。 这两个引导选项在运行时默认情况下都在localhost:8545上运行,因此连接到它们的过程是相同的。

Now that our app’s rules and features are laid out and our private blockchain is set up and we can connect to it, let’s focus on tools, packages, and dependencies next.

现在,我们的应用程序的规则和功能已经列出,我们的私有区块链已经设置好,我们可以连接到它,接下来,我们将重点介绍工具,软件包和依赖项。

翻译自: https://www.sitepoint.com/building-ethereum-dapps-app-rules-blockchain-setup/

区块链dapp

你可能感兴趣的:(java,区块链,python,比特币,opencv)