本文尝试教程序员快速在centos环境,对Ethereum有个快速了解,知道一些Ethereum的概念。
搭建Ethereum私有网络创建账号挖挖矿,获得金币来个转账
搭建Ethereum私有网络
执行如下命令安装程序:
# 安装go
yum install go
# 任意目录,建议$HOME目录,下载ethereum代码
git clone https://github.com/ethereum/go-ethereum
# 生成编译文件
make geth
# 查看文件
cd go-ethereum/build
ls -lh bin/geth # <---- 这个就是go语言实现的ethereum 节点和client程序
构建创始区块:
mkdir ~/ethereumConfig
cd ~/ethereumConfig
vi genesis.json
# 文件里面放入该内容
{
"config": {
"chainId": 33,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000033",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x8000000",
"difficulty": "0x100",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
}
}
# 创建区块链目录
mkdir ~/ethereumData
# 执行初始化(跳到前面的go-ethereum的build目录)
bin/geth init ~/ethereumConfig/genesis.json --datadir=~/ethereumData/
创建账户
这个时候,我们启动私有网络,这样避免公有网络数据节点同步(同步很耗时)。
# 创建账户
bin/geth --datadir=~/ethereumData/ account new
# 根据提示输入密码,然后获得一个Address,即账户
# 可以操作2次,获得2个账户
挖挖矿,获得金币
进入控制台,使用控制台命令继续玩玩。
# 进入控制台
bin/geth --datadir=~/ethereumData/ --networkid 65535 --nodiscover console
# 以下都在console里面执行,console命令提示符 >
# 查看账户
> personal
# 你会看到2个账户,并且状态是locked的
# 激活账户吧: address是前面创建账户时候得到的Address信息,用单引号括注;password:密码
# duration:激活时间,暂时就输入30000吧。对刚才2个账户都运行下激活
> personal.unlockAccount(address,password,duration)
# 刚才2个账户都运行下激活,再次查看状态
> personal
# 以下是我电脑返回信息
{
listAccounts: ["0x4b6875499547a6fcb3db46a5a22e56eb76e03235", "0xfab3c694104f07249810112201bb9ff6c6454e9d"],
listWallets: [{
accounts: [{...}],
status: "Unlocked",
url: "keystore:///root/data/keystore/UTC--2018-07-07T09-52-49.108160985Z--4b6875499547a6fcb3db46a5a22e56eb76e03235"
}, {
accounts: [{...}],
status: "Unlocked",
url: "keystore:///root/data/keystore/UTC--2018-07-07T09-53-07.947653053Z--fab3c694104f07249810112201bb9ff6c6454e9d"
}],
deriveAccount: function(),
ecRecover: function(),
getListAccounts: function(callback),
getListWallets: function(callback),
importRawKey: function(),
lockAccount: function(),
newAccount: function github.com/ethereum/go-ethereum/console.(*bridge).NewAccount-fm(),
openWallet: function github.com/ethereum/go-ethereum/console.(*bridge).OpenWallet-fm(),
sendTransaction: function(),
sign: function github.com/ethereum/go-ethereum/console.(*bridge).Sign-fm(),
signTransaction: function(),
unlockAccount: function github.com/ethereum/go-ethereum/console.(*bridge).UnlockAccount-fm()
}
查看余额
# 继续在console里面创建一个函数
function checkAllBalances() {
var totalBal = 0;
for (var acctNum in eth.accounts) {
var acct = eth.accounts[acctNum];
var acctBal = web3.fromWei(eth.getBalance(acct), "ether");
totalBal += parseFloat(acctBal);
console.log(" eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");
}
console.log(" Total balance: " + totalBal + " ether");
};
# 运行刚才函数
checkAllBalances()
# 发现账户没币
挖矿挣钱
# 继续在console里面操作
> miner.start()
# 可以看到各种打印
INFO [07-07|18:28:28.581] Commit new mining work number=65 txs=0 uncles=0 elapsed=100.609µs
INFO [07-07|18:28:30.972] Successfully sealed new block number=65 hash=2a3b00…c14f64
INFO [07-07|18:28:30.973] block reached canonical chain number=60 hash=724880…ee6a9c
INFO [07-07|18:28:30.973] mined potential block number=65 hash=2a3b00…c14f64
INFO [07-07|18:28:30.973] Commit new mining work number=66 txs=0 uncles=0 elapsed=135.417µs
INFO [07-07|18:28:34.340] Generating DAG in progress epoch=1 percentage=46 elapsed=7m17.985s
INFO [07-07|18:28:42.762] Generating DAG in progress epoch=1 percentage=47 elapsed=7m26.407s
INFO [07-07|18:28:47.234] Successfully sealed new block number=66 hash=f4d928…3b61c9
INFO [07-07|18:28:47.234] block reached canonical chain number=61 hash=f40be0…2390d8
INFO [07-07|18:28:47.234] mined potential block number=66 hash=f4d928…3b61c9
INFO [07-07|18:28:47.248] Commit new mining work number=67 txs=0 uncles=0 elapsed=174.488µs
INFO [07-07|18:28:48.676] Successfully sealed new block number=67 hash=2f8968…497bb2
INFO [07-07|18:28:48.676] block reached canonical chain number=62 hash=9bd350…943bda
INFO [07-07|18:28:48.676] mined potential block number=67 hash=2f8968…497bb2
INFO [07-07|18:28:48.676] Commit new mining work number=68 txs=0 uncles=0 elapsed=109.854µs
# 当你想停止时候,输入:
> miner.stop()
# 一般需要等待几分钟,就真的停止了
# 查看下余额
> checkAllBalances()
eth.accounts[0]: 0x4b6875499547a6fcb3db46a5a22e56eb76e03235 balance: 390 ether
eth.accounts[1]: 0xfab3c694104f07249810112201bb9ff6c6454e9d balance: 0 ether
Total balance: 390 ether
undefined
嗯,有钱了。转账吧!
来个转账
# 继续刚才console里面执行
eth.sendTransaction({from:0x4b6875499547a6fcb3db46a5a22e56eb76e03235,to:0xfab3c694104f07249810112201bb9ff6c6454e9d,value:web3.toWei(1,ether)})
INFO [07-07|18:45:49.378] Submitted transaction fullhash=0x88452a0de99f849cf31467df39a96878c7061fe69e3c369a37c20c09a0bfcac7 recipient=0xFaB3c694104f07249810112201bb9FF6c6454e9d
"0x88452a0de99f849cf31467df39a96878c7061fe69e3c369a37c20c09a0bfcac7"
>
>
> checkAllBalances()
eth.accounts[0]: 0x4b6875499547a6fcb3db46a5a22e56eb76e03235 balance: 390 ether
eth.accounts[1]: 0xfab3c694104f07249810112201bb9ff6c6454e9d balance: 0 ether
Total balance: 390 ether
undefined
嗯,钱好像没有出去。余额没有变动。挖矿。。。。让矿工创建区块!
# 继续刚才console里面执行
> miner.start()
# 等待几分钟
> miner.stop()
> checkAllBalances()
eth.accounts[0]: 0x4b6875499547a6fcb3db46a5a22e56eb76e03235 balance: 564 ether
eth.accounts[1]: 0xfab3c694104f07249810112201bb9ff6c6454e9d balance: 1 ether
Total balance: 565 ether
undefined
此时,第二个账户收到币了。同时账户1因为挖矿,又多了很多币。
总结
Ethereum跑起来:下载,安装,创始块,初始化
创建账户:geth account new命令
激活账户:geth console里面执行personal.unlockAccount命令
挖矿获得币:
转账:帐需要被miner(矿工)记录下来,放入区块(最终形成区块链)
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
相关标签: