松露泡酒的功效与作用_松露迁移的解释

松露泡酒的功效与作用

Migrations, generally speaking, are ways for developers to automate the deployment of data and its supporting structures. They are very useful for managing the deployment of new software versions, and as such aren’t exclusive to blockchain development.

一般来说,迁移是开发人员自动执行数据及其支持结构部署的方法。 它们对于管理新软件版本的部署非常有用,因此并非仅限于区块链开发。

Truffle migrations enable us to “push” the smart contracts to the Ethereum blockchain (either local, tesnet or mainnet) and to set up necessary steps for linking contracts with other contracts as well as populate contracts with initial data.

松露迁移使我们能够将智能合约“推送”到以太坊区块链( 本地,tesnet或mainnet ),并设置必要的步骤以将合约与其他合约链接以及使用初始数据填充合约。

Where migrations really shine is the management of contract addresses on the blockchain. This usually tedious job gets almost entirely abstracted away with Truffle.

迁移真正令人瞩目的地方是对区块链上合同地址的管理。 松露几乎可以使这项通常乏味的工作几乎完全抽象化。

先决条件 (Prerequisites)

Make sure that you have installed the Truffle Framework and Ganache CLI.

确保已安装Truffle FrameworkGanache CLI

入门 (Getting Started)

For starters, choose a project folder and then run truffle init. You should get an output similar to this:

对于初学者,请选择一个项目文件夹,然后运行truffle init 。 您应该得到类似于以下的输出:

Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!

Commands:

  Compile:        truffle compile
  Migrate:        truffle migrate
  Test contracts: truffle test

This command creates a barebones Truffle project in the directory where you’re positioned. The directory looks like this:

此命令在您所在的目录中创建准系统松露项目。 该目录如下所示:

.
├── contracts
│   └── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js

For starters, in the contracts directory, create a new file called Storage.sol, which should look like this:

首先,请在contracts目录中创建一个名为Storage.sol的新文件,该文件应如下所示:

pragma solidity ^0.4.21;


contract Storage {

    mapping (string => string) private _store;

    function addData(string key, string value) public {
        require(bytes(_store[key]).length == 0);
        _store[key] = value;
    }

    function removeData(string key) public returns (string) {
        require(bytes(_store[key]).length != 0);
        string prev = _store[key];
        delete _store[key];
        return prev;
    }

    function changeData(string key, string newValue) public {
        require(bytes(_store[key]).length != 0);
        _store[key] = newValue;
    }

}

初始迁移 (Initial Migrations)

As you might have noticed, two files are created when you run truffle init. They are Migrations.sol and 1_initial_migration.js.

您可能已经注意到,运行truffle init时会创建两个文件。 它们是Migrations.sol1_initial_migration.js

The initial migration files rarely need to be changed. What they do is essentially keep track of addresses on the blockchain.

初始迁移文件很少需要更改。 他们所做的基本上是跟踪区块链上的地址。

The Migrations.sol file can look any way you want it to, but it must conform to a fixed interface which looks like the interface created by the truffle init command. What you can do in those files is some advanced mangling of migrations, but as I’ve said, it’s rarely needed.

Migrations.sol文件可以按照您希望的任何方式显示,但是必须符合固定接口,该接口看起来类似于由truffle init命令创建的接口。 您可以在这些文件中执行一些高级的迁移管理,但是正如我所说的那样,它很少需要。

The same goes for the 1_initial_migration.js file. What it does is simply push the Migrations.sol file to the desired blockchain.

1_initial_migration.js文件也是1_initial_migration.js所做的只是将Migrations.sol文件推送到所需的区块链。

迁移数据 (Migrations Data)

In order to deploy the smart contracts to the Ethereum blockchain, you must first write migrations. In order to get started, in your migrations directory, create a file called 2_deploy_contracts.js. Your project structure should now look like this:

为了将智能合约部署到以太坊区块链,您必须首先编写迁移。 为了开始,请在您的migrations目录中创建一个名为2_deploy_contracts.js的文件。 您的项目结构现在应如下所示:

.
├── contracts
│   ├── Migrations.sol
│   └── Storage.sol
├── migrations
│   ├── 1_initial_migration.js
│   └── 2_deploy_contracts.js
├── test
├── truffle-config.js
└── truffle.js

In order to deploy smart contracts with migrations, first we need to access their artifacts. These are files which describe the contract addresses, the networks on which the contracts have been deployed and the functions which contracts have.

为了通过迁移部署智能合约,首先我们需要访问其工件 。 这些文件描述了合同地址,已部署合同的网络以及合同所具有的功能。

So where does all of this data come from?

那么,所有这些数据从何而来?

In your project directory, run truffle compile. If all goes well, you should have an output similar to this:

在您的项目目录中,运行truffle compile 。 如果一切顺利,您应该具有类似以下的输出:

Compiling ./contracts/Migrations.sol...
Compiling ./contracts/Storage.sol...

Writing artifacts to ./build/contracts

Depending on the compiler version, you might get some warnings, but as long as there are no errors, you’re good to go.

根据编译器的版本,您可能会收到一些警告,但是只要没有错误,就可以使用。

Now check your project directory structure again:

现在再次检查项目目录结构:

.
├── build
│   └── contracts
│       ├── Migrations.json
│       └── Storage.json
├── contracts
│   ├── Migrations.sol
│   └── Storage.sol
├── migrations
│   ├── 1_initial_migration.js
│   └── 2_deploy_contracts.js
├── test
├── truffle-config.js
└── truffle.js

Notice that there is now a build folder containing two files — Migrations.json and Storage.json — which match the smart contract files in the contracts directory.

请注意,现在有一个build包含两个文件的文件夹- Migrations.jsonStorage.json -智能合同文件中匹配该contracts目录。

These *.json files contain descriptions of their respective smart contracts. The description includes:

这些*.json文件包含其各自智能合约的描述。 描述包括:

  • Contract name

    合约名称
  • Contract ABI (Application Binary Interface — a list of all the functions in the smart contracts along with their parameters and return values)

    合约ABI(应用程序二进制接口-智能合约中所有功能及其参数和返回值的列表)
  • Contract bytecode (compiled contract data)

    合约字节码(编译后的合约数据)
  • Contract deployed bytecode (the latest version of the bytecode which was deployed to the blockchain)

    合同部署的字节码(已部署到区块链的字节码的最新版本)
  • The compiler version with which the contract was last compiled

    上次使用合同的编译器版本
  • A list of networks onto which the contract has been deployed and the address of the contract on each of those networks.

    合同已部署到的网络列表以及这些网络中每个网络上的合同地址。

This file enables Truffle to create a JavaScript wrapper for communicating with the smart contract. For example, when you call contract.address in your JavaScript code, the Truffle framework reads the address from the *.json file and enables effortless transitions between contract versions and networks.

该文件使Truffle可以创建JavaScript包装器以与智能合约进行通信。 例如,当您在JavaScript代码中调用contract.address时,Truffle框架会从*.json文件读取地址,并允许在合同版本和网络之间轻松进行转换。

写作迁移 (Writing Migrations)

Armed with this knowledge, let’s write our first migration. In the 2_deploy_contracts.js file, write this:

有了这些知识,让我们编写我们的第一个迁移。 在2_deploy_contracts.js文件中,输入以下内容:

// Fetch the Storage contract data from the Storage.json file
var Storage = artifacts.require("./Storage.sol");

// JavaScript export
module.exports = function(deployer) {
    // Deployer is the Truffle wrapper for deploying
    // contracts to the network

    // Deploy the contract to the network
    deployer.deploy(Storage);
}

Writing migrations is as simple as that. In order to run the migration script, run the following in the terminal:

编写迁移就这么简单。 为了运行迁移脚本,请在终端中运行以下命令:

truffle migrate

You should get an error saying:

您应该得到一个错误信息:

Error: No network specified. Cannot determine current network.

This means that Truffle couldn’t find the network to which you want to deploy.

这意味着Truffle找不到您要部署到的网络。

In order to use a simulated Ethereum blockchain, open a new tab in the terminal and run ganache-cli. You should get an output similar to this:

为了使用模拟的以太坊区块链,请在终端中打开一个新选项卡并运行ganache-cli 。 您应该得到类似于以下的输出:

Ganache CLI v6.1.0 (ganache-core: 2.1.0)

Available Accounts
==================
(0) 0x828da2e7b47f9480838f2077d470d39906ad1d8e
(1) 0xa4928865329324560185f1c93b5ebafd7ae6c9e8
(2) 0x957b8b855bed52e11b2d7e9b3e6427771f299f3f
(3) 0xf4b6bcabedaf1ccb3d0c89197c4b961460f1f63d
(4) 0x4bcae97be4a0d1f9a6dea4c23df8a2bffdb51291
(5) 0xe855c7cccac3a65ad24f006bf084c85c0197a779
(6) 0x168cb232283701a816a3d118897eedfcae2aec9d
(7) 0x49563e64868e1d378e20b6ab89813c1bbaa0fd48
(8) 0x467c6f6f526eee9f66776197e3a9798c1cbf78e0
(9) 0xf65b47a3c663e2cc17ded8f197057a091686da43

