以太坊系列之二十一 使用web3部署比较复杂的智能合约 搭建私链上的雷电网络

前面博文中已经有一篇介绍如何使用web3部署智能合约了,但是针对比较复杂的智能合约那种方式就不行了.

实际上这篇文章的起因是因为打算研究雷电网络,就想在自己的私链上部署一个雷电网络的合约.结果发现居然很多问题.

1 雷电网络智能合约简单介绍

雷电网络要正常工作必须借助两个事先部署在区块链上的合约才能正常工作,具体来说就是registry和endpointregistry.如果去查看雷电网络的代码可以看到这两个合约地址是事先写好在代码中的.

这两个合约比较复杂,尤其是registry.sol,依赖复杂.我们主要来看几个主要文件之间的依赖关系:
Registry.sol<---ChannelManagerContract.sol<-----ChannelManagerLibrary.sol<-------NettingChannelContract.sol<------NettingChannelLibrary.sol
中间名字里面有Library的,这些文件是library而不是contract,所以在solc编译的时候是不会编译进生成的二进制code中,必须先部署library,然后在对其进行link.
比如:Registry.sol<---ChannelManagerContract.sol<-----ChannelManagerLibrary.sol
那么在生成Registry.sol的code时,里面不会包含ChannelManagerLibrary.sol的代码,只会包含ChannelManagerContract.sol.的代码.这有点类似于前者是动态链接,而后者是静态链接. 附上remix生成的registry的bin code,里面包含了一些字符串__localhost/ChannelManagerLibrary.sol:__,这明显是错误的,这实际上是需要替换成ChannelManagerLibrary部署后的地址才能正常工作.
6060604052341561000f57600080fd5b6040516020806109ae8339810160405280805160018054600160a060020a03909216600160a060020a0319909216919091179055505061095a806100546000396000f30060606040526004361061005e5763ffffffff60e060020a6000350416630b74b620811461006e578063238bfba2146100d45780636785b5001461010f5780636cb30fee146101225780639d76ea5814610141578063f26c6aed14610154575b341561006957600080fd5b600080fd5b341561007957600080fd5b610081610176565b60405160208082528190810183818151815260200191508051906020019060200280838360005b838110156100c05780820151838201526020016100a8565b505050509050019250505060405180910390f35b34156100df57600080fd5b6100f3600160a060020a03600435166102c8565b604051600160a060020a03909116815260200160405180910390f35b341561011a57600080fd5b610081610350565b341561012d57600080fd5b610081600160a060020a03600435166103b9565b341561014c57600080fd5b6100f3610446565b341561015f57600080fd5b6100f3600160a060020a0360043516602435610455565b61017e6108d5565b6000806101896108d5565b60008060006002805490506002026040518059106101a45750595b9080825280602002602001820160405250935060009450600095505b6002548610156102bd5760028054879081106101d857fe5b6000918252602082200154600160a060020a031693508390636d2381b390604051608001526040518163ffffffff1660e060020a028152600401608060405180830381600087803b151561022b57600080fd5b6102c65a03f1151561023c57600080fd5b505050604051805190602001805190602001805190602001805190505092505091508184868151811061026b57fe5b600160a060020a03909216602092830290910190910152600194909401938084868151811061029657fe5b600160a060020a0390921660209283029091019091015260019586019594909401936101c0565b509195945050505050565b600073__localhost/ChannelManagerLibrary.sol:__638a1c00e28284816040516020015260405160e060020a63ffffffff85160281526004810192909252600160a060020a0316602482015260440160206040518083038186803b151561033057600080fd5b6102c65a03f4151561034157600080fd5b50505060405180519392505050565b6103586108d5565b60028054806020026020016040519081016040528092919081815260200182805480156103ae57602002820191906000526020600020905b8154600160a060020a03168152600190910190602001808311610390575b505050505090505b90565b6103c16108d5565b6003600083600160a060020a0316600160a060020a0316815260200190815260200160002080548060200260200160405190810160405280929190818152602001828054801561043a57602002820191906000526020600020905b8154600160a060020a0316815260019091019060200180831161041c575b50505050509050919050565b600154600160a060020a031690565b60008060008060008060008061046a8a6102c8565b600160a060020a033381166000908152600360205260408082208e84168352912092995097509095508716156104f4576104a3876108b5565b156104ad57600080fd5b7fda8d2f351e0f7c8c368e631ce8ab15973e7582ece0c347d75a5cff49eb899eb7338b604051600160a060020a039283168152911660208201526040908101905180910390a15b73__localhost/ChannelManagerLibrary.sol:__63941583a560008c8c826040516020015260405160e060020a63ffffffff86160281526004810193909352600160a060020a039091166024830152604482015260640160206040518083038186803b151561056357600080fd5b6102c65a03f4151561057457600080fd5b5050506040518051945050600160a060020a038716156106a357505050600160a060020a038085166000908152600560209081526040808320543385168085526004808552838620968e168652958452828520549584528285209085529092529091205460028054929392859190859081106105ec57fe5b906000526020600020900160006101000a815481600160a060020a030219169083600160a060020a0316021790555083868381548110151561062a57fe5b906000526020600020900160006101000a815481600160a060020a030219169083600160a060020a0316021790555083858281548110151561066857fe5b6000918252602090912001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a039290921691909117905561084d565b60028054600181016106b583826108e7565b506000918252602090912001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a03861617905585548690600181016106f983826108e7565b506000918252602090912001805473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a038616179055845485906001810161073d83826108e7565b9160005260206000209001600086909190916101000a815481600160a060020a030219169083600160a060020a03160217905550506001600280549050036005600086600160a060020a0316600160a060020a031681526020019081526020016000208190555060018680549050036004600033600160a060020a0316600160a060020a0316815260200190815260200160002060008c600160a060020a0316600160a060020a03168152602001908152602001600020819055506001858054905003600460008c600160a060020a0316600160a060020a03168152602001908152602001600020600033600160a060020a0316600160a060020a03168152602001908152602001600020819055505b7f7bd269696a33040df6c111efd58439c9c77909fcbe90f7511065ac277e175dac84338c8c604051600160a060020a039485168152928416602084015292166040808301919091526060820192909252608001905180910390a1509198975050505050505050565b6000813b818111156108ca57600191506108cf565b600091505b50919050565b60206040519081016040526000815290565b81548183558181151161090b5760008381526020902061090b918101908301610910565b505050565b6103b691905b8082111561092a5760008155600101610916565b50905600a165627a7a7230582000e4924a646ac6cc6f9a6b01f9d4d7d6005bc46a391ce5cd70613d5ad04b015c0029

