Example #1
0
    def test_add_many_reading(self):
        ##
        ## run service
        ##
        checker_service_process = Process(
            target=smart_meter_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ##
        ## create transaction
        ##
        # generate crypto params
        G = setup()[0]
        (provider_priv, provider_pub) = key_gen(setup())

        # set init values
        tariffs = [5, 3, 5, 3, 5]
        readings = [10, 20, 30, 10, 50]
        openings = [G.order().random() for _ in tariffs]
        billing_period = 764

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

        # create meter
        create_meter_transaction = smart_meter.create_meter(
            (token, ), None, None,
            pack(provider_pub), 'Some info about the meter.', dumps(tariffs),
            dumps(billing_period))
        meter = create_meter_transaction['transaction']['outputs'][1]

        # add a readings
        transaction = {}
        input_obj = meter
        for i in range(0, len(tariffs)):
            transaction = smart_meter.add_reading((input_obj, ), None, None,
                                                  pack(provider_priv),
                                                  dumps(readings[i]),
                                                  pack(openings[i]))
            input_obj = transaction['transaction']['outputs'][0]

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

        ##
        ## stop service
        ##
        checker_service_process.terminate()
        checker_service_process.join()
Example #2
0
    def test_add_reading(self):
        ##
        ## run service
        ##
        checker_service_process = Process(
            target=smart_meter_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ##
        ## create transaction
        ##
        # generate crypto params
        G = setup()[0]
        (provider_priv, provider_pub) = key_gen(setup())

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

        # create meter
        create_meter_transaction = smart_meter.create_meter(
            (token, ),
            None,
            None,
            pack(provider_pub),
            'Some info about the meter.',
            dumps([5, 3, 5, 3, 5]),  # the tariffs
            dumps(764)  # billing period         
        )
        meter = create_meter_transaction['transaction']['outputs'][1]

        # add a reading
        transaction = smart_meter.add_reading(
            (meter, ),
            None,
            None,
            pack(provider_priv),
            dumps(10),  # the new reading 
            pack(G.order().random())  # the opening value
        )
        print transaction

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

        ##
        ## stop service
        ##
        checker_service_process.terminate()
        checker_service_process.join()
Example #3
0
    def test_create_meter(self):
        ##
        ## run service
        ##
        checker_service_process = Process(
            target=smart_meter_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ##
        ## create transaction
        ##
        # create provider's public key
        (_, provider_pub) = key_gen(setup())

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

        # create meter
        transaction = smart_meter.create_meter(
            (token, ),
            None,
            None,
            pack(provider_pub),
            'Some info about the meter.',  # some info about the meter
            dumps([5, 3, 5, 3, 5]),  # the tariffs 
            dumps(764)  # the billing period
        )
        print transaction

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

        ##
        ## stop service
        ##
        checker_service_process.terminate()
        checker_service_process.join()
    def test_create_meter(self):
        ##
        ## init
        ##
        transaction = smart_meter.init()
        response = requests.post('http://127.0.0.1:' + port + '/api/' +
                                 version + '/transaction/process',
                                 json=transaction)
        self.assertTrue(response.json()['success'])

        ##
        ## create transaction
        ##
        # create provider's public key
        (_, provider_pub) = key_gen(setup())

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

        # create meter
        transaction = smart_meter.create_meter(
            (token, ),
            None,
            None,
            pack(provider_pub),
            'Some info about the meter.',  # some info about the meter
            dumps([5, 3, 5, 3, 5]),  # the tariffs 
            dumps(764)  # the billing period
        )

        ##
        ## submit transaction
        ##
        response = requests.post('http://127.0.0.1:' + port + '/api/' +
                                 version + '/transaction/process',
                                 json=transaction)
        self.assertTrue(response.json()['success'])
Example #5
0
def main():
    ## get contract and init pint
    smart_meter.contract._populate_empty_checkers()
    print "operation\t\tmean (s)\t\tsd (s)\t\truns"


    # ---------------------------------------------------------
    # get functions
    # ---------------------------------------------------------
    # params
    params = setup()
    G = params[0]
    (provider_priv, provider_pub) = key_gen(params)
    tariffs  = [5, 3, 5, 3, 5]
    readings = [10, 20, 30, 10, 50]
    openings = [G.order().random() for _ in tariffs]

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

    # create smart_meter
    create_meter_tx = smart_meter.create_meter(
        (token,),
        None,
        None,
        pack(provider_pub),
        'Some info about the meter.',   # some info about the meter
        dumps([5, 3, 5, 3, 5]),         # the tariffs
        dumps(764)                      # the billing period
    )
    meter_obj = create_meter_tx['transaction']['outputs'][1]

    # add reading
    add_reading_tx = smart_meter.add_reading(
        (meter_obj,),
        None,
        None,
        pack(provider_priv),
        dumps(10),                 # the new reading
        pack(G.order().random())   # the opening value
    )

    # compute bill
    last_meter_obj = add_reading_tx['transaction']['outputs'][0]
    compute_bill_tx = smart_meter.compute_bill(
        (last_meter_obj,),
        None,
        None,
        dumps(readings),
        pack(openings),
        dumps(tariffs)
    )


    # ---------------------------------------------------------
    # test create_meter
    # ---------------------------------------------------------
    # [gen]
    tester(RUNS, "create_meter [g]", smart_meter.create_meter,
        (token,),
        None,
        None,
        pack(provider_pub),
        'Some info about the meter.',
        dumps([5, 3, 5, 3, 5]),
        dumps(764)
    )
    # [check]
    solution = transaction_to_solution(create_meter_tx)
    tester(RUNS, "create_meter [c]", smart_meter.contract.checkers['create_meter'],
        solution['inputs'],
        solution['referenceInputs'],
        solution['parameters'],
        solution['outputs'],
        solution['returns'],
        solution['dependencies'],
    )


    # ---------------------------------------------------------
    # test add_reading
    # ---------------------------------------------------------
    # [gen]
    tester(RUNS, "add_reading [g]", smart_meter.add_reading,
        (meter_obj,),
        None,
        None,
        pack(provider_priv),
        dumps(10),
        pack(G.order().random())
    )
    # [check]
    solution = transaction_to_solution(add_reading_tx)
    tester(RUNS, "add_reading [c]", smart_meter.contract.checkers['add_reading'],
        solution['inputs'],
        solution['referenceInputs'],
        solution['parameters'],
        solution['outputs'],
        solution['returns'],
        solution['dependencies'],
    )


    # ---------------------------------------------------------
    # test compute bill
    # ---------------------------------------------------------
    # [gen]
    tester(RUNS, "compute_bill [g]", smart_meter.compute_bill,
        (last_meter_obj,),
        None,
        None,
        dumps(readings),
        pack(openings),
        dumps(tariffs)
    )
    # [check]
    solution = transaction_to_solution(compute_bill_tx)
    tester(RUNS, "compute_bill [c]", smart_meter.contract.checkers['compute_bill'],
        solution['inputs'],
        solution['referenceInputs'],
        solution['parameters'],
        solution['outputs'],
        solution['returns'],
        solution['dependencies'],
    )