Private Keys
==================
(0) 8729d0f1d876d692f2f454f564042bd11c1e6d0c9b1808954f171f6f7b926fd6
(1) 452dfeee16e5a0e34fa5348f0ef11f39a8b4635e5f454f77fc228ca9598f6997
(2) 9196ad9fd6234f09ee13726cb889dcbc438c15f98e8ff1feb36a93758fa6d10a
(3) fa47edd832e896314544b98d7e297ac2ce2097b49f8a9d7e7ae0e38154db8760
(4) 7ba1a96db190c14aaee5401dd5faab1af9074d7e6f24bc2f24b5084514bbf405
(5) 90088ce271f227db6be251c3055872c0d3dbdda9fc23ed119cf9d55db7c91259
(6) c36afd6f8f291b45e94ef0059576a86602e9a982b87e0c6fb25cfab4d68e9030
(7) 2766ac8aee110e9ad1ea68d1f28aaafb464fb1ef2a759bf5b2f628d256043c15
(8) 51ccf45f87806e8e9f30f487d6cdd0b44de3ad103f0d8daf9f1e20d9a4728dd9
(9) 398c0f079448c1e3724c9267f07ca4ab88233fc995a3d463c7c64d1a191688f5

HD Wallet
==================
Mnemonic:      void august badge future common warfare dismiss earn dog shell vintage dice
Base HD Path:  m/44'/60'/0'/0/{account_index}

Listening on localhost:8545

This means that you’ve spun up a private blockchain and it’s running on localhost:8545. Now let’s set up Truffle to deploy to that network.

这意味着您已经建立了一个私有区块链,并在localhost:8545上运行。 现在,让我们设置Truffle部署到该网络。

Place the following in the truffle.js file:

将以下内容放入truffle.js文件:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*"
    }
  }
};

This simply means you’re deploying your contract to the network running on localhost:8545.

这仅表示您正在将合同部署到在localhost:8545上运行的网络上。

Now run truffle migrate. You should get an output similar to this:

现在运行truffle migrate 。 您应该得到类似于以下的输出:

Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x06595c0eccde8cb0cf642df07beefea11e3e96bfb470e8dbaf6567cecc37aed8
  Migrations: 0x6008e9a2c213d51093d0f18536d1aa3b00a7e058
Saving successful migration to network...
  ... 0x392fb34c755970d1044dc83c56df6e51d5c4d4011319f659026ba27884126d7b
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Storage...
  ... 0xb8ec575a9f3eca4a11a3f61170231a1816f7c68940d8487e56567adcf5c0a21e
  Storage: 0xd8e2af5be9af2a45fc3ee7cdcb68d9bcc37a3c81
Saving successful migration to network...
  ... 0x15498a1f9d2ce0f867b64cdf4b22ddff56f76aee9cd3d3a92b03b7aa4d881bac
Saving artifacts...

Truffle migrated your contract to the network and saved the artifacts. In the build directory, in the Storage.json file, check that this is correct by inspecting the networks object. You should see something similar to this:

Truffle将您的合同迁移到网络并保存了工件。 在构建目录的Storage.json文件中,通过检查networks对象来检查它是否正确。 您应该看到类似以下内容:

"networks": {
  "1525343635906": {
    "events": {},
    "links": {},
    "address": "0xd8e2af5be9af2a45fc3ee7cdcb68d9bcc37a3c81",
    "transactionHash": "0xb8ec575a9f3eca4a11a3f61170231a1816f7c68940d8487e56567adcf5c0a21e"
  }
}

1525343635906 is the ID of the network. (The Ethereum main network and all the major testnets have fixed IDs like 1,2,3 etc.)

1525343635906是网络的ID。 (以太坊主网络和所有主要测试网都有固定的ID,例如1,2,3等。)

address is the address to which the contract was deployed.

address是合同部署到的地址。

transactionHash is the hash of the transaction which was used for contract deployment.

transactionHash是用于合同部署的交易的哈希。

We’ll see how this is useful later on in the tutorial.

我们将在本教程的后面部分中看到它的用处。

多个合约 (Multiple contracts)

Where Truffle migrations really shine is when there are multiple contracts to compile, deploy and keep track of (which almost all blockchain projects have).

Truffle迁移真正发挥作用的地方是有多个合同可以编译,部署和跟踪(几乎所有区块链项目都有)。

Not only do migrations allow us to deploy multiple contracts with a single command, they allow us to run arbitrary functions on the contracts, get return values of those functions and pass them to subsequent contracts.

