all files / contracts/Entity/ Milestones.sol

98.95% Statements 189/191
91.96% Branches 103/112
100% Functions 29/29
98.88% Lines 176/178
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583                                                                                                          113× 113× 113×   113×   113× 113× 113×   113×   113× 113× 113×   113× 113×   113× 113×     113× 113× 113× 113×       113× 113× 113× 113×         111× 111× 111× 111×       111×       239×       24×                                                   556×   556× 556× 556× 556×             556× 556×       340× 340×         404× 404×   404× 404×   404×   69× 69× 69×     404×   404× 404× 404×             1367×         509×   858×                         1572× 1572×   1572×   509×   274× 207×         207×         509×       1572×         1092× 1092× 1092×   1092× 69×   1092× 528×     1092×             69× 69×         404×       404×   404×           48×       71×                 55× 55×               71× 71×           206× 206× 206×   199×           18×   18× 12×         359×       359×         48× 48×         263×       52× 51× 50×   50×   50× 50× 50×                 55×     55× 55×   55×   41×   41× 41× 41×                                                 69× 69× 69×                                 69× 69×                               70×   70× 70×   70× 69×                           1572×   1572× 1572× 1572×   1572×       1367×   151×   84×         207×       237×   100×       137× 135×       137×   14×                     148× 102×         263×   263× 126×     263× 18×         168× 84×           18× 12×         24× 12×           151× 96×     55× 28× 14×   14×     27×                         205× 87×       1572×       3144× 2734×   410×       188× 188× 188× 15×   173×      
/*
 
 * @name        Milestones Contract
 * @package     BlockBitsIO
 * @author      Micky Socaci <micky@nowlive.ro>
 
 Contains the Milestones Contract code deployed and linked to the Application Entity
 
*/
 
pragma solidity ^0.4.17;
 
