区块链:雷电网络开发预览版x.docx
区块链:雷电网络开发预览版x区块链:雷电网络开发预览版 • 雷电网络开发预览版-初体验 – Raiden 环境搭建 1下载 Raiden 执行文件 2安装 Parity 客户端 3打算账号 4启动 Parity 同步 ropsten 测试网络 5在 Ropsten 给账号增加以太 balance 6启动 Raiden • 7. 检查是否运行正常 – 节点交互 1调用 RTT 合约转账给 A、B 账户使其先拥有 RTT 余额 2A、B 节点自动连接 RTT network(可选)3与 B 建立干脆的状态通道 4A 节点查看状态通道信息 5向状态通道 deposit(可选)6A 向 B 节点 发起 RTT 转账 1 7查询通道的事务信息 8关闭状态通道 • 9. 节点离开 RTT network(可选)– 其他 以下内容在 ubuntu 机器 ,Ropsten 测试网络实测 ok。留意:随着 Raiden 开发的不断推动,本文内容有可能不适用将来最新版本Raiden 环境搭建 1. 下载 Raiden 执行文件 wget mv raiden-x86_64.AppImage raidenchmod +x raiden2. 安装 Parity 客户端 wget https:/parity-downloads-mirror.parity.io/v1.7.6/x86_64-unknown-linux-gnu/parity_1.7.6_amd64.debdpkg -i parity_1.7.6_amd64.deb3. 打算账号 parity account new -chain ropsten -keys-path /.local/share/io.parity.ethereum/keys/ethereum/新建两个 eth 账户。Raiden 节点运用 eth 地址来标识。下文假设 A 账户为 0x607f1724469a0beeed74e1429c3c5adcf8290fa3,B 账户为0x17cb47f30f844b6969e8cc6cd3c506eb25f17819。4. 启动 Parity 同步 ropsten 测试网络 #parity db kill -chain ropstenparity -geth -keys-path /.local/share/io.parity.ethereum/keys/ethereum/ -chain ropsten -bootnodes enode:/20c9ad97c081d63397d7b685a412227a40e23c8bdc6688c6f37e97cfbc22d2b4d1db1510d8f61e6a8866ad7f0e17c02b14182d37ea7c3c8b9c2683aeb6b733a152.169.14.227:30303,enode:/6ce05930c72abc632c58e2e4324f7c7ea478cec0ed4fa2528982cf34483094e9cbc9216e7aa349691242576d552a2a56aaeae426c5303ded677ce455ba1acd9d13.84.180.240:30303parity 默认已开启 Warp sync。等待网络同步完成再执行后面的操作。否则 raiden启动的时候会报 Raiden 的 Registry 合约无法找到的错误。5. 在 在 Ropsten 给账号增加以太 balance 因为 raiden 中加入网络和建立通道都须要发送交易,所以要求对应账户的 eth 余额不能为 0。打开 http:/faucet.ropsten.be:3001/ 给 4 中的账户发送 ether。6. 启动 Raiden 配置不同的端口,启动两个节点:./raiden -keystore-path /.local/share/io.parity.ethereum/keys/ethereum/test/ -api-address 0.0.0.0:9545 -listen-address 0.0.0.0:4001 ./raiden -keystore-path /.local/share/io.parity.ethereum/keys/ethereum/test/ -api-address 0.0.0.0:9546 -listen-address 0.0.0.0:4002选择不同的账户,并输入密码解锁。正常启动状况下会输出 API 端口信息。后面与 Raiden 都是通过 Restful API 来交互。7. 检查是否运行正常 curl http:/127.0.0.1:9545/api/1/address#返回 our_address: 0x607f1724469a0beeed74e1429c3c5adcf8290fa3 curl http:/127.0.0.1:9546/api/1/address#返回 our_address: 0x17cb47f30f844b6969e8cc6cd3c506eb25f17819输出的地址与启动时候所选择的账户地址一样,则已正常运行。至此,两个节点的 Raiden 环境已经搭建起来了。节点交互 通过 API 可以向网络注册新的 Token Network,也可以干脆接入一个已注册的Token Network,可以与其他 Raiden 节点建立 Channel 并 Deposit 到 Channel,还有其他的一些相关操作。具体可以看 Raiden 的 API 说明 。Raiden 供应了一个已注册好的 Raiden Testnet Token (RTT)。RTT 地址是0x0f114a1e9db192502e7856309cc899952b3db1ed 。下面的内容是对部分 API 的简洁体验过程。1. 调用 RTT 合约转账给 A 、B 账户使其先拥有 RTT 余额 运用 web3.js 调用以下代码发起账户转账 RTT 代币的交易。留意调用之前先解锁账户。var addr="0x607f1724469a0beeed74e1429c3c5adcf8290fa3"#改成自己的账户地址var rtt_token_abi = Contract ABI; #可以从 https:/ropsten.etherscan.io/address/0x0f114a1e9db192502e7856309cc899952b3db1ed#code 获得var rtt_token_address = 0x0f114a1e9db192502e7856309cc899952b3db1ed;var rtt_token = web3.eth.contract(rtt_token_abi).at(rtt_token_address);rtt_token.mint(from:addr); / adjust to your raiden account and unlock first! console.log(rtt_token.balanceOf(addr).toString();每调用一次,RTT 增加 100。调用完之后,可以在https:/ropsten.etherscan.io/address/账户地址 查看交易状态。假如交易上链了,账户的 RTT 代币余额就会增加,可以多调几次,使得余额足够,便利后面的通道转账。2. A 、B 节点自动连接 RTT network (可选)#对 A 节点curl -X PUT -H Content-Type: application/json -d "funds:100" http:/127.0.0.1:9545/api/1/connection/0x0f114a1e9db192502e7856309cc899952b3db1ed #对 B 节点curl -X PUT -H Content-Type: application/json -d "funds:100" http:/127.0.0.1:9546/api/1/connection/0x0f114a1e9db192502e7856309cc899952b3db1ed正常状况下输出状态变更事务消息:INFO:work.rpc.clientnew_netting_channel called peer1=a6c07564 netting_channel=4b644413 peer2=0f658429INFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveNewChannel object at 0x7fa497756bd0>INFO:work.rpc.clientdeposit called amount=400 contract=4b644413INFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveBalance object at 0x7fa497706f10>INFO:work.rpc.clientnew_netting_channel called peer1=a6c07564 netting_channel=ea955179 peer2=9598decbINFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveNewChannel object at 0x7fa497706ad0>INFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveBalance object at 0x7fa497730890>INFO:work.rpc.clientdeposit called amount=400 contract=ea955179ERROR:raiden.tasksalarm missed 1 blocksERROR:raiden.tasksalarm missed 2 blocksINFO:work.rpc.clientnew_netting_channel called peer1=a6c07564 netting_channel=7c2bfbda peer2=82710a71INFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveNewChannel object at 0x7fa497730f90>INFO:work.rpc.clientdeposit called amount=400 contract=7c2bfbdaINFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveBalance object at 0x7fa49773c1d0>每个节点执行胜利之后,节点就会自动加入 RTT network 并且与其他三个随机的节点建立状态通道,并且每个 channel 会 deposit 20%的 funds,剩余的 40%funds留给新加进来的节点来建立状态通道。假如是 RTT 余额不足,Raiden 客户端会提示:INFO:work.rpc.clientnew_netting_channel called peer1=1fb444d0 netting_channel=fd0a1d0e peer2=82710a713. A 与 与 B 建立干脆的状态通道 curl -X PUT -H Content-Type: application/json -d "partner_address: 0x17cb47f30f844b6969e8cc6cd3c506eb25f17819,token_address: 0x0f114a1e9db192502e7856309cc899952b3db1ed,balance: 100,settle_timeout: 600" http:/127.0.0.1:9545/api/1/channels其中 partner_address 是节点地址,即 B 节点地址,token_address 是 token 地址,即 RTT 的地址。正常建立状况下会有类似提示:INFO:raiden.event_handler state_change received state_change=<raiden.transfer.mediated_transfer.state_change.ContractReceiveNewChannel object at 0x7f7f1ed34e50>INFO:work.rpc.clientdeposit called amount=100 contract=721d03ec4. A 节点查看状态 通道信息 curl -X GET http:/127.0.0.1:9545/api/1/channels返回:balance: 100,channel_address: 0x721d03ec76ccbfdce0ad3fc4602b59d622304d25,partner_address: 0x17cb47f30f844b6969e8cc6cd3c506eb25f17819,reveal_timeout: 10,settle_timeout: 600,state: opened,token_address: 0x0f114a1e9db192502e7856309cc899952b3db1ed curl -X GET http:/127.0.0.1:9546/api/1/channels返回:balance: 0,channel_address: 0x504750b7b1e0940119f3d0bad11e2fc766ab97a1,partner_address: 0x0c599ace7e0a7ff7c0ce6ba1c50f35ec58c04e86,reveal_timeout: 10,settle_timeout: 400,state: opened,token_address: 0x0f114a1e9db192502e7856309cc899952b3db1ed从返回可以看出,state 为 opened,状态通道中 A 余额为 100,B 余额为 0。5. 向状态通道 deposit (可选)从 4 中可以看到 B 和 A 节点的状态通道的 balance。也可以 deposit。curl -X PATCH -H Content-Type: application/json -d "balance:50" http:/127.0.0.1:9545/api/1/channels/0x721d03ec76ccbfdce0ad3fc4602b59d622304d25 #留意是 channel_address 6. A 向 向 B 节点 发起 RTT 转账 1 curl -H Content-Type:application/json -X POST -data "amount:1" http:/127.0.0.1:9545/api/1/transfers/0x0f114a1e9db192502e7856309cc899952b3db1ed/0x17cb47f30f844b6969e8cc6cd3c506eb25f17819# 节点地址返回:amount: 1,identifier: 6913515701748887592,initiator_address: 0x607f1724469a0beeed74e1429c3c5adcf8290fa3,target_address: 0x17cb47f30f844b6969e8cc6cd3c506eb25f17819,token_address: 0x0f114a1e9db192502e7856309cc899952b3db1ed转账胜利。此时再次查询状态通道信息 curl -X GET http:/127.0.0.1:9545/api/1/channelsbalance: 99,channel_address: 0x721d03ec76ccbfdce0ad3fc4602b59d622304d25,partner_address: 0x17cb47f30f844b6969e8cc6cd3c506eb25f17819,reveal_timeout: 10,settle_timeout: 600,state: opened,token_address: 0x0f114a1e9db192502e7856309cc899952b3db1ed curl -X GET http:/127.0.0.1:9546/api/1/channelsbalance: 1,channel_address: 0x721d03ec76ccbfdce0ad3fc4602b59d622304d25,partner_address: 0x607f1724469a0beeed74e1429c3c5adcf8290fa3,reveal_timeout: 10,settle_timeout: 600,state: opened,token_address: 0x0f114a1e9db192502e7856309cc899952b3db1ed从返回可以看出,状态通道中 A 余额为 99,B 余额为 1。7. 查询通道的事务信息 curl -X GET http:/127.0.0.1:9545/api/1/events/channels/0x721d03ec76ccbfdce0ad3fc4602b59d622304d25#状态通道地址 返回block_number: 1892761, participant: 607f1724469a0beeed74e1429c3c5adcf8290fa3, event_type: ChannelNewBalance, balance: 100, token_address: 0f114a1e9db192502e7856309cc899952b3db1ed, block_number: 1892768, participant: 607f1724469a0beeed74e1429c3c5adcf8290fa3, event_type: ChannelNewBalance, balance: 600, token_address: 0f114a1e9db192502e7856309cc899952b3db1ed, block_number: 1892781, participant: 607f1724469a0beeed74e1429c3c5adcf8290fa3, event_type: ChannelNewBalance, balance: 650, token_address: 0f114a1e9db192502e7856309cc899952b3db1ed, block_number: 1892786, event_type: EventTransferSentSuccess, identifier: 6913515701748887592会看到状态通道全部的事务消息。8. 关闭状态通道 curl -X PATCH -H Content-Type: application/json -d "state:closed" http:/127.0.0.1:9545/api/1/channels/0x721d03ec76ccbfdce0ad3fc4602b59d622304d25 #留意是 channel_address此时再次查询状态通道信息 curl -X GET http:/127.0.0.1:9545/api/1/channels 9. 节点离开 RTT network (可选)curl -X DELETE -H Content-Type: application/json -d "only_receiving_channels: false" http:/127.0.0.1:9546/api/1/connection/0x0f114a1e9db192502e7856309cc899952b3db1ed其他 为了简化测试体验,Raiden 供应了一个特别的 Raiden Echo Node。Raiden Echo Node 地址是 0x02f4b6bc65561a792836212ebc54434db0ab759a 。体验过程可以只建立一个节点与 Raiden Echo Node 交互。