以太坊初探

理论部分

POW

工作证明(PoW)是一种机制,它允许分散的以太坊网络达成共识,或就账户余额和交易顺序等事项达成一致。这可以防止用户“双倍消费”他们的硬币,并确保以太坊链极难被攻击或覆盖。

工作和挖矿证明

工作证明是为矿工工作设定难度和规则的底层算法。挖矿本身就是“工作”。这是向链中添加有效块的行为。这一点很重要,因为链的长度有助于网络发现有效的以太坊链并了解以太坊的当前状态。“工作”做得越多,链条越长,区块号越高,网络对事物的当前状态就越有把握。

区块内容

block difficulty – for example: 3,324,092,183,262,715
mixHash – for example: 0x44bca881b07a6a09f83b130798072441705d9a665c5ac8bdf2f39a3cdf3bee29
nonce – for example: 0xd3ee432b4fb3d26b

关于nonce和其他概念可以看这里 
https://blog.csdn.net/starzhou/article/details/80153011

区块共识

当竞相创建区块时,矿工会反复放置一个数据集,你只能通过数学函数下载并运行整个链(就像矿工一样)。这是为了生成一个低于目标nonce的mixHash,这取决于块的难度。最好的办法就是试错。
难度决定了散列的目标。目标值越低,有效哈希集越小。一旦生成,对于其他矿工和客户来说,验证起来非常容易。即使一项交易发生变化,散列值也会完全不同,表明存在欺诈行为
散列HASH使欺诈很容易被发现。但PoW作为一个过程,也是一个很大的威慑可能导致攻击链。

更多细节在这里
https://ethereum.org/en/developers/docs/consensus-mechanisms/pow/

关于共识,这个论文最详尽。
https://blog.csdn.net/starzhou/article/details/80153011

搭建

搭建ganache

搭建参考  https://www.trufflesuite.com/docs/ganache/overview
 搭建ganache GUI和ganache-cli 。它应该是truffle的升级版。

搭建ethereum node

a. 使用现成ETH提供的测试网

从这里去搭建
https://geth.ethereum.org/docs/getting-started

从文章来看,ETH有三种测试网,分别支持三种组网模式:
  
Ropsten: Proof-of-work test network
Rinkeby: Proof-of-authority test network
Görli: Proof-of-authority test network

Geth可以选择连入他们进行联网测试。本文中我们想自己组网尝试一下。

b. 使用自己组网方式测试

使用 geth --dev  这种 dev模式开启的测试网具有以下特性。

Initializes the data directory with a testing genesis block
Sets max peers to 0
Turns off discovery by other nodes
Sets the gas price to 0
Uses the Clique PoA consensus engine with which allows blocks to be mined as-needed without excessive CPU and memory consumption
Uses on-demand block generation, producing blocks when transactions are waiting to be mined

