Exemple #1
0
    def tx_evaluate(model_name):
        '''
        Launch tx and evaluate tx committed time

        Args:
            model_name: model file
        Returns:
            tx committed reulst
        '''
        # 1) Load model from file
        model=ModelUtils.load_model(model_name)

        # 2) calculate hash value for model
        hash_value=ModelUtils.hash_model(model)

        # 3) evaluate tx committed time
        start_time=time.time()
        logger.info("tx hashed model: {} to blockchain...\n".format(model_name)) 
        # -------- prepare parameter for tx ------------
        tx_json = {}
        key_str = model_name
        value_str = TypesUtil.string_to_hex(hash_value)
        tx_data = key_str + "=" + value_str 
        # --------- build parameter string: tx=? --------
        tx_json['tx']='"' + tx_data +'"' 
        tx_ret=Tender_RPC.broadcast_tx_commit(tx_json)
        exec_time=time.time()-start_time
        logger.info("tx committed time: {:.3f}\n".format(exec_time, '.3f')) 
        FileUtil.save_testlog('test_results', 'exec_tx_commit_tendermint.log', format(exec_time, '.3f'))

        return tx_ret
def test_maskedModel(args):
    use_cuda = args.cuda and torch.cuda.is_available()

    torch.manual_seed(args.seed)

    device = torch.device("cuda" if use_cuda else "cpu")
    kwargs = {"num_workers": 1, "pin_memory": True} if use_cuda else {}

    logger.info("test_loader setup...\n")
    test_loader = torch.utils.data.DataLoader(
        datasets.MNIST(
            root="./data",
            train=False,
            download=True,
            transform=transforms.Compose([
                transforms.ToTensor(),
                transforms.Normalize((0.1307, ), (0.3081, ))
            ]),
        ),
        batch_size=args.test_batch_size,
        shuffle=True,
        **kwargs,
    )

    model = ModelUtils.load_model("mnist_cnn.pt", False)

    for i in range(args.tx_round):
        if (args.mask_model == 0):
            # test mask input
            logger.info("Test run:{}".format(i + 1))
            start_time = time.time()
            masked_model = ModelUtils.mask_model(model, 0.1)
            str_time_exec = format((time.time() - start_time) * 1000, '.3f')
            FileUtil.save_testlog('test_results', 'mask_model.log',
                                  str_time_exec)

            if (args.eval_model):
                logger.info("test masked module...\n")
                ModelUtils.evaluate_model(masked_model, device, test_loader)

                unmask_model = ModelUtils.mask_model(masked_model, -0.1)

                logger.info("test unmasked module...\n")
                ModelUtils.evaluate_model(unmask_model, device, test_loader)
        else:
            # test sum masked value
            logger.info("Test run:{}".format(i + 1))
            model_list = []
            for i in range(1, 21):
                model_list.append(model)

            start_time = time.time()
            fedavg_model = ModelUtils.FedAvg_model(model_list)
            str_time_exec = format((time.time() - start_time) * 1000, '.3f')
            FileUtil.save_testlog('test_results', 'sum_model.log',
                                  str_time_exec)

            if (args.eval_model):
                logger.info("test fedavg_model module...\n")
                ModelUtils.evaluate_model(fedavg_model, device, test_loader)
Exemple #3
0
def getService():

    start_time = time.time()
    service_data = mySrvExchange.getService()
    exec_time = time.time() - start_time
    FileUtil.save_testlog('test_results', 'exec_getService.log',
                          format(exec_time * 1000, '.3f'))

    json_data = {}

    json_data['dealer'] = {}
    json_data['dealer']['uid'] = service_data[0]
    json_data['dealer']['balance'] = service_data[1]

    json_data['provider'] = {}
    json_data['provider']['vid'] = service_data[2]
    json_data['provider']['serviceinfo'] = service_data[3]
    json_data['provider']['status'] = service_data[4]

    json_data['recipient'] = {}
    json_data['recipient']['vid'] = service_data[5]
    json_data['recipient']['serviceinfo'] = service_data[6]
    json_data['recipient']['status'] = service_data[7]

    return jsonify({'result': 'Succeed', 'data': json_data}), 201
