@@ -109,8 +109,8 @@ contract SFC is Initializable, Ownable, Version {
109
109
// the governance contract (to recalculate votes when the stake changes)
110
110
address public voteBookAddress;
111
111
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 ;
114
114
115
115
// address authorized to initiate redirection
116
116
address public redirectionAuthorizer;
@@ -358,7 +358,7 @@ contract SFC is Initializable, Ownable, Version {
358
358
if (pubkey.length != 66 || pubkey[0 ] != 0xc0 ) {
359
359
revert MalformedPubkey ();
360
360
}
361
- if (pubkeyHashToValidatorID[ keccak256 (pubkey)] != 0 ) {
361
+ if (pubkeyAddressToValidatorID[ _pubkeyToAddress (pubkey)] != 0 ) {
362
362
revert PubkeyUsedByOtherValidator ();
363
363
}
364
364
_createValidator (msg .sender , pubkey);
@@ -932,7 +932,7 @@ contract SFC is Initializable, Ownable, Version {
932
932
}
933
933
934
934
/// Create a new validator.
935
- function _createValidator (address auth , bytes memory pubkey ) internal {
935
+ function _createValidator (address auth , bytes calldata pubkey ) internal {
936
936
uint256 validatorID = ++ lastValidatorID;
937
937
_rawCreateValidator (auth, validatorID, pubkey, OK_STATUS, currentEpoch (), _now (), 0 , 0 );
938
938
}
@@ -941,7 +941,7 @@ contract SFC is Initializable, Ownable, Version {
941
941
function _rawCreateValidator (
942
942
address auth ,
943
943
uint256 validatorID ,
944
- bytes memory pubkey ,
944
+ bytes calldata pubkey ,
945
945
uint256 status ,
946
946
uint256 createdEpoch ,
947
947
uint256 createdTime ,
@@ -959,7 +959,7 @@ contract SFC is Initializable, Ownable, Version {
959
959
getValidator[validatorID].deactivatedEpoch = deactivatedEpoch;
960
960
getValidator[validatorID].auth = auth;
961
961
getValidatorPubkey[validatorID] = pubkey;
962
- pubkeyHashToValidatorID[ keccak256 (pubkey)] = validatorID;
962
+ pubkeyAddressToValidatorID[ _pubkeyToAddress (pubkey)] = validatorID;
963
963
964
964
emit CreatedValidator (validatorID, auth, createdEpoch, createdTime);
965
965
if (deactivatedEpoch != 0 ) {
@@ -1068,6 +1068,11 @@ contract SFC is Initializable, Ownable, Version {
1068
1068
return (rawReward * commission) / Decimal.unit ();
1069
1069
}
1070
1070
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
+
1071
1076
/// Get current time.
1072
1077
function _now () internal view virtual returns (uint256 ) {
1073
1078
return block .timestamp ;
0 commit comments