Пример #1
0
    def test_init(self):
        ##
        ## run service
        ##
        checker_service_process = Process(target=vote_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ##
        ## create transaction
        ##
        transaction = vote.init()

        ##
        ## submit transaction
        ##
        response = requests.post(
            'http://127.0.0.1:5000/' + vote_contract.contract_name + '/init', json=transaction_to_solution(transaction)
        )
        self.assertTrue(response.json()['success'])

        ##
        ## stop service
        ##
        checker_service_process.terminate()
        checker_service_process.join()
Пример #2
0
    def test_init(self):

        ## create transaction
        transaction = vote.init()

        ## submit transaction
        response = requests.post('http://127.0.0.1:' + port + '/api/' +
                                 version + '/transaction/process',
                                 json=transaction)
        self.assertTrue(response.json()['success'])
Пример #3
0
    def test_create_vote(self):

        ##
        ## init
        ##
        transaction = vote.init()
        response = requests.post('http://127.0.0.1:' + port + '/api/' +
                                 version + '/transaction/process',
                                 json=transaction)
        self.assertTrue(response.json()['success'])

        ##
        ## create transaction
        ##
        # create keys
        params = setup()
        (tally_priv, tally_pub) = key_gen(params)
        (_, voter1_pub) = key_gen(params)
        (_, voter2_pub) = key_gen(params)
        (_, voter3_pub) = key_gen(params)

        # set up options, particpants, and tally's key
        options = ['alice', 'bob']
        participants = [pack(voter1_pub), pack(voter2_pub), pack(voter3_pub)]

        # init
        init_transaction = vote.init()
        token = init_transaction['transaction']['outputs'][0]

        # initialise vote (all votes are zero)
        transaction = vote.create_vote((token, ), None, None, dumps(options),
                                       dumps(participants), pack(tally_priv),
                                       pack(tally_pub))

        ##
        ## submit transaction
        ##
        response = requests.post('http://127.0.0.1:' + port + '/api/' +
                                 version + '/transaction/process',
                                 json=transaction)
        self.assertTrue(response.json()['success'])
Пример #4
0
    def test_create_vote(self):
        ##
        ## run service
        ##
        checker_service_process = Process(target=vote_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ##
        ## create transaction
        ##
        # create keys
        params = setup()
        (tally_priv, tally_pub)  = key_gen(params)
        (_, voter1_pub) = key_gen(params)
        (_, voter2_pub) = key_gen(params)
        (_, voter3_pub) = key_gen(params)

        # set up options, particpants, and tally's key
        options      = ['alice', 'bob']
        participants = [pack(voter1_pub), pack(voter2_pub), pack(voter3_pub)]

        # init
        init_transaction = vote.init()
        token = init_transaction['transaction']['outputs'][0]

        # initialise vote (all votes are zero)
        transaction = vote.create_vote(
            (token, ),
            None,
            None,
            dumps(options),
            dumps(participants),
            pack(tally_priv),
            pack(tally_pub)
        )
        print transaction

        ##
        ## submit transaction
        ##
        response = requests.post(
            'http://127.0.0.1:5000/' + vote_contract.contract_name + '/create_vote', json=transaction_to_solution(transaction)
        )
        print response.json()
        self.assertTrue(response.json()['success'])

        ##
        ## stop service
        ##
        checker_service_process.terminate()
        checker_service_process.join()
Пример #5
0
def main():
    ## get contract and init pint
    vote.contract._populate_empty_checkers()
    print "operation\t\tmean (s)\t\tsd (s)\t\truns"

    # ---------------------------------------------------------
    # get functions
    # ---------------------------------------------------------
    # params
    params = setup()
    (tally_priv, tally_pub) = key_gen(params)
    (voter1_priv, voter1_pub) = key_gen(params)
    options = ['alice', 'bob']
    participants = [pack(voter1_pub)]

    # init
    init_tx = vote.init()
    token = init_tx['transaction']['outputs'][0]

    # create vote
    create_vote_tx = vote.create_vote((token, ), None, None, dumps(options),
                                      dumps(participants), pack(tally_priv),
                                      pack(tally_pub))
    vote_obj = create_vote_tx['transaction']['outputs'][1]

    # add vote
    add_vote_tx = vote.add_vote((vote_obj, ), None, None, dumps([1]),
                                pack(voter1_priv), pack(voter1_pub))

    # tally
    last_vote_obj = add_vote_tx['transaction']['outputs'][0]
    tally_tx = vote.tally((last_vote_obj, ), None, None, pack(tally_priv),
                          pack(tally_pub))

    # ---------------------------------------------------------
    # test create_vote
    # ---------------------------------------------------------
    # [gen]
    tester(RUNS, "create_vote [g]", vote.create_vote, (token, ), None, None,
           dumps(options), dumps(participants), pack(tally_priv),
           pack(tally_pub))
    # [check]
    solution = transaction_to_solution(create_vote_tx)
    tester(
        RUNS,
        "create_vote [c]",
        vote.contract.checkers['create_vote'],
        solution['inputs'],
        solution['referenceInputs'],
        solution['parameters'],
        solution['outputs'],
        solution['returns'],
        solution['dependencies'],
    )

    # ---------------------------------------------------------
    # test add_vote
    # ---------------------------------------------------------
    # [gen]
    tester(RUNS, "add_vote [g]\t", vote.add_vote, (vote_obj, ), None, None,
           dumps([1, 0]), pack(voter1_priv), pack(voter1_pub))
    # [check]
    solution = transaction_to_solution(add_vote_tx)
    tester(
        RUNS,
        "add_vote [c]\t",
        vote.contract.checkers['add_vote'],
        solution['inputs'],
        solution['referenceInputs'],
        solution['parameters'],
        solution['outputs'],
        solution['returns'],
        solution['dependencies'],
    )

    # ---------------------------------------------------------
    # test compute bill
    # ---------------------------------------------------------
    # [gen]
    tester(RUNS, "tally [g]\t", vote.tally, (last_vote_obj, ), None, None,
           pack(tally_priv), pack(tally_pub))
    # [check]
    solution = transaction_to_solution(tally_tx)
    tester(
        RUNS,
        "tally [c]\t",
        vote.contract.checkers['tally'],
        solution['inputs'],
        solution['referenceInputs'],
        solution['parameters'],
        solution['outputs'],
        solution['returns'],
        solution['dependencies'],
    )
Пример #6
0
        json=transaction_to_solution(tx)
    )


params = setup()
(tally_priv, tally_pub) = key_gen(params)
(voter1_priv, voter1_pub) = key_gen(params)
(voter2_priv, voter2_pub) = key_gen(params)
(voter3_priv, voter3_pub) = key_gen(params)

# set up options, participants, and tally's key
options = ['alice', 'bob']
participants = [pack(voter1_pub), pack(voter2_pub), pack(voter3_pub)]


init_transaction = vote.init()

# pp_object(init_transaction)

post_transaction("init", init_transaction)

# pp_object(init_transaction)

vote_token = init_transaction['transaction']['outputs'][0]

# Create the vote
tx_create_vote = vote.create_vote((vote_token, ), None, None, json.dumps(options), json.dumps(participants), pack(tally_priv), pack(tally_pub))
post_transaction("create_vote", tx_create_vote)
vote_root = tx_create_vote['transaction']['outputs'][1]

Пример #7
0
    def test_read(self):
        ##
        ## run service
        ##
        checker_service_process = Process(target=vote_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ##
        ## create transaction
        ##
        # number of voters and values
        options = ['alice', 'bob', 'sally']
        num_voters = 50
        values = [[1, 0, 0] for _ in range(0, num_voters)]

        # create keys and particpants
        params = setup()
        (tally_priv, tally_pub) = key_gen(params)
        keys = [key_gen(params) for _ in range(0, num_voters)]
        participants = [pack(pub) for (_, pub) in keys]

        # get init token
        init_transaction = vote.init()

        # get initial scores
        create_vote_transaction = vote.create_vote(
            (init_transaction['transaction']['outputs'][0],),
            None,
            None,
            dumps(options),
            dumps(participants),
            pack(tally_priv),
            pack(tally_pub)
        )
        vote_0 = create_vote_transaction['transaction']['outputs'][1]

        # add votes
        transaction = {}
        input_obj = vote_0
        for i in range(0, num_voters):
            transaction = vote.add_vote(
                (input_obj,),
                None,
                None,
                dumps(values[i]), # votes' valu (0 or 1)
                pack(keys[i][0]), # voter's priv key
                pack(keys[i][1])  # voter's pub key
            )
            input_obj = transaction['transaction']['outputs'][0]

        # tally
        transaction = vote.tally(
            (input_obj,),
            None,
            None,
            pack(tally_priv),
            pack(tally_pub)
        )
        result = transaction['transaction']['outputs'][0]

        # read result
        transaction = vote.read(
            None,
            (result,),
            None,
        )

        # print result
        print transaction['transaction']['returns']


        ##
        ## submit transaction
        ##
        response = requests.post(
            'http://127.0.0.1:5000/' + vote_contract.contract_name + '/read', json=transaction_to_solution(transaction)
        )
        self.assertTrue(response.json()['success'])

        ##
        ## stop service
        ##
        checker_service_process.terminate()
        checker_service_process.join()