Geth的安装与配置(Windows版)
目录
1.下载
Go-ethereum,也就是Geth,下载地址为:https://geth.ethereum.org/downloads/
2.安装
windows下正常安装
路径设置为:C:\Users\username\geth\
确认路径:
where geth
C:\Users\tangyao\geth\geth.exe
3.配置Geth私网运行
(1)创建genesis.json
在geth文件夹下创建private_net文件夹用于保存,在private_net下创建genesis.json,代码如下:
{
"config":{
"chainId": 33,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0
},
"nonce": "0x0000000000000033",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gaslimit": "0x8000000",
"difficulty": "0x100",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {}
}
(2)Geth初始化
Windows版:
geth --datadir C:\users\tangyao\geth\private_net init C:\users\tangyao\geth\private_net\genesis.json
(3)启动Geth
geth --networkid "10" --nodiscover --datadir H:\geth\private_net --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" --allow-insecure-unlock --targetgaslimit "20000000" console 2>> H:\geth\private_net\error.log
成功启动:
Welcome to the Geth JavaScript console!
instance: Geth/v1.9.25-stable-e7872729/windows-amd64/go1.15.6
at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: H:\geth\private_net
modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0
rpc:1.0 txpool:1.0 web3:1.0
关闭Geth:> exit
(4)创建账户
启动Geht后,准备密码字符串,使用 personal.newAccount(string) 指令创建账户:
> personal.newAccount("password")
"0xbdcfef6cede1108e7d716c0124755a98b9126657"
> personal.newAccount("password")
"0x25f904a59b41f5448120e0aae576e7575b0a0577"
> personal.newAccount("password")
"0x1f47f0c4d9b259d05697840b979c5789a149c0e7"
其中 “0xbdcfef6cede1108e7d716c0124755a98b9126657” 为生成的外部账户地址, “password” 作为密码解锁该账户,一定要记好。
生成多个账户用于操作使用,记录好相应的地址和密码。
(5)启动自动密码模式
在一些账户操作时,比如转账,需要输入密码验证,比较麻烦。通过配置密码文件,可以启动免输入密码的模式。
配置密码文件: 在private_net文件夹下创建文件名为password.txt的文件,在文件中按顺序分别换行输入各个账户的密码。
进行解锁设置并启动Geth:
geth --networkid "10" --nodiscover --datadir H:\geth\private_net --rpc --rpcaddr "localhost" --rpcport "8545" --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" --allow-insecure-unlock --targetgaslimit "20000000" console 2>> H:\geth\private_net\error.log --unlock 0xbdcfef6cede1108e7d716c0124755a98b9126657,0x25f904a59b41f5448120e0aae576e7575b0a0577,0x1f47f0c4d9b259d05697840b979c5789a149c0e7 --password H:\geth\private_net\password.txt
再进行转账等操作,则无需输入密码了。
4.Geth的简单操作指令
(1)账户操作
- 查询账户:
eth.accounts
> eth.accounts
["0xbdcfef6cede1108e7d716c0124755a98b9126657",
"0x25f904a59b41f5448120e0aae576e7575b0a0577",
"0x1f47f0c4d9b259d05697840b979c5789a149c0e7"]
- 标记账户:
eth.accounts[ ]
> eth.accounts[0]
"0xbdcfef6cede1108e7d716c0124755a98b9126657"
- 确认coinbase账户:
eth.coinbase
> eth.accounts[0]
"0xbdcfef6cede1108e7d716c0124755a98b9126657"
> eth.coinbase
"0xbdcfef6cede1108e7d716c0124755a98b9126657"
- 变更coinbase账户:
miner.setEtherbase(eth.accounts[ ])
> miner.setEtherbase(eth.accounts[1])
true
> eth.coinbase
"0x25f904a59b41f5448120e0aae576e7575b0a0577"
- 查询账户余额:
eth.getBalance(eth.accounts[])
> eth.getBalance(eth.accounts[0])
90000000000000000000
- 默认以ether显示,数值比较大,可通过
web3.fromWei( ,"ether")进行单位转化:
> web3.fromWei(eth.getBalance(eth.accounts[0]),"ether")
105
- 普通转账:
eth.sendTransaction({from:eth.accounts[],to:eth.accounts[],value:web3.toWei(n,"ether")})n为转账数量。返回交易哈希。
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(5,"ether")})
"0x9d04784d1c674628602edeb76798819dba190d9a76b6ca18fd64175de308665d"
(2)区块操作
- 开始挖矿:
miner.start(1)
括号内的数字代表挖矿操作的线程数 - 确认挖矿状态:
eth.mining - 停止挖矿:
miner.stop()
> miner.start(1)
null
> eth.mining
true
> miner.stop()
null
- 查询区块:
eth.getBlock()
以太坊的区块数据结构如下:(创世区块,不包含交易和叔区块)
> eth.getBlock(0)
{
difficulty: 256,
extraData: "0x",
gasLimit: 134217728,
gasUsed: 0,
hash: "0x5704d029fe80f4fb605c0cb5e31d591511f10a46a0cb8166f97d8d559f9bc5b0",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x3333333333333333333333333333333333333333",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000033",
number: 0,
parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 507,
stateRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
timestamp: 0,
totalDifficulty: 256,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}
- 查询交易:
eth.getTransaction()
交易的数据结构如下:
> eth.getTransaction("0x9d04784d1c674628602edeb76798819dba190d9a76b6ca18fd64175de308665d")
{
blockHash: "0xddea639b05f4a3ebac609e436e72f1c7882cf54772e6af086f31b0431d6b63b2",
blockNumber: 63,
from: "0xbdcfef6cede1108e7d716c0124755a98b9126657",
gas: 21000,
gasPrice: 1000000000,
hash: "0x9d04784d1c674628602edeb76798819dba190d9a76b6ca18fd64175de308665d",
input: "0x",
nonce: 0,
r: "0x9b82fc5257f7f67d353870f1ad0150cd94f7ac22112314113507cc63c1a5a5eb",
s: "0x43ae5331e55c82995c67a6ba03d29c0bb0002286025ee8a73530876fc0fdd169",
to: "0x25f904a59b41f5448120e0aae576e7575b0a0577",
transactionIndex: 0,
v: "0x66",
value: 5000000000000000000
}
- 查询交易回执Transaction Receipt:
eth.getTransactionReceipt()
Transaction Receipt数据结构如下:
> eth.getTransactionReceipt("0x9d04784d1c674628602edeb76798819dba190d9a76b6ca18fd64175de308665d")
{
blockHash: "0xddea639b05f4a3ebac609e436e72f1c7882cf54772e6af086f31b0431d6b63b2",
blockNumber: 63,
contractAddress: null,
cumulativeGasUsed: 21000,
from: "0xbdcfef6cede1108e7d716c0124755a98b9126657",
gasUsed: 21000,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0xb2148d8f70560dc109b105c93f3f52914ee27a283dd0e3121dcd7831b54289bb",
to: "0x25f904a59b41f5448120e0aae576e7575b0a0577",
transactionHash: "0x9d04784d1c674628602edeb76798819dba190d9a76b6ca18fd64175de308665d",
transactionIndex: 0
}
版权声明:本文为TANGYAO97原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。