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

2372 stake table add tests to the withdrawfunds function #2394

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
978cd8f
add stake table tests
alysiahuggins Dec 5, 2024
2bc6d75
remove stake types
alysiahuggins Dec 5, 2024
c8812ef
verify token allowance, balance and reprioritize verification order o…
alysiahuggins Dec 5, 2024
e33bf9b
set the fixed stake amount, added related tests, updated data types
alysiahuggins Dec 5, 2024
7bd7f4b
add more verification checks to the withdraw function
alysiahuggins Dec 5, 2024
2b2411c
updated errror types
alysiahuggins Dec 6, 2024
d7a352d
Merge branch 'main' into 2304-stake-table-registration
alysiahuggins Dec 6, 2024
584e320
added TODO statements in comments to be explicit about outdated funct…
alysiahuggins Dec 6, 2024
59c2109
a validator/node is identified by the msg.sender, blskey and schnorrk…
alysiahuggins Dec 6, 2024
23b0fb0
Merge branch 'main' into 2304-stake-table-registration
alysiahuggins Dec 6, 2024
ec4b02b
Merge branch '2304-stake-table-registration' into 2370-staketable-upd…
alysiahuggins Dec 6, 2024
9777b99
merged from the fixed stake branch and modified test to include the i…
alysiahuggins Dec 6, 2024
b9c7022
not required to get the blsSig for a withdrawal since we have the eth…
alysiahuggins Dec 6, 2024
9303444
add the ability to update consensus keys
alysiahuggins Dec 6, 2024
3a264c1
add the ability to update consensus keys
alysiahuggins Dec 6, 2024
955a69d
merge with main
alysiahuggins Dec 6, 2024
1f171dd
remove vscode settings
alysiahuggins Dec 6, 2024
f237074
add test or happy path of updating consensus keys
alysiahuggins Dec 9, 2024
8800893
added unhappy path tests on based on no key changes and changed the b…
alysiahuggins Dec 9, 2024
cee5a6f
added comment to newly added function on StakeTable and added tests t…
alysiahuggins Dec 9, 2024
f8c0210
change the lookup node and lookup stake functions to use their ethere…
alysiahuggins Dec 9, 2024
5124d9d
updated updateConsensusKeys function, enhance test coverage
alysiahuggins Dec 9, 2024
6a34dae
added todo
alysiahuggins Dec 10, 2024
548b28c
Merge branch 'main' into 2370-staketable-update-validator-identificat…
alysiahuggins Dec 10, 2024
5df564c
updated test to use the new seed so that a new schnorr key could be g…
alysiahuggins Dec 11, 2024
f5ffee2
Merge branch '2370-staketable-update-validator-identification-keys' i…
alysiahuggins Dec 12, 2024
3ad4a14
add withdrawFunds tests, removed epoch logic until from withdrawFunds…
alysiahuggins Dec 12, 2024
63f7be7
Merge branch 'main' into 2372-stake-table-add-tests-to-the-withdrawfu…
alysiahuggins Jan 6, 2025
ef4e465
Merge branch 'main' into 2372-stake-table-add-tests-to-the-withdrawfu…
alysiahuggins Jan 15, 2025
f9bfe6d
add test to prove that withdrawing twice fails
alysiahuggins Jan 15, 2025
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
19 changes: 7 additions & 12 deletions contracts/src/StakeTable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ contract StakeTable is AbstractStakeTable {

/// @notice Get the next available epoch and queue size in that epoch
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function nextRegistrationEpoch() external view override returns (uint64, uint64) {
uint64 epoch;
uint64 queueSize;
Expand All @@ -175,22 +174,19 @@ contract StakeTable is AbstractStakeTable {
// @param queueSize current size of the registration queue (after insertion of new element in
// the queue)
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function appendRegistrationQueue(uint64 epoch, uint64 queueSize) private {
firstAvailableRegistrationEpoch = epoch;
_numPendingRegistrations = queueSize + 1;
}

/// @notice Get the number of pending registration requests in the waiting queue
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function numPendingRegistrations() external view override returns (uint64) {
return _numPendingRegistrations;
}

/// @notice Get the next available epoch for exit and queue size in that epoch
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function nextExitEpoch() external view override returns (uint64, uint64) {
uint64 epoch;
uint64 queueSize;
Expand All @@ -212,15 +208,13 @@ contract StakeTable is AbstractStakeTable {
// @param epoch next available exit epoch
// @param queueSize current size of the exit queue (after insertion of new element in the queue)
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function appendExitQueue(uint64 epoch, uint64 queueSize) private {
firstAvailableExitEpoch = epoch;
_numPendingExits = queueSize + 1;
}

/// @notice Get the number of pending exit requests in the waiting queue
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function numPendingExits() external view override returns (uint64) {
return _numPendingExits;
}
Expand All @@ -240,7 +234,6 @@ contract StakeTable is AbstractStakeTable {
/// @param node node which is assigned an exit escrow period.
/// @return Number of epochs post exit after which funds can be withdrawn.
/// TODO modify this according to the current spec
/// TODO modify this according to the current spec
function exitEscrowPeriod(Node memory node) public pure returns (uint64) {
if (node.balance > 100) {
return 10;
Expand Down Expand Up @@ -402,7 +395,6 @@ contract StakeTable is AbstractStakeTable {
function requestExit() external override {
Node memory node = nodes[msg.sender];

// TODO test this behaviour when re-implementing the logic for handling epochs
// if the node is not registered, revert
if (node.account == address(0)) {
revert NodeNotRegistered();
Expand Down Expand Up @@ -437,6 +429,8 @@ contract StakeTable is AbstractStakeTable {
/// withdraw past their `exitEpoch`.
///
/// @return The total amount withdrawn, equal to `Node.balance` associated with `blsVK`
/// TODO: add epoch logic so that we can ensure the node has first requested to exit and waiting
/// for the exit escrow period to be over
function withdrawFunds() external override returns (uint256) {
Node memory node = nodes[msg.sender];

Expand All @@ -456,10 +450,11 @@ contract StakeTable is AbstractStakeTable {
revert InsufficientStakeBalance(0);
}

// Verify that the exit escrow period is over.
if (currentEpoch() < node.exitEpoch + exitEscrowPeriod(node)) {
revert PrematureWithdrawal();
}
// // Verify that the exit escrow period is over.
// if (currentEpoch() < node.exitEpoch + exitEscrowPeriod(node)) {
// revert PrematureWithdrawal();
// }
totalStake -= balance;

// Delete the node from the stake table.
delete nodes[msg.sender];
philippecamacho marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
72 changes: 72 additions & 0 deletions contracts/test/StakeTable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,76 @@ contract StakeTable_register_Test is Test {
);
assertTrue(EdOnBN254.isEqual(node.schnorrVK, EdOnBN254.EdOnBN254Point(0, 0)));
}

function test_WithdrawFunds_succeeds() public {
philippecamacho marked this conversation as resolved.
Show resolved Hide resolved
// Register the node and set exit epoch
uint64 depositAmount = 10 ether;
uint64 validUntilEpoch = 5;
string memory seed = "123";

(
BN254.G2Point memory blsVK,
EdOnBN254.EdOnBN254Point memory schnorrVK,
BN254.G1Point memory sig
) = genClientWallet(exampleTokenCreator, seed);

// Prepare for the token transfer by granting allowance to the contract
vm.startPrank(exampleTokenCreator);
token.approve(address(stakeTable), depositAmount);

// Balances before registration
assertEq(token.balanceOf(exampleTokenCreator), INITIAL_BALANCE);

// register the node
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.Registered(exampleTokenCreator, 1, depositAmount);
stakeTable.register(blsVK, schnorrVK, depositAmount, sig, validUntilEpoch);

// Withdraw the funds
uint256 balance = stakeTable.withdrawFunds();

// verify the withdraw
assertEq(balance, depositAmount);
assertEq(token.balanceOf(exampleTokenCreator), INITIAL_BALANCE);
assertEq(stakeTable.totalStake(), 0);
assertEq(stakeTable.lookupNode(exampleTokenCreator).balance, 0);
assertEq(stakeTable.lookupNode(exampleTokenCreator).account, address(0));

// test withdraw fails if the user tries to withdraw again
vm.expectRevert(S.NodeNotRegistered.selector);
stakeTable.withdrawFunds();

vm.stopPrank();
}

function test_WithdrawFunds_RevertWhen_NodeNotRegistered() public {
// Register the node and set exit epoch
uint64 depositAmount = 10 ether;
uint64 validUntilEpoch = 5;
string memory seed = "123";

// generate a new blsVK and schnorrVK and register this node
(
BN254.G2Point memory blsVK,
EdOnBN254.EdOnBN254Point memory schnorrVK,
BN254.G1Point memory sig
) = genClientWallet(exampleTokenCreator, seed);

// Prepare for the token transfer by granting allowance to the contract
vm.startPrank(exampleTokenCreator);
token.approve(address(stakeTable), depositAmount);

// register the node
vm.expectEmit(false, false, false, true, address(stakeTable));
emit AbstractStakeTable.Registered(exampleTokenCreator, 1, depositAmount);
stakeTable.register(blsVK, schnorrVK, depositAmount, sig, validUntilEpoch);

vm.stopPrank();

vm.startPrank(makeAddr("randomUser"));
// withdraw the funds
vm.expectRevert(S.NodeNotRegistered.selector);
stakeTable.withdrawFunds();
vm.stopPrank();
}
}
Loading