Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick95550 committed Jun 21, 2024
1 parent 3b38c2e commit 768aad3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 60 deletions.
6 changes: 3 additions & 3 deletions contracts/staking/seekers/ISeekerStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import "./SeekerStatsOracle.sol";

interface ISeekerStakingManager {
struct StakedSeeker {
uint256 seekerId;
address node;
address user;
uint256 seekerId; // bridged seeker id in TRN
address node; // sylo node futureverse address
address user; // msg.sender - the seeker owner, futureverse address in TRN
}

function stakeSeeker(
Expand Down
80 changes: 44 additions & 36 deletions contracts/staking/seekers/SeekerStakingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ contract SeekerStakingManager is
ERC165
{
/**
* @notice ERC721 contract for bridged Seekers. Used for verifying ownership
* @notice ERC721 contract for bridged Seekers in TRN. Used for verifying ownership
* of a seeker.
*/
IERC721 public rootSeekers;

/**
* @notice SeekerStatsOracle contract
* @notice Holds the Seekers metadata from Ethereum, set manually in TRN
*/
SeekerStatsOracle public oracle;

Expand All @@ -42,35 +42,35 @@ contract SeekerStakingManager is
mapping(address => uint256[]) public stakedSeekersByUser;

/**
* @notice default staked seeker
* @notice variable used for comparision with the mapping
* "stakedSeekersById", specificly whether the value for a given
* key has been defined.
*/
StakedSeeker public defaultStakedSeeker;
StakedSeeker private defaultStakedSeeker;

error NodeAddressCannotBeNil();
error FromNodeAddressCannotBeNil();
error ToNodeAddressCannotBeNil();
error SeekerProofIsEmpty();
error SeekerProofCannotBeEmpty();
error RootSeekersCannotBeZeroAddress();
error SeekerStatsOracleCannotBeZeroAddress();
error SenderAccountMustOwnSeekerId();
error SeekerStatsOracleCannotBeNil();
error SenderMustOwnSeekerId();
error SeekerNotYetStaked();
error SeekerNotStakedToNode();
error SeekerNotStakedBySender();
error CannotTransferSeekerToSameNode();

enum StakedErrors {
SEEKER_NOT_YET_STAKED,
SEEKER_NOT_STAKED_TO_NODE,
SEEKER_NOT_STAKED_BY_SENDER,
NIL
NIL // No error
}

function initialize(IERC721 _rootSeekers, SeekerStatsOracle _oracle) external initializer {
if (address(_rootSeekers) == address(0)) {
revert RootSeekersCannotBeZeroAddress();
}
if (address(_oracle) == address(0)) {
revert SeekerStatsOracleCannotBeZeroAddress();
revert SeekerStatsOracleCannotBeNil();
}

Ownable2StepUpgradeable.__Ownable2Step_init();
Expand All @@ -80,16 +80,16 @@ contract SeekerStakingManager is
}

function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return
interfaceId == type(ISeekerStakingManager).interfaceId ||
super.supportsInterface(interfaceId);
return interfaceId == type(ISeekerStakingManager).interfaceId;
}

/**
* @notice Stakes a seeker
* @param node Address of node to stake seeker against
* @param seeker The object containing the seekers statistics
* @param seekerStatsProof The signature of the seekers proof message, signed by the oracle account.
* @param seekerStatsProof The signature of the seekers proof
* message, signed by the oracle account and only required if
* the seeker is not registered yet.
*/
function stakeSeeker(
address node,
Expand All @@ -99,6 +99,20 @@ contract SeekerStakingManager is
_stakeSeeker(node, seeker, seekerStatsProof);
}

/**
* @param seekerStatsProof The signature of the seekers proof
* message, signed by the oracle account and only required if
* the seeker is not registered yet.
*/

/**
* @notice Stake a multiple seekers
* @param node Address of node to stake seeker against
* @param seekers A list of objects containing the seekers statistics
* @param seekerStatsProofs A list of seeker proof message
* signatures, signed by the oracle account and only required if
* the seeker is not registered yet.
*/
function stakeSeekers(
address node,
SeekerStatsOracle.Seeker[] calldata seekers,
Expand All @@ -109,12 +123,6 @@ contract SeekerStakingManager is
}
}

/**
* @notice Stakes a seeker
* @param node Address of node to stake seeker against
* @param seeker The object containing the seekers statistics
* @param seekerStatsProof The signature of the seekers proof message, signed by the oracle account.
*/
function _stakeSeeker(
address node,
SeekerStatsOracle.Seeker calldata seeker,
Expand All @@ -124,17 +132,17 @@ contract SeekerStakingManager is
revert NodeAddressCannotBeNil();
}
if (rootSeekers.ownerOf(seeker.seekerId) != msg.sender) {
revert SenderAccountMustOwnSeekerId();
revert SenderMustOwnSeekerId();
}
// Register a seeker if not registered yet
if (!oracle.isSeekerRegistered(seeker)) {
if (seekerStatsProof.length == 0) {
revert SeekerProofIsEmpty();
revert SeekerProofCannotBeEmpty();
}
oracle.registerSeeker(seeker, seekerStatsProof);
}

StakedErrors err = _isSeekerStakedError(node, seeker.seekerId);
if (err == StakedErrors.NIL) {
if (_validateStakedSeeker(node, seeker.seekerId) == StakedErrors.NIL) {
_unstakeSeeker(node, seeker.seekerId);
}

Expand All @@ -148,7 +156,12 @@ contract SeekerStakingManager is
}

/**
* @notice Unstake a seeker
* @notice Unstakes a seeker, will revert with staked error with
* one of four cases.
* 1) Sender does not own seeker
* 2) Seeker is not yet staked
* 3) Seeker is not staked by the sender
* 4) Seeker is not staked to provided node address
* @param node Address of node to unstake seeker from
* @param seekerId Seeker ID of staked seeker
*/
Expand All @@ -157,10 +170,10 @@ contract SeekerStakingManager is
revert FromNodeAddressCannotBeNil();
}
if (rootSeekers.ownerOf(seekerId) != msg.sender) {
revert SenderAccountMustOwnSeekerId();
revert SenderMustOwnSeekerId();
}