import "./../ApplicationAsset.sol";
import "./FundingManager.sol";
import "./Meetings.sol";
import "./Proposals.sol";
 
 
contract Milestones is ApplicationAsset {
 
    FundingManager FundingManagerEntity;
    Proposals ProposalsEntity;
    Meetings MeetingsEntity;
 
    struct Record {
        bytes32 name;
        string description;                     // will change to hash pointer ( external storage )
        uint8 state;
        uint256 duration;
        uint256 time_start;                     // start at unixtimestamp
        uint256 last_state_change_time;         // time of last state change
        uint256 time_end;                       // estimated end time >> can be increased by proposal
        uint256 time_ended;                     // actual end time
        uint256 meeting_time;
        uint8 funding_percentage;
        uint8 index;
    }
 
    mapping (uint8 => Record) public Collection;
    uint8 public currentRecord = 1;
 
    event DebugRecordRequiredChanges( bytes32 indexed _assetName, uint8 indexed _current, uint8 indexed _required );
    event DebugCallAgain(uint8 indexed _who);
 
    event EventEntityProcessor(bytes32 indexed _assetName, uint8 indexed _current, uint8 indexed _required);
    event EventRecordProcessor(bytes32 indexed _assetName, uint8 indexed _current, uint8 indexed _required);
 
    event DebugAction(bytes32 indexed _name, bool indexed _allowed);
 
 
    function setAssetStates() internal {
 
        // Contract States
        EntityStates["__IGNORED__"]                  = 0;
        EntityStates["NEW"]                          = 1;
        EntityStates["WAITING"]                      = 2;
 
        EntityStates["IN_DEVELOPMENT"]               = 5;
 
        EntityStates["WAITING_MEETING_TIME"]         = 10;
        EntityStates["DEADLINE_MEETING_TIME_YES"]    = 11;
        EntityStates["DEADLINE_MEETING_TIME_FAILED"] = 12;
 
        EntityStates["VOTING_IN_PROGRESS"]           = 20;
        // EntityStates["VOTING_ENDED"]              = 21;
        EntityStates["VOTING_ENDED_YES"]             = 22;
        EntityStates["VOTING_ENDED_NO"]              = 23;
        EntityStates["VOTING_ENDED_NO_FINAL"]        = 25;
 
        EntityStates["VOTING_FUNDS_PROCESSED"]       = 30;
        EntityStates["FINAL"]                        = 50;
 
        EntityStates["CASHBACK_OWNER_MIA"]           = 99;
        EntityStates["DEVELOPMENT_COMPLETE"]         = 250;
 
        // Funding Stage States
        RecordStates["__IGNORED__"]     = 0;
        RecordStates["NEW"]             = 1;
        RecordStates["IN_PROGRESS"]     = 2;
        RecordStates["FINAL"]           = 3;
    }
 
    function runBeforeInitialization() internal requireNotInitialised {
        FundingManagerEntity = FundingManager( getApplicationAssetAddressByName('FundingManager') );
        MeetingsEntity = Meetings( getApplicationAssetAddressByName('Meetings') );
        ProposalsEntity = Proposals( getApplicationAssetAddressByName('Proposals') );
        EventRunBeforeInit(assetName);
    }
 
    function runBeforeApplyingSettings() internal requireInitialised requireSettingsNotApplied  {
        // setup first milestone
        Record storage rec = Collection[currentRecord];
            rec.time_start = getBylawsProjectDevelopmentStart();
            rec.time_end = rec.time_start + rec.duration;
        EventRunBeforeApplyingSettings(assetName);
    }
 
    function getBylawsProjectDevelopmentStart() public view returns (uint256) {
        return getAppBylawUint256("development_start");
    }
 
    function getBylawsMinTimeInTheFutureForMeetingCreation() public view returns (uint256) {
        return getAppBylawUint256("meeting_time_set_req");
    }
 
    function getBylawsCashBackVoteRejectedDuration() public view returns (uint256) {
        return getAppBylawUint256("cashback_investor_no");
    }
 
    /*
    * Add Record
    *
    * @param        bytes32 _name
    * @param        string _description
    * @param        uint256 _duration
    * @param        uint256 _funding_percentage
    *
    * @access       public
    * @type         method
    * @modifiers    onlyDeployer, requireNotInitialised
    */
    function addRecord(
        bytes32 _name,
        string _description,
        uint256 _duration,
        uint8   _funding_percentage
    )
        public
        onlyDeployer
        requireSettingsNotApplied
    {
 
        Record storage rec = Collection[++RecordNum];
 
        rec.name                = _name;
        rec.description         = _description;
        rec.duration            = _duration;
        rec.state               = getRecordState("NEW");
 
        // save some gas
        // rec.time_start          = 0;
        // rec.time_end            = 0;
        // rec.time_ended          = 0;
        // rec.meeting_time        = 0;
        rec.funding_percentage  = _funding_percentage;
        rec.index               = RecordNum;
    }
 
    function getMilestoneFundingPercentage(uint8 recordId) public view returns (uint8){
        Record memory rec = Collection[recordId];
        return rec.funding_percentage;
    }
 
    function doStateChanges() public {
 
        var (CurrentRecordState, RecordStateRequired, EntityStateRequired) = getRequiredStateChanges();
        bool callAgain = false;
 
        DebugRecordRequiredChanges( assetName, CurrentRecordState, RecordStateRequired );
        DebugEntityRequiredChanges( assetName, CurrentEntityState, EntityStateRequired );
 
        if( RecordStateRequired != getRecordState("__IGNORED__") ) {
            // process record changes.
            RecordProcessor(CurrentRecordState, RecordStateRequired);
            DebugCallAgain(2);
            callAgain = true;
        }
 
        Eif(EntityStateRequired != getEntityState("__IGNORED__") ) {
            // process entity changes.
            EntityProcessor(EntityStateRequired);
            DebugCallAgain(1);
            callAgain = true;
        }
 
 
    }
 
    function MilestonesCanChange() internal view returns (bool) {
        if(
            CurrentEntityState == getEntityState("WAITING") ||
            CurrentEntityState == getEntityState("IN_DEVELOPMENT") ||
            CurrentEntityState == getEntityState("VOTING_FUNDS_PROCESSED")
        ) {
            return true;
        }
        return false;
    }
 
 
    /*
     * Method: Get Record Required State Changes
     *
     * @access       public
     * @type         method
     *
     * @return       uint8 RecordStateRequired
     */
    function getRecordStateRequiredChanges() public view returns (uint8) {
        Record memory record = Collection[currentRecord];
        uint8 RecordStateRequired = getRecordState("__IGNORED__");
 
        if( ApplicationIsInDevelopment() && MilestonesCanChange() ) {
 
            if( record.state == getRecordState("NEW") ) {
 
                if( getTimestamp() >= record.time_start ) {
                    RecordStateRequired = getRecordState("IN_PROGRESS");
                }
 
            } else if( record.state == getRecordState("IN_PROGRESS") ) {
 
                Iif( getTimestamp() >= record.time_end || ( getTimestamp() >= record.meeting_time && record.meeting_time > 0 ) ) {
                    RecordStateRequired = getRecordState("FINAL");
                }
            }
 
            Iif( record.state == RecordStateRequired ) {
                RecordStateRequired = getRecordState("__IGNORED__");
            }
        }
        return RecordStateRequired;
    }
 
 
    function hasRequiredStateChanges() public view returns (bool) {
        bool hasChanges = false;
        var (CurrentRecordState, RecordStateRequired, EntityStateRequired) = getRequiredStateChanges();
        CurrentRecordState = 0;
 
        if( RecordStateRequired != getRecordState("__IGNORED__") ) {
            hasChanges = true;
        }
        if(EntityStateRequired != getEntityState("__IGNORED__") ) {
            hasChanges = true;
        }
 
        return hasChanges;
    }
 
    // view methods decide if changes are to be made
    // in case of tasks, we do them in the Processors.
 
    function RecordProcessor(uint8 CurrentRecordState, uint8 RecordStateRequired) internal {
        EventRecordProcessor( assetName, CurrentRecordState, RecordStateRequired );
        updateRecord( RecordStateRequired );
    }
 
 
    function EntityProcessor(uint8 EntityStateRequired) internal {
        EventEntityProcessor( assetName, CurrentEntityState, EntityStateRequired );
 
        // Do State Specific Updates
        // Update our Entity State
        CurrentEntityState = EntityStateRequired;
 
        if ( CurrentEntityState == getEntityState("DEADLINE_MEETING_TIME_YES") ) {
            // create meeting
            // Meetings.create("internal", "MILESTONE_END", "");
 
        } else if( CurrentEntityState == getEntityState("VOTING_IN_PROGRESS") ) {
            // create proposal and start voting on it
            createMilestoneAcceptanceProposal();
 
        } else if( CurrentEntityState == getEntityState("WAITING_MEETING_TIME") ) {
 
            PostponeMeetingIfApproved();
 
        } else if( CurrentEntityState == getEntityState("VOTING_ENDED_YES") ) {
 
        } else if( CurrentEntityState == getEntityState("VOTING_ENDED_NO") ) {
 
            // possible cashback time starts from now
            MilestoneCashBackTime = getTimestamp();
 
        } else if( CurrentEntityState == getEntityState("VOTING_FUNDS_PROCESSED") ) {
            MilestoneCashBackTime = 0;
            startNextMilestone();
        }
 
    }
 
    mapping (bytes32 => bool) public MilestonePostponingHash;
 
    function PostponeMeetingIfApproved() internal {
        Eif(MilestonePostponingHash[ bytes32(currentRecord) ] == false ) {
            if(PostponeForCurrentMilestoneIsApproved()) {
                uint256 time = ProposalsEntity.getCurrentMilestonePostponingProposalDuration();
                Record storage record = Collection[currentRecord];
                record.time_end = record.time_end + time;
                MilestonePostponingHash[ bytes32(currentRecord) ] = true;
            }
        }
    }
 
    function PostponeForCurrentMilestoneIsApproved() internal view returns ( bool ) {
        uint8 ProposalActionType = ProposalsEntity.getActionType("MILESTONE_POSTPONING");
        uint8 ProposalRecordState = ProposalsEntity.getCurrentMilestoneProposalStatusForType( ProposalActionType  );
        if(ProposalRecordState == ProposalsEntity.getRecordState("VOTING_RESULT_YES") ) {
            return true;
        }
        return false;
    }
 
    uint256 public MilestoneCashBackTime = 0;
 
    function afterVoteNoCashBackTime() public view returns ( bool ) {
        uint256 time =  MilestoneCashBackTime + getBylawsCashBackVoteRejectedDuration();
        // after cash back time
        if(getTimestamp() > time) {
            return true;
        }
        return false;
    }
 
    function getHash(uint8 actionType, bytes32 arg1, bytes32 arg2) public pure returns ( bytes32 ) {
        return keccak256(actionType, arg1, arg2);
    }
 
    function getCurrentHash() public view returns ( bytes32 ) {
        return getHash(1, bytes32(currentRecord), 0);
    }
 
    mapping (bytes32 => uint256) public ProposalIdByHash;
    function createMilestoneAcceptanceProposal() internal {
        Eif(ProposalIdByHash[ getCurrentHash() ] == 0x0 ) {
            ProposalIdByHash[ getCurrentHash() ] = ProposalsEntity.createMilestoneAcceptanceProposal();
        }
    }
 
    function getCurrentProposalId() internal view returns ( uint256 ) {
        return ProposalIdByHash[ getCurrentHash() ];
    }
 
    function setCurrentMilestoneMeetingTime(uint256 _meeting_time) public onlyDeployer {
        if ( CurrentEntityState == getEntityState("WAITING_MEETING_TIME") ) {
            if(MeetingTimeSetFailure() == false ) {
                Record storage record = Collection[currentRecord];
                // minimum x days into the future
                uint256 min = getTimestamp() + getBylawsMinTimeInTheFutureForMeetingCreation();
                // minimum days before end date
                uint256 max = record.time_end + 24 * 3600;
                Eif(_meeting_time > min && _meeting_time < max ) {
                    record.meeting_time = _meeting_time;
                }
            } else {
                revert();
            }
        } else {
            revert();
        }
    }
 
    function startNextMilestone() internal {
        Record storage rec = Collection[currentRecord];
 
        // set current record end date etc
        rec.time_ended = getTimestamp();
        rec.state = getRecordState("FINAL");
 
        if(currentRecord < RecordNum) {
            // jump to next milestone
            currentRecord++;
 
            Record storage nextRec = Collection[currentRecord];
                nextRec.time_start = rec.time_ended;
                nextRec.time_end = rec.time_ended + nextRec.duration;
        }
 
    }
 
    /*
    * Update Existing Record
    *
    * @param        uint8 _record_id
    * @param        uint8 _new_state
    * @param        uint8 _duration
    *
    * @access       public
    * @type         method
    * @modifiers    onlyOwner, requireInitialised, RecordUpdateAllowed
    *
    * @return       void
    */
 
    function updateRecord( uint8 _new_state )
        internal
        requireInitialised
        RecordUpdateAllowed(_new_state)
        returns (bool)
    {
        Record storage rec = Collection[currentRecord];
        rec.state       = _new_state;
        return true;
    }
 
 
    /*
    * Modifier: Validate if record updates are allowed
    *
    * @type         modifier
    *
    * @param        uint8 _record_id
    * @param        uint8 _new_state
    * @param        uint256 _duration
    *
    * @return       bool
    */
 
    modifier RecordUpdateAllowed(uint8 _new_state) {
        Erequire( isRecordUpdateAllowed( _new_state )  );
        _;
    }
 
    /*
     * Method: Validate if record can be updated to requested state
     *
     * @access       public
     * @type         method
     *
     * @param        uint8 _record_id
     * @param        uint8 _new_state
     *
     * @return       bool
     */
    function isRecordUpdateAllowed(uint8 _new_state ) public view returns (bool) {
 
        var (CurrentRecordState, RecordStateRequired, EntityStateRequired) = getRequiredStateChanges();
 
        CurrentRecordState = 0;
        EntityStateRequired = 0;
 
        if(_new_state == uint8(RecordStateRequired)) {
            return true;
        }
        return false;
    }
 
    /*
     * Method: Get Record and Entity State Changes
     *
     * @access       public
     * @type         method
     *
     * @return       ( uint8 CurrentRecordState, uint8 RecordStateRequired, uint8 EntityStateRequired)
     */
    function getRequiredStateChanges() public view returns (uint8, uint8, uint8) {
 
        Record memory record = Collection[currentRecord];
 
        uint8 CurrentRecordState = record.state;
        uint8 RecordStateRequired = getRecordStateRequiredChanges();
        uint8 EntityStateRequired = getEntityState("__IGNORED__");
 
        if( ApplicationIsInDevelopment() ) {
 
            // Do Entity Checks
 
            if ( CurrentEntityState == getEntityState("WAITING") ) {
 
                if(RecordStateRequired == getRecordState("IN_PROGRESS") ) {
                    // both record and entity states need to move to IN_PROGRESS
                    EntityStateRequired = getEntityState("IN_DEVELOPMENT");
                }
 
            } else if ( CurrentEntityState == getEntityState("IN_DEVELOPMENT") ) {
 
                EntityStateRequired = getEntityState("WAITING_MEETING_TIME");
 
            } else if ( CurrentEntityState == getEntityState("WAITING_MEETING_TIME") ) {
 
                if(record.meeting_time > 0) {
 
                    EntityStateRequired = getEntityState("DEADLINE_MEETING_TIME_YES");
 
                } else {
 
                    if(MilestonePostponingHash[ bytes32(currentRecord) ] == false) {
                        if(PostponeForCurrentMilestoneIsApproved()) {
                            EntityStateRequired = getEntityState("WAITING_MEETING_TIME");
                        }
                    }
 
                    if(MeetingTimeSetFailure()) {
                        // Force Owner Missing in Action - Cash Back Procedure
                        EntityStateRequired = getEntityState("DEADLINE_MEETING_TIME_FAILED");
                    }
                }
 
            } else Iif ( CurrentEntityState == getEntityState("DEADLINE_MEETING_TIME_FAILED") ) {
 
 
            } else if ( CurrentEntityState == getEntityState("DEADLINE_MEETING_TIME_YES") ) {
 
                // create proposal
                // start voting if time passed
                if(getTimestamp() >= record.meeting_time ) {
                    EntityStateRequired = getEntityState("VOTING_IN_PROGRESS");
                }
 
            } else if ( CurrentEntityState == getEntityState("VOTING_IN_PROGRESS") ) {
 
                uint8 ProposalRecordState = ProposalsEntity.getProposalState( getCurrentProposalId() );
 
                if ( ProposalRecordState == ProposalsEntity.getRecordState("VOTING_RESULT_YES") ) {
                    EntityStateRequired = getEntityState("VOTING_ENDED_YES");
                }
 
                if (ProposalRecordState == ProposalsEntity.getRecordState("VOTING_RESULT_NO") ) {
                    EntityStateRequired = getEntityState("VOTING_ENDED_NO");
                }
 
            } else if ( CurrentEntityState == getEntityState("VOTING_ENDED_YES") ) {
 
                if( FundingManagerEntity.CurrentEntityState() == FundingManagerEntity.getEntityState("MILESTONE_PROCESS_DONE")) {
                    EntityStateRequired = getEntityState("VOTING_FUNDS_PROCESSED");
                }
 
            } else if ( CurrentEntityState == getEntityState("VOTING_ENDED_NO") ) {
 
                // check if milestone cashout period has passed and if so process fund releases
                if(afterVoteNoCashBackTime()) {
                    EntityStateRequired = getEntityState("VOTING_ENDED_NO_FINAL");
                }
 
            } else if ( CurrentEntityState == getEntityState("VOTING_ENDED_NO_FINAL") ) {
 
                if( FundingManagerEntity.CurrentEntityState() == FundingManagerEntity.getEntityState("MILESTONE_PROCESS_DONE")) {
                    EntityStateRequired = getEntityState("VOTING_FUNDS_PROCESSED");
                }
 
            } else Eif ( CurrentEntityState == getEntityState("VOTING_FUNDS_PROCESSED") ) {
 
 
                if(currentRecord < RecordNum) {
                    EntityStateRequired = getEntityState("IN_DEVELOPMENT");
                } else {
 
                    if(FundingManagerEntity.getCurrentMilestoneProcessed() == true) {
                        if(FundingManagerEntity.CurrentEntityState() == FundingManagerEntity.getEntityState("COMPLETE_PROCESS_DONE")) {
                            EntityStateRequired = getEntityState("DEVELOPMENT_COMPLETE");
                        } else {
                            EntityStateRequired = getEntityState("VOTING_FUNDS_PROCESSED");
                        }
                    } else {
                        EntityStateRequired = getEntityState("IN_DEVELOPMENT");
                    }
                }
 
            }
            /*
            else if ( CurrentEntityState == getEntityState("DEVELOPMENT_COMPLETE") ) {
 
            }
            */
 
        } else {
 
            if( CurrentEntityState == getEntityState("NEW") ) {
                EntityStateRequired = getEntityState("WAITING");
            }
        }
 
        return (CurrentRecordState, RecordStateRequired, EntityStateRequired);
    }
 
    function ApplicationIsInDevelopment() public view returns(bool) {
        if( getApplicationState() == getApplicationEntityState("IN_DEVELOPMENT") ) {
            return true;
        }
        return false;
    }
 
    function MeetingTimeSetFailure() public view returns (bool) {
        Record memory record = Collection[currentRecord];
        uint256 meetingCreationMaxTime = record.time_end - getBylawsMinTimeInTheFutureForMeetingCreation();
        if(getTimestamp() >= meetingCreationMaxTime ) {
            return true;
        }
        return false;
    }
 
}