Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add penalty into Withdrawn event #104

Merged
merged 8 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion contracts/interfaces/ISFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ interface ISFC {
);
event Delegated(address indexed delegator, uint256 indexed toValidatorID, uint256 amount);
event Undelegated(address indexed delegator, uint256 indexed toValidatorID, uint256 indexed wrID, uint256 amount);
event Withdrawn(address indexed delegator, uint256 indexed toValidatorID, uint256 indexed wrID, uint256 amount);
event Withdrawn(
address indexed delegator,
uint256 indexed toValidatorID,
uint256 indexed wrID,
uint256 amount,
uint256 penalty
);
event ClaimedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 rewards);
event RestakedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 rewards);
event BurntFTM(uint256 amount);
Expand Down
8 changes: 3 additions & 5 deletions contracts/sfc/ConstantsManager.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;

import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Decimal} from "../common/Decimal.sol";

/**
* @custom:security-contact [email protected]
*/
contract ConstantsManager is OwnableUpgradeable {
contract ConstantsManager is Ownable {
// Minimum amount of stake for a validator, i.e., 500000 FTM
uint256 public minSelfStake;
// Maximum ratio of delegations a validator can have, say, 15 times of self-stake
Expand Down Expand Up @@ -47,9 +47,7 @@ contract ConstantsManager is OwnableUpgradeable {
*/
error ValueTooLarge();

constructor(address owner) initializer {
__Ownable_init(owner);
}
constructor(address owner) Ownable(owner) {}

function updateMinSelfStake(uint256 v) external virtual onlyOwner {
if (v < 100000 * 1e18) {
Expand Down
2 changes: 2 additions & 0 deletions contracts/sfc/NodeDriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,6 @@ contract NodeDriver is OwnableUpgradeable, UUPSUpgradeable, INodeDriver {
function sealEpochValidators(uint256[] calldata nextValidatorIDs) external onlyNode {
backend.sealEpochValidators(nextValidatorIDs);
}

uint256[50] private __gap;
}
2 changes: 2 additions & 0 deletions contracts/sfc/NodeDriverAuth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@ contract NodeDriverAuth is OwnableUpgradeable, UUPSUpgradeable {
}
return codeHash;
}

uint256[50] private __gap;
}
44 changes: 42 additions & 2 deletions contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
// total stake of active (OK_STATUS) validators (total weight)
uint256 public totalActiveStake;

// unresolved fees that failed to be send to the treasury
uint256 public unresolvedTreasuryFees;

// delegator => validator ID => stashed rewards (to be claimed/restaked)
mapping(address delegator => mapping(uint256 validatorID => uint256 stashedRewards)) internal _rewardsStash;

Expand Down Expand Up @@ -190,6 +193,10 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
error ValidatorNotSlashed();
error RefundRatioTooHigh();

// treasury
error TreasuryNotSet();
error NoUnresolvedTreasuryFees();

event DeactivatedValidator(uint256 indexed validatorID, uint256 deactivatedEpoch, uint256 deactivatedTime);
event ChangedValidatorStatus(uint256 indexed validatorID, uint256 status);
event CreatedValidator(
Expand All @@ -200,13 +207,20 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
);
event Delegated(address indexed delegator, uint256 indexed toValidatorID, uint256 amount);
event Undelegated(address indexed delegator, uint256 indexed toValidatorID, uint256 indexed wrID, uint256 amount);
event Withdrawn(address indexed delegator, uint256 indexed toValidatorID, uint256 indexed wrID, uint256 amount);
event Withdrawn(
address indexed delegator,
uint256 indexed toValidatorID,
uint256 indexed wrID,
uint256 amount,
uint256 penalty
);
event ClaimedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 rewards);
event RestakedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 rewards);
event BurntFTM(uint256 amount);
event UpdatedSlashingRefundRatio(uint256 indexed validatorID, uint256 refundRatio);
event RefundedSlashedLegacyDelegation(address indexed delegator, uint256 indexed validatorID, uint256 amount);
event AnnouncedRedirection(address indexed from, address indexed to);
event TreasuryFeesResolved(uint256 amount);

modifier onlyDriver() {
if (!isNode(msg.sender)) {
Expand Down Expand Up @@ -419,6 +433,27 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
}
}

/// Resolve failed treasury transfers and send the unresolved fees to the treasury address.
function resolveTreasuryFees() external {
if (treasuryAddress == address(0)) {
revert TreasuryNotSet();
}
if (unresolvedTreasuryFees == 0) {
revert NoUnresolvedTreasuryFees();
}

// zero the fees before sending to prevent re-entrancy
uint256 fees = unresolvedTreasuryFees;
unresolvedTreasuryFees = 0;

(bool success, ) = treasuryAddress.call{value: fees, gas: 1000000}("");
if (!success) {
revert TransferFailed();
}

emit TreasuryFeesResolved(fees);
}

/// burnFTM allows SFC to burn an arbitrary amount of FTM tokens.
function burnFTM(uint256 amount) external onlyOwner {
_burnFTM(amount);
Expand Down Expand Up @@ -706,7 +741,7 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
}
_burnFTM(penalty);

emit Withdrawn(delegator, toValidatorID, wrID, amount);
emit Withdrawn(delegator, toValidatorID, wrID, amount - penalty, penalty);
}

