实际项目中,我们从业务视角来看,智能合约只需要做两件事,其一是如何定义数据的结构和读写方式,其二是如何处理数据并对外提供服务接口。
为了更好的做好模块抽象和合约结构分层,将这两件事分开,既是将业务控制逻辑和数据从合约代码层面就做好分离,这样的处理在复杂业务逻辑场景中经过实践是当前被认为最佳的模式。
这个模式简称为CD(Controller-Data)模式。将合约分为两类:控制器合约(Controller Contract)与数据合约(Data Contract)。
现在我们通过简单的例子来演练一下
准备:
1.基于Ganache私链来操作
2.移植脚本文件样例文件如下
文件名:4_example_migration.js
module.exports = function(deployer) {
// deployment steps
deployer.deploy(MyContract);
};
注意,文件名称是以数字开头以文件描述结尾。为了记录migration文件是否成功运行,所以文件名称中的前缀编号是必需的。后缀纯粹是为了便于增加阅读者的可读性和理解。
3.布署命令解读
truffle compile
可选参数:
- --compile-all: 强制编译所有合约。
- --network 名称:指定使用的网络,保存编译的结果到指定的网络上。
truffle migrate 如果缺省表示从上次完成的部署开始
可选的参数:
- -f <number> 部署特定的合约,number是部署脚本文件名前面的数字
- --network <name> 指定网络
- --compile-all 编译所有合约
- --verbose-rpc 记录truffle和以太坊客户端之间的通信
- --reset: 从头运行所有的移植。
- --to number:将版本从当前版本移植到序号指定的版本。
开始
1.首先,我们先创建一个新项目,myUpdate
> mkdir myUpdate
> truffle init //初始化项目
我们把合约分别命名为Data.sol,Control.sol
合约测试代码contracts/Data.sol
pragma solidity ^0.4.5;
contract Data {
mapping( address => uint256 ) public balanceOf;
function setBalance (address _addr,uint256 v) public {
balanceOf[_addr] = v;
}
}
使用balanceOf来存储用户余额变化
合约测试代码contracts/Control.sol
pragma solidity ^0.4.5;
import './Data.sol';
contract Control {
Data dataContract;
constructor (address _dataaddr) public {
dataContract = Data(_dataaddr);
}
function addAmount(address _addr) public {
dataContract.setBalance(_addr,99);
}
function getAmount(address _addr) public view returns (uint256) {
return dataContract.balanceOf(_addr);
}
}
逻辑层合约引入数据层合约,分别用add,get方法来控制余额修改和获取
接下来我们编写布署脚本,分别命名为,2_deploy_all.js,3_deploy_control.js
migrations/2_deploy_all.js
var Data = artifacts.require("Data");
var Control = artifacts.require('Control');
module.exports = function(deployer) {
deployer.deploy(Data).then(function() {
return deployer.deploy(Control, Data.address);
});
};
migrations/3_deploy_control.js
var Control = artifacts.require('Control');
module.exports = function(deployer) {
deployer.deploy(Control, '0x64e8d52dd97a736776cc3f5e43fc14652ca63b9d');
};
配置truffle.js
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*" // Match any network id
}
}
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
};
到目前为止,所以编码工作已经完成,接下来我们测试我们的合约与升级
truffle compile
Compiling ./contracts/Control.sol...
Compiling ./contracts/Data.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts
truffle migrate
Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x3a99affa612a98b9d3637a68b9653dacbf90cdb1ec82d5a923a9b8dbf1b4d1f0
Migrations: 0xeac98cb1747bc444cbedada623962ed4cecc7404
Saving successful migration to network...
... 0x537bf3aaaa67bdf2ac04f893bd0732c06cbd18df05e5da8cfb983dc4a3667b79
Saving artifacts...
Running migration: 2_deploy_all.js
Replacing Data...
... 0x156435affe2ed95c79b0950b335115876f10ddaeb6c8fe6cf7f3f2e25119abe0
Data: 0x0d3785d06a2e47dede591e030d8882b61725a0eb
Replacing Control...
... 0x6b38ec968e2795368974d6e29b26986f6855a6562247425edb9764049fa4a364
Control: 0x4f568294bf77708816d26b12bb9d4be0e93ac980
Saving successful migration to network...
... 0x349c9e4744bb1dd6e7fb58509c55262a5e0821eee619e551281e571e8702beac
Saving artifacts...
Running migration: 3_deploy_control.js-control.js
Replacing Control...
... 0x58b597cca558afd38e4882c954719bc35870b889e08c839562b376167c063943
Control: 0x1829c2ef807ebfe0fa54c5f60bd8103ee161969d
Saving successful migration to network...
... 0x92b6b89efe42f664bbd68a28f038e109d7b210d86825e2848bda40e471b38256
Saving artifacts...
上面布署日志可以看出,Data合约地址是0x0d3785d06a2e47dede591e030d8882b61725a0eb,然后我们把3_deploy_control.js里这个合约地址修改为当前合约地址,为单独升级逻辑合约使用
进入控制台
truffle console
web3.eth.accounts
//可以查看当前网络已创建的账号
[ '0x9a87db858e99ba82d715c5b4925221a44d026b70',
'0xcd7dde8ee1a57823b7f22104b6c8979d8bfbb9d6',
'0x48fe9dd0fd06831009ba81e509e607025c82c49c',
'0x2a4c95b87be7fd8b87473732e31ec10391b89566',
'0x3bf570b45eba233b3d5a560c61f7ffc6b88e5460',
'0x1fd037e1fb3f778534955be8a5471d97533449bd',
'0x687e58f3a73bd64b02ac1623ee145dc5896af770',
'0xaf849eeb028c8d4b742d67fa2bcc0a2b3b9fb332',
'0xdf35a4958c8dc7752ead2fdcec9734992fad8ad5',
'0x3253d96da15d58d7d1a13f73df6d838cea0376bb',
'0x82f26c825918f369067e1f90a39c0f4fd81d2c45',
'0xf28e10ded92b18711770ea0168f91fb65b29a7de',
'0x7e959e434831ffc3be8a3aee0a1f4ccd8902fef5',
'0x87cb6858f011333594c810a94460357a9d7bc442',
'0xadef9f52aee5bcfc055405d77f610af65eff9114',
'0xc1d748b074d63b9b77828f112d5c2de857904e5e',
'0x51b07e43ac84002c44e323b803f36995d0f7ac3f',
'0x2df4e067f2096fe34e181012727d24f84703a54a',
'0x6dd335ee207ca4b039c3d935a633e47c8015df70',
'0x4cc38e67132f4e0312a5d915e07dba834da7f161' ]
var contract;Control.deployed().then(function(instance){contract= instance;});
//定义一个常量contract并实例化
contract.getAmount('0x4cc38e67132f4e0312a5d915e07dba834da7f161’);
BigNumber { s: 1, e: 0, c: [ 0 ] }
当前账号余额为0,现在我们设置它的余额
contract.addAmount('0x4cc38e67132f4e0312a5d915e07dba834da7f161');
{ tx: '0xd78509b83aece326a304275d4c274e87fbe80ccc2685a0070483e8f99200cc1e',
receipt:
{ transactionHash: '0xd78509b83aece326a304275d4c274e87fbe80ccc2685a0070483e8f99200cc1e',
transactionIndex: 0,
blockHash: '0x78f4398675e1fe574a1e3ba3279e2b1dce74ec35dcd0e686f2a3dc3d5d24fa1e',
blockNumber: 41,
gasUsed: 45072,
cumulativeGasUsed: 45072,
contractAddress: null,
logs: [],
status: '0x01',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
logs: []
}
我们再次查看账号
contract.getAmount('0x4cc38e67132f4e0312a5d915e07dba834da7f161');
BigNumber { s: 1, e: 1, c: [ 99 ] }
可以看见我们设置已经成功,接着我们继续设置其它账号,方便对照升级有没有生效
contract.addAmount('0x6dd335ee207ca4b039c3d935a633e47c8015df70');
contract.addAmount('0x2df4e067f2096fe34e181012727d24f84703a54a');
当逻辑合约升级,而数据不用升级,我们只需要升级逻辑合约即可
我们把逻辑合约里的addAmount方法修改一下
function addAmount(address _addr) public {
dataContract.setBalance(_addr,100);
}
truffle compile
//重新编译合译
Compiling ./contracts/Control.sol...
Compiling ./contracts/Data.sol...
Writing artifacts to ./build/contracts
truffle migrate -f 3
//只升级3_deploy_control.js配置的逻辑合约
Using network 'development'.
Running migration: 3_deploy_control.js
Replacing Control...
... 0xab559a0d07c42f0aed2203aee04fd79a3b70848c7e8d96aa7970928a13f61a47
Control: 0x153b0299b6ba2419338bfa5c11a0fd8155046290
Saving successful migration to network...
... 0xa99b248a30478ca7e53df845059cb38dd689a5d0d8273548308c7425c7d23c40
Saving artifacts...
我们再次进入控制台
truffle console
contract.getAmount('0x2df4e067f2096fe34e181012727d24f84703a54a');
BigNumber { s: 1, e: 1, c: [ 99 ] }
可以看出,数据层被保留了
contract.getAmount('0x6dd335ee207ca4b039c3d935a633e47c8015df70');
BigNumber { s: 1, e: 1, c: [ 99 ] }
contract.getAmount('0x4cc38e67132f4e0312a5d915e07dba834da7f161');
BigNumber { s: 1, e: 1, c: [ 99 ] }
完美
我们对0x6dd335ee207ca4b039c3d935a633e47c8015df70账号重设余额
contract.addAmount('0x6dd335ee207ca4b039c3d935a633e47c8015df70');
{ tx: '0x8a740db89856642fc09c194ac000b4b117ac2ec1208372f11bcb09ce1e2d75b6',
receipt:
{ transactionHash: '0x8a740db89856642fc09c194ac000b4b117ac2ec1208372f11bcb09ce1e2d75b6',
transactionIndex: 0,
blockHash: '0x2ed1d5350bdb9576f062f85b1130fd2b6b385946717a27848156120bfdd51bf2',
blockNumber: 46,
gasUsed: 30072,
cumulativeGasUsed: 30072,
contractAddress: null,
logs: [],
status: '0x01',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' },
logs: [] }
重新查看一下设置余额是否成功
contract.getAmount('0x6dd335ee207ca4b039c3d935a633e47c8015df70');
BigNumber { s: 1, e: 2, c: [ 100 ] }
余额已经由99变成100了,完全符合当初的设想
查看其它2个账号的余额,看是否有变化
contract.getAmount('0x4cc38e67132f4e0312a5d915e07dba834da7f161');
BigNumber { s: 1, e: 1, c: [ 99 ] }
contract.getAmount('0x2df4e067f2096fe34e181012727d24f84703a54a');
BigNumber { s: 1, e: 1, c: [ 99 ] }
完全正确,不受逻辑合约升级影响
到目前为止,整个合约创建、升级已经全部完成。