900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 以太坊学习记录:私有链 私有链集群 智能合约

以太坊学习记录:私有链 私有链集群 智能合约

时间:2020-12-25 20:45:01

相关推荐

以太坊学习记录:私有链 私有链集群 智能合约

启动控制台

geth --networkid 15 --datadir privateChain2 --rpc --rpccorsdomain * --allow-insecure-unlock console

在geth中查看账户余额

web3.fromWei(eth.getBalance(acc1),'ether')

转账操作

eth.sendTransaction({from:acc1,to:acc2,value:web3.toWei(100 ,'ether')});

转账前需要解锁

personal.unlockAccount(acc1,'123456')personal.unlockAccount(acc2,'123456')

转账命令完成后,提示信息如下:

> eth.sendTransaction({from:acc1, to:acc2, value:web3.toWei(100,'ether')})INFO [03-21|15:58:51.358] Setting new local accountaddress=0xa70237F0f9b15E38a653C1eB6fa39376654bF026INFO [03-21|15:58:51.371] Submitted transactionfullhash=0x146c9681812e039624318cbd1359f0b89f156f0b1e93ff048f5e193a92b0aa39 recipient=0xE434fd4AbCAf1286CD4294F427365D0b67bFA0D4"0x146c9681812e039624318cbd1359f0b89f156f0b1e93ff048f5e193a92b0aa39"

提交转账后,需要进行挖矿才能转账成功。

部署合约

在remix ide上编写好以后,点击 编译详情 按钮,复制页面中的web3deploy代码。

粘贴到geth控制台中,按回车。出现如下界面后,输入命令让其挖矿,完成部署。

INFO [03-21|16:28:26.327] Submitted contract creation fullhash=0xe925a53103f218d6c28e97c3aabd3892c2b602cad4941bd10cd6dea4617bc1b1 contract=0x96C7dD56969ea5a684295104fD8fA4E03D18B134null [object Object]undefined

挖矿过程中,出现如下界面,则代表部署完成。

> miner.start()INFO [03-21|16:32:23.236] Updated mining threads threads=4INFO [03-21|16:32:23.246] Transaction pool price threshold updated price=1000000000null> INFO [03-21|16:32:23.335] Commit new mining work number=686 sealhash=593bd0…188a6d uncles=0 txs=0 gas=0fees=0 elapsed=0sINFO [03-21|16:32:23.384] Commit new mining work number=686 sealhash=f2057d…b6b120 uncles=0 txs=1 gas=112213 fees=0.000112213 elapsed=48.871msINFO [03-21|16:32:24.601] Successfully sealed new block number=686 sealhash=f2057d…b6b120 hash=d5d871…3d316b elapsed=1.216sINFO [03-21|16:32:24.656] �🔨 mined pottential block number=686 hash=d5d871…3d31INFO [03-21|16:32:24.602] Commit new mining work number=687 sealhash=401ada…6ed87e uncles=0 txs=0 gas=0fees=0 elapsed=0sINFO [03-21|16:32:24.920] Successfully sealed new block number=687 sealhash=401ada…6ed87e hash=38250d…a5d6b6 elapsed=318.150msINFO [03-21|16:32:25.116] �🔨 mined potential block number=687 hansuhll= 3[o8b2je5ct0 Od…ab5jd6bct]INFOC ontr[a03-ct m21|16i:32n:24.ed92! address: 00x96c] 7Cdod56969ea5ma684m295104fd8fa4e03d18b134 transactionHash: 0xe925a53103f218d6c28e97c3iaabd3892c2b602cad4941bd10cd6dea4617bc1b1t new mining work number=688 sealhash=701691…0560f1 uncles=0 txs=0 gas=0fees=0 elapsed=0sINFO [03-21|16:32:25.891] Successfully sealed new block number=688 sealhash=701691…0560f1 hash=fd0197…9a67cf elapsed=971.402ms

合约地址为:0x96c7dd56969ea5a684295104fd8fa4e03d18b134

交易地址为:0xe925a53103f218d6c28e97c3aabd3892c2b602cad4941bd10cd6dea4617bc1b1

调用智能合约:

> web3.fromWei(eth.getBalance(acc1),'ether')3365> simplestorage.set(1,{from:acc1}) # 使用账户1对其进行签名INFO [03-21|16:40:18.397] Submitted transactionfullhash=0xa2dbcef31f8dff7106210f33606746edc92ff9a41eaed592c9a34234e21b4d3f recipient=0x96C7dD56969ea5a684295104fD8fA4E03D18B134"0xa2dbcef31f8dff7106210f33606746edc92ff9a41eaed592c9a34234e21b4d3f"

