以太坊入门一 常用语句

区块链技术从1.0时代以比特币为代表的数字化支付;到2.0时代以太坊为代表,数字支付和智能合约相结合;至今3.0时代,超出金融领域,为各行各业提出去中心化解决方法。

国家也提出以联盟链为发展方向进军区块链世界。

区块链所需要的环境配置好以后,接下来内容均为个人开发记录,仅供参考。(geth 客户端、web3j.py 交互、python开发使用)

初始化创世区块

 1 {
 2   "config": {
 3     "chainId": 666,
 4     "homesteadBlock": 0,
 5     "eip150Block": 0,
 6     "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
 7     "eip155Block": 0,
 8     "eip158Block": 0,
 9     "byzantiumBlock": 0,
10     "constantinopleBlock": 0,
11     "petersburgBlock": 0,
12     "istanbulBlock": 0,
13     "ethash": {}
14   },
15   "nonce": "0x0",
16   "timestamp": "0x5ddf8f3e",
17   "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
18   "gasLimit": "0x47b760",
19   "difficulty": "0x00002",
20   "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
21   "coinbase": "0x0000000000000000000000000000000000000000",
22   "alloc": { },
23   "number": "0x0",
24   "gasUsed": "0x0",
25   "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
26 }
genesis

初始化区块

geth --datadir ".\db" init .\genesis.json   #  datadir 指定节点数据存放目录

出现下面提示即表示成功:

INFO [03-02|17:47:24.159] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-02|17:47:24.360] Allocated cache and file handles         database=D:\\eth-test\\db\\geth\\chaindata cache=16.00MiB handles=16
INFO [03-02|17:47:24.388] Writing custom genesis block
INFO [03-02|17:47:24.392] Persisted trie from memory database      nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-02|17:47:24.419] Successfully wrote genesis state         database=chaindata                         hash=d3d6bb…c5304a
INFO [03-02|17:47:24.426] Allocated cache and file handles         database=D:\\eth-test\\db\\geth\\lightchaindata cache=16.00MiB handles=16
INFO [03-02|17:47:24.442] Writing custom genesis block
INFO [03-02|17:47:24.445] Persisted trie from memory database      nodes=0 size=0.00B time=0s gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [03-02|17:47:24.468] Successfully wrote genesis state         database=lightchaindata                         hash=d3d6bb…c5304a
View Code

启动节点

geth --datadir ./db/ --rpc --rpcapi "eth,net,web3,personal,admin,txpool,debug,miner" --nodiscover console --allow-insecure-unlock 
# console 表示启动客户端后 随后进入 web3.js 控制台
# --allow-insecure-unlock 表示可以解锁账户(转账方必须进行解锁才可以)

提示内容如下:

