github.com/codingfuture/orig-energi3@v0.8.4/energi/contracts/test/SporkRegistryV2.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 MockProxy = artifacts.require('MockProxy');
    22  const MockContract = artifacts.require('MockContract');
    23  const SporkRegistryV2 = artifacts.require('SporkRegistryV2');
    24  const ISporkRegistry = artifacts.require('ISporkRegistry');
    25  const IUpgradeProposal = artifacts.require('IUpgradeProposal');
    26  const UpgradeProposalV1 = artifacts.require('UpgradeProposalV1');
    27  
    28  const MasternodeRegistryV1 = artifacts.require('MasternodeRegistryV1');
    29  const MasternodeTokenV1 = artifacts.require('MasternodeTokenV1');
    30  
    31  const common = require('./common');
    32  
    33  contract("SporkRegistryV2", async accounts => {
    34      const s = {
    35          artifacts,
    36          accounts,
    37          assert,
    38          it,
    39          web3,
    40      };
    41  
    42      before(async () => {
    43          s.emergency_signer = accounts[1];
    44  
    45          s.registry_orig = await MasternodeRegistryV1.deployed();
    46          s.registry = await MasternodeRegistryV1.at(await s.registry_orig.proxy());
    47  
    48          s.mntoken_orig = await MasternodeTokenV1.deployed();
    49          s.mntoken = await MasternodeTokenV1.at(await s.mntoken_orig.proxy());
    50  
    51          s.orig = await SporkRegistryV2.deployed();
    52          s.proxy = await MockProxy.at(await s.orig.proxy());
    53          s.mnregistry_proxy = await MockProxy.at(await s.orig.mnregistry_proxy());
    54          s.fake = await MockContract.new(s.proxy.address);
    55  
    56          s.orig = await SporkRegistryV2.new(s.proxy.address, s.mnregistry_proxy.address, s.emergency_signer);
    57          s.proxy_abi = await SporkRegistryV2.at(s.proxy.address);
    58          s.token_abi = await ISporkRegistry.at(s.proxy.address);
    59          await s.proxy.setImpl(s.orig.address);
    60  
    61          Object.freeze(s);
    62      });
    63  
    64      after(async () => {
    65          const impl = await SporkRegistryV2.new(s.proxy.address, s.mnregistry_proxy.address, common.emergency_signer);
    66          await s.proxy.setImpl(impl.address);
    67      });
    68  
    69      describe('common pre', () => common.govPreTests(s) );
    70  
    71      //---
    72      describe('Primary', () => {
    73          const { fromAscii, toBN, toWei } = web3.utils;
    74  
    75          const fee_amount = toBN(toWei('10000', 'ether'));
    76  
    77          const collateral1 = toBN(toWei('50000', 'ether'));
    78          const owner1 = accounts[0];
    79          const masternode1 = accounts[9];
    80          const ip1 = toBN(0x12345678);
    81          const enode_common = '123456789012345678901234567890'
    82          const enode1 = [fromAscii(enode_common + '11'), fromAscii(enode_common + '11')];
    83          let orig_spork = null;
    84  
    85          before(async () => {
    86              orig_spork = await s.proxy.spork_proxy();
    87              await s.proxy.setSporkProxy(s.proxy.address);
    88  
    89              await s.mntoken.depositCollateral({
    90                  from: owner1,
    91                  value: collateral1,
    92              });
    93              await s.registry.announce(masternode1, ip1, enode1, {from: owner1});
    94          });
    95  
    96          after(async () => {
    97              await s.mntoken.withdrawCollateral(collateral1, {
    98                  from: owner1,
    99              });
   100  
   101              await s.proxy.setSporkProxy(orig_spork);
   102          });
   103  
   104          it ('should handle createUpgradeProposal() errors for emergency signer', async () => {
   105              try {
   106                  await s.proxy.proposeUpgrade(
   107                      s.fake.address, 14*24*60*60,
   108                      { value: fee_amount, from: s.emergency_signer });
   109                  assert.fail('It should fail');
   110              } catch (e) {
   111                  assert.match(e.message, /Invalid fee/);
   112              }
   113  
   114              try {
   115                  await s.proxy.proposeUpgrade(
   116                      s.fake.address, 14*24*60*60,
   117                      { value: 0, from: s.emergency_signer });
   118                  assert.fail('It should fail');
   119              } catch (e) {
   120                  assert.match(e.message, /Invalid period/);
   121              }
   122          });
   123  
   124          it ('should createUpgradeProposal() for emergency signer', async () => {
   125              await s.proxy.proposeUpgrade(
   126                  s.fake.address, 0,
   127                  { value: 0, from: s.emergency_signer });
   128  
   129              const evt = await s.proxy.getPastEvents('UpgradeProposal', common.evt_last_block);
   130              const proposal = await IUpgradeProposal.at(evt[0].args.proposal.toString());
   131  
   132              expect(await proposal.impl()).equal(s.fake.address);
   133              expect(await proposal.isAccepted()).true;
   134              expect(await proposal.isFinished()).true;
   135              await proposal.voteAccept();
   136              await proposal.voteReject();
   137          });
   138  
   139          it ('should refuse to createUpgradeProposal() with invalid fee', async () => {
   140              try {
   141                  await s.token_abi.createUpgradeProposal(
   142                      s.fake.address, 14*24*60*60, accounts[0],
   143                      { value: fee_amount.add(toBN(1)) });
   144                  assert.fail('It should fail');
   145              } catch (e) {
   146                  assert.match(e.message, /Invalid fee/);
   147              }
   148  
   149              await s.token_abi.createUpgradeProposal(
   150                  s.fake.address, 14*24*60*60, accounts[0],
   151                  { value: fee_amount });
   152  
   153              try {
   154                  await s.token_abi.createUpgradeProposal(
   155                      s.fake.address, 14*24*60*60, accounts[0],
   156                      { value: fee_amount.sub(toBN(1)) });
   157                  assert.fail('It should fail');
   158              } catch (e) {
   159                  assert.match(e.message, /Invalid fee/);
   160              }
   161          });
   162  
   163          it ('should refuse to createUpgradeProposal() with invalid period', async () => {
   164              try {
   165                  await s.token_abi.createUpgradeProposal(
   166                      s.fake.address, 14*24*60*60-1, accounts[0],
   167                      { value: fee_amount });
   168                  assert.fail('It should fail');
   169              } catch (e) {
   170                  assert.match(e.message, /Period min/);
   171              }
   172  
   173              await s.token_abi.createUpgradeProposal(
   174                  s.fake.address, 14*24*60*60, accounts[0],
   175                  { value: fee_amount });
   176  
   177              await s.token_abi.createUpgradeProposal(
   178                  s.fake.address, 365*24*60*60, accounts[0],
   179                  { value: fee_amount });
   180  
   181              try {
   182                  await s.token_abi.createUpgradeProposal(
   183                      s.fake.address, 365*24*60*60+1, accounts[0],
   184                      { value: fee_amount });
   185                  assert.fail('It should fail');
   186              } catch (e) {
   187                  assert.match(e.message, /Period max/);
   188              }
   189          });
   190  
   191          it ('should consensusGasLimits()', async () => {
   192              const res = await s.token_abi.consensusGasLimits();
   193              expect(res[0].toString()).eql(web3.utils.toBN(15e6).toString());
   194              expect(res[1].toString()).eql(web3.utils.toBN(30e6).toString());
   195          });
   196  
   197          describe('UpgradeProposalV1', () => {
   198              it ('show allow setFee() only by creator', async () => {
   199                  const proposal = await UpgradeProposalV1.new(
   200                      accounts[2], s.fake.address, s.mnregistry_proxy.address,
   201                      14*24*60*60, accounts[1]);
   202  
   203                  try {
   204                      await proposal.setFee({ value: 1, from: accounts[2] });
   205                      assert.fail('It should fail');
   206                  } catch (e) {
   207                      assert.match(e.message, /Only parent/);
   208                  }
   209              });
   210  
   211              it ('show allow setFee() only by proxy', async () => {
   212                  const proposal = await UpgradeProposalV1.new(
   213                      accounts[2], s.fake.address, s.mnregistry_proxy.address,
   214                      14*24*60*60, accounts[1]);
   215  
   216                  try {
   217                      await proposal.destroy();
   218                      assert.fail('It should fail');
   219                  } catch (e) {
   220                      assert.match(e.message, /Only parent/);
   221                  }
   222  
   223                  await proposal.destroy({ from: accounts[2] });
   224              });
   225          });
   226      });
   227  
   228      //---
   229      describe('common post', () => common.govPostTests(s) );
   230  });
   231