迁移不仅使我们能够使用单个命令部署多个合约,还使我们能够在合约上运行任意功能,获取这些功能的返回值并将其传递给后续合约。

Now in your contracts directory, create a file called InfoManager.sol. In the file write a contract like this:

现在,在您的contracts目录中,创建一个名为InfoManager.sol的文件。 在文件中写一个这样的合同:

pragma solidity ^0.4.21;

import "./Storage.sol";


contract InfoManager {

    Storage private _dataStore;

    uint private _lastAdded;

    function InfoManager(Storage dataStore) public {
        _dataStore = dataStore;
    }

    function addData(string key, string value) public {
        require((now - 1 days) > _lastAdded);
        _dataStore.addData(key, value);
    }

}

As we can see, this contract depends on the Storage contract. Not only that, it takes the Storage contract as a parameter in its constructor. Let’s examine the migrations which will make this possible. The migrations are contained in the same file called 2_deploy_contracts.js:

如我们所见,该合同取决于Storage合同。 不仅如此,它还将Storage协定作为其构造函数中的参数。 让我们研究一下将使之成为可能的迁移。 迁移包含在名为2_deploy_contracts.js的同一文件中:

var Storage = artifacts.require("./Storage.sol");
var InfoManager = artifacts.require("./InfoManager.sol");

module.exports = function(deployer) {

    // Deploy the Storage contract
    deployer.deploy(Storage)
        // Wait until the storage contract is deployed
        .then(() => Storage.deployed())
        // Deploy the InfoManager contract, while passing the address of the
        // Storage contract
        .then(() => deployer.deploy(InfoManager, Storage.address));
}

The syntax for deploying is:

部署的语法为:

...
deployer.deploy(`ContractName`, [`constructor params`]) // Returns a promise
...

Since the deploy(...) function returns a promise, you can handle it any way you like, with the notable exception of async not working in migrations for some reason.

由于deploy(...)函数会返回一个promise,因此您可以按照自己喜欢的任何方式进行处理,但由于某种原因async无法在迁移中工作,这是一个明显的例外。

You can also run custom steps after the contract has been deployed. For example, the migration could look like this:

部署合同后,您还可以运行自定义步骤。 例如,迁移可能如下所示:

deployer.deploy(Storage)
    .then(() => Storage.deployed())
    .then((instance) => {
        instance.addData("Hello", "world")
    }).then(() => deployer.deploy(InfoManager, Storage.address));

This would populate the Storage contract with a string world at the key data before deploying the InfoManager contract.

在部署InfoManager合约之前,这将在关键data使用字符串world填充Storage合约。

This is useful because sometimes the interdependence between contracts can be such that some data must be either retrieved or inserted outside of the scope of the contract constructor.

这很有用,因为有时合同之间的相互依存关系使得某些数据必须在合同构造函数范围之外检索或插入。

网路 (Networks)

You’re able to conditionally run certain migrations depending on which network you’re on. This can be very useful for either populating mock data in the development phase or inputting already deployed mainnet contracts into your contracts.

您可以根据所在的网络有条件地运行某些迁移。 这对于在开发阶段填充模拟数据或将已经部署的主网合同输入到您的合同中非常有用。

This is done by “expanding” the inserted parameters of the module.exports function:

这是通过“扩展” module.exports函数的插入参数来完成的:

module.exports = function(deployer, network) {
    if (network == "live") {
        // do one thing
    } else if (network == "development") {
        // do other thing
    }
}

帐目 (Accounts)

The module.exports default function also exposes the accounts which you have access to through your Ethereum node or the wallet provider. Here’s an example:

module.exports默认功能还公开了您可以通过以太坊节点或钱包提供商访问的帐户。 这是一个例子:

module.exports = function(deployer, network, accounts) {
    var defaultAccount;
    if (network == "live") {
        defaultAccount = accounts[0]
    } else {
        defaultAccount = accounts[1]
    }
}

图书馆 (Libraries)

You’re also able to link existing libraries (already deployed), by using the deployer.link(...) function:

您还可以使用deployer.link(...)函数链接现有库(已部署):

...
deployer.deploy(MyLibrary);
deployer.link(MyLibrary, MyContract);
deployer.deploy(MyContract);
...

结论 (Conclusion)

By using the techniques outlined above, you can automate most of your blockchain deployments and reduce much of the boilerplate work involved in the development of decentralized applications.

通过使用上面概述的技术,您可以自动化大多数区块链部署,并减少去中心化应用程序开发中涉及的许多样板工作。

翻译自: https://www.sitepoint.com/truffle-migrations-explained/

松露泡酒的功效与作用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值