INFO [03-02|17:49:35.738] Bumping default cache on mainnet         provided=1024 updated=4096
WARN [03-02|17:49:35.757] Sanitizing cache to Go's GC limits       provided=4096 updated=2704
INFO [03-02|17:49:35.765] Maximum peer count                       ETH=50 LES=0 total=50
INFO [03-02|17:49:35.911] Starting peer-to-peer node               instance=Geth/v1.9.11-stable-6a62fe39/windows-amd64/go1.13.8
INFO [03-02|17:49:35.917] Allocated trie memory caches             clean=676.00MiB dirty=676.00MiB
INFO [03-02|17:49:35.923] Allocated cache and file handles         database=D:\\eth-test\\db\\geth\\chaindata cache=1.32GiB handles=8192
INFO [03-02|17:49:35.960] Opened ancient database                  database=D:\\eth-test\\db\\geth\\chaindata\\ancient
INFO [03-02|17:49:35.967] Initialised chain configuration          config="{ChainID: 666 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: 0, Muir Glacier: <nil>, Engine: ethash}"
INFO [03-02|17:49:35.980] Disk storage enabled for ethash caches   dir=D:\\eth-test\\db\\geth\\ethash count=3
INFO [03-02|17:49:35.986] Disk storage enabled for ethash DAGs     dir=C:\\Users\\Administrator\\AppData\\Local\\Ethash count=2
INFO [03-02|17:49:35.994] Initialising Ethereum protocol           versions="[65 64 63]" network=1 dbversion=<nil>
WARN [03-02|17:49:36.001] Upgrade blockchain database version      from=<nil> to=7
INFO [03-02|17:49:36.007] Loaded most recent local header          number=0 hash=d3d6bb…c5304a td=2 age=3mo5d38m
INFO [03-02|17:49:36.014] Loaded most recent local full block      number=0 hash=d3d6bb…c5304a td=2 age=3mo5d38m
INFO [03-02|17:49:36.021] Loaded most recent local fast block      number=0 hash=d3d6bb…c5304a td=2 age=3mo5d38m
INFO [03-02|17:49:36.030] Regenerated local transaction journal    transactions=0 accounts=0
INFO [03-02|17:49:36.056] Allocated fast sync bloom                size=1.32GiB
INFO [03-02|17:49:36.060] Initialized fast sync bloom              items=0 errorrate=0.000 elapsed=0s
INFO [03-02|17:49:36.078] New local node record                    seq=1 id=6d017d8e59468ff8 ip=127.0.0.1 udp=0 tcp=30303
INFO [03-02|17:49:36.079] IPC endpoint opened                      url=\\\\.\\pipe\\geth.ipc
INFO [03-02|17:49:36.085] Started P2P networking                   self="enode://1f1c94a828eb5e9102845e4823489619ca67f347c4707712dc2c73e07e11fd6ea89d96d87ff8b04df6f99d9bd59d1f80b6b75b1db66814332a8a0612ca2e2fd0@127.0.0.1:30303?discport=0"
INFO [03-02|17:49:36.092] HTTP endpoint opened                     url=http://127.0.0.1:8545 cors= vhosts=localhost
INFO [03-02|17:49:36.144] Mapped network port                      proto=tcp extport=30303 intport=30303 interface=NAT-PMP(192.168.31.1)
INFO [03-02|17:49:36.187] Etherbase automatically configured       address=0xdb11B7d7d6D52d01D169e497f290c9392dB1C4E5
Welcome to the Geth JavaScript console!
View Code

查看账户

> eth.accounts
["0xdb11b7d7d6d52d01d169e497f290c9392db1c4e5"]   #这是默认给我生成的账户

创建新账户

> personal.newAccount("xsmile")

提示内容如下:

INFO [03-02|17:52:02.621] Your new key was generated               address=0x5c51e3c26C0f67b3EEb2F4CabFc93AFB616e5849
WARN [03-02|17:52:02.626] Please backup your key file!             path=D:\\eth-test\\db\\keystore\\UTC--2020-03-02T09-52-00.518728700Z--5c51e3c26c0f67b3eeb2f4cabfc93afb616e5849
WARN [03-02|17:52:02.635] Please remember your password!
"0x5c51e3c26c0f67b3eeb2f4cabfc93afb616e5849"
View Code

设置挖矿奖励收益账户

> miner.setEtherbase("0x5c51e3c26c0f67b3eeb2f4cabfc93afb616e5849")

查看挖矿收益账户

> eth.coinbase
"0x5c51e3c26c0f67b3eeb2f4cabfc93afb616e5849"

开始挖矿

> miner.start(1) # 1 代表开启一个线程进行挖矿

第一次开始挖矿时间会很长(取决硬盘质量,在 centos 服务器上这一步,没成功或许后续会有问题,这才转到 windows 上来)。

提示内容如下:

