Skip to content

Commit 8e6a609

Browse files
committed
Replace pubkeyHashToValidatorID by pubkeyAddressToValidatorID
1 parent 5aa16da commit 8e6a609

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

contracts/interfaces/ISFC.sol

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ interface ISFC {
6363

6464
function getValidatorPubkey(uint256) external view returns (bytes memory);
6565

66+
function pubkeyAddressToValidatorID(address pubkeyAddress) external view returns (uint256);
67+
6668
function getWithdrawalRequest(
6769
address,
6870
uint256,

contracts/sfc/SFC.sol

+11-6
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ contract SFC is Initializable, Ownable, Version {
109109
// the governance contract (to recalculate votes when the stake changes)
110110
address public voteBookAddress;
111111

112-
// keccak256(pubkey bytes) => validator ID (prevents using the same key by multiple validators)
113-
mapping(bytes32 pubkeyHash => uint256 validatorID) internal pubkeyHashToValidatorID;
112+
// address derived from the validator pubkey => validator id
113+
mapping(address pubkeyAddress => uint256 validatorID) public pubkeyAddressToValidatorID;
114114

115115
// address authorized to initiate redirection
116116
address public redirectionAuthorizer;
@@ -358,7 +358,7 @@ contract SFC is Initializable, Ownable, Version {
358358
if (pubkey.length != 66 || pubkey[0] != 0xc0) {
359359
revert MalformedPubkey();
360360
}
361-
if (pubkeyHashToValidatorID[keccak256(pubkey)] != 0) {
361+
if (pubkeyAddressToValidatorID[_pubkeyToAddress(pubkey)] != 0) {
362362
revert PubkeyUsedByOtherValidator();
363363
}
364364
_createValidator(msg.sender, pubkey);
@@ -932,7 +932,7 @@ contract SFC is Initializable, Ownable, Version {
932932
}
933933

934934
/// Create a new validator.
935-
function _createValidator(address auth, bytes memory pubkey) internal {
935+
function _createValidator(address auth, bytes calldata pubkey) internal {
936936
uint256 validatorID = ++lastValidatorID;
937937
_rawCreateValidator(auth, validatorID, pubkey, OK_STATUS, currentEpoch(), _now(), 0, 0);
938938
}
@@ -941,7 +941,7 @@ contract SFC is Initializable, Ownable, Version {
941941
function _rawCreateValidator(
942942
address auth,
943943
uint256 validatorID,
944-
bytes memory pubkey,
944+
bytes calldata pubkey,
945945
uint256 status,
946946
uint256 createdEpoch,
947947
uint256 createdTime,
@@ -959,7 +959,7 @@ contract SFC is Initializable, Ownable, Version {
959959
getValidator[validatorID].deactivatedEpoch = deactivatedEpoch;
960960
getValidator[validatorID].auth = auth;
961961
getValidatorPubkey[validatorID] = pubkey;
962-
pubkeyHashToValidatorID[keccak256(pubkey)] = validatorID;
962+
pubkeyAddressToValidatorID[_pubkeyToAddress(pubkey)] = validatorID;
963963

964964
emit CreatedValidator(validatorID, auth, createdEpoch, createdTime);
965965
if (deactivatedEpoch != 0) {
@@ -1068,6 +1068,11 @@ contract SFC is Initializable, Ownable, Version {
10681068
return (rawReward * commission) / Decimal.unit();
10691069
}
10701070

1071+
/// Derive address from validator private key
1072+
function _pubkeyToAddress(bytes calldata pubkey) private pure returns (address) {
1073+
return address(uint160(uint256(keccak256(pubkey[2:]))));
1074+
}
1075+
10711076
/// Get current time.
10721077
function _now() internal view virtual returns (uint256) {
10731078
return block.timestamp;

0 commit comments

Comments
 (0)