2 remix 无法部署使用了library的contract

网上也找了相关的文章看了看,总的来说就是因为如果一个contract使用了library(注意不是import 其他sol),那么编译生成的二进制code是不完整的,必须进行link,目前remix (solidity IDE)是不支持的,所以只能绕过.

比如在部署registry.sol时,具体报错:

Error deploying required libraries: Library localhost/ChannelManagerLibrary.sol: not found

3 使用web3部署完整的雷电网络合约

3.1 部署NettingChannelLibrary.sol

 这是依赖的最底层,必须先部署.
部署代码和以前的类似,直接给出完整代码:
//参考文章:http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#eth-contract
var Web3 = require('web3');
console.log("Web3 Version:",Web3.version); //version 1.0 以下代码只在此版本测试,更早版本确定不行
var net = require('net');
var web3 = new Web3(new Web3.providers.IpcProvider("\\\\.\\pipe\\geth.ipc",net));
var eth=web3.eth;
deployNettingChannelLibraryContract()
function deployNettingChannelLibraryContract(){
var nettingchannellibraryContract =new web3.eth.Contract([{"constant":false,/*忽略*/}]);
nettingchannellibraryContract.deploy({
data: '0x60606040523.....',
})
.send({
from: "0x1a9ec3b0b807464e6d3398a59d6b0a369bf422fa",
gas: 15000000,
gasPrice: '30000000000000'
}, function(error, transactionHash){ })
.on('error', function(error){
console.log('deploy error')
})
.on('transactionHash', function(transactionHash){ })
.on('receipt', function(receipt){
//console.log(receipt.contractAddress) // contains the new contract address
})
.on('confirmation', function(confirmationNumber, receipt){ })
.then(function(newContractInstance){
console.log("nettingchannellibraryContract",newContractInstance.options.address) // instance with the new contract address
deployChannelManagerLibrary(newContractInstance.options.address)
});
}

