Switch from a cache to a query contract, cache is out of scope

This commit is contained in:
William Entriken 2018-02-20 17:33:49 -05:00 committed by GitHub
parent b675b4b3fa
commit 12fe0b505c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -115,48 +115,29 @@ Following is a caching contract that detects which interfaces other contracts im
```solidity ```solidity
pragma solidity ^0.4.20; pragma solidity ^0.4.20;
contract ERC165Cache { contract ERC165Query {
bytes4 constant InvalidID = 0xffffffff; bytes4 constant InvalidID = 0xffffffff;
bytes4 constant ERC165ID = 0x01ffc9a7; bytes4 constant ERC165ID = 0x01ffc9a7;
enum ImplStatus { Unknown, No, Yes } function doesContractImplementInterface(address _contract, bytes4 _interfaceId) external view returns (bool) {
mapping (address => mapping (bytes4 => ImplStatus)) cache;
// Return value from cache if available
function interfaceSupported(address _contract, bytes4 _interfaceId) external returns (bool) {
ImplStatus status = cache[_contract][_interfaceId];
if (status == ImplStatus.Unknown) {
return checkInterfaceSupported(_contract, _interfaceId);
}
return status == ImplStatus.Yes;
}
// Repull result into cache
function checkInterfaceSupported(address _contract, bytes4 _interfaceId) public returns (bool) {
ImplStatus status = determineInterfaceImplementationStatus(_contract, _interfaceId);
cache[_contract][_interfaceId] = status;
return status == ImplStatus.Yes;
}
function determineInterfaceImplementationStatus(address _contract, bytes4 _interfaceId) internal view returns (ImplStatus) {
uint256 success; uint256 success;
uint256 result; uint256 result;
(success, result) = noThrowCall(_contract, ERC165ID); (success, result) = noThrowCall(_contract, ERC165ID);
if ((success==0)||(result==0)) { if ((success==0)||(result==0)) {
return ImplStatus.No; return false;
} }
(success, result) = noThrowCall(_contract, InvalidID); (success, result) = noThrowCall(_contract, InvalidID);
if ((success==0)||(result!=0)) { if ((success==0)||(result!=0)) {
return ImplStatus.No; return false;
} }
(success, result) = noThrowCall(_contract, _interfaceId); (success, result) = noThrowCall(_contract, _interfaceId);
if ((success==1)&&(result==1)) { if ((success==1)&&(result==1)) {
return ImplStatus.Yes; return true;
} }
return ImplStatus.No; return false;
} }
function noThrowCall(address _contract, bytes4 _interfaceId) constant internal returns (uint256 success, uint256 result) { function noThrowCall(address _contract, bytes4 _interfaceId) constant internal returns (uint256 success, uint256 result) {
@ -171,7 +152,7 @@ contract ERC165Cache {
30000, // 30k gas 30000, // 30k gas
_contract, // To addr _contract, // To addr
x, // Inputs are stored at location x x, // Inputs are stored at location x
0x20, // Inputs are 8 byes long 0x20, // Inputs are 8 byes long
x, // Store output over input (saves space) x, // Store output over input (saves space)
0x20) // Outputs are 32 bytes long 0x20) // Outputs are 32 bytes long