Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick95550 committed Jun 26, 2024
1 parent ff59bae commit b96fa24
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion contracts/Directory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract Directory is IDirectory, Initializable, Ownable2StepUpgradeable, ERC165
* the directory. This can allow gas costs to be low if this needs to be
* used in a transaction.
* @param point The point, which will usually be a hash of a public key.
* @param stakingPeriodId The epoch id associated with the directory to scan.
* @param stakingPeriodId The period id associated with the directory to scan.
*/
function _scan(uint128 point, uint256 stakingPeriodId) internal view returns (address stakee) {
uint256 currentRewardCycle = protocolTimeManager.getCurrentCycle();
Expand Down
10 changes: 7 additions & 3 deletions contracts/staking/StakingOrchestrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ contract StakingOrchestrator is
Ownable2StepUpgradeable,
ERC165
{
function getNodeCurrentStake(address node) external returns (uint256) {
return 0;
mapping(address => uint256) nodeStake;

function getNodeCurrentStake(address node) external view returns (uint256) {
return nodeStake[node];
}

function getUserCurrentStake(address node, address user) external returns (uint256) {
Expand All @@ -30,7 +32,9 @@ contract StakingOrchestrator is
return 0;
}

function syloStakeAdded(address node, address user, uint256 newAmount) external {}
function syloStakeAdded(address node, address user, uint256 newAmount) external {
nodeStake[node] += newAmount;
}

function syloStakeRemoved(address node, address user, uint256 newAmount) external {}

Expand Down
91 changes: 87 additions & 4 deletions test/Directory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ethers } from 'hardhat';
import { Signer } from 'ethers';
import { deployContracts } from './utils';
import { expect } from 'chai';
import { expect, assert } from 'chai';
import { SyloContracts } from '../common/contracts';
import {
Directory,
Expand All @@ -13,20 +14,39 @@ describe.only('Directory', () => {
let directory: Directory;
let stakingOrchestator: StakingOrchestrator;
let protocolTimeManager: ProtocolTimeManager;
let accounts: Signer[];
let nodeOne: Signer;
let nodeTwo: Signer;
let nodeOneAddress: string;
let nodeTwoAddress: string;

beforeEach(async () => {
contracts = await deployContracts();
accounts = await ethers.getSigners();
nodeOne = accounts[1];
nodeTwo = accounts[2];
nodeOneAddress = await accounts[1].getAddress();
nodeTwoAddress = await accounts[2].getAddress();
directory = contracts.directory;
stakingOrchestator = contracts.stakingOrchestrator;
protocolTimeManager = contracts.protocolTimeManager;
});

it('cannot initialize directory more than once', async () => {
await expect(
directory.initialize(
await stakingOrchestator.getAddress(),
await protocolTimeManager.getAddress(),
),
).to.be.revertedWith('Initializable: contract is already initialized');
});

it('cannot initialize directory with empty StakingOrchestrator address', async () => {
const directoryFactory = await ethers.getContractFactory('Directory');
const directoryTemp = await directoryFactory.deploy();

await expect(
directory.initialize(
directoryTemp.initialize(
ethers.ZeroAddress,
await protocolTimeManager.getAddress(),
),
Expand All @@ -41,13 +61,76 @@ describe.only('Directory', () => {
const directoryTemp = await directoryFactory.deploy();

await expect(
directory.initialize(
directoryTemp.initialize(
await stakingOrchestator.getAddress(),
ethers.ZeroAddress,
),
).to.be.revertedWithCustomError(
directoryTemp,
'CannotInitialiseWithZeroStakingOrchestratorAddress',
'CannotInitialiseWithZeroProtocolTimeManagerAddress',
);
});

it('cannot join directory without stake', async () => {
await expect(directory.joinNextDirectory()).to.be.revertedWithCustomError(
directory,
'CannotJoinDirectoryWithZeroStake',
);
});

it('cannot join same directory twice', async () => {
await stakingOrchestator.syloStakeAdded(nodeOne, ethers.ZeroAddress, 1000);
await directory.connect(nodeOne).joinNextDirectory();
await expect(
directory.connect(nodeOne).joinNextDirectory(),
).to.be.revertedWithCustomError(directory, 'StakeeAlreadyJoinedEpoch');
});

it('scans for a point in the current period', async () => {
await stakingOrchestator.syloStakeAdded(nodeOne, ethers.ZeroAddress, 1000);
await directory.connect(nodeOne).joinNextDirectory();
const address = await directory.scan(0);
assert.equal(address, await nodeOne.getAddress());
});

it.only('should correctly scan accounts based on their stake proportions', async () => {
for (let i = 0; i < 5; i++) {
await stakingOrchestator.syloStakeAdded(
await accounts[i].getAddress(),
ethers.ZeroAddress,
1,
);
await directory.connect(accounts[i]).joinNextDirectory();
}

const fifthPoint = (2n ** 128n - 1n) / 5n;
const points = [
0n,
fifthPoint + 1n,
fifthPoint * 2n + 2n,
fifthPoint * 3n + 3n,
fifthPoint * 4n + 4n,
];

for (let i = 0; i < 5; i++) {
// check scan
const address = await directory.scan(points[i]);
assert.equal(
address,
await accounts[i].getAddress(),
'Expected scan to return correct result',
);

// check scan with staking period
const addressWithEpochId = await directory.scanWithStakingPeriod(
points[i],
1,
);
assert.equal(
addressWithEpochId,
await accounts[i].getAddress(),
'Expected scan with staking period to return correct result',
);
}
});
});

0 comments on commit b96fa24

Please sign in to comment.