美文网首页DApp
1. 底层网络描述

1. 底层网络描述

作者: furnace | 来源:发表于2018-10-27 23:26 被阅读31次

    底层网络描述

    本文描述了已经搭建好的区块链底层网络,后续所有的示例都基于这些网络。

    目前,主要有四种网络:基于 PoA 共识算法的私有链、基于 PoW 共识算法的私有链、内存链 Ganache、和 Ganche 的命令行版本 ganache-cli。

    这些私有链统一通过开放 RPC 接口的方式对外提供服务。

    外部可以基于 JSON-RPC 协议访问这些私有链。

    1. 私有链信息

    1.1 PoA 私有链

    PoA 网络目前有四个节点。两个普通节点,两个 Singer 节点。

    • 节点 Node1:通过 192.168.60.30:8100 提供 RPC 服务。
    • 节点 Node2:通过 192.168.60.30:8101 提供 RPC 服务。
    • 节点 Signer1:通过 192.168.60.30:8102 提供 RPC 服务。
    • 节点 Singer2:通过 192.168.60.30:8103 提供 RPC 服务。

    1.2 PoW 私有链

    PoW 网络目前有两个节点。两个都是挖矿节点。

    • 节点 Node1:通过 192.168.60.31:8100 提供 RPC 服务。
    • 节点 Node2:通过 192.168.60.31:8101 提供 RPC 服务。

    1.3 Ganache

    这是一个图形化客户端,在设置选项里面将地址调整为 0.0.0.0,端口设定为 7545,自动挖矿的时间调整为 15 秒一个区块。

    • 节点 Node1:通过 192.168.60.12:7545 提供 RPC 服务。

    1.4 Ganache-cli

    这是 Ganache 的命令界面,启动时将地址设定为 0.0.0.0,端口设定为 8545。

    • 节点 Node1:通过 192.168.60.12:8545 提供 RPC 服务。
    ganache-cli -h 0.0.0.0 -p 8545
    

    2. 本地远程访问网络

    2.1 本地通过 geth attach 远程网络

    可以通过 geth attach 从本地远程操作网络节点。

    [furnace@localhost ~]$ geth attach http://192.168.60.30:8102
    WARN [10-09|22:43:55.096] Sanitizing cache to Go's GC limits       provided=1024 updated=613
    Welcome to the Geth JavaScript console!
    
    instance: Geth/v1.8.11-stable-dea1ce05/linux-amd64/go1.10.3
    coinbase: 0x99fcb46fccf902fb84e40546586542764f6e1214
    at block: 252924 (Tue, 09 Oct 2018 22:43:52 EDT)
     modules: eth:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0
    
    > eth.accounts
    ["0x261fe7219b33a3e4aa91d2744825d7e4d4aed5ad", "0x430e67cd68e18ec3073da0c560c94528cae4131f"]
    > acc0 = eth.accounts[0]
    "0x261fe7219b33a3e4aa91d2744825d7e4d4aed5ad"
    > web3.fromWei(web3.eth.getBalance(acc0), "ether")
    10000000000.000378
    > acc1 = eth.accounts[1]
    "0x430e67cd68e18ec3073da0c560c94528cae4131f"
    > web3.fromWei(web3.eth.getBalance(acc1), "ether")
    10000000000
    >
    

    2.2 查询节点的账户信息

    通过 geth attach 连接到远程节点之后,可以查看该节点的账户信息,及账户对应的以太币数量。

    由于智能合约的运行需要消耗以太币,所以可以用具有以太币的账户部署智能合约。

    2.2.1 PoA 网络

    [furnace@localhost ~]$ geth attach http://192.168.60.30:8102
    > eth.accounts
    ["0x261fe7219b33a3e4aa91d2744825d7e4d4aed5ad", "0x430e67cd68e18ec3073da0c560c94528cae4131f"]
    > eth.coinbase
    "0x261fe7219b33a3e4aa91d2744825d7e4d4aed5ad"
    > web3.fromWei(web3.eth.getBalance(eth.coinbase), "ether")
    10000000000.000378
    >
    

    可知,PoA 网络可以使用节点 Singer1(192.168.60.30:8102)中的账户 "0x430e67cd68e18ec3073da0c560c94528cae4131f" 部署智能合约。

    2.2.2 PoW 网络

    [furnace@localhost ~]$ geth attach http://192.168.60.31:8100
    > eth.accounts
    ["0x99fcb46fccf902fb84e40546586542764f6e1214"]
    > eth.coinbase
    "0x99fcb46fccf902fb84e40546586542764f6e1214"
    > web3.fromWei(web3.eth.getBalance(eth.coinbase), "ether")
    150326.25
    >
    

    可知,PoW 网络可以使用节点 Node1(192.168.60.31:8100)中的账户 "0x99fcb46fccf902fb84e40546586542764f6e1214" 部署智能合约。

    2.2.3 Ganache 网络

    [furnace@localhost ~]$ geth attach http://192.168.60.12:7545
    > eth.accounts
    ["0x1d31f7400ee8c6d4079d8dd9687d61bd28356610", "0x8846acfbca6cdb0e6c10ccb536b343361a26fe40", "0xc77653df2163bf145e367aa4613efb6d5a1ae4eb", "0xfa011cf50ee9b13ebba58ef74e1019c0966d1609", "0xc3fbd3c465c9eaf676c71b5046e7336c76281eb8", "0xeef8466d0d258394bee3d9d2fc0daf74e1b4f720", "0x12789569d1df377bb4ce17ba091ffea14bfc72ff", "0xd3d338b855f4421aeee5536773fa341a1c1a9767", "0x2c8a745cc111c40e8b0b0c62703ea4debfe9d251", "0xe24b0d69593193ad002a627a813bb7509b15ab4a"]
    >
    

    Ganache 通过 192.168.60.12:7545 提供服务。

    需要注意的是,这是模拟以太坊的内存节点,重启后里面的数据会全部重置或丢失。

    2.2.4 ganache-cli 网络

    [furnace@localhost ~]$ geth attach http://192.168.60.12:8545
    > eth.accounts
    ["0x0eab741eaa2637466fa92a9821720843c5fa904f", "0x33d90f2745cd679c61c0c2d4e947772b2880a9ac", "0x4581d96ed54e1da119fe68305c84573d45bf122c", "0xc50ef1c1135cfe91103d04f4523fd225ffdec1c0", "0xf2e3d1ff8ac8a28eb19d16a5dd4351d923d3bcf1", "0xfef1a4b74bbe3cc6e40778b4fffb8a4850862548", "0x1cacef06016aa79053fa2c9497871a79c3668d66", "0xddb1d44a1b88fbaa8dfcf1945b807712c01aef5e", "0xe6c77dc5f99d1c230854b9f45961f87bc8e32c0c", "0x6b50df8ff657b61ea524b472320acf4491e1a23e"]
    >
    

    ganache-cli 通过 192.168.60.12:8545 提供服务。

    需要注意的是,这是模拟以太坊的内存节点,重启后里面的数据会全部重置或丢失。

    3. 基于 JSON-RPC 访问网络

    可通过上述网络提供的 RPC 地址和端口,基于 JSON-RPC 协议进行访问。

    这里以访问 PoA 网络的节点 Singer1(192.168.60.30:8102) 为例,访问其它节点只要修改对应的 RPC 地址和端口即可。

    3.1 基于 CURL 调用 JSON-RPC

    可使用 curl 工具调用 JSON-RPC 接口访问网络,示例如下。

    [furnace@localhost wiki]$ curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}' 192.168.80.30:8102
    {"jsonrpc":"2.0","id":1,"result":"55661"}
    [furnace@localhost wiki]$
    

    更详细的示例可参考 JSON-RPC 目录。

    3.2 基于 Python 调用 JSON-RPC

    可使用 Python 脚本文件调用 JSON-RPC 接口访问网络,示例如下。

    文件:python/raw/net_version.py

    import requests
    
    # create persistent HTTP connection
    session = requests.Session()
    
    # as defined in https://github.com/ethereum/wiki/wiki/JSON-RPC#net_version
    method = 'net_version'
    params = []
    payload= {"jsonrpc":"2.0",
               "method":method,
               "params":params,
               "id":1}
    headers = {'Content-type': 'application/json'}
    
    response = session.post('http://192.168.60.30:8102', json=payload, headers=headers)
    print('raw json response: {}'.format(response.json()))
    print('result: {}'.format(response.json()['result']))
    

    运行结果如下:

    # [furnace@localhost wiki]$ python3.6 python/raw/net_version.py
    # raw json response: {'jsonrpc': '2.0', 'id': 1, 'result': '55661'}
    # result: 55661
    # [furnace@localhost wiki]$
    

    Contributor

    1. Windstamp, https://github.com/windstamp

    相关文章

      网友评论

        本文标题:1. 底层网络描述

        本文链接:https://www.haomeiwen.com/subject/mfkktqtx.html