def getBroker():

    start_time = time.time()
    publisher_info = mySrvExchange.getPublisher()
    subscriber_info = mySrvExchange.getSubscriber()
    exec_time = time.time() - start_time
    FileUtil.save_testlog('test_results', 'exec_getBroker.log',
                          format(exec_time * 1000, '.3f'))

    host_account = mySrvExchange.getHostAccounts()[0]
    host_balance = mySrvExchange.getHostBalance(host_account)

    json_data = {}

    json_data['host'] = {}
    json_data['host']['account'] = host_account
    json_data['host']['balance'] = format(host_balance, '.3f')

    json_data['publisher'] = {}
    json_data['publisher']['vid'] = publisher_info[0]
    json_data['publisher']['zid'] = publisher_info[1]
    json_data['publisher']['status'] = publisher_info[2]
    json_data['publisher']['balance'] = publisher_info[3]
    json_data['publisher']['txs'] = publisher_info[4]

    json_data['subscriber'] = {}
    json_data['subscriber']['vid'] = subscriber_info[0]
    json_data['subscriber']['zid'] = subscriber_info[1]
    json_data['subscriber']['status'] = subscriber_info[2]
    json_data['subscriber']['balance'] = subscriber_info[3]
    json_data['subscriber']['txs'] = subscriber_info[4]

    return jsonify({'result': 'Succeed', 'data': json_data}), 201
Exemple #5
0
    def verify_hashmodel(model_name, target_address="0.0.0.0:8080"):
        '''
        Verify model hash value by querying blockchain

        Args:
            model_name: model file
        Returns:
            Verified result: True or False
        '''
        # 1) Load model from file
        ls_time_exec = []
        start_time=time.time()
        model=ModelUtils.load_model(model_name)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        # 2) Calculate hash value of model
        start_time=time.time()
        hash_value=ModelUtils.hash_model(model)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        model_hash={}
        model_hash[model_name]=str(hash_value)

        # 3) Read token data using call
        query_json = {}
        value_str = str(hash_value)
        query_json[model_name]=value_str
        # print(query_json)
        start_time=time.time()
        query_ret=Micro_RPC.tx_query(target_address, query_json)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        # # -------- parse value from response and display it ------------
        verify_result = False
        # print(query_ret)
        logger.info("Fetched model hash value:")
        if(query_ret!={}):
            tx_json = TypesUtil.string_to_json(query_ret)
            for _name, _value in tx_json.items():
                logger.info("model: {}".format(_name) )
                logger.info("value: {}".format(_value) )
            verify_result = True

        
        # Prepare log messgae
        str_time_exec=" ".join(ls_time_exec)
        FileUtil.save_testlog('test_results', 'exec_verify_hashmodel_microchain.log', str_time_exec)

        # 4) return verify hash model result
        return verify_result
Exemple #6
0
    def verify_hashmodel(model_name):
        '''
        Verify model hash value by querying blockchain

        Args:
            model_name: model file
        Returns:
            Verified result: True or False
        '''
        # 1) Load model from file
        ls_time_exec = []
        start_time=time.time()
        model=ModelUtils.load_model(model_name)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        # 2) Calculate hash value of model
        start_time=time.time()
        hash_value=ModelUtils.hash_model(model)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        model_hash={}
        model_hash[model_name]=str(hash_value)

        # 3) Read token data using call
        query_json = {}
        query_json['data']='"' + model_name +'"'
        start_time=time.time()
        query_ret=Tender_RPC.abci_query(query_json)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        # -------- parse value from response and display it ------------
        key_str=query_ret['result']['response']['key']
        value_str=query_ret['result']['response']['value']
        logger.info("Fetched model hash value:")
        logger.info("model: {}".format(TypesUtil.base64_to_ascii(key_str)) )
        if( value_str!= None):
            query_hash_value = TypesUtil.hex_to_string(TypesUtil.base64_to_ascii(value_str))
        else:
            query_hash_value = ''
        logger.info("value: {}".format(query_hash_value))
        
        # Prepare log messgae
        str_time_exec=" ".join(ls_time_exec)
        FileUtil.save_testlog('test_results', 'exec_verify_hashmodel_tendermint.log', str_time_exec)

        # 4) return verify hash model result
        return model_hash[model_name]==str(query_hash_value)
Exemple #7
0
    def tx_evaluate(model_name, target_address="0.0.0.0:8080"):
        '''
        Launch tx and evaluate tx committed time

        Args:
            model_name: model file
        Returns:
            tx committed reulst
        '''
        # 1) Load model from file
        model=ModelUtils.load_model(model_name)

        # 2) calculate hash value for model
        hash_value=ModelUtils.hash_model(model)

        # 3) evaluate tx committed time
        tx_time = 0.0
        start_time=time.time()
        logger.info("tx hashed model: {} to blockchain...\n".format(model_name)) 
        # -------- prepare transaction data ------------
        tx_json = {}
        # value_str = TypesUtil.string_to_hex(hash_value)
        value_str = str(hash_value)
        tx_json[model_name]=value_str
        # print(tx_json)
        tx_ret=Micro_RPC.broadcast_tx_commit(target_address, tx_json)

        while(True):
            query_ret=Micro_RPC.tx_query(target_address, tx_json)

            if( query_ret!={} ):
                break
            time.sleep(0.5)
            tx_time +=0.5
            if(tx_time>=TX_TIMEOUT):
                logger.info("Timeout, tx commit fail.") 
                return False

        exec_time=time.time()-start_time
        logger.info("tx committed time: {:.3f}\n".format(exec_time, '.3f')) 
        FileUtil.save_testlog('test_results', 'exec_tx_commit_microchain.log', format(exec_time, '.3f'))

        # return tx_ret
        return tx_ret
