Skip to content

Commit

Permalink
update unitest relock stake
Browse files Browse the repository at this point in the history
  • Loading branch information
thang14 committed Aug 21, 2024
1 parent 3dd11ae commit 2769709
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 207 deletions.
15 changes: 13 additions & 2 deletions contracts/sfc/SFCLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1414,12 +1414,23 @@ contract SFCLib is SFCBase {
external
view
returns (
uint256 fromEpoch,
uint256 endTime,
uint256 duration,
uint256 stashedLockupExtraReward,
uint256 stashedLockupBaseReward
uint256 stashedLockupBaseReward,
uint256 stashedRewardsUntilEpoch
)
{
LockedDelegationV2 memory ld = getLockupInfoV2[delAddr][valIdx][lId];
return (ld.stashedLockupExtraReward, ld.stashedLockupBaseReward);
return (
ld.fromEpoch,
ld.endTime,
ld.duration,
ld.stashedLockupExtraReward,
ld.stashedLockupBaseReward,
ld.stashedRewardsUntilEpoch
);
}

function setEnabledAutoRelock(uint256 valId, bool enabled) external {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"compile": "hardhat compile",
"test": "hardhat test",
"flatten": "hardhat flatten",
"start:node": "hardhat node"
"start:node": "hardhat node",
"coverage": "npx hardhat coverage"
},
"devDependencies": {
"@openzeppelin/contracts": "^2.5.1",
Expand Down
204 changes: 0 additions & 204 deletions test/SFC.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ chai.use(chaiAsPromised);
const UnitTestSFC = artifacts.require('UnitTestSFC');
const UnitTestSFCLib = artifacts.require('UnitTestSFCLib');
const SFCI = artifacts.require('SFCUnitTestI');
const SFCIV2 = artifacts.require('SFCUnitTestIV2');

const NodeDriverAuth = artifacts.require('NodeDriverAuth');
const NodeDriver = artifacts.require('NodeDriver');
Expand Down Expand Up @@ -1812,206 +1811,3 @@ contract('SFC', async ([firstValidator,,,,, account1, account2, account3]) => {
});
});
});

contract('SFC', async ([firstValidator,,,, thirdDelegator, account1, account2, account3]) => {
let testValidator1ID;
let testValidator3ID;

beforeEach(async () => {
const sfc = await UnitTestSFC.new();

this.sfcv1 = await SFCI.at(sfc.address);
this.sfc = await SFCIV2.at(sfc.address);
const nodeIRaw = await NodeDriver.new();
const evmWriter = await StubEvmWriter.new();
this.nodeI = await NodeDriverAuth.new();
this.sfcLib = await UnitTestSFCLib.new();
const initializer = await NetworkInitializer.new();
await initializer.initializeAll(0, 0, this.sfc.address, this.sfcLib.address, this.nodeI.address, nodeIRaw.address, evmWriter.address, firstValidator);
this.consts = await ConstantsManager.at(await this.sfc.constsAddress.call());
await this.sfc.rebaseTime();
await this.sfc.enableNonNodeCalls();

await this.consts.updateBaseRewardPerSecond(amount18('1'));

await this.sfc.createValidator(pubkey, {
from: account1,
value: amount18('10'),
});

await this.sfc.createValidator(pubkey, {
from: account2,
value: amount18('5'),
});

await this.sfc.createValidator(pubkey, {
from: account3,
value: amount18('1'),
});

await sealEpoch(this.sfc, (new BN(0)).toString());

testValidator1ID = await this.sfc.getValidatorID(account1);
testValidator3ID = await this.sfc.getValidatorID(account3);

await this.sfc.lockStake(testValidator3ID, (60 * 60 * 24 * 364), amount18('1'),
{ from: account3 });

await sealEpoch(this.sfc, (new BN(0)).toString());
});

describe('auto validator relock', () => {
it('should not be able to relock', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());
await expectRevert(this.sfc.relockByAuthorizedAddress(testValidator1ID, { from: thirdDelegator }), 'only authorized Address');
});
});

describe('lock stake v2', () => {
it('Should not be able to lock 0 amount', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await expectRevert(this.sfc.createLockStake(testValidator1ID, (2 * 60 * 60 * 24 * 365), amount18('0'), {
from: thirdDelegator,
}), 'zero amount');
});

it('Should not be able to lock more than a year', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await expectRevert(this.sfc.createLockStake(testValidator3ID, (2 * 60 * 60 * 24 * 365), amount18('1'), {
from: thirdDelegator,
}), 'incorrect duration');
});

it('Should not be able to lock more than validator lockup period', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await expectRevert(this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 365), amount18('1'),
{ from: thirdDelegator }), 'validator lockup period will end earlier');
});

it('Should be able to lock for 1 month', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

await sealEpoch(this.sfc, (new BN(60 * 60 * 24 * 14)).toString());
});

it('Should not unlock if not locked up U2U', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

await sealEpoch(this.sfc, (new BN(60 * 60 * 24 * 14)).toString());

await expectRevert(this.sfc.unlockStake(testValidator3ID, 1, amount18('10')), 'not locked up');
});

it('Should not be able to unlock more than locked stake', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

await sealEpoch(this.sfc, (new BN(60 * 60 * 24 * 14)).toString());

await expectRevert(this.sfc.unlockStake(testValidator3ID, 1, amount18('10'), { from: thirdDelegator }), 'not enough locked stake');
});

