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

New staking - part 1 and 2 - SWIP-19 and 20 combined #252

Merged
merged 39 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
de1413d
reformat code and add some state optimizations
0xCardinalError Apr 10, 2024
955a3da
refactor code to be more concise, refactor test for proper deployment
0xCardinalError Apr 10, 2024
bad3a0e
remove reverts use custom errors
0xCardinalError Apr 10, 2024
170f00e
rewrite tests to match new errors
0xCardinalError Apr 10, 2024
52fb11f
cleanup errors
0xCardinalError Apr 10, 2024
6c5df4a
swap all the overlays keys with address keys and use them
0xCardinalError Apr 16, 2024
c317a3a
change redis contract to use new staking mappings
0xCardinalError Apr 16, 2024
0373ea6
fixing all staking tests
0xCardinalError Apr 16, 2024
6de0e5a
fix redistribution tests, 5 more failing
0xCardinalError Apr 16, 2024
df5b180
fix test errors, add function to check overlay is proper
0xCardinalError Apr 16, 2024
fd0dfd3
fix game stakes
0xCardinalError Apr 18, 2024
b3e1537
add code to change overlays, add tests to test changing of overlays t…
0xCardinalError Apr 18, 2024
a8c47fc
remove owner from staking mapping, adjust tests to work with that and…
0xCardinalError May 6, 2024
dc217e6
remove obsolete var
0xCardinalError May 6, 2024
ad14c60
change event name
0xCardinalError May 17, 2024
6c707f2
remove obsolete parametar
0xCardinalError May 17, 2024
25e7b18
change owner to sender, add crucial revert for hopping not to be hack…
0xCardinalError May 17, 2024
d7541d6
fix tests to work with new contract changes
0xCardinalError May 17, 2024
d88fd0c
change the name
0xCardinalError May 17, 2024
9bb99bc
switch to using overload function for isParticipatingInUpcomingRound
0xCardinalError May 20, 2024
cc9d49c
remove overlay from commit, use lookup for whole struct and reuse it
0xCardinalError May 20, 2024
6121290
change reveal to not use overlay, adjust tests
0xCardinalError May 20, 2024
a847545
add most optimized version of struct reading
0xCardinalError May 22, 2024
1cc528e
cleanup
0xCardinalError May 22, 2024
850bbb3
add direct lookups or better gas util
0xCardinalError May 23, 2024
fc1534b
fix tests to use actuall node address on all places
0xCardinalError May 24, 2024
b40526c
cleanup logs, cleanup tests
0xCardinalError May 24, 2024
551da73
remove old logs from tests
0xCardinalError May 24, 2024
3776f1b
remove console log
0xCardinalError May 24, 2024
5673ba1
remove double staking calls
0xCardinalError May 28, 2024
8e4e93d
fix doc and line break
0xCardinalError May 28, 2024
75072a4
Merge branch 'master' of github.com:ethersphere/storage-incentives in…
0xCardinalError May 29, 2024
e285072
remove oveloading function, no need for it as its not called by contr…
0xCardinalError Jun 10, 2024
420f219
change functionality of depositStake to be one function to do all thi…
0xCardinalError Jun 10, 2024
c558efb
change to manageState
0xCardinalError Jun 10, 2024
4b492c3
adjust tests
0xCardinalError Jun 10, 2024
a276407
add deployments with updated contracts
0xCardinalError Jul 3, 2024
0d6540e
Improved staking (new staking - part 2 - SWIP-20) (#264)
0xCardinalError Aug 26, 2024
87e8c6a
Merge branch 'master' into new_staking
0xCardinalError Aug 26, 2024
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
77 changes: 53 additions & 24 deletions src/Redistribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ interface IPriceOracle {
}

interface IStakeRegistry {
function freezeDeposit(bytes32 overlay, uint256 time) external;
struct Stake {
bytes32 overlay;
uint256 stakeAmount;
uint256 lastUpdatedBlockNumber;
bool isValue;
}

function freezeDeposit(address _owner, uint256 _time) external;
0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved

function lastUpdatedBlockNumberOfOverlay(bytes32 overlay) external view returns (uint256);
function lastUpdatedBlockNumberOfAddress(address _owner) external view returns (uint256);

function ownerOfOverlay(bytes32 overlay) external view returns (address);
function overlayOfAddress(address _owner) external view returns (bytes32);

function stakeOfOverlay(bytes32 overlay) external view returns (uint256);
function stakeOfAddress(address _owner) external view returns (uint256);

function getStakeStruct(address _owner) external view returns (Stake memory);
0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -274,12 +283,12 @@ contract Redistribution is AccessControl, Pausable {
* _obfuscatedHash_ by providing their _overlay_, reported storage _depth_, reserve commitment _hash_ and a
* randomly generated, and secret _revealNonce_ to the _wrapCommit_ method.
* @param _obfuscatedHash The calculated hash resultant of the required pre-image values.
0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved
* @param _overlay The overlay referenced in the pre-image. Must be staked by at least the minimum value,
* and be derived from the same key pair as the message sender.
*/
function commit(bytes32 _obfuscatedHash, bytes32 _overlay, uint64 _roundNumber) external whenNotPaused {
function commit(bytes32 _obfuscatedHash, uint64 _roundNumber) external whenNotPaused {
uint64 cr = currentRound();
uint256 nstake = Stakes.stakeOfOverlay(_overlay);
bytes32 _overlay = Stakes.overlayOfAddress(msg.sender);
uint256 _stake = Stakes.stakeOfAddress(msg.sender);
0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved

if (!currentPhaseCommit()) {
revert NotCommitPhase();
Expand All @@ -296,15 +305,11 @@ contract Redistribution is AccessControl, Pausable {
revert CommitRoundNotStarted();
}

if (nstake < MIN_STAKE) {
if (_stake < MIN_STAKE) {
revert BelowMinimumStake();
}

if (Stakes.ownerOfOverlay(_overlay) != msg.sender) {
revert NotMatchingOwner();
}

if (Stakes.lastUpdatedBlockNumberOfOverlay(_overlay) >= block.number - 2 * ROUND_LENGTH) {
if (Stakes.lastUpdatedBlockNumberOfAddress(msg.sender) >= block.number - 2 * ROUND_LENGTH) {
revert MustStake2Rounds();
}

Expand Down Expand Up @@ -332,7 +337,7 @@ contract Redistribution is AccessControl, Pausable {
overlay: _overlay,
owner: msg.sender,
revealed: false,
stake: nstake,
stake: _stake,
obfuscatedHash: _obfuscatedHash,
revealIndex: 0
})
Expand All @@ -343,13 +348,13 @@ contract Redistribution is AccessControl, Pausable {

/**
* @notice Reveal the pre-image values used to generate commit provided during this round's commit phase.
* @param _overlay The overlay address of the applicant.
* @param _depth The reported depth.
* @param _hash The reserve commitment hash.
* @param _revealNonce The nonce used to generate the commit that is being revealed.
*/
function reveal(bytes32 _overlay, uint8 _depth, bytes32 _hash, bytes32 _revealNonce) external whenNotPaused {
function reveal(uint8 _depth, bytes32 _hash, bytes32 _revealNonce) external whenNotPaused {
uint64 cr = currentRound();
bytes32 _overlay = Stakes.overlayOfAddress(msg.sender);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


if (_depth < currentMinimumDepth()) {
revert OutOfDepth();
Expand All @@ -373,6 +378,7 @@ contract Redistribution is AccessControl, Pausable {
}

bytes32 obfuscatedHash = wrapCommit(_overlay, _depth, _hash, _revealNonce);

0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved
uint256 id = findCommit(_overlay, obfuscatedHash);
Commit memory revealedCommit = currentCommits[id];

Expand Down Expand Up @@ -532,7 +538,7 @@ contract Redistribution is AccessControl, Pausable {
(truthRevealedHash != currentReveal.hash || truthRevealedDepth != currentReveal.depth)
) {
Stakes.freezeDeposit(
currentReveal.overlay,
currentReveal.owner,
penaltyMultiplierDisagreement * ROUND_LENGTH * uint256(2 ** truthRevealedDepth)
);
}
Expand All @@ -542,7 +548,7 @@ contract Redistribution is AccessControl, Pausable {
// slash in later phase (ph5)
// Stakes.slashDeposit(currentCommits[i].overlay, currentCommits[i].stake);
Stakes.freezeDeposit(
currentCommit.overlay,
currentCommit.owner,
penaltyMultiplierNonRevealed * ROUND_LENGTH * uint256(2 ** truthRevealedDepth)
);
}
Expand Down Expand Up @@ -769,6 +775,7 @@ contract Redistribution is AccessControl, Pausable {
if (minimum == 0) {
return true;
}

return uint256(A ^ B) < uint256(2 ** (256 - minimum));
}

Expand All @@ -793,23 +800,45 @@ contract Redistribution is AccessControl, Pausable {

/**
* @notice Determine if a the owner of a given overlay can participate in the upcoming round.
* @param overlay The overlay address of the applicant.
* @param depth The storage depth the applicant intends to report.
* use msg.sender as default parametar to check value
* @param _depth The storage depth the applicant intends to report.
*/
function isParticipatingInUpcomingRound(uint8 _depth) public view returns (bool) {
if (currentPhaseReveal()) {
revert WrongPhase();
}

if (Stakes.lastUpdatedBlockNumberOfAddress(msg.sender) >= block.number - 2 * ROUND_LENGTH) {
revert MustStake2Rounds();
}

if (Stakes.stakeOfAddress(msg.sender) < MIN_STAKE) {
revert BelowMinimumStake();
}

return inProximity(Stakes.overlayOfAddress(msg.sender), currentRoundAnchor(), _depth);
}

/**
* @notice Determine if a the owner of a given overlay can participate in the upcoming round.
* overloading function for default one with msg.sender
* @param _owner The address of the applicant from.
0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved
* @param _depth The storage depth the applicant intends to report.
*/
function isParticipatingInUpcomingRound(bytes32 overlay, uint8 depth) public view returns (bool) {
function isParticipatingInUpcomingRound(address _owner, uint8 _depth) public view returns (bool) {
if (currentPhaseReveal()) {
revert WrongPhase();
}

if (Stakes.lastUpdatedBlockNumberOfOverlay(overlay) >= block.number - 2 * ROUND_LENGTH) {
if (Stakes.lastUpdatedBlockNumberOfAddress(_owner) >= block.number - 2 * ROUND_LENGTH) {
revert MustStake2Rounds();
}

if (Stakes.stakeOfOverlay(overlay) < MIN_STAKE) {
if (Stakes.stakeOfAddress(_owner) < MIN_STAKE) {
revert BelowMinimumStake();
}

return inProximity(overlay, currentRoundAnchor(), depth);
return inProximity(Stakes.overlayOfAddress(_owner), currentRoundAnchor(), _depth);
0xCardinalError marked this conversation as resolved.
Show resolved Hide resolved
}

// ----------------------------- Reveal ------------------------------
Expand Down
Loading
Loading