原文链接:https://blog.csdn.net/zhj_fly/article/details/80586022
本篇文章介绍通过truffle来部署智能合约。
1、首先需要有一个联盟链quorum的环境,如上篇文章所示。
2、部署合约,这里介绍两种方法,一是教程里的使用solc编译,获取编译后的abi和bytecode,然后在geth控制台里部署;第二种方法是使用truffle直接部署。
下面分别介绍:
(1)使用solc编译
智能合约文件:simplestorage.sol
pragma solidity ^0.4.15;
contract simplestorage {
uint public storedData;
functionsimplestorage(uint initVal)public{
storedData = initVal;
}
functionset(uint x)public{
storedData = x;
}
functionget()constantpublicreturns(uint retVal){
returnstoredData;
}
}
编译:
solcjs --bin --abi simplestorage.sol
会生成两个文件: simplestorage_sol_simplestorage.abi 和 simplestorage_sol_simplestorage.bin分别是abi和bytecode,在部署时会用到。(或者使用在线编译器remix,更加方便)
然后进入geth客户端(第一个节点):
geth attach qdata/dd1/geth.ipc
然后在部署合约和调用合约:
>varabi = [{"constant":true,"inputs":[],"name":"storedData","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initVal","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
undefined
>varbytecode ="0x6060604052341561000f57600080fd5b60405160208061014b833981016040528080519060200190919050508060008190555050610109806100426000396000f3006060604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632a1afcd914605857806360fe47b114607e5780636d4ce63c14609e575b600080fd5b3415606257600080fd5b606860c4565b6040518082815260200191505060405180910390f35b3415608857600080fd5b609c600480803590602001909190505060ca565b005b341560a857600080fd5b60ae60d4565b6040518082815260200191505060405180910390f35b60005481565b8060008190555050565b600080549050905600a165627a7a72305820e0660d73fff0ee27ccbbbb63012dedeb1191c88555c15f78e81c856f58125d9a0029"
undefined
>varsimpleContract = web3.eth.contract(abi);
undefined
> simpleContract.new(42, {from:web3.eth.accounts[0],data: bytecode,gas:0x47b760,privateFor: ["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]})
{
abi: [{
constant:true,
inputs: [],
name:"storedData",
outputs: [{...}],
payable:false,
stateMutability:"view",
type:"function"
}, {
constant:false,
inputs: [{...}],
name:"set",
outputs: [],
payable:false,
stateMutability:"nonpayable",
type:"function"
}, {
constant:true,
inputs: [],
name:"get",
outputs: [{...}],
payable:false,
stateMutability:"view",
type:"function"
}, {
inputs: [{...}],
payable:false,
stateMutability:"nonpayable",
type:"constructor"
}],
address:undefined,
transactionHash:"0x5c7fc60d8370bed6eb14e3f776365f77806eda9653e6a3f682827f68e056c0f8"
}
部署成功,其中simpleContract.new中
privateFor: ["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]}
是指明这个合约是私有合约,只有创建合约的节点1和地址代表的节点7可以正常调用。下面节点1调用合约,需要用地址和abi来加载已经部署好的合约,其中地址在日志文件/qdata/logs/1.log中,找到这么一行:
INFO [06-07|19:57:38] Submitted contract creation fullhash=0x5c7fc60d8370bed6eb14e3f776365f77806eda9653e6a3f682827f68e056c0f8to=0x4D3bfd7821E237fFE84209d8E638f9f309865b87
to后面的地址就是合约的地址,
>varprivate = eth.contract(abi).at("0x4D3bfd7821E237fFE84209d8E638f9f309865b87")
undefined
> private.get()
42
然后节点2调用合约,同样需要地址和abi来加载合约,执行结果:
> private.get()
0
节点7执行合约:
> private.get()
42
然后在节点1调用set,改变数值:
> private.set(65656, {from:eth.coinbase,privateFor:["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]})
"0xb42a5483ef98f6a7b3dc35d7ffaaf0170360de63fb7d14c8514532906fb10395"
> private.get()
65656
同样在节点2和节点7获取修改的值,结果分别为0和65656
到此,部署和调用智能合约结束。
(2)下面介绍使用truffle来部署智能合约(这里使用truffle主要是编译和部署合约)
新建一个目录test,然后进入目录初始化一个truffle项目:
truffle init
然后在contracts目录中编写智能合约:
pragma solidity ^0.4.15;
contract simplestorage {
uint public storedData;
functionsimplestorage(uint initVal)public{
storedData = initVal;
}
functionset(uint x)public{
storedData = x;
}
functionget()constantpublicreturns(uint retVal){
returnstoredData;
}
}
在test目录下编译合约:
truffle compile
部署合约:
在migrations文件夹里添加新的部署文件:
2_deploy_simplestorage.js
varsimple_storage = artifacts.require("SimpleStorage");
module.exports =function(deployer){
deployer.deploy(simple_storage,123, {privateFor:["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]});
};
修改truffle.js文件:
module.exports = {
networks: {
development: {
host:"localhost",
port:22000,
network_id:"*",
gas:4600000,
gasPrice:0
},
second_node: {
host:"localhost",
port:22001,
network_id:"*",
gas:4600000,
gasPrice:0
},
third_node: {
host:"localhost",
port:22002,
network_id:"*",
gas:4600000,
gasPrice:0
},
forth_node: {
host:"localhost",
port:22003,
network_id:"*",
gas:4600000,
gasPrice:0
},
fifth_node:{
host:"localhost",
port:22004,
network_id:"*",
gas:4600000,
gasPrice:0
},
sixth_node: {
host:"localhost",
port:22005,
network_id:"*",
gas:4600000,
gasPrice:0
},
seventh_node: {
host:"localhost",
port:22006,
network_id:"*",
gas:4600000,
gasPrice:0
}
}
};
在test目录下执行命令:truffle migrate --network development --reset
>truffle migrate --network development --reset
Running migration:1_initial_migration.js
Replacing Migrations...
...0x3d22b37608222c33cf2485bbcd746f46979e76e2c31f1be96571098a958f230f
Migrations:0xd9d64b7dc034fafdba5dc2902875a67b5d586420
Saving successful migration to network...
...0x7b6d86cadb782c0e41a35f6ebd64dd4650ccb7a7a11f6b07e63df6fcaba2311e
Saving artifacts...
Running migration:2_deploy_simplestorage.js
Replacing simplestorage...
...0x43ad5af6cf9276a78febde309b7f214af988797d62fb9ecc71f7a879cbbc6306
simplestorage:0x938781b9796aea6376e40ca158f67fa89d5d8a18
Saving successful migration to network...
...0xb9dce27942015a3a93b0f6475cc3ef59b1bc82405b90faaaa4053e8eeb835886
Saving artifacts...
其中倒数第四行中是合约的地址。
3、进入geth控制台
geth attach qdata/dd1/geth.ipc
合约地址和abi加载合约,再调用。这里就和上面方法一样了,不再重复。