以太坊 geth 命令相关


创建测试用私有链:

1.首先,将自定义的创始区块放入

目录下,创始区块必须是.json文件,文件名可自定,这里设置为piccgenesis.json,文件内容如下:
{
  "config": {
        "chainId": 10,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x02000000",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}  
2.初始化一条私有链:

geth --datadir "%cd%\chain" init piccgenesis.json  

3.运行并进入该私有链的控制台:

geth --identity "PICCetherum" --rpc --rpccorsdomain "*" --datadir "chain" --port "30303"  --rpcapi "db,eth,net,web3" --networkid 95518 console  

geth -datadir "chain" console

geth --identity "PICCetherum" --rpc --rpccorsdomain "*" --datadir "chain" --port "30303"  --rpcapi "db,eth,net,web3" --networkid 95518 console


私有链1
geth --datadir ./data/00 --networkid 314590 --ipcdisable --port 61910 --rpcport 8200 console
私有链2
先初始化
geth --datadir ./data/01 init ./piccgenesis.json 
端口不能重复(在新节点中添加其他节点,也可在新建节点之后,在节点中添加其他节点)
geth --datadir ./data/01 --networkid 314590 --ipcdisable --port 61911 --rpcport 8101 --bootnodes "enode://1aa0cd7beec412fdaf9be8577df9e6016e5315e7908573cd1b18cf88a909feedfd406b0e95ffff89b93ada317d5e2812ecf351f24408ca770fd73b7d7e883875@[127.0.0.1]:61910" console


同步公共网中的完整区块链
geth --datadir "dir"


控制台基本操作:

1.查询账户:

eth.accounts  

2.创建账户,密码为“123456”:

personal.newAccount('123456') 
 
3.账户赋值给变量:

user1 =eth.accounts[0]  

4.查询账户余额:

eth.getBalance(user1)  
web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")

5.显示当前区块:

eth.blockNumber  

6.开始挖矿(默认第一个账户得到挖矿收益):

miner.start()   或者  miner.start(1) 1是挖矿使用的线程数 默认是8

7.停止挖矿:

miner.stop()  

8.解锁账户(获得账户使用权):

personal.unlockAccount(user1, "123456")  

9.user1转账3以太币给user2:

eth.sendTransaction({from: user1, to: user2, value: web3.toWei(3,"ether")}) 

查看帐户余额的方法如下:

eth.getBalance("0xbe323cc4fde114269a9513a27d3e985f82b9e25d")

eth.getBalance("0x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70")


每次记一长串的地址很麻烦,我们可以通过设置变量来acc0表示帐户10xbe323cc4fde114269a9513a27d3e985f82b9e25d,acc1表示帐户20x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70.

> acc0 = web3.eth.accounts[0]
"0xbe323cc4fde114269a9513a27d3e985f82b9e25d"
> acc1 = web3.eth.accounts[1]
"0x3b0ec02b4193d14abdc9cc5b264b5e3f39624d70"
> web3.eth.getBalance(acc0)
1.245e+21
> web3.eth.getBalance(acc1)
0

## 使用这个方法可以查看格式化的以太币
> web3.fromWei(web3.eth.getBalance(acc0))
1234


联盟链
在私有链中生成多个节点
创世节点
{
 "alloc": {},
 "config": {
   "chainID": 72,
   "homesteadBlock": 0,
   "eip155Block": 0,
   "eip158Block": 0
 },
 "nonce": "0x0000000000000000",
 "difficulty": "0x4000",
 "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
 "coinbase": "0x0000000000000000000000000000000000000000",
 "timestamp": "0x00",
 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
 "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
 "gasLimit": "0xffffffff"
}

每生成一个新的节点首先都要在新的目录下初始化这个创世节点
step1:geth --datadir data-init1 init genesis.json
step2:geth --datadir data-init1 --port 30306 --networkid 88 --nodiscover --rpcapi "db,eth,net,web3,personal,miner" --rpc console
step3:admin.addPeer()

--rpc 开启rpc服务
--rpcapi "db,eth,net,web3,personal,miner" 制定rpc可用的api
--rpcport 8555 设置rpc端口

注释:
1.开启控制台时候,networkid需要是一致的,端口号不可以重复,不填写的话会有一个默认的端口号
2.nodiscover 此参数确保geth不去寻找peers,主要是为了严格控制联盟链连入的节点
3.交易之后需要链中的其他或本节点去挖矿确认交易,确认后的交易才会生效


命令:
personal.listAccounts  显示当前节点下所有的账户
personal.listWallets   显示当前节点下所有账户的详细信息
admin.peers   显示整个链的节点信息
admin.nodeInfo.enode   显示本节点的enode信息
admin.addPeer  添加其他节点的链接
eth.sendTransaction({from: user1, to: user2, value: web3.toWei(3,"ether")})  交易
net.peerCount  显示节点数
web3.eth.getTransaction(transactionHash [, callback])  通过交易hash,得到交易的具体内容

节点建好之后,进入到节点控制到,添加其他节点到该节点
admin.addPeer("enode://0ae884ee151a1e4fba052ee017c13999e92c20ae1f0539519ccbaa7542c24c0a1cb0c2c50061777ca2cddd5522fb59fa8e6dc03315ab795a96561f2e5c744ef9@127.0.0.1:30303?discport=0")
> INFO [04-28|16:48:38] Submitted transaction                    fullhash=0x2e88ea5cd27171c5d9c76971a85b26928149ec700a488d90676c63348c4ea165 recipient=0x8CcAC9A996281af1c2f1512485Ce852319E16cbE

查看交易细节,值是交易的hash值
eth.getTransactionReceipt("0xee74bcb4461c9712ec9aca96a5a3a4c3c64be1213854d519fc8e5432b554f7a1")

猜你喜欢

转载自blog.csdn.net/mashengjun1989/article/details/81697651
今日推荐