solc编译器需要的cmake版本要大于3.9.0,所以要安装高于这个版本的cmake,安装步骤如下:
wget https://cmake.org/files/v3.9/cmake-3.9.1-Linux-x86_64.tar.gz
tar -zxvf cmake-3.9.1-Linux-x86_64.tar.gz
# 解压出来的包,将其放在 /opt 目录下,其他目录也可以,主要别以后不小心删了
sudo mv cmake-3.13.0-Linux-x86_64 /opt/cmake-3.13.0
# 建立软链接
sudo ln -sf /opt/cmake-3.13.0/bin/* /usr/bin/
# 查看 cmake 版本
cmake --version
编译solc需要依赖z3,所以需要提前安装一下,安装步骤如下:
git clone https://github.com/Z3Prover/z3.git
cd z3
python scripts/mk_make.py
cd build
make
sudo make install
https://sourceforge.net/projects/boost/files/boost/1.74.0/
./bootstrap.sh
./b2
sudo ./b2 --prefix=/usr/local/boost install
vim /etc/environment
向文件中增加如下路径:
CPLUS_INCLUDE_PATH=/usr/local/boost/include
LIBRARY_PATH=/usr/local/boost/lib
sudo npm install -g solc
sudo apt install -y build-essential cmake libboost-all-dev
git clone --recursive https://github.com/ethereum/solidity.git
cd solidity
//external resources
./scripts/install_deps.sh
mkdir build
cd build
cmake .. && make
期间可能还需要安装:
sudo apt-get install ruby-dev
sudo gem install jekyll
{
"config": {
"chainId": 3,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
geth --datadir "./myblockchain" init genesis.json
如下:
初始化结束后,当前目录会新建目录myblockchain,myblockchain目录下多了geth和keystore两个文件夹:
1) geth保存的该链上的区块数据
2)keystore保存该链上的用户信息
目录结构如下:
3. 启动私有链
启动私链:
geth --identity "myethereum" --rpc --rpcaddr "10.10.71.22" --rpcport "8545" --rpccorsdomain "*" --datadir ./myblockchain --rpcapi "db,eth,net,web3,admin,personal" --networkid 666 console --allow-insecure-unlock
这里先介绍一下一些参数:
参数名 | 描述 |
---|---|
identity | 区块链的标识,用于标识目前网络的名字 |
init | 指定创世块文件的位置,并创建创世块 |
datadir | 当前区块链数据存放的位置 |
port | 网络监听端口,默认30303 |
rpc | 启动rpc通信 |
rpcapi | 设置允许rpc的客户端,一般为db,eth,net,web3 |
rpccorsdomain | 指定什么url能连接到你的节点执行rpc定制端任务,如果输入是“*”,则任何url都可以连接到你的rpc实例。 |
rpcaddr | 连接rpc的地址,默认为localhost,为了方便访问建议使用本机IP地址 |
rpcport | 连接rpc的端口,默认为8545 |
networkid | 设置当前区块链的网络ID,用于区分不同的网络,是一个数字,公链为1;这个和创世块文件的chainid没有直接关联 |
console | 启动命令行模式,可以再geth中执行命令 |
屏幕输出:
4. 连接第二个geth终端
为了方便使用,经常要打开多个终端连接到同一个私链节点,可以采用如下命令,其中ipc就是启动私链是输出的“ipc endpoint opened”
geth attach ipc:/home/ss/F/myChain/myblockchain/geth.ipc
5. geth创建账号、启动挖矿、停止挖矿
初始化后系统无账号,需要创建新账号,才能启动挖矿:
eth.accounts
personal.newAccount("12345678");
eth.getBalance("0xe76c73baf75a67dd9637ad2da33d13288d73ee16");
miner.start()
miner.stop()
personal.unlockAccount("0xe76c73baf75a67dd9637ad2da33d13288d73ee16");
交易池状态:
txpool.status
区块总数:
eth.blockNumber
获取交易:
eth.getTransaction("0x5cff7cd36fe841a7eb35ba30f99c8851a4e57715e4b648aeb341c3c2619879cd");
获取第421个区块的信息:
eth.getBlock(421);
连接到其他节点
admin.addPeer();
设置挖矿账户:
miner.setEtherbase(eth.coinbase)
转账交易,要先检索账号:
eth.sendTransaction({
from:"0xe76c73baf75a67dd9637ad2da33d13288d73ee16",to:"0x62232534fda9ffe0c5f32cf6d48568a861065fea",value:web3.toWei(100,"ether")})
var _msg = "helloworld" ;
var helloworldContract = web3.eth.contract([{
"constant":true,"inputs":[],"name":"say","outputs":[{
"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{
"inputs":[{
"name":"_msg","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var helloworld = helloworldContract.new(
_msg,
{
from: web3.eth.accounts[0],
data: '0x608060405234801561001057600080fd5b506040516102a83803806102a8833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100f5565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009157805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100be5782518255916020019190600101906100a3565b5b5090506100cc91906100d0565b5090565b6100f291905b808211156100ee5760008160009055506001016100d6565b5090565b90565b6101a4806101046000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b34801561005257600080fd5b5061005b6100d6565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009b578082015181840152602081019050610080565b50505050905090810190601f1680156100c85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561016e5780601f106101435761010080835404028352916020019161016e565b820191906000526020600020905b81548152906001019060200180831161015157829003601f168201915b50505050509050905600a165627a7a7230582025df814d9752d3868447b99930736a0ca4659b89c7a3bbc54b278cb1a62162490029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
在线编译器:
http://remix.ethereum.org/