all files / contracts/Entity/ Proposals.sol

97.69% Statements 254/260
90% Branches 99/110
100% Functions 47/47
97.66% Lines 250/256
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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838                                                              1907×       1549×       264×             120× 120× 120× 120×     120× 120×           120×   120× 120× 120× 120× 120×                   113× 113×   113× 113×   113× 113× 113×   113× 113×   113× 113×       74×                   330×                                                       58×   68×           60×             936× 378× 59×     877×                                     17×   17×         17×                                   50× 50×                                                                   206× 206× 206× 206× 206× 206×                                                                                                                                                   78×   74× 74× 74× 74× 74× 74× 74× 74× 74× 74× 74× 74×   74×           74× 74× 74×                                 74× 74×   74×   65×             61×     61×                   74× 74×   74×                                                                       117×     117×     117×           117×       106×     106×     106×     11×         133× 133× 25×   108×                                           106× 106× 106× 106× 106× 106× 106×   106×   106×             127×   127× 127× 127× 93×   34×               137× 137×   137×           137× 137× 135×   135×   29×       137×                                                                       23×   23× 21×     23×                   174× 174× 139× 110×         35×         58×       129×       552× 552× 104×   448×       378× 52×     326× 326×           319×           174× 116×     174× 52×           116× 116×         116×   19×   15×           116×         52× 52×     52×     47×   41×           52×         74× 74×   74×         74×         73× 73× 88× 73×   88× 76×       73×               176×   176×   69×                                 69×         20×                   17× 17× 17×   17× 17×         27× 27× 27× 27×            
/*
 
 * @name        Proposals Contract
 * @package     BlockBitsIO
 * @author      Micky Socaci <micky@nowlive.ro>
 
 Contains the Proposals Contract code deployed and linked to the Application Entity
 
*/
 
pragma solidity ^0.4.17;
 
import "./Token.sol";
import "./TokenManager.sol";
 
import "./../ApplicationAsset.sol";
import "./../ApplicationEntityABI.sol";
import "./ListingContract.sol";
import "./FundingVault.sol";
 