调用后,进行挖矿,完成交易。

调用get方法查看情况。

> simplestorage.get()1

puppeth 可生成创世块文件

创世块文件注释

{"config": {"chainId": 13953,"homesteadBlock": 0, //homesteadBlock硬分叉的高度,设置为0 即可"eip150Block": 0, //EIP 150硬分叉的高度,设置为0即可。//EIP 150的hash值, 设置为0即可。"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000","eip155Block": 0, EIP 155硬分叉的高度,设置为0即可。"eip158Block": 0, EIP 158硬分叉的高度,设置为0即可"byzantiumBlock": 0, Byzantium硬分叉的高度"constantinopleBlock": 0,"petersburgBlock": 0,"istanbulBlock": 0,"ethash": {}},"nonce": "0x0", //nonce是加密安全的挖掘工作证明,证明在确定该令牌时已经花费了特定量的计算"timestamp": "0x5e761188", //在此块开始时的标量值等于Unix time()函数的合理输出。"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000","gasLimit": "0xf7b760","difficulty": "0x20000","mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000","coinbase": "0x0000000000000000000000000000000000000000","number": "0x0","gasUsed": "0x0",//整个父块头的Keccak 256位哈希(包括其nonce和mixhash)。指向父块的指针,从而有效地构建块链。在Genesis块的情况下,仅在这种情况下,它为0。"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"}

多节点启动

参数说明:

–datadir,私有链数据存放目录–nodiscover,既然是私有链,就要尽可能的避免和其他节点的自动交互。这里的设置为除非手工添加其他节点,否则不会自动发现。–rpc,是否打开RPC接口–rpcport,RPC监听端口,默认是8545,如果一台电脑上多个节点则必须设置不同的端口–rpcapi,在RPC接口上打开哪些功能,私有链可以尽情开放,但是注意公链上这一部分的设置一定要小心。因为第三方可以通过调用你的这个RPC来实现你打开的所有功能–rpcaddr,RPC链接地址,默认是localhost,如果想让别的电脑或Html或web3J访问则必须设置为本机IP地址–rpccorsdomain,设置哪些URL可以连接你的RPC接口–identity,给当前节点起名,标记节点–networkid,私有链的网络ID,用于区分不同的网络,1代表是公链,刚才我们在创世区块配置文件中已经设置,所以值应该是刚才设置的: 1234–nat,根据自己网络情况选择是否添加–port,geth的网络端口,默认是30303,如果一台电脑上多个节点一定设置不同的端口–ipcdisable,win10上必须开启,不开会报错

打开两个终端,分别启动各自节点:

开启nodiscover需要手动连接节点

geth --networkid "12" --identity "node0" --rpc --rpcport "8121" --rpccorsdomain '*' --datadir node0 --port "8545" --nodiscover --ipcdisable console

geth --networkid "12" --identity "node1" --rpc --rpcport "8122" --rpccorsdomain '*' --datadir node1 --port "8546" --nodiscover --ipcdisable console

geth --networkid "12" --identity "node2" --rpc --rpcport "8123" --rpccorsdomain '*' --datadir node2 --port "8547" --ipcdisable console

注:-- rpccorsdomain "*"后面需要使用双引号,单引号没用

多节点中相关的命令

查看网络中节点的数量

net.peerCount

查看enode信息

admin.nodeInfo.enode"enode://61972c3d899525ba78b2b43ad420f26ff7dcd5cba657a74955e55869039acf6241b32114abaef16ff3ad2a90dcdf385af1605c6e8219f53b0bc49ed651889f33@127.0.0.1:8546?discport=0"

两个节点通过enode添加连接

>admin.addPeer("enode://61972c3d899525ba78b2b43ad420f26ff7dcd5cba657a74955e55869039acf6241b32114abaef16ff3ad2a90dcdf385af1605c6e8219f53b0bc49ed651889f33@127.0.0.1:8546?discport=0")true

添加成功后,可以在一个节点开启挖矿,然后另一个节点会同步挖矿的信息。

在之前创建的两个节点之上,再创建一个节点。

然后再连接到另外两个节点中。这是区块链中不会产生冲突,节点3挖的矿,是如何插入节点1与节点2的网络中。

然后在创建一个节点4,独自进行一段时间的挖矿。然后再让其加入到主网络中会出现同步问题。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。