it('Should scale unlocking penalty', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

await sealEpoch(this.sfc, (new BN(100)).toString());

expect(await this.sfc.unlockStake.call(testValidator3ID, 1, amount18('1'), { from: thirdDelegator })).to.be.bignumber.equal(amount18('0.085410180572851805'));
expect(await this.sfc.unlockStake.call(testValidator3ID, 1, amount18('0.5'), { from: thirdDelegator })).to.be.bignumber.equal(amount18('0.042705090286425902'));
expect(await this.sfc.unlockStake.call(testValidator3ID, 1, amount18('0.01'), { from: thirdDelegator })).to.be.bignumber.equal(amount18('0.000854101805728517'));
await this.sfc.unlockStake(testValidator3ID, 1, amount18('0.5'), { from: thirdDelegator });
await expectRevert(this.sfc.unlockStake(testValidator3ID, 1, amount18('0.51'), { from: thirdDelegator }), 'not enough locked stake');
expect(await this.sfc.unlockStake.call(testValidator3ID, 1, amount18('0.5'), { from: thirdDelegator })).to.be.bignumber.equal(amount18('0.042705090286425903'));
expect(await this.sfc.unlockStake.call(testValidator3ID, 1, amount18('0.01'), { from: thirdDelegator })).to.be.bignumber.equal(amount18('0.000854101805728517'));
});

it('Should unlock after period ended and stash rewards', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

let unlockedStake = await this.sfc.getUnlockedStake(thirdDelegator, testValidator3ID, { from: thirdDelegator });
let pendingRewards = await this.sfc.pendingRewards(thirdDelegator, testValidator3ID, 1, { from: thirdDelegator });

expect(unlockedStake.toString()).to.equal('10000000000000000000');
expect(web3.utils.fromWei(pendingRewards.toString(), 'ether')).to.equal('0');
await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

unlockedStake = await this.sfc.getUnlockedStake(thirdDelegator, testValidator3ID, { from: thirdDelegator });
pendingRewards = await this.sfc.pendingRewards(thirdDelegator, testValidator3ID, 1, { from: thirdDelegator });

expect(unlockedStake.toString()).to.equal('9000000000000000000');
expect(web3.utils.fromWei(pendingRewards.toString(), 'ether')).to.equal('0');
await sealEpoch(this.sfc, (new BN(60 * 60 * 24 * 14)).toString());

unlockedStake = await this.sfc.getUnlockedStake(thirdDelegator, testValidator3ID, { from: thirdDelegator });
pendingRewards = await this.sfc.pendingRewards(thirdDelegator, testValidator3ID, 1, { from: thirdDelegator });
let pendingRewardsv1 = await this.sfcv1.pendingRewards(thirdDelegator, testValidator3ID, { from: thirdDelegator });

expect(unlockedStake.toString()).to.equal('9000000000000000000');
expect(web3.utils.fromWei(pendingRewards.toString(), 'ether')).to.equal('1909.394271481942710817');
expect(web3.utils.fromWei(pendingRewardsv1.toString(), 'ether')).to.equal('15772.909090909090909088');

await this.sfc.unlockStake(testValidator3ID, 1, amount18('1'), { from: thirdDelegator });
await sealEpoch(this.sfc, (new BN(60 * 60 * 24 * 14)).toString());
pendingRewards = await this.sfc.pendingRewards(thirdDelegator, testValidator3ID, 1, { from: thirdDelegator });
pendingRewardsv1 = await this.sfcv1.pendingRewards(thirdDelegator, testValidator3ID, { from: thirdDelegator });

expect(web3.utils.fromWei(pendingRewards.toString(), 'ether')).to.equal('1909.394271481942710817');
expect(web3.utils.fromWei(pendingRewardsv1.toString(), 'ether')).to.equal('138068.694970782642011512');

unlockedStake = await this.sfc.getUnlockedStake(thirdDelegator, testValidator3ID, { from: thirdDelegator });
expect(unlockedStake.toString()).to.equal('10000000000000000000');

await expectRevert(this.sfc.stashRewards(thirdDelegator, testValidator3ID, 1, { from: thirdDelegator }), 'nothing to stash');
});
});
});
21 changes: 21 additions & 0 deletions test/SFCV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,27 @@ contract('SFC', async ([firstValidator,,,, thirdDelegator, account1, account2, a

await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

await expectRevert(this.sfc.relockStake(testValidator3ID, 1, 60 * 60 * 24 * 14, amount18('0'), { from: thirdDelegator }), 'zero amount');
await expectRevert(this.sfc.relockStake(testValidator3ID, 1, 60 * 60 * 24 * 366, amount18('1'), { from: thirdDelegator }), 'incorrect duration');
await expectRevert(this.sfc.relockStake(testValidator3ID, 1, 60 * 60 * 24 * 13, amount18('1'), { from: thirdDelegator }), 'incorrect duration');
await expectRevert(this.sfc.relockStake(testValidator3ID, 1, 60 * 60 * 24 * 15, amount18('10000'), { from: thirdDelegator }), 'not enough stake');
await expectRevert(this.sfc.relockStake(testValidator1ID, 2, 60 * 60 * 24 * 15, amount18('10000'), { from: thirdDelegator }), 'not enough stake');
await expectRevert(this.sfc.relockStake(testValidator3ID, 1, 60 * 60 * 24 * 365, amount18('1'), { from: thirdDelegator }), 'validator lockup period will end earlier');
});

it('should be relock stake', async () => {
await sealEpoch(this.sfc, (new BN(1000)).toString());

await this.sfc.delegate(testValidator3ID, {
from: thirdDelegator,
value: amount18('10'),
});

await this.sfc.createLockStake(testValidator3ID, (60 * 60 * 24 * 14), amount18('1'),
{ from: thirdDelegator });

await this.sfc.relockStake(testValidator3ID, 1, 60 * 60 * 24 * 14, amount18('1'), { from: thirdDelegator });
});
});
});

0 comments on commit 2769709

Please sign in to comment.