contract Proposals is ApplicationAsset {
 
    ApplicationEntityABI public Application;
    ListingContract public ListingContractEntity;
    Funding public FundingEntity;
    FundingManager public FundingManagerEntity;
    TokenManager public TokenManagerEntity;
    Token public TokenEntity;
    Milestones public MilestonesEntity;
 
    function getRecordState(bytes32 name) public view returns (uint8) {
        return RecordStates[name];
    }
 
    function getActionType(bytes32 name) public view returns (uint8) {
        return ActionTypes[name];
    }
 
    function getProposalState(uint256 _proposalId) public view returns (uint8) {
        return ProposalsById[_proposalId].state;
    }
 
    mapping (bytes32 => uint8) public ActionTypes;
 
    function setActionTypes() internal {
        // owner initiated
        ActionTypes["MILESTONE_DEADLINE"] = 1;
        ActionTypes["MILESTONE_POSTPONING"] = 2;
        ActionTypes["EMERGENCY_FUND_RELEASE"] = 60;
        ActionTypes["IN_DEVELOPMENT_CODE_UPGRADE"] = 50;
 
        // shareholder initiated
        ActionTypes["AFTER_COMPLETE_CODE_UPGRADE"] = 51;
        ActionTypes["PROJECT_DELISTING"] = 75;
    }
 
 
    function setAssetStates() internal {
 
        setActionTypes();
 
        RecordStates["NEW"]                 = 1;
        RecordStates["ACCEPTING_VOTES"]     = 2;
        RecordStates["VOTING_ENDED"]        = 3;
        RecordStates["VOTING_RESULT_YES"]   = 10;
        RecordStates["VOTING_RESULT_NO"]    = 20;
    }
 
    event EventNewProposalCreated ( bytes32 indexed _hash, uint256 indexed _proposalId );
 
    function runBeforeApplyingSettings()
        internal
        requireInitialised
        requireSettingsNotApplied
    {
        address FundingAddress = getApplicationAssetAddressByName('Funding');
        FundingEntity = Funding(FundingAddress);
 
        address FundingManagerAddress = getApplicationAssetAddressByName('FundingManager');
        FundingManagerEntity = FundingManager(FundingManagerAddress);
 
        address TokenManagerAddress = getApplicationAssetAddressByName('TokenManager');
        TokenManagerEntity = TokenManager(TokenManagerAddress);
        TokenEntity = Token(TokenManagerEntity.TokenEntity());
 
        address ListingContractAddress = getApplicationAssetAddressByName('ListingContract');
        ListingContractEntity = ListingContract(ListingContractAddress);
 
        address MilestonesContractAddress = getApplicationAssetAddressByName('Milestones');
        MilestonesEntity = Milestones(MilestonesContractAddress);
    }
 
    function getBylawsProposalVotingDuration() public view returns (uint256) {
        return getAppBylawUint256("proposal_voting_duration");
    }
 
    function getBylawsMilestoneMinPostponing() public view returns (uint256) {
        return getAppBylawUint256("min_postponing");
    }
 
    function getBylawsMilestoneMaxPostponing() public view returns (uint256) {
        return getAppBylawUint256("max_postponing");
    }
 
    function getHash(uint8 actionType, bytes32 arg1, bytes32 arg2) public pure returns ( bytes32 ) {
        return keccak256(actionType, arg1, arg2);
    }
 
 
    // need to implement a way to just iterate through active proposals, and remove the ones we already processed
    // otherwise someone with malicious intent could add a ton of proposals, just to make our contract cost a ton of gas.
 
    // to that end, we allow individual proposal processing. so that we don't get affected by people with too much
    // money and time on their hands.
 
    // whenever the system created a proposal, it will store the id, and process it when required.
 
    // not that much of an issue at this stage because:
    // NOW:
    // - only the system can create - MILESTONE_DEADLINE
    // - only the deployer can create - MILESTONE_POSTPONING / EMERGENCY_FUND_RELEASE / IN_DEVELOPMENT_CODE_UPGRADE
 
    // FUTURE:
    // - PROJECT_DELISTING is tied into an existing "listing id" which will be created by the system ( if requested by
    // someone, but at quite a significant cost )
    // - AFTER_COMPLETE_CODE_UPGRADE
 
    mapping (uint8 => uint256) public ActiveProposalIds;
    uint8 public ActiveProposalNum = 0;
 
    mapping (uint256 => bool) public ExpiredProposalIds;
 
    function process() public onlyApplicationEntity {
        for(uint8 i = 0; i < ActiveProposalNum; i++) {
 
            if(
                getProposalType(ActiveProposalIds[i]) == getActionType("PROJECT_DELISTING") ||
                getProposalType(ActiveProposalIds[i]) == getActionType("AFTER_COMPLETE_CODE_UPGRADE")
            ) {
                ProcessVoteTotals( ActiveProposalIds[i], VoteCountPerProcess );
            } else {
                // try expiry ending
                tryEndVoting(ActiveProposalIds[i]);
            }
 
        }
    }
 
    function hasRequiredStateChanges() public view returns (bool) {
        for(uint8 i = 0; i < ActiveProposalNum; i++) {
            if( needsProcessing( ActiveProposalIds[i] ) ) {
                return true;
            }
        }
        return false;
    }
 
    function getRequiredStateChanges() public view returns (uint8) {
        Iif(hasRequiredStateChanges()) {
            return ActiveProposalNum;
        }
        return 0;
    }
 
    function addCodeUpgradeProposal(address _addr, bytes32 _sourceCodeUrl)
        external
        onlyApplicationEntity   // shareholder check is done directly in Gateway by calling applicationEntity to confirm
        returns (uint256)
    {
 
        // hash enforces only 1 possible voting of this type per record.
        // basically if a vote failed, you need to deploy it with changes to a new address. that simple.
 
        // depending on the application overall state, we have 2 different voting implementations.
 
        uint8 thisAction;
 
        if(getApplicationState() == getApplicationEntityState("IN_DEVELOPMENT") ) {
            thisAction = getActionType("IN_DEVELOPMENT_CODE_UPGRADE");
 
        } else if(getApplicationState() == getApplicationEntityState("DEVELOPMENT_COMPLETE") ) {
            thisAction = getActionType("AFTER_COMPLETE_CODE_UPGRADE");
        }
 
        return createProposal(
            msg.sender,
            "CODE_UPGRADE",
            getHash( thisAction, bytes32(_addr), 0 ),
            thisAction,
            _addr,
            _sourceCodeUrl,
            0
        );
    }
 
 
    function createMilestoneAcceptanceProposal()
        external
        onlyAsset("Milestones")
        returns (uint256)
    {
 
        uint8 recordId = MilestonesEntity.currentRecord();
        return createProposal(
            msg.sender,
            "MILESTONE_DEADLINE",
            getHash( getActionType("MILESTONE_DEADLINE"), bytes32( recordId ), 0 ),
            getActionType("MILESTONE_DEADLINE"),
            0,
            0,
            uint256(recordId)
        );
    }
 
    function createMilestonePostponingProposal(uint256 _duration)
        external
        onlyDeployer
        returns (uint256)
    {
        if(_duration >= getBylawsMilestoneMinPostponing() && _duration <= getBylawsMilestoneMaxPostponing() ) {
 
            uint8 recordId = MilestonesEntity.currentRecord();
            return createProposal(
                msg.sender,
                "MILESTONE_POSTPONING",
                getHash( getActionType("MILESTONE_POSTPONING"), bytes32( recordId ), 0 ),
                getActionType("MILESTONE_POSTPONING"),
                0,
                0,
                _duration
            );
        } else {
            revert();
        }
    }
 
    function getCurrentMilestonePostponingProposalDuration() public view returns (uint256) {
        uint8 recordId = MilestonesEntity.currentRecord();
        bytes32 hash = getHash( getActionType("MILESTONE_POSTPONING"), bytes32( recordId ), 0 );
        ProposalRecord memory proposal = ProposalsById[ ProposalIdByHash[hash] ];
        return proposal.extra;
    }
 
    function getCurrentMilestoneProposalStatusForType(uint8 _actionType ) public view returns (uint8) {
 
        Eif(_actionType == getActionType("MILESTONE_DEADLINE") || _actionType == getActionType("MILESTONE_POSTPONING")) {
            uint8 recordId = MilestonesEntity.currentRecord();
            bytes32 hash = getHash( _actionType, bytes32( recordId ), 0 );
            uint256 ProposalId = ProposalIdByHash[hash];
            ProposalRecord memory proposal = ProposalsById[ProposalId];
            return proposal.state;
        }
        return 0;
    }
 
    function createEmergencyFundReleaseProposal()
        external
        onlyDeployer
        returns (uint256)
    {
        return createProposal(
            msg.sender,
            "EMERGENCY_FUND_RELEASE",
            getHash( getActionType("EMERGENCY_FUND_RELEASE"), 0, 0 ),
            getActionType("EMERGENCY_FUND_RELEASE"),
            0,
            0,
            0
        );
    }
 
    function createDelistingProposal(uint256 _projectId)
        external
        onlyTokenHolder
        returns (uint256)
    {
        // let's validate the project is actually listed first in order to remove any spamming ability.
        if( ListingContractEntity.canBeDelisted(_projectId) == true) {
 
            return createProposal(
                msg.sender,
                "PROJECT_DELISTING",
                getHash( getActionType("PROJECT_DELISTING"), bytes32(_projectId), 0 ),
                getActionType("PROJECT_DELISTING"),
                0,
                0,
                _projectId
            );
        } else {
            revert();
        }
    }
 
    modifier onlyTokenHolder() {
        require( getTotalTokenVotingPower(msg.sender) > 0 );
        _;
    }
 
    struct ProposalRecord {
        address creator;
        bytes32 name;
        uint8 actionType;
        uint8 state;
        bytes32 hash;                       // action name + args hash
        address addr;
        bytes32 sourceCodeUrl;
        uint256 extra;
        uint256 time_start;
        uint256 time_end;
        uint256 index;
    }
 
    mapping (uint256 => ProposalRecord) public ProposalsById;
    mapping (bytes32 => uint256) public ProposalIdByHash;
 
    function createProposal(
        address _creator,
        bytes32 _name,
        bytes32 _hash,
        uint8   _action,
        address _addr,
        bytes32 _sourceCodeUrl,
        uint256 _extra
    )
        internal
        returns (uint256)
    {
 
        // if(_action > 0) {
 
        if(ProposalIdByHash[_hash] == 0) {
 
            ProposalRecord storage proposal = ProposalsById[++RecordNum];
            proposal.creator        = _creator;
            proposal.name           = _name;
            proposal.actionType     = _action;
            proposal.addr           = _addr;
            proposal.sourceCodeUrl  = _sourceCodeUrl;
            proposal.extra          = _extra;
            proposal.hash           = _hash;
            proposal.state          = getRecordState("NEW");
            proposal.time_start     = getTimestamp();
            proposal.time_end       = getTimestamp() + getBylawsProposalVotingDuration();
            proposal.index          = RecordNum;
 
            ProposalIdByHash[_hash] = RecordNum;
 
        } else {
            // already exists!
            revert();
        }
 
        initProposalVoting(RecordNum);
        EventNewProposalCreated ( _hash, RecordNum );
        return RecordNum;
 
        /*
        } else {
            // no action?!
            revert();
        }
        */
    }
 
    function acceptCodeUpgrade(uint256 _proposalId) internal {
        ProposalRecord storage proposal = ProposalsById[_proposalId];
        // reinitialize this each time, because we rely on "owner" as the address, and it will change
        Application = ApplicationEntityABI(owner);
        Application.acceptCodeUpgradeProposal(proposal.addr);
    }
 
 
    function initProposalVoting(uint256 _proposalId) internal {
 
        ResultRecord storage result = ResultsByProposalId[_proposalId];
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        if(getApplicationState() == getApplicationEntityState("IN_DEVELOPMENT") ) {
 
            if(proposal.actionType == getActionType("PROJECT_DELISTING") ) {
                // while in development project delisting can be voted by all available tokens, except owner
                uint256 ownerLockedTokens = TokenEntity.balanceOf(TokenManagerEntity);
                result.totalAvailable = TokenEntity.totalSupply() - ownerLockedTokens;
 
                // since we're counting unlocked tokens, we need to recount votes each time we want to end the voting period
                result.requiresCounting = true;
 
            } else {
                // any other proposal is only voted by "locked ether", thus we use locked tokens
                result.totalAvailable = FundingManagerEntity.LockedVotingTokens();
 
                // locked tokens do not require recounting.
                result.requiresCounting = false;
            }
 
        } else if(getApplicationState() == getApplicationEntityState("DEVELOPMENT_COMPLETE") ) {
            // remove residual token balance from TokenManagerEntity.
            uint256 residualLockedTokens = TokenEntity.balanceOf(TokenManagerEntity);
            result.totalAvailable = TokenEntity.totalSupply() - residualLockedTokens;
 
            // since we're counting unlocked tokens, we need to recount votes each time we want to end the voting period
            result.requiresCounting = true;
        }
        result.requiredForResult = result.totalAvailable / 2;   // 50%
 
        proposal.state = getRecordState("ACCEPTING_VOTES");
        addActiveProposal(_proposalId);
 
        tryFinaliseNonLockedTokensProposal(_proposalId);
    }
 
 
 
    /*
 
    Voting
 
    */
 
    struct VoteStruct {
        address voter;
        uint256 time;
        bool    vote;
        uint256 power;
        bool    annulled;
        uint256 index;
    }
 
    struct ResultRecord {
        uint256 totalAvailable;
        uint256 requiredForResult;
        uint256 totalSoFar;
        uint256 yes;
        uint256 no;
        bool    requiresCounting;
    }
 
 
    mapping (uint256 => mapping (uint256 => VoteStruct) ) public VotesByProposalId;
    mapping (uint256 => mapping (address => VoteStruct) ) public VotesByCaster;
    mapping (uint256 => uint256 ) public VotesNumByProposalId;
    mapping (uint256 => ResultRecord ) public ResultsByProposalId;
 
    function RegisterVote(uint256 _proposalId, bool _myVote) public {
        address Voter = msg.sender;
 
        // get voting power
        uint256 VoterPower = getVotingPower(_proposalId, Voter);
 
        // get proposal for state
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        // make sure voting power is greater than 0
        // make sure proposal.state allows receiving votes
        // make sure proposal.time_end has not passed.
 
        if(VoterPower > 0 && proposal.state == getRecordState("ACCEPTING_VOTES")) {
 
            // first check if this Voter has a record registered,
            // and if they did, annul initial vote, update results, and add new one
            if( hasPreviousVote(_proposalId, Voter) ) {
                undoPreviousVote(_proposalId, Voter);
            }
 
            registerNewVote(_proposalId, Voter, _myVote, VoterPower);
 
            // this is where we can end voting before time if result.yes or result.no > totalSoFar
            tryEndVoting(_proposalId);
 
        } else {
            revert();
        }
    }
 
    function hasPreviousVote(uint256 _proposalId, address _voter) public view returns (bool) {
        VoteStruct storage previousVoteByCaster = VotesByCaster[_proposalId][_voter];
        if( previousVoteByCaster.power > 0 ) {
            return true;
        }
        return false;
    }
 
    function undoPreviousVote(uint256 _proposalId, address _voter) internal {
 
        VoteStruct storage previousVoteByCaster = VotesByCaster[_proposalId][_voter];
 
        // if( previousVoteByCaster.power > 0 ) {
            previousVoteByCaster.annulled = true;
 
            VoteStruct storage previousVoteByProposalId = VotesByProposalId[_proposalId][previousVoteByCaster.index];
            previousVoteByProposalId.annulled = true;
 
            ResultRecord storage result = ResultsByProposalId[_proposalId];
 
            // update total so far as well
            result.totalSoFar-= previousVoteByProposalId.power;
 
            if(previousVoteByProposalId.vote == true) {
                result.yes-= previousVoteByProposalId.power;
            // } else if(previousVoteByProposalId.vote == false) {
            } else {
                result.no-= previousVoteByProposalId.power;
            }
        // }
 
    }
 
    function registerNewVote(uint256 _proposalId, address _voter, bool _myVote, uint256 _power) internal {
 
        // handle new vote
        uint256 currentVoteId = VotesNumByProposalId[_proposalId]++;
        VoteStruct storage vote = VotesByProposalId[_proposalId][currentVoteId];
            vote.voter = _voter;
            vote.time = getTimestamp();
            vote.vote = _myVote;
            vote.power = _power;
            vote.index = currentVoteId;
 
        VotesByCaster[_proposalId][_voter] = VotesByProposalId[_proposalId][currentVoteId];
 
        addVoteIntoResult(_proposalId, _myVote, _power );
    }
 
    event EventAddVoteIntoResult ( uint256 indexed _proposalId, bool indexed _type, uint256 indexed _power );
 
    function addVoteIntoResult(uint256 _proposalId, bool _type, uint256 _power ) internal {
 
        EventAddVoteIntoResult(_proposalId, _type, _power );
 
        ResultRecord storage newResult = ResultsByProposalId[_proposalId];
        newResult.totalSoFar+= _power;
        if(_type == true) {
            newResult.yes+= _power;
        } else {
            newResult.no+= _power;
        }
    }
 
    function getTotalTokenVotingPower(address _voter) public view returns ( uint256 ) {
        address VaultAddress = FundingManagerEntity.getMyVaultAddress(_voter);
        uint256 VotingPower = TokenEntity.balanceOf(VaultAddress);
        VotingPower+= TokenEntity.balanceOf(_voter);
        return VotingPower;
    }
 
    function getVotingPower(uint256 _proposalId, address _voter) public view returns ( uint256 ) {
        uint256 VotingPower = 0;
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        Iif(proposal.actionType == getActionType("AFTER_COMPLETE_CODE_UPGRADE")) {
 
            return TokenEntity.balanceOf(_voter);
 
        } else {
 
            address VaultAddress = FundingManagerEntity.getMyVaultAddress(_voter);
            if(VaultAddress != address(0x0)) {
                VotingPower = TokenEntity.balanceOf(VaultAddress);
 
                if( proposal.actionType == getActionType("PROJECT_DELISTING") ) {
                    // for project delisting, we want to also include tokens in the voter's wallet.
                    VotingPower+= TokenEntity.balanceOf(_voter);
                }
            }
        }
        return VotingPower;
    }
 
 
    mapping( uint256 => uint256 ) public lastProcessedVoteIdByProposal;
    mapping( uint256 => uint256 ) public ProcessedVotesByProposal;
    mapping( uint256 => uint256 ) public VoteCountAtProcessingStartByProposal;
    uint256 public VoteCountPerProcess = 10;
 
    function setVoteCountPerProcess(uint256 _perProcess) external onlyDeployer {
        if(_perProcess > 0) {
            VoteCountPerProcess = _perProcess;
        } else {
            revert();
        }
    }
 
    event EventProcessVoteTotals ( uint256 indexed _proposalId, uint256 indexed start, uint256 indexed end );
 
    function ProcessVoteTotals(uint256 _proposalId, uint256 length) public onlyApplicationEntity {
 
        uint256 start = lastProcessedVoteIdByProposal[_proposalId] + 1;
        uint256 end = start + length - 1;
        Eif(end > VotesNumByProposalId[_proposalId]) {
            end = VotesNumByProposalId[_proposalId];
        }
 
        EventProcessVoteTotals(_proposalId, start, end);
 
        // first run
        Eif(start == 1) {
            // save vote count at start, so we can reset if it changes
            VoteCountAtProcessingStartByProposal[_proposalId] = VotesNumByProposalId[_proposalId];
 
            // reset vote totals to 0
            ResultRecord storage result = ResultsByProposalId[_proposalId];
            result.yes = 0;
            result.no = 0;
            result.totalSoFar = 0;
        }
 
        // reset to start if vote count has changed in the middle of processing run
        Iif(VoteCountAtProcessingStartByProposal[_proposalId] != VotesNumByProposalId[_proposalId]) {
            // we received votes while counting
            // reset from start
            lastProcessedVoteIdByProposal[_proposalId] = 0;
            // exit
            return;
        }
 
        for(uint256 i = start; i <= end; i++) {
 
            VoteStruct storage vote = VotesByProposalId[_proposalId][i - 1];
            // process vote into totals.
            if(vote.annulled != true) {
                addVoteIntoResult(_proposalId, vote.vote, vote.power );
            }
 
            lastProcessedVoteIdByProposal[_proposalId]++;
        }
 
        // reset iterator so we can call it again.
        Eif(lastProcessedVoteIdByProposal[_proposalId] >= VotesNumByProposalId[_proposalId] ) {
 
            ProcessedVotesByProposal[_proposalId] = lastProcessedVoteIdByProposal[_proposalId];
            lastProcessedVoteIdByProposal[_proposalId] = 0;
            tryEndVoting(_proposalId);
        }
    }
 
    function canEndVoting(uint256 _proposalId) public view returns (bool) {
 
        ResultRecord memory result = ResultsByProposalId[_proposalId];
        if(result.requiresCounting == false) {
            if(result.yes > result.requiredForResult || result.no > result.requiredForResult) {
                return true;
            }
        }
        else {
 
            if(ProcessedVotesByProposal[_proposalId] == VotesNumByProposalId[_proposalId]) {
                if(result.yes > result.requiredForResult || result.no > result.requiredForResult) {
                    return true;
                }
            }
 
        }
        return false;
    }
 
    function getProposalType(uint256 _proposalId) public view returns (uint8) {
        return ProposalsById[_proposalId].actionType;
    }
 
    function expiryChangesState(uint256 _proposalId) public view returns (bool) {
        ProposalRecord memory proposal = ProposalsById[_proposalId];
        if( proposal.state == getRecordState("ACCEPTING_VOTES") && proposal.time_end < getTimestamp() ) {
            return true;
        }
        return false;
    }
 
    function needsProcessing(uint256 _proposalId) public view returns (bool) {
        if( expiryChangesState(_proposalId) ) {
            return true;
        }
 
        ResultRecord memory result = ResultsByProposalId[_proposalId];
        if(result.requiresCounting == true) {
            Eif( lastProcessedVoteIdByProposal[_proposalId] < VotesNumByProposalId[_proposalId] ) {
                Iif(ProcessedVotesByProposal[_proposalId] == VotesNumByProposalId[_proposalId]) {
                    return false;
                }
            }
 
        } else {
            return false;
        }
 
        return true;
    }
 
    function tryEndVoting(uint256 _proposalId) internal {
        if(canEndVoting(_proposalId)) {
            finaliseProposal(_proposalId);
        }
 
        if(expiryChangesState(_proposalId) ) {
            finaliseExpiredProposal(_proposalId);
        }
    }
 
    function finaliseProposal(uint256 _proposalId) internal {
 
        ResultRecord storage result = ResultsByProposalId[_proposalId];
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        // Milestone Deadline proposals cannot be ended "by majority vote", we rely on finaliseExpiredProposal here
        // because we want to allow everyone to be able to vote "NO" if they choose to cashback.
 
        if( proposal.actionType != getActionType("MILESTONE_DEADLINE")) {
            // read results,
            if(result.yes > result.requiredForResult) {
                // voting resulted in YES
                proposal.state = getRecordState("VOTING_RESULT_YES");
            } else Eif (result.no >= result.requiredForResult) {
                // voting resulted in NO
                proposal.state = getRecordState("VOTING_RESULT_NO");
            }
        }
 
        runActionAfterResult(_proposalId);
    }
 
    function finaliseExpiredProposal(uint256 _proposalId) internal {
 
        ResultRecord storage result = ResultsByProposalId[_proposalId];
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        // an expired proposal with no votes will end as YES
        if(result.yes == 0 && result.no == 0) {
            proposal.state = getRecordState("VOTING_RESULT_YES");
        } else {
            // read results,
            if(result.yes > result.no) {
                // voting resulted in YES
                proposal.state = getRecordState("VOTING_RESULT_YES");
            } else Eif (result.no >= result.yes) {
                // tie equals no
                // voting resulted in NO
                proposal.state = getRecordState("VOTING_RESULT_NO");
            }
        }
        runActionAfterResult(_proposalId);
    }
 
    function tryFinaliseNonLockedTokensProposal(uint256 _proposalId) internal {
 
        ResultRecord storage result = ResultsByProposalId[_proposalId];
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        if(result.requiredForResult == 0) {
            proposal.state = getRecordState("VOTING_RESULT_YES");
            runActionAfterResult(_proposalId);
        }
    }
 
    function addActiveProposal(uint256 _proposalId) internal {
        ActiveProposalIds[ActiveProposalNum++]= _proposalId;
    }
 
    function removeAndReindexActive(uint256 _proposalId) internal {
 
        bool found = false;
        for (uint8 i = 0; i < ActiveProposalNum; i++) {
            if(ActiveProposalIds[i] == _proposalId) {
                found = true;
            }
            if(found) {
                ActiveProposalIds[i] = ActiveProposalIds[i+1];
            }
        }
 
        ActiveProposalNum--;
    }
 
 
    bool public EmergencyFundingReleaseApproved = false;
 
    function runActionAfterResult(uint256 _proposalId) internal {
 
        ProposalRecord storage proposal = ProposalsById[_proposalId];
 
        if(proposal.state == getRecordState("VOTING_RESULT_YES")) {
 
            if(proposal.actionType == getActionType("MILESTONE_DEADLINE")) {
 
            } else if (proposal.actionType == getActionType("MILESTONE_POSTPONING")) {
 
            } else if (proposal.actionType == getActionType("EMERGENCY_FUND_RELEASE")) {
                EmergencyFundingReleaseApproved = true;
 
            } else if (proposal.actionType == getActionType("PROJECT_DELISTING")) {
 
                ListingContractEntity.delistChild( proposal.extra );
 
            } else if (
                proposal.actionType == getActionType("IN_DEVELOPMENT_CODE_UPGRADE") ||
                proposal.actionType == getActionType("AFTER_COMPLETE_CODE_UPGRADE")
            ) {
 
                // initiate code upgrade
                acceptCodeUpgrade(_proposalId);
            }
 
            removeAndReindexActive(_proposalId);
 
        } else if(proposal.state == getRecordState("VOTING_RESULT_NO")) {
 
            //
            if(proposal.actionType == getActionType("MILESTONE_DEADLINE")) {
 
            } else {
                removeAndReindexActive(_proposalId);
            }
        }
    }
 
    // used by vault cash back
    function getMyVoteForCurrentMilestoneRelease(address _voter) public view returns (bool) {
        // find proposal id for current milestone
        uint8 recordId = MilestonesEntity.currentRecord();
        bytes32 hash = getHash( getActionType("MILESTONE_DEADLINE"), bytes32( recordId ), 0 );
        uint256 proposalId = ProposalIdByHash[hash];
        // based on that proposal id, find my vote
        VoteStruct memory vote = VotesByCaster[proposalId][_voter];
        return vote.vote;
    }
 
    function getHasVoteForCurrentMilestoneRelease(address _voter) public view returns (bool) {
        // find proposal id for current milestone
        uint8 recordId = MilestonesEntity.currentRecord();
        bytes32 hash = getHash( getActionType("MILESTONE_DEADLINE"), bytes32( recordId ), 0 );
        uint256 proposalId = ProposalIdByHash[hash];
        return hasPreviousVote(proposalId, _voter);
    }
 
    function getMyVote(uint256 _proposalId, address _voter) public view returns (bool) {
        VoteStruct memory vote = VotesByCaster[_proposalId][_voter];
        return vote.vote;
    }
 
}