从零构建基于以太坊Ethereum钱包Parity联盟链

以下内容,直接实战,开发人员不需要图解释,嘿嘿大笑


先来看看,PoA Chain特点有
有别于PoW(Proof-of-Work)需要解数学难题来产生block,PoA是依靠预设好的Authority nodes,负责产生block。
可依照需求设定Authority node数量。
可指定产生block的时间,例如收到交易的5秒后产生block。
一般的Ethereum node也可以连接到PoA Chain,正常发起transactions,contracts等。

一、Parity钱包下载安装
Parity钱包下载安装https://parity.io。

如官网所示,The fastest and most secure way of interacting with the Ethereum blockchainParity是最快最安全的钱包。


打开官网,我们看到有三种安装方式,第一种,直接下载安装,第二种,Brew安装,第三种,Docker安装。


在我们案例中,我们通过Brew来进行安装。


1、Getting Homebrew


/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2、Adding Parity to your list of Homebrew ‘kegs’


打开终端,输入下面的命令,按enter。


brew tap paritytech/paritytech
3、Installing Parity


稳定版
brew install parity --stable
最新版
brew install parity
最新开发版
brew install parity --master
更新最新版本
brew update && brew upgrade parity
and


brew reinstall parity
4、查看安装版本


liyuechun:~ yuechunli$ parity --version
二、设置chain spec
PoA chain 需要设置一个创世区块。
{
  "name": "DemoPoA",
  "engine": {
    "authorityRound": {
      "params": {
        "stepDuration": "5",
        "validators": {
          "list": [
            
          ]
        }
      }
    }
  },
  "params": {
    "gasLimitBoundDivisor": "0x0400",
    "maximumExtraDataSize": "0x20",
    "minGasLimit": "0x1388",
    "networkID": "0x2323"
  },
  "genesis": {
    "seal": {
      "authorityRound": {
        "step": "0x0",
        "signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
      }
    },
    "difficulty": "0x20000",
    "gasLimit": "0x5B8D80"
  },
  "accounts": {
    "0x0000000000000000000000000000000000000001": {
      "balance": "1",
      "builtin": {
        "name": "ecrecover",
        "pricing": {
          "linear": {
            "base": 3000,
            "word": 0
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000002": {
      "balance": "1",
      "builtin": {
        "name": "sha256",
        "pricing": {
          "linear": {
            "base": 60,
            "word": 12
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000003": {
      "balance": "1",
      "builtin": {
        "name": "ripemd160",
        "pricing": {
          "linear": {
            "base": 600,
            "word": 120
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000004": {
      "balance": "1",
      "builtin": {
        "name": "identity",
        "pricing": {
          "linear": {
            "base": 15,
            "word": 3
          }
        }
      }
    }
  }
}
stepDuration 设定成5秒产生一个区块。
validators 设定Authority的地方,目前先空著,后面创建account之后再回来填入。
将上面的文件保存到桌面的一个文件中,保存为demo-spec.json。


三、设置两个节点
在我们这篇文章中,我们在同一台电脑设置两个节点,跟我们讲解以太坊私网建立 (2) - 同一台电脑/不同电脑运行多个节点时,如果在同一台电脑设置两个节点,需要将rpcport和port设置为不同的值,否则就会发生冲突,POA chain中也是一样,需要将一些参数设置为不同的值。


-d:指定存储资料与账号的目录
--dport:指定Parity的network port,可用来让其他node连接
--jsonrpc-port:这是JSON RPC port,使用web3.js时会需要
ui-port:Parity提供的Web-based UI port
可以用下列指令启动Parity node。


parity --chain demo-spec.json -d parity0 --port 30300  --ui-port 8180  --jsonrpc-port 8540 --jsonrpc-apis web3,eth,net,personal,parity,parity_set,traces,rpc,parity_accounts
除了打一长串的指令外,Parity也提供更为简洁的config档案设定方式,使用 --config 即可引用配置文件。


node0 使用如下配置文件 node0.toml:
[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8180
[websockets]
port = 8456
node1 使用如下配置文件 node1.toml:
[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8181
[websockets]
port = 8457
四、设置账号(Account)
我们总共需要设置三个账号,两个Authority和一个user账号。


第一步:首先启动node0 : parity --config node0.toml


启动 parity node0


打开网页http://localhost:8180,按照步骤创建一个用户账号。






新增Authority account,使用Restore功能,为了示范一致性,我们使用 node0 当作 pass phrase。
Recover account from recovery phrase


到目前为止我们已经完成node0的账号设置。


Authority account:0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e
User account:0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC
第二步:设置node1的账号,启动parity --config node1.toml。步骤相同,连接到 http://localhost:8181 ,pass phrase使用 node1。


启动 node1


parity node1


这样就完成了node1的账号设置。


Authority account:0x00F9B30838ca40c8A53c672840acbDec6fCDb180
第三步:将Authority account 写入 demo-spec.json 文件


"validators": {
  "list": [
    "0x00F9B30838ca40c8A53c672840acbDec6fCDb180",
    "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
  ]
}
再将user account加入accounts,並给一些balance,后续可以使用。


"0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC": {
  "balance": "10000000000000000000000"
}
完成后的demo-spec.json如下:


{
  "name": "DemoPoA",
  "engine": {
    "authorityRound": {
      "params": {
        "stepDuration": "5",
        "validators": {
          "list": [
            "0x00F9B30838ca40c8A53c672840acbDec6fCDb180",
            "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
          ]
        }
      }
    }
  },
  "params": {
    "gasLimitBoundDivisor": "0x0400",
    "maximumExtraDataSize": "0x20",
    "minGasLimit": "0x1388",
    "networkID": "0x2323"
  },
  "genesis": {
    "seal": {
      "authorityRound": {
        "step": "0x0",
        "signature": "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
      }
    },
    "difficulty": "0x20000",
    "gasLimit": "0x5B8D80"
  },
  "accounts": {
    "0x0000000000000000000000000000000000000001": {
      "balance": "1",
      "builtin": {
        "name": "ecrecover",
        "pricing": {
          "linear": {
            "base": 3000,
            "word": 0
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000002": {
      "balance": "1",
      "builtin": {
        "name": "sha256",
        "pricing": {
          "linear": {
            "base": 60,
            "word": 12
          }
        }
      }
    },
    "0x0000000000000000000000000000000000000003": {
      "balance": "1",
      "builtin": {
        "name": "ripemd160",
        "pricing": {
          "linear": {
            "base": 600,
            "word": 120
          }
        }
      }
    },
    "0x0064B0999c0142eE99aB0ceC054BAb53fe0a3EcC": {
      "balance": "10000000000000000000000"
    },
    "0x0000000000000000000000000000000000000004": {
      "balance": "1",
      "builtin": {
        "name": "identity",
        "pricing": {
          "linear": {
            "base": 15,
            "word": 3
          }
        }
      }
    }
  }
}
五、启动Authority node
为了启动Authority node来产生区块,我们必须设定负责产生block的signer,分別是 node0 和 node1 account。


1、第一步,创建一个node.pwds文件,写入node0与node1的password,内容如下:


node0
node1
2、第二步,在node0.toml文件中加入[account]及[mining]设置,如下:


[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8180
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"
3、第三步,在node1.toml文件中加入[account]及[mining]设置,如下:


[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
[ui]
port = 8181
[websockets]
port = 8457
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
4、第四步,Step 4 分別启动两个node


parity --config node0.toml
parity --config node1.toml
六、连接两个节点
使用Postman透过JSON RPC来测试。


1、第一步,Post下列JSON数据至 http://localhost:8540 以取得 node0 的enode资料


{
 "jsonrpc":"2.0",
 "method":"parity_enode",
 "params":[],
 "id":0
}




获取到的数据如下:


{
    "jsonrpc": "2.0",
    "result": "enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300",
    "id": 0
}
"enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300"是node0的标识。下一步中我们将将它加入到node1中以实现两个节点之间的连接。


2、第二步,将 node0 的enode加入 node1 ,Post下列JSONs数据至node1 (http://localhost:8541 )


{
 "jsonrpc":"2.0",
 "method":"parity_addReservedPeer",
 "params":["enode://cfb3af513da3a7a8138450f0dc01fa38cb2ac837744dc645038940287f4dce3f416f0e7e17fd10619a263c360d9324fd2dcd8753c4500fcc54cf84e076b39cd6@192.168.10.101:30300"],
 "id":0
}




返回的数据如下,result为true,说明连接成功:


{
    "jsonrpc": "2.0",
    "result": true,
    "id": 0
}
再切换到node1的终端,会看到下面的数据:


1/25 peers   13 KiB chain 11 KiB db 0 bytes queue 10 KiB sync  RPC:  0 conn,  0 req/s,  24 µs




连接成功。


七、发送交易
在我们这个案例中,我们一个创建了三个账号,一个用户账号,两个POA账号,刚开始的时候我们为用户账号初始化了10000 ETH。如


账号与账号之间可以相互转账。


八、分享给其他节点
在开发时通常会将node跑在server上,让其他人可以通过JSON RPC port连接上去使用,此时只要在config文件里面加入 [interface] 设置即可。


假设server ip为192.168.1.5,将 node0.toml 修改如下:


[parity]
chain = "demo-spec.json"
base_path = "parity0"
[network]
port = 30300
[rpc]
port = 8540
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.5"
[ui]
port = 8180
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e"
reseal_on_txs = "none"
node1.toml 修改如下:


[parity]
chain = "demo-spec.json"
base_path = "parity1"
[network]
port = 30301
[rpc]
port = 8541
apis = ["web3", "eth", "net", "personal", "parity", "parity_set", "traces", "rpc", "parity_accounts"]
interface = "192.168.1.5"
[ui]
port = 8181
[websockets]
port = 8457
[account]
password = ["node.pwds"]
[mining]
engine_signer = "0x00F9B30838ca40c8A53c672840acbDec6fCDb180"
reseal_on_txs = "none"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

devopen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值