Skip to content

Commit

Permalink
restrict power set calls to not allow zero
Browse files Browse the repository at this point in the history
  • Loading branch information
JCSanPedro committed Jan 16, 2024
1 parent 2f87c39 commit 94af5c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions contracts/SeekerPowerOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ contract SeekerPowerOracle is ISeekerPowerOracle, Initializable, Ownable2StepUpg

error UnauthorizedRegisterSeekerPowerCall();
error NonceCannotBeReused();
error PowerCannotBeZero();

function initialize(address _oracle) external initializer {
Ownable2StepUpgradeable.__Ownable2Step_init();
Expand All @@ -61,6 +62,10 @@ contract SeekerPowerOracle is ISeekerPowerOracle, Initializable, Ownable2StepUpg
revert UnauthorizedRegisterSeekerPowerCall();
}

if (power == 0) {
revert PowerCannotBeZero();
}

seekerPowers[seekerId] = power;
emit SeekerPowerUpdated(seekerId, power);
}
Expand All @@ -81,6 +86,10 @@ contract SeekerPowerOracle is ISeekerPowerOracle, Initializable, Ownable2StepUpg
revert NonceCannotBeReused();
}

if (power == 0) {
revert PowerCannotBeZero();
}

bytes memory proofMessage = getProofMessage(seekerId, power, nonce);
bytes32 ecdsaHash = ECDSA.toEthSignedMessageHash(proofMessage);

Expand Down
35 changes: 34 additions & 1 deletion test/seekerOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,39 @@ describe('Seeker Power Oracle', () => {
);
});

it('can not set seeker power to 0', async () => {
const seekerId = 111;

await expect(
contracts.seekerPowerOracle.registerSeekerPowerRestricted(seekerId, 0),
).to.be.revertedWithCustomError(
contracts.seekerPowerOracle,
'PowerCannotBeZero',
);

const seekerPower = 0;
const nonce = randomBytes(32);

const proofMessage = await contracts.seekerPowerOracle.getProofMessage(
seekerId,
seekerPower,
nonce,
);

const proof = await accounts[1].signMessage(
Buffer.from(proofMessage.slice(2), 'hex'),
);

await expect(
contracts.seekerPowerOracle
.connect(accounts[2])
.registerSeekerPower(seekerId, seekerPower, nonce, proof),
).to.be.revertedWithCustomError(
contracts.seekerPowerOracle,
'PowerCannotBeZero',
);
});

it('can update multiple seeker powers', async () => {
for (let i = 1; i < 11; i++) {
const seekerId = i;
Expand Down Expand Up @@ -216,7 +249,7 @@ describe('Seeker Power Oracle', () => {
});

it('can update the same seeker power multiple times', async () => {
for (let i = 0; i < 5; i++) {
for (let i = 1; i < 6; i++) {
const seekerId = 1;
const seekerPower = i * 1111;
const nonce = randomBytes(32);
Expand Down

0 comments on commit 94af5c5

Please sign in to comment.