@@ -35,7 +35,7 @@ const CANDIDATE_LIMIT = 2000;
3535
3636describe ( "Governance" , function ( ) {
3737
38- let Governance : any ;
38+ let Governance : any , Policy : any ;
3939 let user : any , candidate1 : any , candidate2 : any ;
4040
4141 beforeEach ( async function ( ) {
@@ -59,8 +59,11 @@ describe("Governance", function () {
5959
6060 const policy_code = await ethers . provider . send ( "eth_getCode" , [ policy_deploy . target ] ) ;
6161 await ethers . provider . send ( "hardhat_setCode" , [ POLICY_PROXY , policy_code ] ) ;
62- const contract = require ( "../artifacts/solidity/Governance.sol/Governance.json" ) ;
63- Governance = new ethers . Contract ( GOV_PROXY , contract . abi , user ) ;
62+
63+ const governance_contract = require ( "../artifacts/solidity/Governance.sol/Governance.json" ) ;
64+ Governance = new ethers . Contract ( GOV_PROXY , governance_contract . abi , user ) ;
65+ const policy_contract = require ( "../artifacts/solidity/Policy.sol/Policy.json" ) ;
66+ Policy = new ethers . Contract ( POLICY_PROXY , policy_contract . abi , user ) ;
6467
6568 // Write Governance config to storage
6669 await ethers . provider . send ( "hardhat_setStorageAt" , [ GOV_PROXY , "0x1" , ethers . toBeHex ( CONSENSUS_SIZE , 32 ) ] ) ;
@@ -86,7 +89,7 @@ describe("Governance", function () {
8689 await ethers . provider . send ( "hardhat_setStorageAt" , [ GOV_PROXY , "0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6c" , ethers . toBeHex ( STANDBY_VALIDATORS [ 4 ] , 32 ) ] ) ;
8790 await ethers . provider . send ( "hardhat_setStorageAt" , [ GOV_PROXY , "0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6d" , ethers . toBeHex ( STANDBY_VALIDATORS [ 5 ] , 32 ) ] ) ;
8891 await ethers . provider . send ( "hardhat_setStorageAt" , [ GOV_PROXY , "0x31ecc21a745e3968a04e9570e4425bc18fa8019c68028196b546d1669c200c6e" , ethers . toBeHex ( STANDBY_VALIDATORS [ 6 ] , 32 ) ] ) ;
89-
92+
9093 // Write Policy config to storage
9194 await ethers . provider . send ( "hardhat_setStorageAt" , [ POLICY_PROXY , "0x2" , ethers . toBeHex ( MIN_GAS_TIP_CAP , 32 ) ] ) ;
9295 await ethers . provider . send ( "hardhat_setStorageAt" , [ POLICY_PROXY , "0x3" , ethers . toBeHex ( BASE_FEE , 32 ) ] ) ;
@@ -180,7 +183,7 @@ describe("Governance", function () {
180183 it ( "Should emit an event when a new candidate is registered" , async function ( ) {
181184 await expect (
182185 Governance . connect ( candidate1 ) . registerCandidate ( 500 , { value : REGISTER_FEE } )
183- ) . emit ( Governance , "Register " ) ;
186+ ) . emit ( Governance , "Activate " ) ;
184187 } ) ;
185188 } ) ;
186189
@@ -211,7 +214,7 @@ describe("Governance", function () {
211214
212215 await expect (
213216 Governance . connect ( candidate1 ) . exitCandidate ( )
214- ) . emit ( Governance , "Exit " ) ;
217+ ) . emit ( Governance , "Deactivate " ) ;
215218 } ) ;
216219 } ) ;
217220
@@ -276,6 +279,110 @@ describe("Governance", function () {
276279 } ) ;
277280 } ) ;
278281
282+ describe ( "deactivateCandidate" , function ( ) {
283+ let MockSysCall : any ;
284+ let GovReward : any ;
285+
286+ beforeEach ( async function ( ) {
287+ // Deploy Mock SYS_CALL
288+ const deploy_mock = await ethers . deployContract ( "MockSysCall" ) ;
289+ const code_mock = await ethers . provider . send ( "eth_getCode" , [ deploy_mock . target ] ) ;
290+ await ethers . provider . send ( "hardhat_setCode" , [ SYS_CALL , code_mock ] ) ;
291+ const contract_mock = require ( "../artifacts/solidity/test/MockSysCall.sol/MockSysCall.json" ) ;
292+ MockSysCall = new ethers . Contract ( SYS_CALL , contract_mock . abi , user ) ;
293+ // Deploy GovReward to native address
294+ const deploy_reward = await ethers . deployContract ( "GovReward" ) ;
295+ const code_reward = await ethers . provider . send ( "eth_getCode" , [ deploy_reward . target ] ) ;
296+ await ethers . provider . send ( "hardhat_setCode" , [ REWARD_PROXY , code_reward ] ) ;
297+ const contract_reward = require ( "../artifacts/solidity/GovReward.sol/GovReward.json" ) ;
298+ GovReward = new ethers . Contract ( REWARD_PROXY , contract_reward . abi , user ) ;
299+ } ) ;
300+
301+ it ( "Should revert if caller is not Policy" , async function ( ) {
302+ await expect ( Governance . connect ( user ) . deactivateCandidate ( user . address ) ) . to . be . revertedWithCustomError (
303+ Governance ,
304+ ERRORS . SIDE_CALL_OT_ALLOWED
305+ ) ;
306+ } ) ;
307+
308+ it ( "Should update storage if a candidate is deactivated" , async function ( ) {
309+ let signers = await ethers . getSigners ( ) ;
310+ for ( let i = 0 ; i < CONSENSUS_SIZE ; i ++ ) {
311+ await Governance . connect ( signers [ i ] ) . registerCandidate ( 500 , { value : REGISTER_FEE } ) ;
312+ await Governance . connect ( signers [ i ] ) . vote ( signers [ i ] , { value : VOTE_TARGET_AMOUNT } ) ;
313+ }
314+ await mine ( EPOCH_DURATION ) ;
315+ await MockSysCall . call_onPersist ( Governance ) ;
316+
317+ for ( let i = 0 ; i < 4 ; i ++ ) {
318+ await expect (
319+ Policy . connect ( signers [ i ] ) . addBlackList ( signers [ 0 ] )
320+ ) . not . to . be . reverted ;
321+ }
322+
323+ expect ( await Governance . blacklistedCandidates ( ) ) . to . equal ( 1 ) ;
324+ expect ( await Governance . exitHeightOf ( signers [ 0 ] . address ) ) . to . gt ( 0 ) ;
325+ expect ( await Governance . shareRateOf ( signers [ 0 ] . address ) ) . to . equal ( 500 ) ;
326+ expect ( await Governance . receivedVotes ( signers [ 0 ] . address ) ) . to . equal ( VOTE_TARGET_AMOUNT ) ;
327+ expect ( await Governance . totalVotes ( ) ) . to . equal ( BigInt ( CONSENSUS_SIZE - 1 ) * VOTE_TARGET_AMOUNT ) ;
328+ } ) ;
329+ } ) ;
330+
331+ describe ( "activateCandidate" , function ( ) {
332+ let MockSysCall : any ;
333+ let GovReward : any ;
334+
335+ beforeEach ( async function ( ) {
336+ // Deploy Mock SYS_CALL
337+ const deploy_mock = await ethers . deployContract ( "MockSysCall" ) ;
338+ const code_mock = await ethers . provider . send ( "eth_getCode" , [ deploy_mock . target ] ) ;
339+ await ethers . provider . send ( "hardhat_setCode" , [ SYS_CALL , code_mock ] ) ;
340+ const contract_mock = require ( "../artifacts/solidity/test/MockSysCall.sol/MockSysCall.json" ) ;
341+ MockSysCall = new ethers . Contract ( SYS_CALL , contract_mock . abi , user ) ;
342+ // Deploy GovReward to native address
343+ const deploy_reward = await ethers . deployContract ( "GovReward" ) ;
344+ const code_reward = await ethers . provider . send ( "eth_getCode" , [ deploy_reward . target ] ) ;
345+ await ethers . provider . send ( "hardhat_setCode" , [ REWARD_PROXY , code_reward ] ) ;
346+ const contract_reward = require ( "../artifacts/solidity/GovReward.sol/GovReward.json" ) ;
347+ GovReward = new ethers . Contract ( REWARD_PROXY , contract_reward . abi , user ) ;
348+ } ) ;
349+
350+ it ( "Should revert if caller is not Policy" , async function ( ) {
351+ await expect ( Governance . connect ( user ) . activateCandidate ( user . address ) ) . to . be . revertedWithCustomError (
352+ Governance ,
353+ ERRORS . SIDE_CALL_OT_ALLOWED
354+ ) ;
355+ } ) ;
356+
357+ it ( "Should update storage if a candidate is activated" , async function ( ) {
358+ let signers = await ethers . getSigners ( ) ;
359+ for ( let i = 0 ; i < CONSENSUS_SIZE ; i ++ ) {
360+ await Governance . connect ( signers [ i ] ) . registerCandidate ( 500 , { value : REGISTER_FEE } ) ;
361+ await Governance . connect ( signers [ i ] ) . vote ( signers [ i ] , { value : VOTE_TARGET_AMOUNT } ) ;
362+ }
363+ await mine ( EPOCH_DURATION ) ;
364+ await MockSysCall . call_onPersist ( Governance ) ;
365+
366+ for ( let i = 0 ; i < 4 ; i ++ ) {
367+ await expect (
368+ Policy . connect ( signers [ i ] ) . addBlackList ( signers [ 0 ] )
369+ ) . not . to . be . reverted ;
370+ }
371+
372+ for ( let i = 0 ; i < 4 ; i ++ ) {
373+ await expect (
374+ Policy . connect ( signers [ i ] ) . removeBlackList ( signers [ 0 ] )
375+ ) . not . to . be . reverted ;
376+ }
377+
378+ expect ( await Governance . blacklistedCandidates ( ) ) . to . equal ( 0 ) ;
379+ expect ( await Governance . exitHeightOf ( signers [ 0 ] . address ) ) . to . equal ( 0 ) ;
380+ expect ( await Governance . shareRateOf ( signers [ 0 ] . address ) ) . to . equal ( 500 ) ;
381+ expect ( await Governance . receivedVotes ( signers [ 0 ] . address ) ) . to . equal ( VOTE_TARGET_AMOUNT ) ;
382+ expect ( await Governance . totalVotes ( ) ) . to . equal ( BigInt ( CONSENSUS_SIZE ) * VOTE_TARGET_AMOUNT ) ;
383+ } ) ;
384+ } ) ;
385+
279386 describe ( "getCandidates" , function ( ) {
280387 it ( "Should return an empty list of candidates initially" , async function ( ) {
281388 const candidates = await Governance . getCandidates ( ) ;
@@ -410,7 +517,7 @@ describe("Governance", function () {
410517
411518 it ( "Should revert if target is not a candidate" , async function ( ) {
412519 await expect (
413- Governance . connect ( candidate1 ) . vote ( candidate1 , { value : MIN_VOTE_AMOUNT } )
520+ Governance . connect ( candidate1 ) . vote ( candidate1 , { value : MIN_VOTE_AMOUNT } )
414521 ) . to . be . revertedWithCustomError ( Governance , ERRORS . CANDIDATE_NOT_EXISTS ) ;
415522 } ) ;
416523
0 commit comments