概述
API 参考 https://web3js.readthedocs.io/en/v1.4.0/index.html
docker-compose.yml
version: '3.1'
services:
ethereum:
image: ethereum/client-go
restart: always
container_name: ethereum-node
ports:
- 8545:8545
- 30303:30303
volumes:
- ./root:/root
genesis.json
{
"config": {
"chainId": 1024,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0
},
"alloc": {},
"coinbase": "0x0000000000000000000000000000000000000000",
"difficulty": "0x400",
"extraData": "",
"gasLimit": "0x2fefd8",
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x00"
}
进入容器
docker exec -it ethereum-node /bin/sh
cd /root/appeth
geth --nousb --datadir=/root/appeth/data0 init /root/appeth/genesis.json
~/appeth # geth --nousb --datadir=/root/appeth/data0 init /root/appeth/genesis.json
INFO [08-28|19:15:40.044] Maximum peer count ETH=50 LES=0 total=50
INFO [08-28|19:15:40.044] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
WARN [08-28|19:15:40.044] Option nousb is deprecated and USB is deactivated by default. Use --usb to enable
INFO [08-28|19:15:40.049] Set global gas cap cap=50,000,000
INFO [08-28|19:15:40.049] Allocated cache and file handles database=/root/appeth/data0/geth/chaindata cache=16.00MiB handles=16
INFO [08-28|19:15:40.084] Writing custom genesis block
INFO [08-28|19:15:40.084] Persisted trie from memory database nodes=0 size=0.00B time="4.4µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [08-28|19:15:40.085] Successfully wrote genesis state database=chaindata hash=b052b0..1553c1
INFO [08-28|19:15:40.085] Allocated cache and file handles database=/root/appeth/data0/geth/lightchaindata cache=16.00MiB handles=16
INFO [08-28|19:15:40.111] Writing custom genesis block
INFO [08-28|19:15:40.112] Persisted trie from memory database nodes=0 size=0.00B time="8.598µs" gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [08-28|19:15:40.113] Successfully wrote genesis state database=lightchaindata hash=b052b0..1553c1
-
用于存放账户地址
/root/appeth/data0/keystore -
客户端
/root/appeth/data0/geth -
存放区块
chaindata、lightchaindata
-rw-r--r-- 1 root root 0 Aug 28 19:15 LOCK
drwxr-xr-x 2 root root 4096 Aug 28 19:15 chaindata
drwxr-xr-x 2 root root 4096 Aug 28 19:15 lightchaindata
-rw------- 1 root root 64 Aug 28 19:15 nodekey
cd /root/appeth/
进入控制台
geth --nousb --identity "CreationNode" --rpc --rpcport "8548" --rpcapi eth,web3,persional --allow-insecure-unlock --datadir /root/appeth/data0 --port "30304" --nodiscover console
- --nodiscover: 禁用发现
执行后提示信息
INFO [08-28|19:25:36.632] Starting Geth on Ethereum mainnet...
INFO [08-28|19:25:36.632] Bumping default cache on mainnet provided=1024 updated=4096
INFO [08-28|19:25:36.636] Maximum peer count ETH=50 LES=0 total=50
WARN [08-28|19:25:36.636] The flag --rpc is deprecated and will be removed June 2021, please use --http
WARN [08-28|19:25:36.636] The flag --rpcport is deprecated and will be removed June 2021, please use --http.port
WARN [08-28|19:25:36.636] The flag --rpcapi is deprecated and will be removed June 2021, please use --http.api
INFO [08-28|19:25:36.636] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
WARN [08-28|19:25:36.636] Option nousb is deprecated and USB is deactivated by default. Use --usb to enable
WARN [08-28|19:25:36.638] Sanitizing cache to Go's GC limits provided=4096 updated=2657
INFO [08-28|19:25:36.646] Set global gas cap cap=50,000,000
INFO [08-28|19:25:36.646] Allocated trie memory caches clean=398.00MiB dirty=664.00MiB
INFO [08-28|19:25:36.646] Allocated cache and file handles database=/root/appeth/data0/geth/chaindata cache=1.30GiB handles=524,288
INFO [08-28|19:25:36.742] Opened ancient database database=/root/appeth/data0/geth/chaindata/ancient readonly=false
INFO [08-28|19:25:36.744] Initialised chain configuration config="{ChainID: 1024 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: 0, Muir Glacier: <nil>, Berlin: 0, London: <nil>, Engine: unknown}"
INFO [08-28|19:25:36.744] Disk storage enabled for ethash caches dir=/root/appeth/data0/geth/ethash count=3
INFO [08-28|19:25:36.745] Disk storage enabled for ethash DAGs dir=/root/.ethash count=2
INFO [08-28|19:25:36.745] Initialising Ethereum protocol network=1 dbversion=<nil>
INFO [08-28|19:25:36.748] Loaded most recent local header number=0 hash=b052b0..1553c1 td=1024 age=52y4mo3w
INFO [08-28|19:25:36.748] Loaded most recent local full block number=0 hash=b052b0..1553c1 td=1024 age=52y4mo3w
INFO [08-28|19:25:36.748] Loaded most recent local fast block number=0 hash=b052b0..1553c1 td=1024 age=52y4mo3w
WARN [08-28|19:25:36.748] Failed to load snapshot, regenerating err="missing or corrupted snapshot"
INFO [08-28|19:25:36.750] Rebuilding state snapshot
INFO [08-28|19:25:36.752] Resuming state snapshot generation root=56e81f..63b421 accounts=0 slots=0 storage=0.00B elapsed=2.169ms
INFO [08-28|19:25:36.761] Generated state snapshot accounts=0 slots=0 storage=0.00B elapsed=11.136ms
INFO [08-28|19:25:36.763] Regenerated local transaction journal transactions=0 accounts=0
INFO [08-28|19:25:36.764] Gasprice oracle is ignoring threshold set threshold=2
WARN [08-28|19:25:36.765] Error reading unclean shutdown markers error="leveldb: not found"
INFO [08-28|19:25:36.765] Starting peer-to-peer node instance=Geth/CreationNode/v1.10.9-unstable-ffae2043/linux-amd64/go1.17
INFO [08-28|19:25:36.810] New local node record seq=1 id=ffd129a05e25fb13 ip=127.0.0.1 udp=0 tcp=30304
INFO [08-28|19:25:36.810] Started P2P networking self="enode://55b9fe76a49076e23b96fadd242a171d04cb195767ad02c69e7011992a2904153efcb327b09dc40c928aa3fd29366c4b348690d45e0f87d469dfb3808fa41672@127.0.0.1:30304?discport=0"
INFO [08-28|19:25:36.813] IPC endpoint opened url=/root/appeth/data0/geth.ipc
ERROR[08-28|19:25:36.813] Unavailable modules in HTTP API list unavailable=[persional] available="[admin debug web3 eth txpool personal ethash miner net]"
INFO [08-28|19:25:36.814] HTTP server started endpoint=127.0.0.1:8548 prefix= cors= vhosts=localhost
WARN [08-28|19:25:37.005] Served eth_coinbase reqid=3 t="41.701µs" err="etherbase must be explicitly specified"
Welcome to the Geth JavaScript console!
instance: Geth/CreationNode/v1.10.9-unstable-ffae2043/linux-amd64/go1.17
at block: 0 (Thu Jan 01 1970 00:00:00 GMT+0000 (UTC))
datadir: /root/appeth/data0
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
>
创建账户 A
personal.newAccount()
提示信息
Passphrase: 123456
Repeat passphrase: 123456
INFO [08-28|19:27:18.434] Your new key was generated address=0xb68edb8cc590706199c1ef498d53a235e985c2ee
WARN [08-28|19:27:18.435] Please backup your key file! path=/root/appeth/data0/keystore/UTC--2021-08-28T19-27-15.070526306Z--835bd1b38711cf971496cbad059edd035782c5f7
WARN [08-28|19:27:18.435] Please remember your password!
"0xb68edb8cc590706199c1ef498d53a235e985c2ee"
地址: 0xb68edb8cc590706199c1ef498d53a235e985c2ee
密码: 123456
查看余额
eth.getBalance(eth.accounts[0])
eth.getBalance("0xb68edb8cc590706199c1ef498d53a235e985c2ee")
开始挖矿
miner.start(1)
停止挖矿
miner.stop()
提示
INFO [08-28|19:32:30.291] Updated mining threads threads=1
INFO [08-28|19:32:30.291] Transaction pool price threshold updated price=1,000,000,000
INFO [08-28|19:32:30.291] Etherbase automatically configured address=0x835Bd1B38711Cf971496CbAD059eDd035782C5F7
INFO [08-28|19:32:30.292] Commit new mining work number=1 sealhash=8e2999..029aec uncles=0 txs=0 gas=0 fees=0 elapsed="887.63µs"
null
> INFO [08-28|19:32:40.636] Generating DAG in progress epoch=0 percentage=0 elapsed=9.033s
INFO [08-28|19:32:46.875] Generating DAG in progress epoch=0 percentage=1 elapsed=15.272s
INFO [08-28|19:32:54.913] Generating DAG in progress epoch=0 percentage=2 elapsed=23.310s
INFO [08-28|19:33:04.049] Generating DAG in progress epoch=0 percentage=3 elapsed=32.446s
INFO [08-28|19:33:10.989] Generating DAG in progress epoch=0 percentage=4 elapsed=39.386s
...
INFO [08-28|19:44:26.183] Generating DAG in progress epoch=0 percentage=96 elapsed=11m54.579s
INFO [08-28|19:44:33.496] Generating DAG in progress epoch=0 percentage=97 elapsed=12m1.892s
INFO [08-28|19:44:40.546] Generating DAG in progress epoch=0 percentage=98 elapsed=12m8.943s
INFO [08-28|19:44:48.166] Generating DAG in progress epoch=0 percentage=99 elapsed=12m16.563s
INFO [08-28|19:44:48.172] Generated ethash verification cache epoch=0 elapsed=12m16.569s
INFO [08-28|19:44:54.469] Successfully sealed new block number=1 sealhash=8e2999..029aec hash=d2c745..3d3ce1 elapsed=12m24.176s
INFO [08-28|19:44:54.469] mined potential block number=1 hash=d2c745..3d3ce1
INFO [08-28|19:44:54.486] Commit new mining work number=2 sealhash=426c5c..5e7545 uncles=0 txs=0 gas=0 fees=0 elapsed="179.142µs"
INFO [08-28|19:45:03.064] Generating DAG in progress epoch=1 percentage=0 elapsed=12.422s
INFO [08-28|19:45:08.158] Successfully sealed new block number=2 sealhash=426c5c..5e7545 hash=7ebe37..d21e48 elapsed=13.672s
INFO [08-28|19:45:08.159] mined potential block number=2 hash=7ebe37..d21e48
INFO [08-28|19:45:08.178] Commit new mining work number=3 sealhash=d0afc0..b92919 uncles=0 txs=0 gas=0 fees=0 elapsed="200.047µs"
INFO [08-28|19:45:09.647] Successfully sealed new block number=3 sealhash=d0afc0..b92919 hash=c4941b..e8bebe elapsed=1.469s
INFO [08-28|19:45:09.664] mined potential block number=3 hash=c4941b..e8bebe
INFO [08-28|19:45:09.665] Commit new mining work number=4 sealhash=a71824..edc569 uncles=0 txs=0 gas=0 fees=0 elapsed=17.465ms
INFO [08-28|19:45:12.916] Generating DAG in progress epoch=1 percentage=1 elapsed=22.274s
INFO [08-28|19:45:18.990] Successfully sealed new block number=4 sealhash=a71824..edc569 hash=37ba16..5b6f60 elapsed=9.325s
INFO [08-28|19:45:18.997] mined potential block number=4 hash=37ba16..5b6f60
INFO [08-28|19:45:18.997] Commit new mining work number=5 sealhash=185dd1..7cf0ab uncles=0 txs=0 gas=0 fees=0 elapsed="301.637µs"
- number: 区块的数量
换算单位
web3.fromWei()
# 显示
"456"
交易以太坊
创建账户 B
personal.newAccount()
账户: 0xc2b037baad241b6b5fa0cf6a87f47cce7152bd1d
密码: 123456
解锁账户
personal.unlockAccount(eth.accounts[0])
# 显示
Unlock account 0xb68edb8cc590706199c1ef498d53a235e985c2ee
Passphrase:
true
发送交易
amount = web3.toWei(5,'ether')
eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount})
# 显示
INFO [08-28|22:36:47.905] Setting new local account address=0xB68Edb8cC590706199c1ef498D53a235E985c2ee
INFO [08-28|22:36:47.905] Submitted transaction hash=0x6e2c4ad74a9aed0070a506f4ee04bcb7e3740cf05d5d91c8646e986b4cfa2a19 from=0xB68Edb8cC590706199c1ef498D53a235E985c2ee nonce=0 recipient=0xc2B037BaaD241B6B5fa0cF6a87f47cce7152bd1d value=5,000,000,000,000,000,000
"0x6e2c4ad74a9aed0070a506f4ee04bcb7e3740cf05d5d91c8646e986b4cfa2a19"
# 查看未确认交易
txpool.status
# 显示
{
pending: 1,
queued: 0
}
# 等待确认交易
eth.getBlock("pending", true).transactions
[{
blockHash: "0xe6f043176c1c8dd9cce8ebd730012bcabedda742cca79ffa9917432fee4b0399",
blockNumber: 5,
from: "0xb68edb8cc590706199c1ef498d53a235e985c2ee",
gas: 21000,
gasPrice: 1000000000,
hash: "0x6e2c4ad74a9aed0070a506f4ee04bcb7e3740cf05d5d91c8646e986b4cfa2a19",
input: "0x",
nonce: 0,
r: "0xc77252d1fafca68fcf54a1e5b90c4c95fdabec334f866a32619f70ba2c9ac598",
s: "0x325a448dcb498f6c2fd10a53825a457f6ebc528819dcd296b0ce20c39ac8ed69",
to: "0xc2b037baad241b6b5fa0cf6a87f47cce7152bd1d",
transactionIndex: 0,
type: "0x0",
v: "0x823",
# 5个以太坊 从 s: 转到 to:
value: 5000000000000000000
}]
确认交易
miner.start(1);admin.sleepBlocks(1);miner.stop();
> miner.start(1);admin.sleepBlocks(1);miner.stop();
INFO [08-28|22:38:19.503] Updated mining threads threads=1
INFO [08-28|22:38:19.503] Transaction pool price threshold updated price=1,000,000,000
INFO [08-28|22:38:19.503] Commit new mining work number=5 sealhash=6d86f1..78ad6a uncles=0 txs=0 gas=0 fees=0 elapsed="319.556µs"
INFO [08-28|22:38:19.504] Commit new mining work number=5 sealhash=d84f6a..8f0b8f uncles=0 txs=1 gas=21000 fees=2.1e-05 elapsed="929.701µs"
INFO [08-28|22:38:36.721] Generating DAG in progress epoch=1 percentage=0 elapsed=15.738s
INFO [08-28|22:38:48.064] Generating DAG in progress epoch=1 percentage=1 elapsed=27.080s
INFO [08-28|22:39:00.637] Generating DAG in progress epoch=1 percentage=2 elapsed=39.653s
INFO [08-28|22:39:15.554] Generating DAG in progress epoch=1 percentage=3 elapsed=54.570s
INFO [08-28|22:39:18.625] Successfully sealed new block number=5 sealhash=d84f6a..8f0b8f hash=878de6..a6c02c elapsed=59.121s
INFO [08-28|22:39:18.625] mined potential block number=5 hash=878de6..a6c02c
INFO [08-28|22:39:18.626] Commit new mining work number=6 sealhash=e17a7a..ab82ee uncles=0 txs=0 gas=0 fees=0 elapsed="434.019µs"
INFO [08-28|22:39:18.635] Commit new mining work number=6 sealhash=e17a7a..ab82ee uncles=0 txs=0 gas=0 fees=0 elapsed=9.723ms
null
> eth.blockNumber
229
> eth.getBalance(eth.accounts[0])
5
> eth.getBalance(eth.accounts[1])
5