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] # First voter tx_add_vote_1 = vote.add_vote((vote_root,), None, None, json.dumps([1, 0]), pack(voter1_priv), pack(voter1_pub)) post_transaction("add_vote", tx_add_vote_1) vote_1 = tx_add_vote_1['transaction']['outputs'][0] # Second voter tx_add_vote_2 = vote.add_vote((vote_1,), None, None, json.dumps([0, 1]), pack(voter2_priv), pack(voter2_pub)) post_transaction("add_vote", tx_add_vote_2) vote_2 = tx_add_vote_2['transaction']['outputs'][0] # Third voter tx_add_vote_3 = vote.add_vote((vote_2,), None, None, json.dumps([1, 0]), pack(voter3_priv), pack(voter3_pub)) post_transaction("add_vote", tx_add_vote_3) vote_3 = tx_add_vote_3['transaction']['outputs'][0] # Tally the results tx_tally = vote.tally((vote_3,), None, None, pack(tally_priv), pack(tally_pub)) post_transaction("tally", tx_tally) pp_json(tx_tally) checker_service_process.terminate() checker_service_process.join()
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'], )
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()