/// Get highest epoch for which can be claimed rewards for the given validator.
Expand Down Expand Up @@ -909,6 +944,9 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
if (!success) {
// ignore treasury transfer failure
// the treasury failure must not endanger the epoch sealing

// store the unresolved treasury fees to be resolved later
unresolvedTreasuryFees += feeShare;
}
}
}
Expand Down Expand Up @@ -1117,4 +1155,6 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
function _now() internal view virtual returns (uint256) {
return block.timestamp;
}

uint256[50] private __gap;
}
9 changes: 9 additions & 0 deletions contracts/test/FailingReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.27;

contract FailingReceiver {
// Fallback function to reject any received Ether
receive() external payable {
revert("Forced transfer failure");
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"typescript-eslint": "^8.8.0"
},
"dependencies": {
"@openzeppelin/contracts": "^5.1.0",
"@openzeppelin/contracts-upgradeable": "^5.1.0",
"dotenv": "^16.0.3"
}
Expand Down
92 changes: 75 additions & 17 deletions test/SFC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,10 @@ describe('SFC', () => {
});

it('Should succeed and seal epochs', async function () {
const validatorsMetrics: Map<number, ValidatorMetrics> = new Map();
const validatorsMetrics: Map<bigint, ValidatorMetrics> = new Map();
const validatorIDs = await this.sfc.lastValidatorID();

for (let i = 0; i < validatorIDs; i++) {
for (let i = 1n; i <= validatorIDs; i++) {
validatorsMetrics.set(i, {
offlineTime: 0,
offlineBlocks: 0,
Expand All @@ -661,8 +661,8 @@ describe('SFC', () => {
const offlineBlocks = [];
const uptimes = [];
const originatedTxsFees = [];
for (let i = 0; i < validatorIDs; i++) {
allValidators.push(i + 1);
for (let i = 1n; i <= validatorIDs; i++) {
allValidators.push(i);
offlineTimes.push(validatorsMetrics.get(i)!.offlineTime);
offlineBlocks.push(validatorsMetrics.get(i)!.offlineBlocks);
uptimes.push(validatorsMetrics.get(i)!.uptime);
Expand All @@ -674,11 +674,69 @@ describe('SFC', () => {
await this.sfc.sealEpochValidators(allValidators);
});

describe('Treasury', () => {
it('Should revert when treasury is not set', async function () {
await expect(this.sfc.resolveTreasuryFees()).to.be.revertedWithCustomError(this.sfc, 'TreasuryNotSet');
});

it('Should revert when no unresolved treasury fees are available', async function () {
const treasury = ethers.Wallet.createRandom();
await this.sfc.connect(this.owner).updateTreasuryAddress(treasury);
await expect(this.sfc.resolveTreasuryFees()).to.be.revertedWithCustomError(
this.sfc,
'NoUnresolvedTreasuryFees',
);
});

it('Should succeed and resolve treasury fees', async function () {
// set treasury as failing receiver to trigger treasury fee accumulation
const failingReceiver = await ethers.deployContract('FailingReceiver');
await this.sfc.connect(this.owner).updateTreasuryAddress(failingReceiver);

// set validators metrics and their fees
const validatorsMetrics: Map<bigint, ValidatorMetrics> = new Map();
const validatorIDs = await this.sfc.lastValidatorID();
for (let i = 1n; i <= validatorIDs; i++) {
validatorsMetrics.set(i, {
offlineTime: 0,
offlineBlocks: 0,
uptime: 24 * 60 * 60,
originatedTxsFee: ethers.parseEther('100'),
});
}

// seal epoch to trigger fees calculation and distribution
await this.blockchainNode.sealEpoch(24 * 60 * 60, validatorsMetrics);

const fees =
(validatorIDs * ethers.parseEther('100') * (await this.constants.treasuryFeeShare())) / BigInt(1e18);
expect(await this.sfc.unresolvedTreasuryFees()).to.equal(fees);

// update treasury to a valid receiver
const treasury = ethers.Wallet.createRandom();
await this.sfc.connect(this.owner).updateTreasuryAddress(treasury);

// set sfc some balance to cover treasury fees
// the funds cannot be sent directly as it rejects any incoming transfers
await ethers.provider.send('hardhat_setBalance', [
await this.sfc.getAddress(),
ethers.toBeHex(ethers.parseEther('1000')),
]);

// resolve treasury fees
const tx = await this.sfc.resolveTreasuryFees();
await expect(tx).to.emit(this.sfc, 'TreasuryFeesResolved').withArgs(fees);
await expect(tx).to.changeEtherBalance(treasury, fees);
await expect(tx).to.changeEtherBalance(this.sfc, -fees);
expect(await this.sfc.unresolvedTreasuryFees()).to.equal(0);
});
});

it('Should succeed and seal epoch on Validators', async function () {
const validatorsMetrics: Map<number, ValidatorMetrics> = new Map();
const validatorsMetrics: Map<bigint, ValidatorMetrics> = new Map();
const validatorIDs = await this.sfc.lastValidatorID();

for (let i = 0; i < validatorIDs; i++) {
for (let i = 1n; i <= validatorIDs; i++) {
validatorsMetrics.set(i, {
offlineTime: 0,
offlineBlocks: 0,
Expand All @@ -692,8 +750,8 @@ describe('SFC', () => {
const offlineBlocks = [];
const uptimes = [];
const originatedTxsFees = [];
for (let i = 0; i < validatorIDs; i++) {
allValidators.push(i + 1);
for (let i = 1n; i <= validatorIDs; i++) {
allValidators.push(i);
offlineTimes.push(validatorsMetrics.get(i)!.offlineTime);
offlineBlocks.push(validatorsMetrics.get(i)!.offlineBlocks);
uptimes.push(validatorsMetrics.get(i)!.uptime);
Expand Down Expand Up @@ -746,10 +804,10 @@ describe('SFC', () => {
});

it('Should revert when calling sealEpoch if not NodeDriver', async function () {
const validatorsMetrics: Map<number, ValidatorMetrics> = new Map();
const validatorsMetrics: Map<bigint, ValidatorMetrics> = new Map();
const validatorIDs = await this.sfc.lastValidatorID();

for (let i = 0; i < validatorIDs; i++) {
for (let i = 1n; i <= validatorIDs; i++) {
validatorsMetrics.set(i, {
offlineTime: 0,
offlineBlocks: 0,
Expand All @@ -763,8 +821,8 @@ describe('SFC', () => {
const offlineBlocks = [];
const uptimes = [];
const originatedTxsFees = [];
for (let i = 0; i < validatorIDs; i++) {
allValidators.push(i + 1);
for (let i = 1n; i <= validatorIDs; i++) {
allValidators.push(i);
offlineTimes.push(validatorsMetrics.get(i)!.offlineTime);
offlineBlocks.push(validatorsMetrics.get(i)!.offlineBlocks);
uptimes.push(validatorsMetrics.get(i)!.uptime);
Expand Down Expand Up @@ -982,7 +1040,7 @@ describe('SFC', () => {
// validator online 100% of time in the first epoch => average 100%
await this.blockchainNode.sealEpoch(
100,
new Map<number, ValidatorMetrics>([[this.validatorId as number, new ValidatorMetrics(0, 0, 100, 0n)]]),
new Map<bigint, ValidatorMetrics>([[this.validatorId, new ValidatorMetrics(0, 0, 100, 0n)]]),
);
expect(await this.sfc.getEpochAverageUptime(await this.sfc.currentSealedEpoch(), this.validatorId)).to.equal(
1000000000000000000n,
Expand All @@ -991,7 +1049,7 @@ describe('SFC', () => {
// validator online 20% of time in the second epoch => average 60%
await this.blockchainNode.sealEpoch(
100,
new Map<number, ValidatorMetrics>([[this.validatorId as number, new ValidatorMetrics(0, 0, 20, 0n)]]),
new Map<bigint, ValidatorMetrics>([[this.validatorId, new ValidatorMetrics(0, 0, 20, 0n)]]),
);
expect(await this.sfc.getEpochAverageUptime(await this.sfc.currentSealedEpoch(), this.validatorId)).to.equal(
600000000000000000n,
Expand All @@ -1000,7 +1058,7 @@ describe('SFC', () => {
// validator online 30% of time in the third epoch => average 50%
await this.blockchainNode.sealEpoch(
100,
new Map<number, ValidatorMetrics>([[this.validatorId as number, new ValidatorMetrics(0, 0, 30, 0n)]]),
new Map<bigint, ValidatorMetrics>([[this.validatorId, new ValidatorMetrics(0, 0, 30, 0n)]]),
);
expect(await this.sfc.getEpochAverageUptime(await this.sfc.currentSealedEpoch(), this.validatorId)).to.equal(
500000000000000000n,
Expand All @@ -1010,7 +1068,7 @@ describe('SFC', () => {
for (let i = 0; i < 10; i++) {
await this.blockchainNode.sealEpoch(
100,
new Map<number, ValidatorMetrics>([[this.validatorId as number, new ValidatorMetrics(0, 0, 50, 0n)]]),
new Map<bigint, ValidatorMetrics>([[this.validatorId, new ValidatorMetrics(0, 0, 50, 0n)]]),
);
expect(await this.sfc.getEpochAverageUptime(await this.sfc.currentSealedEpoch(), this.validatorId)).to.equal(
500000000000000000n,
Expand All @@ -1020,7 +1078,7 @@ describe('SFC', () => {
// (50 * 10 + 28) / 11 = 48
await this.blockchainNode.sealEpoch(
100,
new Map<number, ValidatorMetrics>([[this.validatorId as number, new ValidatorMetrics(0, 0, 28, 0n)]]),
new Map<bigint, ValidatorMetrics>([[this.validatorId, new ValidatorMetrics(0, 0, 28, 0n)]]),
);
expect(await this.sfc.getEpochAverageUptime(await this.sfc.currentSealedEpoch(), this.validatorId)).to.equal(
480000000000000000n,
Expand Down
12 changes: 6 additions & 6 deletions test/helpers/BlockchainNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SFCUnitTestI } from '../../typechain-types';
import { UnitTestSFC } from '../../typechain-types';
import { TransactionResponse } from 'ethers';
import { ethers } from 'hardhat';

Expand All @@ -17,11 +17,11 @@ class ValidatorMetrics {
}

class BlockchainNode {
public readonly sfc: SFCUnitTestI;
public validatorWeights: Map<number, bigint>;
public nextValidatorWeights: Map<number, bigint>;
public readonly sfc: UnitTestSFC;
public validatorWeights: Map<bigint, bigint>;
public nextValidatorWeights: Map<bigint, bigint>;

constructor(sfc: SFCUnitTestI) {
constructor(sfc: UnitTestSFC) {
this.sfc = sfc;
this.validatorWeights = new Map();
this.nextValidatorWeights = new Map();
Expand All @@ -44,7 +44,7 @@ class BlockchainNode {
}
}

async sealEpoch(duration: number, validatorMetrics?: Map<number, ValidatorMetrics>) {
async sealEpoch(duration: number, validatorMetrics?: Map<bigint, ValidatorMetrics>) {
const validatorIds = Array.from(this.validatorWeights.keys());
const nextValidatorIds = Array.from(this.nextValidatorWeights.keys());

Expand Down