github.com/codingfuture/orig-energi3@v0.8.4/energi/contracts/test/GovernedProxy.spec.js (about) 1 // Copyright 2019 The Energi Core Authors 2 // This file is part of the Energi Core library. 3 // 4 // The Energi Core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The Energi Core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Energi Governance system is the fundamental part of Energi Core. 18 19 'use strict'; 20 21 const GovernedProxy = artifacts.require('GovernedProxy'); 22 const MockProxy = artifacts.require('MockProxy'); 23 const MockContract = artifacts.require('MockContract'); 24 const MockSporkRegistry = artifacts.require('MockSporkRegistry'); 25 const MockProposal = artifacts.require('MockProposal'); 26 27 const common = require('./common'); 28 29 contract("GovernedProxy", async accounts => { 30 let first; 31 let second; 32 let third; 33 let fourth; 34 let proxy; 35 let proxy_abi; 36 let registry; 37 const weeks = 60*60*24*7; 38 39 before(async () => { 40 registry = await MockSporkRegistry.deployed(); 41 const registry_proxy = await MockProxy.new(); 42 await registry_proxy.setImpl(registry.address); 43 first = await MockContract.new(registry.address); 44 proxy = await GovernedProxy.new(first.address, registry_proxy.address); 45 second = await MockContract.new(proxy.address); 46 third = await MockContract.new(proxy.address); 47 fourth = await MockContract.new(proxy.address); 48 proxy_abi = await MockContract.at(proxy.address); 49 }); 50 51 it('should refuse migrate()', async () => { 52 try { 53 await proxy.migrate(second.address, { from: accounts[0] }); 54 assert.fail("It must fail"); 55 } catch (e) { 56 assert.match(e.message, /Good try/); 57 } 58 }); 59 60 it('should refuse destroy()', async () => { 61 try { 62 await proxy.destroy(second.address, { from: accounts[0] }); 63 assert.fail("It must fail"); 64 } catch (e) { 65 assert.match(e.message, /Good try/); 66 } 67 }); 68 69 70 it('should proxy calls', async () => { 71 const res = await proxy_abi.getAddress({ from: accounts[0] }); 72 assert.equal(first.address.valueOf(), res.valueOf()); 73 }); 74 75 it('should listUpgradeProposals() empty', async () => { 76 const res = await proxy.listUpgradeProposals(); 77 common.stringifyBN(web3, res); 78 expect(res).eql([]); 79 }); 80 81 it('should refuse proposal - same impl', async () => { 82 try { 83 await proxy.proposeUpgrade( 84 first.address, 2 * weeks, 85 { from: accounts[0], value: web3.utils.toWei('1', 'ether') }); 86 assert.fail("It must fail"); 87 } catch (e) { 88 assert.match(e.message, /Already active!/); 89 } 90 }); 91 92 it('should refuse proposal - wrong proxy', async () => { 93 try { 94 await proxy.proposeUpgrade( 95 registry.address, 2 * weeks, 96 { from: accounts[0], value: web3.utils.toWei('1', 'ether') }); 97 assert.fail("It must fail"); 98 } catch (e) { 99 //assert.match(e.message, /Wrong proxy!/); 100 assert.match(e.message, /revert/); 101 } 102 103 const evt = await proxy.getPastEvents('UpgradeProposal', common.evt_last_block); 104 expect(evt).lengthOf(0); 105 }); 106 107 it('should accept proposal', async () => { 108 await proxy.proposeUpgrade( 109 second.address, 2 * weeks, 110 // NOTE: it's mock registry - no fee check 111 { from: accounts[0], value: '1' }); 112 113 const evt = await proxy.getPastEvents('UpgradeProposal', common.evt_last_block); 114 expect(evt).lengthOf(1); 115 expect(evt[0].args).include.keys('impl', 'proposal'); 116 }); 117 118 it('should listUpgradeProposals() accepted', async () => { 119 const evt = await proxy.getPastEvents('UpgradeProposal', common.evt_last_block); 120 121 const res = await proxy.listUpgradeProposals(); 122 common.stringifyBN(web3, res); 123 expect(res).eql([ evt[0].args.proposal.toString() ]); 124 }); 125 126 it('should refuse upgrade - Not accepted!', async () => { 127 const { logs } = await proxy.proposeUpgrade( 128 second.address, 2 * weeks, 129 { from: accounts[0], value: '1' }); 130 131 assert.equal(logs.length, 1); 132 const proposal = logs[0].args['1']; 133 134 try { 135 await proxy.upgrade(proposal); 136 assert.fail("It must fail"); 137 } catch (e) { 138 assert.match(e.message, /Not accepted!/); 139 } 140 }); 141 142 it('should refuse upgrade - Not registered!', async () => { 143 let proposal = await MockProposal.new(proxy.address, fourth.address); 144 145 try { 146 await proxy.upgrade(proposal.address); 147 assert.fail("It must fail"); 148 } catch (e) { 149 assert.match(e.message, /Not registered!/); 150 } 151 152 const evt = await proxy.getPastEvents('Upgraded', common.evt_last_block); 153 expect(evt).lengthOf(0); 154 }); 155 156 it('should accept upgrade', async () => { 157 const { logs } = await proxy.proposeUpgrade( 158 second.address, 2 * weeks, 159 { from: accounts[0], value: '1' }); 160 assert.equal(logs.length, 1); 161 const proposal = await MockProposal.at(logs[0].args['1']); 162 163 await proposal.setAccepted(); 164 165 const res = await proxy.upgrade(proposal.address); 166 assert.equal(res.logs.length, 1); 167 168 const evt = await proxy.getPastEvents('Upgraded', common.evt_last_block); 169 expect(evt).lengthOf(1); 170 expect(evt[0].args).include.keys('impl', 'proposal'); 171 }); 172 173 it('should refuse upgrade AFTER upgrade - Not registered!', async () => { 174 const { logs } = await proxy.proposeUpgrade( 175 third.address, 2 * weeks, 176 { from: accounts[0], value: '1' }); 177 const proposal = await MockProposal.at(logs[0].args['1']); 178 179 await proposal.setAccepted(); 180 await proxy.upgrade(proposal.address); 181 182 try { 183 await proxy.upgrade(proposal.address); 184 assert.fail("It must fail"); 185 } catch (e) { 186 assert.match(e.message, /Not registered!/); 187 } 188 }); 189 190 it('should refuse upgrade - Already active!', async () => { 191 let proposal1 = await proxy.proposeUpgrade( 192 fourth.address, 2 * weeks, 193 { from: accounts[0], value: '1' }); 194 let proposal2 = await proxy.proposeUpgrade( 195 fourth.address, 2 * weeks, 196 { from: accounts[0], value: '1' }); 197 proposal1 = await MockProposal.at(proposal1.logs[0].args['1']); 198 proposal2 = await MockProposal.at(proposal2.logs[0].args['1']); 199 await proposal1.setAccepted(); 200 await proposal2.setAccepted(); 201 await proxy.upgrade(proposal1.address); 202 203 try { 204 await proxy.upgrade(proposal2.address); 205 assert.fail("It must fail"); 206 } catch (e) { 207 assert.match(e.message, /Already active!/); 208 } 209 }); 210 211 it('should refuse collect - Not registered!', async () => { 212 let proposal = await MockProposal.new(proxy.address, fourth.address); 213 214 try { 215 await proxy.collectUpgradeProposal(proposal.address); 216 assert.fail("It must fail"); 217 } catch (e) { 218 assert.match(e.message, /Not registered!/); 219 } 220 221 const evt = await proxy.getPastEvents('Upgraded', common.evt_last_block); 222 expect(evt).lengthOf(0); 223 }); 224 225 226 it('should collectUpgradeProposal()', async () => { 227 const start_proposals = (await proxy.listUpgradeProposals()).length; 228 229 let tmp = await MockContract.new(proxy.address); 230 let proposal = await proxy.proposeUpgrade( 231 tmp.address, 2 * weeks, 232 { from: accounts[0], value: '1' }); 233 const proposal_addr = proposal.logs[0].args['1']; 234 proposal = await MockProposal.at(proposal_addr); 235 const proposals_after1 = await proxy.listUpgradeProposals(); 236 expect(proposals_after1.length).equal(start_proposals + 1); 237 expect(proposals_after1).include(proposal_addr); 238 239 await common.moveTime(web3, 2 * weeks + 1); 240 await proxy.collectUpgradeProposal(proposal_addr); 241 const proposals_after2 = await proxy.listUpgradeProposals(); 242 expect(proposals_after2.length).equal(start_proposals); 243 expect(proposals_after2).not.include(proposal_addr); 244 245 try { 246 await proxy.collectUpgradeProposal(proposal_addr); 247 assert.fail("It must fail"); 248 } catch (e) { 249 assert.match(e.message, /Not registered!/); 250 } 251 }); 252 });