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

prevent registering same bls pub key #43

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
23 changes: 21 additions & 2 deletions artifacts/contracts/StakeManager.sol/StakeManager.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions contracts/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ contract StakeManager is IStakeManager, System {
address[] public stakerSigners;
// A contract that manages candidate validators
ICandidateValidatorManager public candidateManager;
// Mapping of BLS public key to owner
// The first 32 bytes of the BLS public key is used as the key.
mapping(bytes32 => address) public blsPublicKeyToOwner;

/*************
* Modifiers *
Expand Down Expand Up @@ -172,6 +175,8 @@ contract StakeManager is IStakeManager, System {

/**
* @inheritdoc IStakeManager
* @notice DOS attack can be possible as anyone can call this function.
* A developer should avoid iteration of `validatorOwners`.
*/
function joinValidator(address operator) external {
address owner = msg.sender;
Expand Down Expand Up @@ -212,10 +217,17 @@ contract StakeManager is IStakeManager, System {
function updateBLSPublicKey(bytes calldata blsPublicKey) external validatorExists(msg.sender) {
address owner = msg.sender;

// BLS public key should be unique, as it is identifier of a vote.
bytes32 mapKey = bytes32(blsPublicKey[0:32]);
if (blsPublicKeyToOwner[mapKey] != address(0)) {
revert AlreadyInUse();
}

Validator storage validator = validators[owner];
bytes memory oldBLSPublicKey = validator.blsPublicKey;

validator.updateBLSPublicKey(blsPublicKey);
blsPublicKeyToOwner[mapKey] = owner;

emit BLSPublicKeyUpdated(owner, oldBLSPublicKey, blsPublicKey);
}
Expand Down
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const config: HardhatUserConfig = {
hardhat: {
initialBaseFeePerGas: 0,
gasPrice: 0,
allowUnlimitedContractSize: true,
tak1827 marked this conversation as resolved.
Show resolved Hide resolved
},
},
mocha: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oasys-genesis-contract",
"version": "1.6.0",
"version": "1.6.1",
"description": "The genesis contracts of Oasys Blockchain",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions test/StakeManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ describe('StakeManager', () => {
tx = validator.updateBLSPublicKey(newBLSPubKey, attacker)
await expect(tx).to.revertedWith('ValidatorDoesNotExist()')

// fail: already registered BLS public key
const validator2 = new Validator(stakeManager, accounts[1], accounts[2])
await validator2.joinValidator()
tx = validator2.updateBLSPublicKey(blsPubKey)
await expect(tx).to.revertedWith('AlreadyInUse()')

// update
await expect(await validator.updateBLSPublicKey(newBLSPubKey))
.to.emit(stakeManager, 'BLSPublicKeyUpdated')
Expand Down