StakedErrors err = _isSeekerStakedError(node, seekerId);
StakedErrors err = _validateStakedSeeker(node, seekerId);
if (err == StakedErrors.SEEKER_NOT_YET_STAKED) {
revert SeekerNotYetStaked();
}
Expand All @@ -174,11 +187,6 @@ contract SeekerStakingManager is
_unstakeSeeker(node, seekerId);
}

/**
* @notice Unstake a seeker
* @param node Address of node to unstake seeker from
* @param seekerId Seeker ID of staked seeker
*/
function _unstakeSeeker(address node, uint256 seekerId) internal {
for (uint256 i = 0; i < stakedSeekersByNode[node].length; ++i) {
if (stakedSeekersByNode[node][i] == seekerId) {
Expand All @@ -205,7 +213,7 @@ contract SeekerStakingManager is
delete stakedSeekersById[seekerId];
}

function _isSeekerStakedError(
function _validateStakedSeeker(
address node,
uint256 seekerId
) internal view returns (StakedErrors) {
Expand All @@ -225,15 +233,15 @@ contract SeekerStakingManager is
}

/**
* @notice Get a staked seeker by node address
* @notice Get all seekers that were staked to a specific node address
* @param node Address of node seeker is staked against
*/
function getStakedSeekersByNode(address node) external view returns (uint256[] memory) {
return stakedSeekersByNode[node];
}

/**
* @notice Get a staked seeker by user address
* @notice Get all staked seekers owned by user address
* @param user Address of user that staked seeker
*/
function getStakedSeekersByUser(address user) external view returns (uint256[] memory) {
Expand Down
30 changes: 14 additions & 16 deletions contracts/staking/seekers/SeekerStatsOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ contract SeekerStatsOracle is ISeekerStatsOracle, Initializable, Ownable2StepUpg
*/
mapping(uint256 => Seeker) public seekerStats;

/**
* @notice variable used for comparision with the mapping
* "seekerStats", specificly whether the value for a given
* key has been defined.
*/
Seeker public defaultSeeker;

/**
Expand All @@ -44,8 +49,7 @@ contract SeekerStatsOracle is ISeekerStatsOracle, Initializable, Ownable2StepUpg

/** errors **/
error OracleAddressCannotBeNil();
error SeekerProofIsEmpty();
error UnauthorizedRegisterSeekerStats();
error SenderMustBeOracelAccount();
error InvalidSignatureForSeekerProof();
error SeekerNotRegistered(uint256 seekerId);

Expand Down Expand Up @@ -82,7 +86,7 @@ contract SeekerStatsOracle is ISeekerStatsOracle, Initializable, Ownable2StepUpg

/**
* @notice Returns true if the oracle account signed the proof message for the given seeker.
* @param seeker The object containing the seekers statistics.
* @param seeker The object containing the seeker's statistics.
* @param signature The signature of the seekers proof message, signed by the oracle account.
*/
function isSeekerStatsProofValid(
Expand All @@ -101,7 +105,7 @@ contract SeekerStatsOracle is ISeekerStatsOracle, Initializable, Ownable2StepUpg

/**
* @notice Creates a unique proofing message for the provided seeker.
* @param seeker The object containing the seekers statistics.
* @param seeker The object containing the seeker's statistics.
*/
function createProofMessage(Seeker calldata seeker) external pure returns (bytes memory) {
return _createProofMessage(seeker);
Expand All @@ -126,12 +130,12 @@ contract SeekerStatsOracle is ISeekerStatsOracle, Initializable, Ownable2StepUpg
}

/**
* @notice Registers a seeker only callable from oracle
* @notice Registers a seeker - only callable from oracle
* @param seeker The object containing the seekers statistics.
*/
function registerSeekerRestricted(Seeker calldata seeker) external {
if (msg.sender != oracle) {
revert UnauthorizedRegisterSeekerStats();
revert SenderMustBeOracelAccount();
}

seekerStats[seeker.seekerId] = seeker;
Expand Down Expand Up @@ -169,23 +173,17 @@ contract SeekerStatsOracle is ISeekerStatsOracle, Initializable, Ownable2StepUpg
}

/**
* @notice Checks if a seeker is registered
* @notice Validates that the contract has registered the given seeker
* @param seeker The object containing the seeker statistics
*/
function isSeekerRegistered(Seeker calldata seeker) external view returns (bool) {
return _isSeekerRegistered(seeker);
}

/**
* @notice Checks if a seeker is registered
* @param seeker The object containing the seeker statistics
*/
function _isSeekerRegistered(Seeker memory seeker) internal view returns (bool) {
Seeker memory registeredSeeker = seekerStats[seeker.seekerId];
if (keccak256(abi.encode(registeredSeeker)) == keccak256(abi.encode(defaultSeeker))) {
return false;
}
return true;
return
keccak256(abi.encode(seekerStats[seeker.seekerId])) !=
keccak256(abi.encode(defaultSeeker));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/seekerStats/stakingStats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Seeker Stats', () => {
seekerStatsOracle.registerSeekerRestricted(seeker),
).to.be.revertedWithCustomError(
seekerStatsOracle,
'UnauthorizedRegisterSeekerStats',
'SenderMustBeOracelAccount',
);
});

Expand Down
11 changes: 7 additions & 4 deletions test/staking/seekerStakingManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Seeker Staking Manager', () => {
),
).to.be.revertedWithCustomError(
seekerStakingManagerTemp,
'SeekerStatsOracleCannotBeZeroAddress',
'SeekerStatsOracleCannotBeNil',
);
});

Expand Down Expand Up @@ -106,7 +106,7 @@ describe('Seeker Staking Manager', () => {
.stakeSeeker(nodeOne, seeker, Buffer.from('')),
).to.be.revertedWithCustomError(
seekerStakingManager,
'SenderAccountMustOwnSeekerId',
'SenderMustOwnSeekerId',
);
});

Expand All @@ -117,7 +117,10 @@ describe('Seeker Staking Manager', () => {

await expect(
seekerStakingManager.stakeSeeker(nodeOne, seeker, Buffer.from('')),
).to.be.revertedWithCustomError(seekerStakingManager, 'SeekerProofIsEmpty');
).to.be.revertedWithCustomError(
seekerStakingManager,
'SeekerProofCannotBeEmpty',
);
});

it('stake seeker emits event', async () => {
Expand Down Expand Up @@ -504,7 +507,7 @@ describe('Seeker Staking Manager', () => {
seekerStakingManager.connect(nonSeekerOwner).unstakeSeeker(nodeOne, 10),
).to.be.revertedWithCustomError(
seekerStakingManager,
'SenderAccountMustOwnSeekerId',
'SenderMustOwnSeekerId',
);
});

Expand Down

0 comments on commit 768aad3

Please sign in to comment.