原文地址:https://blog.youkuaiyun.com/jerry81333/article/details/55748747
go-ethereum
go-ethereum是以太坊的客户端之一,是一个基于Go语言的客户端。以太坊还有别的客户端包括C++,JavaScript,python,Java等,比较常用的就是Go语言实现的客户端geth (go-ethereum),其他常用的还有一个叫testrpc的工具, 它使用了Python客户端pyethereum。
Win10配置:
1.打开Powershell,win10自带,win7版本需要去微软官方下载补丁,是一个类似于Python pip的包管理装置,并需要以管理员身份运行:
2.设置Get-ExecutionPolicy可用,PowerShell中输入:
- set-ExecutionPolicy RemoteSigned
3.安装Chocolatey,这是一个第三方的包管理器,官方网址:https://chocolatey.org/
- iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
- C:\Windows\system32> choco install git
- C:\Windows\system32> choco install golang
- C:\Windows\system32> choco install mingw
- C:\Users\xxx> set "GOPATH=%USERPROFILE%"
- C:\Users\xxx> set "Path=%USERPROFILE%\bin;%Path%"
- C:\Users\xxx> setx GOPATH "%GOPATH%"
- C:\Users\xxx> setx Path "%Path%"
- C:\Users\xxx> mkdir src\github.com\ethereum
- C:\Users\xxx> git clone https://github.com/ethereum/go-ethereum src\github.com\ethereum\go-ethereum
- C:\Users\xxx> cd src\github.com\ethereum\go-ethereum
- C:\Users\xxx> go get -u -v golang.org/x/net/context
- C:\Users\xxx\src\github.com\ethereum\go-ethereum> go install -v ./...
PS:本人配置的时候,不知为何,配置完成后将我原先就有的Python环境完全移除了,也是莫名其妙,这里有Python环境的人要注意下。
Geth:
以开发者的角度,介绍下基本用法:
创建测试用私有链:
1.首先,将自定义的创始区块放入
- C:\Users\XXX:
目录下,创始区块必须是.json文件,文件名可自定,这里设置为piccgenesis.json,文件内容如下:
- {
- "nonce":"0x0000000000000042",
- "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000",
- "difficulty": "0x4000",
- "alloc": {},
- "coinbase":"0x0000000000000000000000000000000000000000",
- "timestamp": "0x00",
- "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
- "extraData": "PICC GenesisBlock",
- "gasLimit":"0xffffffff"
- }
2.初始化一条私有链:
- geth --datadir "%cd%\chain" init piccgenesis.json
- geth --identity "PICCetherum" --rpc --rpccorsdomain "*" --datadir "%cd%\chain" --port "30303" --rpcapi "db,eth,net,web3" --networkid 95518 console
控制台基本操作:
1.查询账户:
- eth.accounts
- personal.newAccount('123456')
- user1 =eth.accounts[0]
- eth.getBalance(user1)
- eth.blockNumber
- miner.start()
- miner.stop()
- personal.unlockAccount(user1, "123456")
- eth.sendTransaction({from: user1, to: user2, value: web3.toWei(3,"ether")})