安装nodejs
安装solidity编译器:
npm install -g solc
安装web3:
npm install -g web3
安装 truffle 框架
npm install -g truffle
安装 webpack 框架
npm install -g webpack
1、初始化私链节点
创世块创建:
"D:\geth.exe" init private.json --datadir ./priv-data/
初始化文件参考的这个文章
{
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x40000",
"extraData" : "",
"gasLimit" : "0xffffffff",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": { }
}
但报错了,信息如下:
D:\data\eth-geth-dev>"D:\geth.exe" init private.json --datadir ./priv-data/
INFO [05-25|23:23:43.478] Maximum peer count ETH=50 LES=0
total=50
INFO [05-25|23:23:43.497] Set global gas cap cap=50,000,00
0
INFO [05-25|23:23:43.500] Allocated cache and file handles database=D:\data\eth-geth-dev\priv-data\geth\chaindata cache=16.00MiB handles=16
INFO [05-25|23:23:43.513] Writing custom genesis block
INFO [05-25|23:23:43.516] Persisted trie from memory database nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
Fatal: Failed to write genesis block: unsupported fork ordering: eip150Block not enabled, but eip155Block enabled at 0
在这儿找到了方案,于是修改了创世文件private.json :
{
"config": {
"chainId": 65535,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x40000",
"gasLimit": "0xffffffff",
"nonce": "0x0000000000000042",
"timestamp": "0x00",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"alloc": {
"0000000000000000000000000000000000000088": {
"balance": "0x2000"
}
}
}
重新初始化:
"D:\geth.exe" init private.json --datadir ./priv-data/
D:\data\eth-geth-dev>"D:\geth.exe" init private.json --datadir ./priv-data/
INFO [05-25|23:37:29.351] Maximum peer count ETH=50 LES=0 total=50
INFO [05-25|23:37:29.369] Set global gas cap cap=50,000,000
INFO [05-25|23:37:29.372] Allocated cache and file handles database=D:\data\eth-geth-dev\priv-data\geth\chaindata cache=16.00MiB handles=16
INFO [05-25|23:37:29.387] Writing custom genesis block
INFO [05-25|23:37:29.391] Persisted trie from memory database nodes=1 size=141.00B time=1ms gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [05-25|23:37:29.400] Successfully wrote genesis state database=chaindata hash=8bdd67..7c2cf0
INFO [05-25|23:37:29.404] Allocated cache and file handles database=D:\data\eth-geth-dev\priv-data\geth\lightchaindata cache=16.00MiB handles=16
INFO [05-25|23:37:29.418] Writing custom genesis block
INFO [05-25|23:37:29.420] Persisted trie from memory database nodes=1 size=141.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [05-25|23:37:29.426] Successfully wrote genesis state database=lightchaindata hash=8bdd67..7c2cf0
编写init.cmd,内容为:
"D:\geth.exe" init private.json --datadir ./priv-data/
priv-data内容如下:
├───geth
│ │ LOCK
│ │ nodekey
│ │
│ ├───chaindata
│ │ 000001.log
│ │ CURRENT
│ │ LOCK
│ │ LOG
│ │ MANIFEST-000000
│ │
│ └───lightchaindata
│ 000001.log
│ CURRENT
│ LOCK
│ LOG
│ MANIFEST-000000
│
└───keystore
有两个子目录:geth和keystore。其中geth目录保存了同步区块链以及相关的数据,keystore目录保存了账户文件。由于私有链刚创建,还没有创建账户,所以keystore目录为空
2、启动私链节点:
按照文章里提供的配置,如下:
geth --rpc --rpcaddr 0.0.0.0 --rpccorsdomain "*" --datadir ./priv-data/ --networkid 3832465536 --rpcapi "eth,net,admin,personal,miner,txpool,web3" console 2>>geth.log
但无法正常启动,提示无--rpc选项。
我的geth版本是: geth version
Geth
Version: 1.10.16-stable
Git Commit: 20356e57b119b4e70ce47665a71964434e15200d
Git Commit Date: 20220215
Architecture: amd64
Go Version: go1.17.5
Operating System: windows
GOPATH=D:\go_path
GOROOT=D:\go
"D:\Program Files\ethereum\geth.exe" --http --http.addr 0.0.0.0 --http.port 58545 --http.api 'eth,net,web3,admin,personal,debug' --http.corsdomain "*" --http.vhosts "*" --datadir ./priv-data/ --networkid 65535 --ws --ws.addr 0.0.0.0 --ws.port 58546 --ws.api 'eth,net,web3' --graphql --graphql.corsdomain "*" --graphql.vhosts "*" --port 50303 console 2>>geth.log
写入到console.cmd作为启动脚本,启动之后,结果如下:
D:\data\eth-geth-dev>"D:\geth.exe" --http --http.addr 0.0.0.0 --http.port 58545 --http.api 'eth,net,web3,admin,personal,debug' --http.corsdomain "*" --http.vhosts "*" --datadir ./priv-data/ --networkid 65535 --ws --ws.addr 0.0.0.0 --ws.port 58546 --ws.api 'eth,net,web3' --graphql --graphql.corsdomain "*" --graphql.vhosts "*" --port 50303 console 2>>geth.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.10.16-stable-20356e57/windows-amd64/go1.17.5 at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
datadir: D:\data\eth-geth-dev\priv-data
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
To exit, press ctrl-d or type exit
> eth.accounts
[]
帐户管理--》查看账户
创建账户:
在 geth 控制台,使用 personal 对象的 newAccount()方法创建一个新账户,参数为你自己选择的密码:
> personal.newAccount('12345678987654321')
"0x7ee41e6b832058e70c1ee714ed1e7416170d63bd"
输出就是新创建的账户地址(公钥), 你的输出丌会和上面的示例相同。 geth 会保存到数据目录下的 keystore 文件中。密码要自己记住,以后还需要用到。
> eth.accounts
["0x7ee41e6b832058e70c1ee714ed1e7416170d63bd"]
查询账户余额:
在 geth 控制台,使用 personal 对象的 getBalance()方法获取指定账户的余额,参数为账户地址:
> eth.getBalance(eth.accounts[0])
0
或者直接输入账户地址:
> eth.getBalance("0x7ee41e6b832058e70c1ee714ed1e7416170d63bd")
0
新创建的账户,余额果然为 0
挖矿操作:
没钱的账户什么也干不了,需要挖矿来挣点钱。
在 geth 控制台执行 miner 对象的 start()方法来启动挖矿:
等几分钟以后,检查账户余额:
> eth.getBalance(eth.accounts[0])
12000000000000000000
执行 miner 对象的 stop()方法停止挖矿:
> miner.stop()
null
> eth.accounts[0]
"0x7ee41e6b832058e70c1ee714ed1e7416170d63bd"
> main_addr=eth.accounts[0]
"0x7ee41e6b832058e70c1ee714ed1e7416170d63bd"
> web3.fromWei(eth.getBalance(main_addr),"ether")
12
attach到js终端:
D:\>"d:\geth.exe" attach http://localhost:58545
Welcome to the Geth JavaScript console!
modules: admin:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0
To exit, press ctrl-d or type exit
>
解锁账户:
在部署合约时需要一个解锁的账户。在geth控制台使用personal对象的unlockAccount()
方法来解锁指定的账户,参数为账户地址和账户密码(在创建账户时指定的那个密码):
> eth.unlockAccount(eth.accounts[0],'12345678987654321')
true
但我执行时报错了:
> eth.unlockAccount(main_addr,"12345678987654321")
TypeError: Object has no member 'unlockAccount'
at <eval>:1:18(5)
使用web3.personal.unlockAccount也报错了,
> web3.personal.unlockAccount("0x7ee41e6b832058e70c1ee714ed1e7416170d63bd", "12345678987654321", 600).then(console.log('Account unlocked!'));
GoError: Error: account unlock with HTTP access is forbidden at web3.js:6365:37(
47)
at github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1 (nat
ive)
at <eval>:1:96(6)
解决方法在
这里,需要增加
--allow-insecure-unlock重启geth>web3.personal.unlockAccount("0x7ee41e6b832058e70c1ee714ed1e7416170d63bd", "12345678987654321", 600)
true
以太坊的javascript控制台中内置了一些以太坊交互对象:
eth:提供了操作区块链相关的方法
net:提供了查看p2p网络状态的方法
admin:提供了管理节点相关的方法
miner:提供启动和停止挖矿的方法
personal:提供了管理账户的方法
txpool:提供了查看交易内存池的方法
web3:除了包含以上对象中有的方法,还包含了一些单位换算的方法