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