all files / contracts/ GatewayInterface.sol

97.22% Statements 35/36
86.36% Branches 19/22
100% Functions 9/9
97.44% Lines 38/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172                                                            81×                                             33×   33×             33×   20×   19× 19×                       13×                     11× 10× 10×               10×                     27× 27× 27×     27× 27×                                             37×   37× 37×   34× 32×               34× 33×    
/*
 
 * @name        Gateway Interface Contract
 * @package     BlockBitsIO
 * @author      Micky Socaci <micky@nowlive.ro>
 
 Used as a resolver to retrieve the latest deployed version of the Application
 
 ENS: gateway.main.blockbits.eth will point directly to this contract.
 
    ADD ENS domain ownership / transfer methods
 
*/
 
pragma solidity ^0.4.17;
 
import "./ApplicationEntity.sol";
 
contract GatewayInterface {
 
    event EventGatewayNewLinkRequest ( address indexed newAddress );
    event EventGatewayNewAddress ( address indexed newAddress );
 
    address public currentApplicationEntityAddress;     // currently linked ApplicationEntity address
    ApplicationEntity private currentApp;
 
    address public deployerAddress;
 
    // constructor
    function GatewayInterface() public {
        deployerAddress = msg.sender;
    }
 
    /*
    * Get current ApplicationEntity Contract address
    *
    * @return       address currentApplicationEntityAddress
    */
    function getApplicationAddress() external view returns (address) {
        return currentApplicationEntityAddress;
    }
 
    /*
    * ApplicationEntity Contract requests to be linked
    *
    * @param        address _newAddress
    * @param        bytes32 _sourceCodeUrl
    * @modifier     validCodeUpgradeInitiator
    */
    function requestCodeUpgrade( address _newAddress, bytes32 _sourceCodeUrl )
        external
        validCodeUpgradeInitiator
        returns (bool)
    {
        Erequire(_newAddress != address(0x0));
 
        EventGatewayNewLinkRequest ( _newAddress );
 
        /*
            case 1 - Newly Deployed Gateway and Application
 
            gateway links to app and initializes
        */
        if(currentApplicationEntityAddress == address(0x0)) {
 
            if(!ApplicationEntity(_newAddress).initializeAssetsToThisApplication()) {
                revert();
            }
            link(_newAddress);
            return true;
        } else {
            /*
                case 2 - Actual Code Upgrade Request
 
                - Current app should exist already
                - Current app
                    - Create a proposal
                    - Vote on result
                    - Get Result
                    - Approve Result
            */
            currentApp.createCodeUpgradeProposal(_newAddress, _sourceCodeUrl);
        }
    }
 
    /*
    * ApplicationEntity Contract approves code Upgrade
    *
    * @param        address _newAddress
    * @modifier     onlyCurrentApplicationEntity
    */
    function approveCodeUpgrade( address _newAddress ) external returns (bool) {
        require(msg.sender == currentApplicationEntityAddress);
        uint8 atState = currentApp.CurrentEntityState();
        lockCurrentApp();
        if(!currentApp.transferAssetsToNewApplication(_newAddress)) {
            revert();
        }
        link(_newAddress);
        currentApp.setUpgradeState( atState );
        return true;
    }
 
    /*
    * Locks current Application Entity
    */
    function lockCurrentApp() internal {
        if(!currentApp.lock()) {
            revert();
        }
    }
 
    /*
    * Link to new Application Entity
    *
    * @param        address _newAddress
    */
    function link( address _newAddress ) internal returns (bool) {
 
        currentApplicationEntityAddress = _newAddress;
        currentApp = ApplicationEntity(currentApplicationEntityAddress);
        Iif( !currentApp.initialize() ) {
            revert();
        }
        EventGatewayNewAddress(currentApplicationEntityAddress);
        return true;
    }
 
    /*
    * Get current News Contract address
    *
    * @return       address NewsContractEntity
    */
    function getNewsContractAddress() external view returns (address) {
        return currentApp.NewsContractEntity();
    }
 
    /*
    * Get current Listing Contract address
    *
    * @return       address ListingContractEntity
    */
    function getListingContractAddress() external view returns (address) {
        return currentApp.ListingContractEntity();
    }
 
    /*
    * Validates if new application's deployer is allowed to upgrade current app
    */
    modifier validCodeUpgradeInitiator() {
        bool valid = false;
 
        ApplicationEntity newDeployedApp = ApplicationEntity(msg.sender);
        address newDeployer = newDeployedApp.deployerAddress();
 
        if(newDeployer == deployerAddress) {
            valid = true;
        } else {
            Eif(currentApplicationEntityAddress != address(0x0)) {
                currentApp = ApplicationEntity(currentApplicationEntityAddress);
                if(currentApp.canInitiateCodeUpgrade(newDeployer)) {
                    valid = true;
                }
            }
        }
 
        // ok if current app accepts newDeployer as a token holder that can do a code upgrade
        // ok if newDeployer is oldDeployer
        require( valid == true );
        _;
    }
}