INFO [03-02|17:55:12.657] Updated mining threads                   threads=1
INFO [03-02|17:55:12.662] Transaction pool price threshold updated price=1000000000
nullINFO
[> 03-02|17:55:12.666] Commit new mining work                   number=1 sealhash=49a853…3269f6 uncles=0 txs=0 gas=0 fees=0 elapsed=0s
INFO [03-02|17:55:15.125] Generating DAG in progress               epoch=0 percentage=0 elapsed=1.663s
INFO [03-02|17:55:16.746] Generating DAG in progress               epoch=0 percentage=1 elapsed=3.284s
INFO [03-02|17:55:18.450] Generating DAG in progress               epoch=0 percentage=2 elapsed=4.988s
INFO [03-02|17:55:20.065] Generating DAG in progress               epoch=0 percentage=3 elapsed=6.603s
... ...
INFO [03-02|17:57:48.596] Generating DAG in progress               epoch=0 percentage=94 elapsed=2m35.134s
INFO [03-02|17:57:50.219] Generating DAG in progress               epoch=0 percentage=95 elapsed=2m36.757s
INFO [03-02|17:57:51.840] Generating DAG in progress               epoch=0 percentage=96 elapsed=2m38.378s
INFO [03-02|17:57:53.477] Generating DAG in progress               epoch=0 percentage=97 elapsed=2m40.015s
INFO [03-02|17:57:55.083] Generating DAG in progress               epoch=0 percentage=98 elapsed=2m41.621s
INFO [03-02|17:57:57.142] Generating DAG in progress               epoch=0 percentage=99 elapsed=2m43.679s
INFO [03-02|17:57:57.150] Generated ethash verification cache      epoch=0 elapsed=2m43.687s
INFO [03-02|17:58:04.001] Generating DAG in progress               epoch=1 percentage=0  elapsed=2.065s
INFO [03-02|17:58:06.062] Generating DAG in progress               epoch=1 percentage=1  elapsed=4.126s
INFO [03-02|17:58:08.067] Generating DAG in progress               epoch=1 percentage=2  elapsed=6.131s
INFO [03-02|17:58:09.358] Successfully sealed new block            number=1 sealhash=49a853…3269f6 hash=c31fdd…635b4d elapsed=2m56.692s
INFO [03-02|17:58:09.371] 🔨 mined potential block                  number=1 hash=c31fdd…635b4d
INFO [03-02|17:58:09.392] Commit new mining work                   number=2 sealhash=0863a0…77b45e uncles=0 txs=0 gas=0 fees=0 elapsed=16.000ms
INFO [03-02|17:58:10.123] Generating DAG in progress               epoch=1 percentage=3  elapsed=8.187s
INFO [03-02|17:58:12.219] Generating DAG in progress               epoch=1 percentage=4  elapsed=10.283s
INFO [03-02|17:58:14.281] Generating DAG in progress               epoch=1 percentage=5  elapsed=12.345s
INFO [03-02|17:58:16.372] Generating DAG in progress               epoch=1 percentage=6  elapsed=14.436s
INFO [03-02|17:58:17.536] Successfully sealed new block            number=2 sealhash=0863a0…77b45e hash=c3e0aa…8e21e5 elapsed=8.156s
View Code

停止挖矿

> miner.stop()
null

查看刚才挖矿收益

> user2 = eth.accounts[1]
"0x5c51e3c26c0f67b3eeb2f4cabfc93afb616e5849"
> eth.getBalance(user2)
170000000000000000000     # 单位是 wei

转账

> eth.sendTransaction({from:user2,to:user3,value:web3.toWei(10,'ether')})

提示信息如下:

INFO [03-02|21:30:03.303] Setting new local account                address=0x5c51e3c26C0f67b3EEb2F4CabFc93AFB616e5849
INFO [03-02|21:30:03.308] Submitted transaction                    fullhash=0xf2c55ee849d2d89535dff41d54e32cf51f0d8faddf2b5bc6d668ec7d6270c37e recipient=0x9A55b9B0009B5b5E3e5B5e7B4d8db2134761dDF7
"0xf2c55ee849d2d89535dff41d54e32cf51f0d8faddf2b5bc6d668ec7d6270c37e"
View Code

如果此时系统不处于挖矿状态,此交易处于等待状态,我们可以通过下面语句查看

待打包信息

> txpool.status
{
  pending: 1, # 代表未打包的交易数
  queued: 0
}

 

原创不易,尊重版权。转载请注明出处:http://www.cnblogs.com/xsmile/

posted @ 2020-03-21 18:40  x_smile  阅读(454)  评论(0编辑  收藏  举报