Exemple #8
0
def getAccount():
    # parse data from request.data
    req_data = TypesUtil.bytes_to_string(request.data)
    json_data = TypesUtil.string_to_json(req_data)

    client_addr = json_data['client_addr']

    start_time = time.time()
    service_data = mySrvExchange.getAccount(client_addr)
    exec_time = time.time() - start_time
    FileUtil.save_testlog('test_results', 'exec_getAccount.log',
                          format(exec_time * 1000, '.3f'))

    json_data = {}

    json_data['account'] = client_addr
    json_data['uid'] = service_data[0]
    json_data['balance'] = service_data[1]
    json_data['status'] = service_data[2]

    return jsonify({'result': 'Succeed', 'data': json_data}), 201
Exemple #9
0
    def tx_evaluate(model_name):
        '''
        Launch tx and evaluate tx committed time

        Args:
            model_name: model file
        Returns:
            tx committed reulst
        '''
        # 1) Load model from file
        model=ModelUtils.load_model(model_name)
        # 2) calculate hash value for model
        hash_value=ModelUtils.hash_model(model)

        # 3) evaluate tx committed time
        token_data=mytoken.getIndexToken(model_name)
        original_id = token_data[0] 

        logger.info("tx hashed model: {} to blockchain...\n".format(model_name)) 
        tx_time = 0.0
        start_time=time.time()
        mytoken.setIndexToken(model_name, str(hash_value))
        while(True):
            token_data=mytoken.getIndexToken(model_name)
            new_id = token_data[0]
            if(new_id > original_id ):
                IndexToken.print_tokendata(token_data)
                break
            time.sleep(0.1)
            tx_time +=0.1
            if(tx_time>=TX_TIMEOUT):
                logger.info("Timeout, tx commit fail.") 
                return False

        exec_time=time.time()-start_time
        logger.info("tx committed time: {:.3f}\n".format(exec_time, '.3f')) 
        FileUtil.save_testlog('test_results', 'exec_tx_commit_ethereum.log', format(exec_time, '.3f'))

        return True
Exemple #10
0
    def verify_hashmodel(model_name):
        '''
        Verify model hash value by querying blockchain

        Args:
            model_name: model file
        Returns:
            Verified result: True or False
        '''
        # 1) Load model from file
        ls_time_exec = []
        start_time=time.time()
        model=ModelUtils.load_model(model_name)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        # 2) Calculate hash value of model
        start_time=time.time()
        hash_value=ModelUtils.hash_model(model)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        model_hash={}
        model_hash[model_name]=str(hash_value)

        # -------- display contract information -------------
        mytoken.Show_ContractInfo()

        # 3) Read token data using call
        start_time=time.time()
        token_data=mytoken.getIndexToken(model_name)
        IndexToken.print_tokendata(token_data)
        ls_time_exec.append( format( time.time()-start_time, '.3f' ) ) 

        # Prepare log messgae
        str_time_exec=" ".join(ls_time_exec)
        FileUtil.save_testlog('test_results', 'exec_verify_hashmodel_ethereum.log', str_time_exec)

        # 4) return verify hash model result
        return model_hash[model_name]==token_data[1]
    if (kv_mode == 0):
        # ----------------- 1) value --------------
        # tx_data = "samuel"
        tx_data = tx_value
    else:
        # ----------------- 2) key:value --------------
        json_value = {}
        json_value['name'] = "samuel_xu999"
        json_value['age'] = 36
        key_str = 'id'
        value_str = TypesUtil.json_to_tx(json_value)
        # print(value_str)

        # In tx_data, " must replace with ', for json: 'id={\'address\':\'hamilton\'}'
        tx_data = key_str + "=" + value_str

    # --------- build parameter string: tx=? --------
    tx_json['tx'] = '"' + tx_data + '"'
    # print(tx_json)

    start_time = time.time()
    # ---------------- deliver tx --------------
    test_tx_commit(tx_json)
    exec_time = time.time() - start_time
    print(format(exec_time * 1000, '.3f'))
    FileUtil.save_testlog('test_results', 'exec_time.log',
                          format(exec_time * 1000, '.3f'))

    pass