我们自己已经搭建好一个镜像,里面包含了以太坊需要的环境,以及这条私链的创世块。现在用这个镜像跑一个容器出来,我们可以快速搭建一个连接到私链的节点。
1 docker run -it –name node6 daocloud.io/ubuntu/node
2接下来的操作都是在node6这个容器里面。我们已经把创世块放在了privategeth目录下,所以进去这个目录,这个时候目录下只有genesis.json这个文件:
cd privategeth
可以看见我们的创世块
{
"config": {
"chainId": 12345,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x400",
"extraData" : "0x123456",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": { }
}
2.1创建数据存放的目录路径和初始化创世块
geth –datadir ./data init genesis.json
列表内容
–datadir后面表示区块链网络数据要存放的位置,这里放在data,这个时候目录多了个data,而data存放了刚才初始化生成的geth 和 keystore目录。区块链的主要信息都放在geth下,keystore用来存放节点的账户,此时为空。
2.2进入geth的交互式平台:
geth –identity “haha” –datadir ./data –networkid 12345 –rpcapi “db,eth,net,web3” –port 2220 –rpcport 3330 –bootnodes “enode://5d1f52b69d7473389e77475a1e972f45215b184ad6734e9b77088f00cc311fe493ed8352359685dee79c64db50041351da60785801328cb5ada82ab206287d1c@172.18.0.2:2220” console
3 现在我们进入到geth交互式平台,开始一些基本的操作
4 部署和调用智能合约
function Sample(uint v){
value=v;
}
function set(uint v){
value=v;
}
function get() constant returns (uint){
return value;
}
}
1)先在以太坊智能合约的浏览器编译器编译合约,得到abi接口和二进制代码
赋值给abi
abi=[{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"v","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"v","type":"uint256"}],"payable":false,"type":"constructor"}]
2)需要使用eth.contract来定义一个合约类
sample=eth.contract(abi)
3)合约的二进制代码赋值给SampleHEX方便使用 SampleHEX="0x6060604052341561000c57fe5b60405160208061013a833981016040528080519060200190919050505b806000819055505b505b60f9806100416000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514604e57806360fe47b11460715780636d4ce63c14608e575bfe5b3415605557fe5b605b60b1565b6040518082815260200191505060405180910390f35b3415607857fe5b608c600480803590602001909190505060b7565b005b3415609557fe5b609b60c2565b6040518082815260200191505060405180910390f35b60005481565b806000819055505b50565b600060005490505b905600a165627a7a72305820208c8101070c8ba5a9b32db2bf4b8062a9ba50bc2869c39ac2297938756540e80029"
4)部署合约,并把1传递给合约一个初始化
thesample=sample.new(1,{from:eth.accounts[0],data:SampleHEX,gas:3000000})
5) 通过部署合约返回的交易哈希查看合约细节
samplerecpt=eth.getTransactionReceipt("0x1d875c7ea3a19458c0ec1473f6c643ebb90c400406f3c160660d12aa3a520995")
6) 通过交易细节得到的合约的地址给合约命名
samplecontract=sample.at("0x80a52a1e0daa54dc6ce3a5bea84cce9c39b162e5")
7)调用合约
调用get()函数查看,并没有改变变量的值所以可以用call,不需要花费gas
samplecontract.get.call()
调用set()函数改变合约参数,所以必须用交易的形式,
samplecontract.set.sendTransaction(9, {from:eth.accounts[0], gas:3000000})