From 761858d014f3949f3c4c2a2744ac1f6cd7c1f7ea Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 9 Oct 2024 12:13:38 +0200 Subject: [PATCH 1/4] Add `endBlock` into epoch snapshot --- contracts/sfc/SFC.sol | 1 + contracts/sfc/SFCState.sol | 1 + 2 files changed, 2 insertions(+) diff --git a/contracts/sfc/SFC.sol b/contracts/sfc/SFC.sol index 42de7f4..026500b 100644 --- a/contracts/sfc/SFC.sol +++ b/contracts/sfc/SFC.sol @@ -369,6 +369,7 @@ contract SFC is SFCBase, Version { currentSealedEpoch = currentEpoch(); snapshot.endTime = _now(); + snapshot.endBlock = block.number; snapshot.baseRewardPerSecond = c.baseRewardPerSecond(); snapshot.totalSupply = totalSupply; } diff --git a/contracts/sfc/SFCState.sol b/contracts/sfc/SFCState.sol index f270769..d1b37c2 100644 --- a/contracts/sfc/SFCState.sol +++ b/contracts/sfc/SFCState.sol @@ -71,6 +71,7 @@ contract SFCState is Initializable, Ownable { mapping(uint256 => uint256) offlineBlocks; uint256[] validatorIDs; uint256 endTime; + uint256 endBlock; uint256 epochFee; uint256 totalBaseRewardWeight; uint256 totalTxRewardWeight; From d8b40334ebe99a27a73c465395fba835803a245e Mon Sep 17 00:00:00 2001 From: Mike-CZ Date: Wed, 9 Oct 2024 17:08:32 +0200 Subject: [PATCH 2/4] Add epoch end block getters and test --- contracts/sfc/SFC.sol | 4 ++++ contracts/sfc/SFCI.sol | 3 +++ contracts/test/UnitTestSFC.sol | 3 +++ test/SFC.ts | 11 +++++++++++ 4 files changed, 21 insertions(+) diff --git a/contracts/sfc/SFC.sol b/contracts/sfc/SFC.sol index 026500b..a202a17 100644 --- a/contracts/sfc/SFC.sol +++ b/contracts/sfc/SFC.sol @@ -75,6 +75,10 @@ contract SFC is SFCBase, Version { return getEpochSnapshot[epoch].offlineBlocks[validatorID]; } + function getEpochEndBlock(uint256 epoch) public view returns (uint256) { + return getEpochSnapshot[epoch].endBlock; + } + function rewardsStash(address delegator, uint256 validatorID) public view returns (uint256) { Rewards memory stash = _rewardsStash[delegator][validatorID]; return stash.lockupBaseReward + stash.lockupExtraReward + stash.unlockedReward; diff --git a/contracts/sfc/SFCI.sol b/contracts/sfc/SFCI.sol index 9ca168b..f6751cb 100644 --- a/contracts/sfc/SFCI.sol +++ b/contracts/sfc/SFCI.sol @@ -44,6 +44,7 @@ interface SFCI { view returns ( uint256 endTime, + uint256 endBlock, uint256 epochFee, uint256 totalBaseRewardWeight, uint256 totalTxRewardWeight, @@ -139,6 +140,8 @@ interface SFCI { function getEpochOfflineBlocks(uint256 epoch, uint256 validatorID) external view returns (uint256); + function getEpochEndBlock(uint256 epoch) external view returns (uint256); + function rewardsStash(address delegator, uint256 validatorID) external view returns (uint256); function getLockedStake(address delegator, uint256 toValidatorID) external view returns (uint256); diff --git a/contracts/test/UnitTestSFC.sol b/contracts/test/UnitTestSFC.sol index a64d325..bbdf895 100644 --- a/contracts/test/UnitTestSFC.sol +++ b/contracts/test/UnitTestSFC.sol @@ -120,6 +120,7 @@ interface SFCUnitTestI { view returns ( uint256 endTime, + uint256 endBlock, uint256 epochFee, uint256 totalBaseRewardWeight, uint256 totalTxRewardWeight, @@ -217,6 +218,8 @@ interface SFCUnitTestI { function getEpochOfflineBlocks(uint256 epoch, uint256 validatorID) external view returns (uint256); + function getEpochEndBlock(uint256 epoch) external view returns (uint256); + function rewardsStash(address delegator, uint256 validatorID) external view returns (uint256); function getLockedStake(address delegator, uint256 toValidatorID) external view returns (uint256); diff --git a/test/SFC.ts b/test/SFC.ts index 2df1df6..5fd6d6b 100644 --- a/test/SFC.ts +++ b/test/SFC.ts @@ -413,6 +413,17 @@ describe('SFC', () => { expect(await this.sfc.currentEpoch.call()).to.equal(6); expect(await this.sfc.currentSealedEpoch()).to.equal(5); }); + + it('Should succeed and return endBlock', async function () { + const epochNumber = await this.sfc.currentEpoch(); + await this.sfc.enableNonNodeCalls(); + await this.sfc.sealEpoch([100, 101, 102], [100, 101, 102], [100, 101, 102], [100, 101, 102], 0); + const lastBlock = await ethers.provider.getBlock('latest'); + expect(lastBlock).to.not.equal(null); + // endBlock is on second position + expect((await this.sfc.getEpochSnapshot(epochNumber))[1]).to.equal(lastBlock!.number); + expect(await this.sfc.getEpochEndBlock(epochNumber)).to.equal(lastBlock!.number); + }); }); }); From 8f9156a2b0853645f9fd925039024b356da9e6f7 Mon Sep 17 00:00:00 2001 From: mike Date: Tue, 22 Oct 2024 11:58:28 +0200 Subject: [PATCH 3/4] GetLastBlock number in test --- test/SFC.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/SFC.ts b/test/SFC.ts index 5fd6d6b..1810d5a 100644 --- a/test/SFC.ts +++ b/test/SFC.ts @@ -418,11 +418,10 @@ describe('SFC', () => { const epochNumber = await this.sfc.currentEpoch(); await this.sfc.enableNonNodeCalls(); await this.sfc.sealEpoch([100, 101, 102], [100, 101, 102], [100, 101, 102], [100, 101, 102], 0); - const lastBlock = await ethers.provider.getBlock('latest'); - expect(lastBlock).to.not.equal(null); + const lastBlock = await ethers.provider.getBlockNumber() // endBlock is on second position - expect((await this.sfc.getEpochSnapshot(epochNumber))[1]).to.equal(lastBlock!.number); - expect(await this.sfc.getEpochEndBlock(epochNumber)).to.equal(lastBlock!.number); + expect((await this.sfc.getEpochSnapshot(epochNumber))[1]).to.equal(lastBlock); + expect(await this.sfc.getEpochEndBlock(epochNumber)).to.equal(lastBlock); }); }); }); From f23cf1bc0b7894a77356f2c8649cee23653276dd Mon Sep 17 00:00:00 2001 From: mike Date: Tue, 22 Oct 2024 11:59:47 +0200 Subject: [PATCH 4/4] Linter --- test/SFC.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/SFC.ts b/test/SFC.ts index 1810d5a..f00b9ba 100644 --- a/test/SFC.ts +++ b/test/SFC.ts @@ -418,7 +418,7 @@ describe('SFC', () => { const epochNumber = await this.sfc.currentEpoch(); await this.sfc.enableNonNodeCalls(); await this.sfc.sealEpoch([100, 101, 102], [100, 101, 102], [100, 101, 102], [100, 101, 102], 0); - const lastBlock = await ethers.provider.getBlockNumber() + const lastBlock = await ethers.provider.getBlockNumber(); // endBlock is on second position expect((await this.sfc.getEpochSnapshot(epochNumber))[1]).to.equal(lastBlock); expect(await this.sfc.getEpochEndBlock(epochNumber)).to.equal(lastBlock);