本地搭建以太坊(Ethereum)详细教程

一、以太坊开发环境搭建

略。包括Go语言、以太坊Ethereum、包管理工具npm和智能合约编译器solc等。

二、以太坊集成开发环境

创建账户

geth account new

本地搭建以太坊(Ethereum)详细教程
以太坊账户地址 528301230cb94d1649534b13e991be243780d3bd

查看账户

geth account list

本地搭建以太坊(Ethereum)详细教程
初始化创世块文件

geth --datadir "./" init genesis.json

本地搭建以太坊(Ethereum)详细教程
启动以太坊

geth --datadir "./" console

本地搭建以太坊(Ethereum)详细教程
启动挖矿

miner.start()

本地搭建以太坊(Ethereum)详细教程
停止挖矿

miner.stop()

本地搭建以太坊(Ethereum)详细教程
初次停止挖矿无法立刻停止,原因参考:The first time you start mining will take some time as your node will need to generate a 1GB dataset for the Proof of Work algorithm. You will see “generating DAG” messages in the other console log, which took about 8 minutes on my laptop. You can read more about this here:
https://github.com/ethereum/wiki/wiki/Ethash-DAG

三、以太坊编程接口

3.1 通过json rpc编译合约

curl --data '{"jsonrpc":"2.0","method": "eth_compileSolidity", "params": ["contract Multiply7 {event Print(uint);function multiply(uint input) returns (uint) {Print(input * 7);return input * 7;}}"], "id": 5}' localhost:8545

本地搭建以太坊(Ethereum)详细教程
提示 port 8545: Connection refused

开启rpc端口

geth --datadir "./" --rpcport "8545" --rpc console

本地搭建以太坊(Ethereum)详细教程
开启8545端口后执行编译指令

curl --data '{"jsonrpc":"2.0","method": "eth_compileSolidity", "params": ["contract Multiply7 {event Print(uint);function multiply(uint input) returns (uint) {Print(input * 7);return input * 7;}}"], "id": 5}' localhost:8545

本地搭建以太坊(Ethereum)详细教程
提示 only application/json is supported

添加头部后执行编译指令

curl -H "Content-Type: application/json"  --data '{"jsonrpc":"2.0","method": "eth_compileSolidity", "params": ["contract Multiply7 {event Print(uint);function multiply(uint input) returns (uint) {Print(input * 7);return input * 7;}}"], "id": 5}' localhost:8545

本地搭建以太坊(Ethereum)详细教程
提示 eth_compileSolidity is not available

新版本中已不能通过json rpc部署合约,官方推荐用Remix进行合约的部署任务。

3.2 通过JavaScript API编译合约

定义合约

var source = 'contract Multiply7 {event Print(uint);function multiply(uint input)
returns (uint) {Print(input * 7);return input * 7;}}'

本地搭建以太坊(Ethereum)详细教程
编译合约

var compiled = web3.eth.compile.solidity(source)

本地搭建以太坊(Ethereum)详细教程
提示 eth_compileSolidity is not available

3.3 通过在线Remix web3编译合约
本地搭建以太坊(Ethereum)详细教程

var multiply7Contract = web3.eth.contract([{"constant":false,"inputs":[{"name":"input","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"","type":"uint256"}],"name":"Print","type":"event"}]);
var multiply7 = multiply7Contract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b5060f58061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50606c600480360381019080803590602001909291905050506082565b6040518082815260200191505060405180910390f35b60007f24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da600783026040518082815260200191505060405180910390a16007820290509190505600a165627a7a723058201d5375a8266e2b7567b08952c4bc14f1536831ea67d18c9f6046297b3540f53b0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

合约部署
本地搭建以太坊(Ethereum)详细教程

合约地址
本地搭建以太坊(Ethereum)详细教程

INFO [10-15|16:24:29.097] Submitted contract creation              fullhash=0x281bc3c1169db4a96172c7c6a73e2642f31fe4195934a97bf4a0416acc2cfcfa contract=0xb7bdeBf286e03793382f3F1Bd4E8Fd32D99D9377
null [object Object]

查看multiply7合约

multiply7

本地搭建以太坊(Ethereum)详细教程
调用call()测试合约函数结果

multiply7.multiply.call(uint);

本地搭建以太坊(Ethereum)详细教程
发起交易之前解锁账户

personal.unlockAccount(eth.coinbase)

本地搭建以太坊(Ethereum)详细教程
发起交易

multiply7.multiply.sendTransaction(7,{from:eth.accounts[0],gas:200000})

本地搭建以太坊(Ethereum)详细教程
获取交易hash 0x831354bf01a9a2033701b17537915b71273420005a43372053ffa8e37f4dd4e9

查看交易

eth.getTransaction("0x831354bf01a9a2033701b17537915b71273420005a43372053ffa8e37f4dd4e9")

本地搭建以太坊(Ethereum)详细教程
3.4 通过在线Remix编译部署合约

本地私有链监听的RPC端口
本地搭建以太坊(Ethereum)详细教程
OK提示 is open (via IPC or RPC)
本地搭建以太坊(Ethereum)详细教程
以web方式连接私有链,必须开启–rpccorsdomain参数
设置IP请求白名单 * 为所有

geth --datadir "./" --rpcport "8545" --rpccorsdomain "*" --rpc console

本地搭建以太坊(Ethereum)详细教程
成功后读取Account列表
本地搭建以太坊(Ethereum)详细教程
部署信息
本地搭建以太坊(Ethereum)详细教程
控制台查看合约部署的合约地址
本地搭建以太坊(Ethereum)详细教程
发起交易

curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0", "method": "eth_sendTransaction", "params":[{"from":"0x528301230cb94d1649534b13e991be243780d3bd", "to":"0x1269e13d9df9f64b9cbf0bdfaf0dcb0e67f4cf06", "data":"0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"}], "id": 8}' localhost:8545

本地搭建以太坊(Ethereum)详细教程
交易提交
本地搭建以太坊(Ethereum)详细教程
查看交易

eth.getTransaction("0x596477fd2d977bc67d5bea5e5146d95fff2fd141a04085e42ae34e9d3ca60759")

本地搭建以太坊(Ethereum)详细教程