1 安装 go-ethereum
参考网址:https://github.com/ethereum/go-ethereum/wiki/Installation-Instructions-for-Mac
brew tap ethereum/ethereum
brew install ethereum
安装solc(solidity语言编译器) 和solc-cli
sudo npm install -g solc solc-cli --save-dev
测试是否安装成功:
geth --help
下载源码并编译
git clone https://github.com/ethereum/go-ethereum
brew install go
cd go-ethereum
make geth
/Users/lemon/go-ethereum/build/bin/geth
2 搭建私有链
1,创建创世文件genesis.json
`{"config": {"chainId":0,"homesteadBlock":0,"eip155Block":0,"eip158Block":0},"alloc": {},"coinbase":"0x0000000000000000000000000000000000000000","difficulty":"0x20000","extraData":"","gasLimit":"0x2fefd8","nonce":"0x0000000000000042","mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x00"}`
{"config": {"chainId":666,"homesteadBlock":0,"eip155Block":0,"eip158Block":0},"alloc": {},"coinbase":"0x0000000000000000000000000000000000000000","difficulty":"0x20000","extraData":"","gasLimit":"0x2fefd8","nonce":"0x0000000000000042","mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x00"}
表头|表头|表头---|:--:|---:内容|内容|内容内容|内容|内容
2,启动私有链
geth --datadir "./" --port "30303" --rpc --rpcaddr "0.0.0.0" --rpccorsdomain "*" --rpcapi "db,eth,net,web3" console 2>>geth.log
3.打印日志
geth --datadir "./" --nodiscover --port "30303" console 2>>geth.log
tail -f geth.log //这句命令可以持续的输出以太坊的日志
3 部署合约
1,编写合约,咱们来个最简单的合约
pragma solidity ^0.4.0;
contract Sample {
uint public value;
function Sample(uint v){
value=v;
}
function set(uint v){
value=v;
}
function get() constant returns (uint){
return value;
}
}
2,编译合约(https://ethereum.github.io/browser-solidity/)在线编译
(1),先配置环境,选择连接我们自己搭建的私链
(2)然后点击create
(3)在点击一个at address 如果出现下方 说明编译成功
(4)查看编译的结果
3,使用命令行将合约部署到私链上
(1)abi=[{"constant": true, "inputs": [],"name": "value","outputs": [ { "name": "", "type": "uint256" } ],"payable": false, "stateMutability": "view", "type": "function"}, {"constant": true,"inputs": [],"name": "get","outputs": [{"name": "","type": "uint256"}],"payable":false,"stateMutability": "view","type": "function"},{"constant": false,"inputs": [{"name": "v","type": "uint256"}],"name": "set","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"inputs": [{"name": "v","type": "uint256"}],"payable": false,"stateMutability": "nonpayable","type": "constructor"}]
(2)sample=eth.contract(abi)
(3)SampleHE = “xxxxxxx”(数据在scenario.json里面的 "bytecode": "608060405234801561001057600xxxxxx",)字段
(4)thesample=sample.new(1,{from:eth.accounts[0],data:SampleHEX,gas:3000000})
切记执行完了记得把挖矿开一会 不然合约不生效
然后这时候合约已经部署成功就要执行掉方法了
thesample.get()
1
thesample.set.sendTransaction(9, {from:eth.accounts[0], gas:3000000})
thesample.get()
9
到此结束~~~