首先安裝go環(huán)境 https://golang.org/dl/ 直接用安裝包安裝。
完成后看下
go env
然后關(guān)注下gopath路徑,
GOPATH="/Users/MacPro/box/
我們要把go版客戶端放這里,go編譯器執(zhí)行都是去設(shè)定好的目錄執(zhí)行。以后寫go的時(shí)候項(xiàng)目也要放這里。
下載go版以太坊客戶端
git clone https://github.com/ethereum/go-ethereum
cd go-ethereum
make geth
編譯成功以后開始以太坊本地測試
先用命令看下,是否成功,
build/bin/geth -h
如果直接用geth 需要做下環(huán)境變量
export PATH=$PATH:/全路徑/build/bin/geth
開始搭建私有測試鏈
1 找一個(gè)目錄存放挖礦數(shù)據(jù) /home/vagrant/
2 創(chuàng)建創(chuàng)世塊配置文件
vim genesis.json
{
"config": {
"chainId": 1024,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000042",
"difficulty": "0x020000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0xffffffff",
"alloc": {}
}
3 初始化配置
geth --datadir data init genesis.json
4 啟動節(jié)點(diǎn)
geth --datadir data --networkid 123456 --rpc --rpccorsdomain "*" --nodiscover console
各參數(shù)代表的含義如下:
networkid 設(shè)置當(dāng)前區(qū)塊鏈的網(wǎng)絡(luò)ID,用于區(qū)分不同的網(wǎng)絡(luò),1表示公鏈
rpc 表示啟動rpc通信,可以進(jìn)行智能合約的部署和調(diào)試
console 表示啟動命令行模式,可以在Geth中執(zhí)行命令
執(zhí)行成功后將進(jìn)入?yún)^(qū)塊鏈的JavaScript控制臺環(huán)境
5 Geth JavaScript控制臺環(huán)境使用說明
創(chuàng)建新賬號
personal.newAccount() 或 personal.newAccount("123456")
查看節(jié)點(diǎn)信息
admin.nodeInfo
挖礦
開始挖礦 miner.start(1)
停止挖礦 miner.stop()
查看當(dāng)前礦工賬號
eth.coinbase 默認(rèn)為第一個(gè)賬戶
修改礦工賬號
miner.setEtherbase(eth.accounts[1])
查看賬戶信息
eth.accounts[0]
查看賬戶余額
eth.getBalance(eth.accounts[0]) 或者 web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
解鎖賬號
personal.unlockAccount(eth.accounts[0])
使用賬戶資金前都需要先解鎖賬號
轉(zhuǎn)賬eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(3,"ether")})
使用txpool.status可以看到交易狀態(tài)
查看區(qū)塊數(shù)據(jù)
eth.blockNumber
eth.getTransaction("0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825")
eth.getBlock(1)通過區(qū)塊號查看區(qū)塊
可以開一個(gè)窗口挖礦,再開一個(gè)窗口做交易。
build/bin/geth attach ipc:/Users/MacPro/go_project/src/github.com/ethereum/go-ethereum/data/geth.ipc
6 智能合約
創(chuàng)建一個(gè)solidity 編寫的 Token.sol 文件,內(nèi)容如下:
contract Token {
address issuer;
mapping (address => uint) balances;
event Issue(address account, uint amount);
event Transfer(address from, address to, uint amount);
function Token() {
issuer = msg.sender;
}
function issue(address account, uint amount) {
if (msg.sender != issuer) throw;
balances[account] += amount;
}
function transfer(address to, uint amount) {
if (balances[msg.sender] < amount) throw;
balances[msg.sender] -= amount;
balances[to] += amount;
Transfer(msg.sender, to, amount);
}
function getBalance(address account) constant returns (uint) {
return balances[account];
}
}
這份代碼實(shí)現(xiàn)了一個(gè)簡單的Token合約功能。
issue 函數(shù)可以向賬戶直接存放token
transfer 函數(shù)可以向其他賬號發(fā)送token
getBalance 函數(shù)可以獲取某個(gè)賬號的token余額
從這個(gè)合約就能了解到,erc20協(xié)議就是在以太坊上存一個(gè)數(shù)字,然后加減所謂交易,其他的其實(shí)也做不了更多。
7部署合約
先到http://remix.ethereum.org/上編譯測試合約,會生成一個(gè)web3格式的部署代碼

先解鎖
personal.unlockAccount(eth.accounts[0])點(diǎn)擊details 左邊WEB3DEPLOY下代碼復(fù)制到geth窗口
然后貼代碼回車
成功以后 執(zhí)行 token 看下abi數(shù)據(jù)
開始執(zhí)行合約方法
此時(shí)挖礦程序要一直工作
>token.issue(eth.accounts[0],30,{from: eth.accounts[0]})
"0xae0fafd672cfcbaf3bf717a2dee49db3017d4da87cb3e087f2b3e2f28f933cf7"
>token.getBalance(eth.accounts[0])
100
> token.transfer(eth.accounts[1], 30, {from: eth.accounts[0]})
"0x0df5fb59e8c96cb0884db40163f50fba9d65ad6e03080c21ba7d9fc2ca9663f0"
> token.getBalance(eth.accounts[1])
30