Ethernet Square Intelligent Contract Voting example Java simulation implementation

Source: Internet
Author: User
Tags static class

Using Java language Simulation to implement the intelligent contract implementation process of Ethernet square

Package contract;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
 /** * Created by Haibiao on 2018/1/5.
    * * Public class Ballotcontract {private static final String TAG = "Ballotcontract"; /** Identity Audit (grant voting rights) deadline/public static final long verify_deadline = 1515168000000l;//2018/01/06 00:00:00.000/** vote (delegate)
    Closing Time/public static final long vote_deadline = Verify_deadline + (1000 * 60 * 2);//within 2 minutes of the end of the identity Audit/** contract form for the voting time * *
    private static Ballotcontract INSTANCE;
    /** Contract creator (voting host) address * * Private final String mchairaddress;
    /** Voter State Save/private final map<string,voter> Mvotermap = new hashmap<> ();
    /** Proposal List * * Private final proposal[] mproposals;

    /** voting results, the most votes of the proposal index * * Private int[] mvoteresult;

    /** each call to the contract will contain the context * * Private Ctx CTX; /** * Create contract, only once * @param the context of the CTX invocation * @param names proposal name array */public static void Create (CTX ctx,string[] Names {if (INSTANCE = = null) {INSTANCE = new ballotcontract (ctx,names); /** * Get Contract Instance * @param the context of the CTX call * @return Voting contract instance/public static ballotcontract ge
        Tdefault (Ctx Ctx) {instance.ctx = CTX;
    return INSTANCE;  /** * Voting contract constructor, initialize some variables * @param ctx CONTEXT * @param names proposal name array */Private Ballotcontract (CTX
        CTX, string[] names) {mchairaddress = Ctx.msg.sender;
        Voter chair = new voter ();
        Chair.weight = 1;
        Mvotermap.put (Mchairaddress,chair);
        Mproposals = new Proposal[names.length];
            for (int i = 0; i < mproposals.length i++) {mproposals[i] = new Proposal ();
        Mproposals[i].name = Names[i];
        }/** * Get the results of the poll, only if the voting stage is over and the method of one vote is invoked. * @return Voting Results/public int[] Getvoteresult () {
    return mvoteresult; /** * gives the right to vote, only the host calls * @param Address given to the destination * @return give the voting result/public Boolean givevoteright (String address) {if System.curren
            Ttimemillis () > Verify_deadline) {log ("Givevoteright time already over");
        return false;
            } if (!mchairaddress.equals (ctx.msg.sender)) {log ("Givevoteright only chairman can call");
        return false;
        } Voter Voter = mvotermap.get (address);
            if (voter = = null) {voter = new voter ();
            Voter.weight = 1;
            Mvotermap.put (Address,voter);
            Log ("givevoteright success, Address =" + address);
        return true;
        }else{log ("Givevoteright failed, yet give,address =" +address);
    return false; /** * Delegate operation, delegate voting power to others * @param to trustee address * @return Delegate result/public Boolean delegate (final Stri
        Ng to) {final long Curr = System.currenttimemillis (); if (Curr <= verify_deAdline) {log ("Delegate failed,the delegate hasn ' t begun");
        return false;
            } if (Curr > Vote_deadline) {log ("delegate failed,delegate already over");
        return false;
        Final String sender = Ctx.msg.sender;
        Voter fromvoter = Mvotermap.get (sender); if (Fromvoter = NULL//No voting rights | | Fromvoter.weight <= 0//No voting rights | | Sender.equals (TO)//delegate
            To himself) {log ("Delegate failed");
        return false;
        } String totemp = to;
        Voter tovoter = null;
            while (true) {Tovoter = Mvotermap.get (totemp);
                if (Tovoter = = null//Trustee does not have the right to vote | | tovoter.weight <= 0//Trustee has no right to vote) {
            return false;
                } if (tovoter.delegate = = null) {tovoter.weight + = Fromvoter.weight;
                fromvoter.weight = 0; FroMvoter.delegate = totemp;
            return true;
            }else{totemp = tovoter.delegate; /** * Executive Vote * @param index of the index proposal * @return Voting Result * * Public boolean vote (int i
        Ndex) {final long Curr = System.currenttimemillis ();
            if (Curr <= verify_deadline) {log ("Vote failed,the delegate hasn ' t begun");
        return false;
            } if (Curr > Vote_deadline) {log ("VOTE Failed,vote already Over");
        return false;
            } if (Index < 0 | | | Index >= mproposals.length) {log ("Proposal index error, index =" +index);
        return false;
        Final String sender = Ctx.msg.sender;
        Voter voter = mvotermap.get (sender); if (voter = NULL//No voting rights | | Voter.weight <= 0//No voting rights) {log ("Vote failed")
            ;
        return false; Final PropoSal proposal = Mproposals[index];
        Proposal.votecount + = Voter.weight;
        voter.weight = 0;
        Voter.proposalindex = index;
    return true; /** * Statistics of votes, anyone can call at any time, when the voting stage is over, call this method will save the final result of the vote * @return the current results of the election/public int[] Statisticsvote
            () {if (System.currenttimemillis () <= verify_deadline) {log ("The statistics vote hasn ' t begun");
        return new int[0];
        Final list<integer> result = new arraylist<> (1);
        int maxcount =-1;
            for (int i = 0; i < mproposals.length; i++) {int count = Mproposals[i].votecount;
                if (Count > Maxcount) {maxcount = count;
                Result.clear ();
            Result.add (i);
            }else if (count = = Maxcount) {result.add (i);
        } int[] arr = new int[result.size ()]; for (int i = 0; i < arr.length i++) {Arr[i] = ResulT.get (i);
        if (System.currenttimemillis () > Vote_deadline) {//voting phase is closed, save the results of the votes mvoteresult = arr;
        Log ("Statistics vote result =" + arrays.tostring (arr));
    return arr;
    private static void log (String msg) {System.out.println (String.Format ("[%s]%s", tag,msg));
        }/** * Voter data structure/private static class voter{int weight;//ticket number String delegate;//Trustee int proposalindex;//proposal INDEX number}/** * Proposal data structure */private static class proposal{String Nam
 e;//proposal name int votecount;//vote number}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.