后面的deployChannelManagerLibrary调用就是说部署NettingChannelLibraryContract成功以后,再去部署上一层的ChannelManagerLibrary

3.2 部署ChannelManagerLibrary

部署ChannelManagerLibrary的关键是要知道NettingChannelLibraryContract在区块链上的地址
所以必须要先对其二进制code进行替换处理.
var data='0x606060405234156105b...73__localhost/NettingChannelLibrary.sol:__63d...29'
data=data.replace(/__localhost\/NettingChannelLibrary.sol:__/g,nettingChannelLibraryAddress.replace("0x",""))
channelManagerLibraryContract.deploy({
data:data,
})

然后才能进行部署,否则会说code中的__localhost/NettingChannelLibrary.sol不是hex编码的字符串

完整的代码,为了凑字数...,由于部署registry的思路是一致的,就直接放到一起了.

function deployChannelManagerLibrary(nettingChannelLibraryAddress) {

  var data='0x606060....29'
data=data.replace(/__localhost\/NettingChannelLibrary.sol:__/g,nettingChannelLibraryAddress.replace("0x",""))
//console.log("after replace data:",data)
var channelManagerLibraryContract =new web3.eth.Contract([{"constant":false,/*忽略*/}]);
channelManagerLibraryContract.deploy({
data:data,
})
.send({
from: "0x1a9ec3b0b807464e6d3398a59d6b0a369bf422fa",
gas: 15000000,
gasPrice: '30000000000000'
}, function(error, transactionHash){ })
.on('error', function(error){
console.log('deploy channelManagerLibraryContract error')
})
.then(function(newContractInstance){
console.log("channelManagerLibraryContract",newContractInstance.options.address) // instance with the new contract address
deployRegistry(newContractInstance.options.address)
});
} function deployRegistry(channelManagerLibraryAddress) {
var data='0x6060604....29'
data=data.replace(/__localhost\/ChannelManagerLibrary.sol:__/g,channelManagerLibraryAddress.replace("0x",""))
//console.log("after replace data:",data)
var registryContract =new web3.eth.Contract([{"constant":true,/*忽略*/}]);
registryContract.deploy({
data:data,
})
.send({
from: "0x1a9ec3b0b807464e6d3398a59d6b0a369bf422fa",
gas: 15000000,
gasPrice: '30000000000000'
}, function(error, transactionHash){ })
.on('error', function(error){
console.log('deploy registryContract error')
})
.then(function(newContractInstance){
console.log("registryContract",newContractInstance.options.address) // instance with the new contract address
});
}

3.3 部署EndpointRegistry.sol

直接部署,和3.1 部署NettingChannelLibrary.sol中介绍一致,不再赘述.

3.4 完整运行结果

node deployregistry.js
Web3 Version: 1.0.0-beta.26
endPointRegistryContract: 0x7eA46670E6A75180e355F9b8fb7501d9618b6D7E
nettingchannellibraryContract 0xCeFE92604e376C99568eb951106c8ADCE9D184eF
channelManagerLibraryContract 0x15659CeC0602e76EC8e8d5325c2D7a0b4f63f86d
registryContract 0x46c9D962c6D5CDc69cF2A7617432eDe2c99a5255

拿到地址,就可以将源码中相应地址进行替换,这样就是一个你自己的雷电网络了.

