使用Geth和truffle进行私有区块链智能合约Ethereum的创建和部署

使用Geth和truffle进行私有区块链智能合约Ethereum的创建和部署

https://www.jianshu.com/p/f68a9ad8119a

96 SeanC52111 关注

2018.03.23 12:54* 字数 856 阅读 47评论 0喜欢 0

环境的配置

注:使用ubuntu系统完成所有实验的操作
安装geth

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

安装trufle请参照:https://www.jianshu.com/p/df7775ab51ce

使用genesis区块创建一个私有区块链

  1. 首先创建一个新的文件夹blockchain然后创建genesis.json的文件,在新创建的blockchain文件夹下又创建了一个privchain文件夹保存区块链数据
mkdir privchain
geth --datadir privchain account new
touch genesis.json
  1. 编辑genesis.json文件然后拷贝以下代码:
{
    "config" : {
    "chainId": 10,
    "homesteadBlock": 0,
    "eip155Block": 0,
    "eip158Block": 0
    },
    "coinbase"   : "0x0000000000000000000000000000000000000000",
    "difficulty" : "0x400",
    "extraData"  : "0x00",
    "gasLimit"   : "0x47e7c4",
    "nonce"      : "0xdeadbeefdeadbeef",
    "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
    "timestamp"  : "0x00",
    "alloc"      : {
    "new account address": {
      "balance": "0x1337000000000000000000"
      }
    }
}

注意,这里的difficulty为挖矿时寻找hash的难度,所以difficulty越小,挖矿速度越快。
现在我们已经准备好了创世区块的配置,可以进行区块链的初始化

  1. 打开一个新的终端,输入以下命令:
geth --datadir privchain init genesis.json

可以看到在privchain文件夹下已经自动生成了一些文件夹和配置文件

使用geth启动区块链节点

geth --identity "newEth" --rpc --rpcport 8545 --rpcaddr 0.0.0.0 --rpccorsdomain "*" --datadir "privchain" --port 30303 --rpcapi "db,eth,net,web3" --networkid 999 console

注意:rpcport 需要和之后的truffle.js中的port相对应, 默认的geth端口为30303,使用的网络id为999,和truffle.js中的网络id相对应。

  1. 进入到终端后可以创建一个账户
personal.newAccount('password')

可以使用以下命令来查看现有账户:

personal.listAccounts
web3.eth.coinbase

使用truffle部署智能合约到私有区块链

  1. 新创建一个项目文件夹truffleproject,开启一个新的终端,使用以下命令初始化truffle环境
truffle init

之后,truffleproject文件夹下会多出contracts, migrations, test三个文件夹和truffle.js文件

  1. 进入到contracts文件夹中,将已经编写好的智能合约存入contracts文件夹。一个简单的合约如下:
pragma solidity ^0.4.4;
contract Test {
    function multiply(uint a) returns(uint d) {
        return a * 7;
    }
}
  1. 修改配置
    在migrations文件夹中创建一个新的文件2_deploy_contracts.js ,内容如下
var Test = artifacts.require("./Test.sol");

module.exports = function(deployer) {
  deployer.deploy(Test);
};

此文件的作用是按顺序将智能合约部署到区块链上,可以实现对各种依赖文件的顺序部署。
修改truffle.js文件内容:

module.exports = {
  networks: {
    live: {
      host: "localhost", //本地地址,因为是在本机上建立的节点
      port: 8545,        //Ethereum的rpc监听的端口号,默认是8545
      network_id: 999    // 自定义网络号
    }
  }
};

注意:port对应于上文提到的--rpcport 8545, network_id:999对应上文geth运行时的参数--networkid 999,一定要做到geth的配置和truffle.js文件中的配置相对应。

准备将智能合约使用truffle部署到区块链上

  1. 使用truffle compile对合约进行编译,使用truffle migrate将合约部署到区块链上
    会出现一下错误:
Deploying Migrations...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: authentication needed: password or unlock

这说明我们要对账户进行解锁才可以使用。

  1. 回到geth 运行的终端中, 输入以下命令进行解锁:
personal.unlockAccount(web3.eth.coinbase, "password", 15000)

其中password部分为之前创建账户的密码,15000对应的是解锁的时间。
如果再次使用truffle migrate进行部署,仍旧出现以下错误:

Deploying Migrations...
Error encountered, bailing. Network state unknown. Review successful transactions manually.
Error: Insufficient funds for gas * price + value

这说明我们没有足够的钱来部署合约,需要矿工挖矿来获得ether、

  1. 输入miner.start()对区块链进行挖矿
  2. 经过一段时间后可以成功将区块加载到现有区块链后。使用miner.stop()可以停止挖矿,使用以下命令查看现有账户获得的ether
web3.fromWei(eth.getBalance(eth.coinbase), "ether")
  1. 现在我们可以部署合约。确保矿工执行挖矿:miner.start(),再次解锁账户。
    使用truffle migrate --network live进行部署, --network live 代表truffle.js中对应的live网络的配置。
Using network 'live'.
Running migration: 1_initial_migration.js
    Replacing Migrations...
    Migrations: 0x1a8c52f9f20aa0eea656fffe07...
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
    Replacing [[ContractName]]...
    [[ContractName]]: 0x27890f0f20a99dc85742759cdf...
Saving successful migration to network...
Saving artifacts...
  1. 合约部署成功后可以使用truffle console和智能合约进行交互
    Contractname.deployed()

猜你喜欢

转载自blog.csdn.net/jfkidear/article/details/82669043