启动时可以选择使用哪种方式和外界交互,有IPC-RPC / HTTP-RPC /WEBSOCKET-RPC 这几种,它消息体格式参照的标准是JSON-RPC2.0 可以看这里 https://geth.ethereum.org/docs/rpc/server

  1. 启动DEV模式

    root@lyp830414-ThinkPad-E470c:~# geth --datadir test-chain-dir --rpc --dev --rpccorsdomain "https://remix.ethereum.org,http://remix.ethereum.org"
     INFO [11-19|15:27:56.878] Starting Geth in ephemeral dev mode... 
     INFO [11-19|15:27:56.879] Maximum peer count                       ETH=50 LES=0 total=50
     WARN [11-19|15:27:56.879] The flag --rpc is deprecated and will be removed in the future, please use --http 
     WARN [11-19|15:27:56.879] The flag --rpccorsdomain is deprecated and will be removed in the future, please use --http.corsdomain 
     INFO [11-19|15:27:56.879] Smartcard socket not found, disabling    err="stat /run/pcscd/pcscd.comm: no such file or directory"
     INFO [11-19|15:27:56.881] Set global gas cap                       cap=25000000
     INFO [11-19|15:27:58.125] Using developer account                  address=0x20132748683890409Dd52104774011555782D7D4
     INFO [11-19|15:27:58.125] Allocated cache and file handles         database=/root/test-chain-dir/geth/chaindata cache=512.00MiB handles=524288
     INFO [11-19|15:27:58.215] Opened ancient database                  database=/root/test-chain-dir/geth/chaindata/ancient
     INFO [11-19|15:27:58.215] Freezer shutting down 
     INFO [11-19|15:27:58.215] Allocated trie memory caches             clean=256.00MiB dirty=256.00MiB
     INFO [11-19|15:27:58.215] Allocated cache and file handles         database=/root/test-chain-dir/geth/chaindata cache=512.00MiB handles=524288
     INFO [11-19|15:27:58.324] Opened ancient database                  database=/root/test-chain-dir/geth/chaindata/ancient
     INFO [11-19|15:27:58.324] Initialised chain configuration          config="{ChainID: 1337 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: 0 EIP155: 0 EIP158: 0 Byzantium: 0 Constantinople: 0 Petersburg: 0 Istanbul: 0, Muir Glacier: <nil>, YOLO v2: <nil>, Engine: clique}"
     INFO [11-19|15:27:58.324] Initialising Ethereum protocol           versions="[65 64 63]" network=1337 dbversion=8
     INFO [11-19|15:27:58.326] Loaded most recent local header          number=0 hash="5fb5eb…312aea" td=1 age=51y7mo2w
     INFO [11-19|15:27:58.326] Loaded most recent local full block      number=0 hash="5fb5eb…312aea" td=1 age=51y7mo2w
     INFO [11-19|15:27:58.326] Loaded most recent local fast block      number=0 hash="5fb5eb…312aea" td=1 age=51y7mo2w
     INFO [11-19|15:27:58.327] Loaded local transaction journal         transactions=0 dropped=0
     INFO [11-19|15:27:58.327] Regenerated local transaction journal    transactions=0 accounts=0
     INFO [11-19|15:27:58.656] Allocated fast sync bloom                size=512.00MiB
     INFO [11-19|15:27:58.656] Initialized fast sync bloom              items=13 errorrate=0.000 elapsed="114.378µs"
     INFO [11-19|15:27:58.656] Starting peer-to-peer node               instance=Geth/v1.9.24-stable-cc05b050/linux-amd64/go1.15.5
     INFO [11-19|15:27:58.752] New local node record                    seq=4 id=7ab312ba607f24e9 ip=127.0.0.1 udp=0 tcp=38891
     INFO [11-19|15:27:58.752] Started P2P networking                   self="enode://226b48f952a2a2f14ab292d0a02013a19c0a8204581191beb8adb2048cb04414ee3065e0090e8efb909eb3ecbee15212264a19ffc3044ca1f84032f4698efd95@127.0.0.1:38891?discport=0"
     INFO [11-19|15:27:58.752] IPC endpoint opened                      url=/root/test-chain-dir/geth.ipc
     INFO [11-19|15:27:58.753] HTTP server started                      endpoint=127.0.0.1:8545 cors=https://remix.ethereum.org,http://remix.ethereum.org vhosts=localhost
     INFO [11-19|15:27:58.753] Transaction pool price threshold updated price=1000000000
     INFO [11-19|15:27:58.753] Transaction pool price threshold updated price=1
     INFO [11-19|15:27:58.753] Etherbase automatically configured       address=0x20132748683890409Dd52104774011555782D7D4
     INFO [11-19|15:27:58.753] Commit new mining work                   number=1 sealhash="6203e9…f54c0b" uncles=0 txs=0 gas=0 fees=0 elapsed="169.615µs"
     INFO [11-19|15:27:58.754] Sealing paused, waiting for transactions 
    
  2. IPC访问GETH的服务
    1) 首先我们先用clief工具注册一个账户ADDRESS

     root@lyp830414-ThinkPad-E470c:~# clef init  &&  clef newaccount
     
     WARNING!
     
     Clef is an account management tool. It may, like any software, contain bugs.
     
     Please take care to
     - backup your keystore files,
     - verify that the keystore(s) can be opened with your password.
     
     Clef is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
     without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE. See the GNU General Public License for more details.
     
     Enter 'ok' to proceed:
     > ok
     
     INFO [11-19|17:01:17.064] Starting clef                            keystore=/root/.ethereum/keystore light-kdf=false
     DEBUG[11-19|17:01:17.096] FS scan times                            list=9.480956ms set="21.663µs" diff="6.324µs"
     ## New account password
     
     Please enter a password for the new account to be created (attempt 0 of 3)
     > 
     -----------------------
     DEBUG[11-19|17:01:28.427] FS scan times                            list="124.247µs" set=46.214643ms diff="6.773µs"
     DEBUG[11-19|17:01:35.093] FS scan times                            list=396.362546ms set=31.705206ms diff="6.503µs"
     INFO [11-19|17:01:34.090] Your new key was generated               address=0xBd68AF486a4A2983482ec25438fe586236C9d85F
     WARN [11-19|17:01:35.955] Please backup your key file!             path=/root/.ethereum/keystore/UTC--2020-11-19T09-01-25.064987129Z--bd68af486a4a2983482ec25438fe586236c9d85f
     WARN [11-19|17:01:35.955] Please remember your password! 
     Generated account 0xBd68AF486a4A2983482ec25438fe586236C9d85F
     root@lyp830414-ThinkPad-E470c:~# 
    
    
    
    查看前面的服务启动信息,我们发现IPC的句柄路径是“/root/test-chain-dir/geth.ipc”
    那么我们用GETH命令行去连入这个IPC:
    
     	root@lyp830414-ThinkPad-E470c:~# geth attach /root/test-chain-dir/geth.ipc
     	Welcome to the Geth JavaScript console!
     	
     	instance: Geth/v1.9.24-stable-cc05b050/linux-amd64/go1.15.5
     	coinbase: 0x20132748683890409dd52104774011555782d7d4
     	at block: 0 (Thu Jan 01 1970 08:00:00 GMT+0800 (CST))
     	 datadir: /root/test-chain-dir
     	 modules: admin:1.0 clique:1.0 debug:1.0 eth: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
     	> web3.fromWei(eth.getBalance("0xBd68AF486a4A2983482ec25438fe586236C9d85F"),"ether")
     	0
     	> 
    
  3. HTTP-RPC访问GETH的服务

    root@lyp830414-ThinkPad-E470c:~# curl -H 'Content-type':'application/json' -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' --url http://127.0.0.1:8545 
     {"jsonrpc":"2.0","id":67,"result":"Geth/v1.9.24-stable-cc05b050/linux-amd64/go1.15.5"}
      
      更多RPC接口可以看这里 https://eth.wiki/json-rpc/API
    
  4. WEBSOCKET-RPC访问GETH的服务

     root@lyp830414-ThinkPad-E470c:~# curl --include  --no-buffer  --header "Connection: Upgrade"  --header "Upgrade: websocket"  --header "Host: 127.0.0.1:8545"  --header "Origin: http://127.0.0.1:8545"  --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ=="  --header "Sec-WebSocket-Version: 13" -H 'Content-type':'application/json'  -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://127.0.0.1:8545 
     HTTP/1.1 200 OK
     Content-Type: application/json
     Vary: Origin
     Date: Thu, 19 Nov 2020 11:16:48 GMT
     Content-Length: 87
     
     {"jsonrpc":"2.0","id":67,"result":"Geth/v1.9.24-stable-cc05b050/linux-amd64/go1.15.5"}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值