使用web3部署一个比较复杂的智能合约的更多相关文章

  1. 使用web3.js监听以太坊智能合约event

    传送门: 柏链项目学院 使用web3.js监听以太坊智能合约event   当我们在前端页面调用合约时发现有些数据不会立即返回,这时还需要再调用更新数据的函数.那么这样的方法使用起来非常不便,监听ev ...

  2. 使用web3+solc编译发布以太坊智能合约

    一.环境安装: 1.安装web3工程:npm install web3 2.安装solc工程:npm install solc二.在node环境中使用 先引用所需环境: var fs = requir ...

  3. 使用Remix编译和部署以太坊智能合约

      Remix 是一個开源的 Solidity 智能合约开发环境,提供基本的编译.部署至本地或测试网络.执行合约等功能.Solidity 是 以太坊Ethereum 官方设计和支持的开发语言,专门用于 ...

  4. 使用Truffle 部署智能合约

    使用Truffle 部署智能合约 之前我们使用Geth,原生的以太坊Golang工具,分析了创世区块的参数内容,在本地创建了私有以太坊区块链,并使用两个账户进行了挖矿和转账操作,对以太坊有了基本了解. ...

  5. EOS智能合约开发(四):智能合约部署及调试(附编程示例)

    EOS智能合约开发(一):EOS环境搭建和创建节点 EOS智能合约开发(二):EOS创建和管理钱包 EOS智能合约开发(三):EOS创建和管理账号 部署智能合约的示例代码如下: $ cleos set ...

  6. 用Solidity在Truffle上构建一个HelloWorld智能合约

    原文地址:石匠的blog Truffle的环境安装在前文已经整理,这次用Solidity在Truffle写一个HelloWorld以太坊智能合约,并在testrpc上进行测试.当前的软件版本信息如下: ...

  7. 以太坊系列之十六: 使用golang与智能合约进行交互

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

  8. BOOM -- 智能合约编程

    译注:原文首发于ConsenSys开发者博客,原作者为Eva以及ConsenSys的开发团队.如果您想要获取更多及时信息,可以访问ConsenSys首页点击左下角Newsletter订阅邮件.本文的翻 ...

  9. 以太坊系列之十六:golang进行智能合约开发

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

随机推荐

  1. NGINX通过Stream转发ftp请求

    一.NGINX 1.9之前,需要安装第三方的TCP插件: http://www.cnblogs.com/i-blog/p/6165378.html 二.1.9之后直接使用Stream配置就可以了,当然 ...

  2. 我是怎么用python模仿勒索软件加密文件的(病毒)

    前言: 今天下午上学,用python写个勒索脚本然后打包成exe是个不错的选择 我们来搞事情吧.看那学校我就不想上学. 0x01:要用到的模块,各位请自行准备 import win32api,win3 ...

  3. Flask之RESTful

    5.4 Restful 2000年,Roy Thomas Fielding博士在他的博士论文<Architectural Styles and the Design of Network-bas ...

  4. Redhat 无线(Wifi)上网命令行配置

    小结两种命令行模式下配置无线wife的方法,实践测试通过(Red Hat Enterprise Linux release 6.0 Beta(Santiago)) 一.使用wpa_supplicant ...

  5. jaxb 专题一(JAXB 实现java对象与xml之间互相转换)

    首先熟悉一下JAXB实现对象与xml互转时常用的一些注解使用: 1.@XmlRootElement,用于类级别的注解,对应xml的跟元素.通过name属性定义这个根节点的名称. 2.@XmlAcces ...

  6. Halcon学习之二:摄像头获取图像和相关参数

    1.close_all_framegrabbers ( : : : ) 关闭所有图像采集设备. 2.close_framegrabber ( : : AcqHandle : ) 关闭Handle为Ac ...

  7. MSComm 串口

    MSComm 串口 http://www.docin.com/p-761416611.html http://blog.sina.com.cn/s/blog_50cfd0fc0102v27p.html

  8. Class.forName和ClassLoader.loadClass区别(转)

    Java中class是如何加载到JVM中的:1.class加载到JVM中有三个步骤    装载:(loading)找到class对应的字节码文件.    连接:(linking)将对应的字节码文件读入 ...

  9. String类的内存分配

    首先说一下Java内存分配.物理的内存是线性结构,并不存在拥有不同功能的不同区域,编译器(或者JVM)为了更高效地处理数据,会用不同的算法把内存分为各种区域,不同的区域拥有各自的特性,Java中,内存 ...

  10. 【原】Coursera—Andrew Ng机器学习—Week 2 习题—Linear Regression with Multiple Variables 多变量线性回归

    Gradient Descent for Multiple Variables [1]多变量线性模型  代价函数 Answer:AB [2]Feature Scaling 特征缩放 Answer:D ...