-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use validator set precompiles in Attestations (#1248)
- Loading branch information
Showing
5 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pragma solidity ^0.5.3; | ||
|
||
|
||
contract UsingPrecompiles { | ||
/** | ||
* @notice Gets a validator address from the current validator set. | ||
* @param index Index of requested validator in the validator set as sorted by the election. | ||
* @return Address of validator at the requested index. | ||
*/ | ||
function validatorAddressFromCurrentSet(uint256 index) public view returns (address) { | ||
address validatorAddress; | ||
assembly { | ||
let newCallDataPosition := mload(0x40) | ||
mstore(newCallDataPosition, index) | ||
let success := staticcall(5000, 0xfa, newCallDataPosition, 32, 0, 0) | ||
returndatacopy(add(newCallDataPosition, 64), 0, 32) | ||
validatorAddress := mload(add(newCallDataPosition, 64)) | ||
} | ||
|
||
return validatorAddress; | ||
} | ||
|
||
/** | ||
* @notice Gets the size of the current elected validator set. | ||
* @return Size of the current elected validator set. | ||
*/ | ||
function numberValidatorsInCurrentSet() public view returns (uint256) { | ||
uint256 numberValidators; | ||
assembly { | ||
let success := staticcall(5000, 0xf9, 0, 0, 0, 0) | ||
let returnData := mload(0x40) | ||
returndatacopy(returnData, 0, 32) | ||
numberValidators := mload(returnData) | ||
} | ||
|
||
return numberValidators; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/protocol/contracts/identity/test/TestAttestations.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
pragma solidity ^0.5.3; | ||
|
||
import "../Attestations.sol"; | ||
|
||
|
||
/* | ||
* We need a test contract that behaves like the actual Attestations contract, | ||
* but mocks the implementations of the validator set getters. Otherwise we | ||
* couldn't test `request` with the current ganache local testnet. | ||
*/ | ||
contract TestAttestations is Attestations { | ||
address[] private __testValidators; | ||
|
||
function __setValidators(address[] memory validators) public { | ||
__testValidators = validators; | ||
} | ||
|
||
function numberValidatorsInCurrentSet() public view returns (uint256) { | ||
return __testValidators.length; | ||
} | ||
|
||
function validatorAddressFromCurrentSet(uint256 index) public view returns (address) { | ||
return __testValidators[index]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters