Example #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
        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
Example #2
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]
Example #3
0
def test_IndexAuth():
	addr_list = './addr_list.json'
	#set sample record 
	record_block={}
	record_block['id']='1'
	#record_block['value']='samuelxu'

	#extract data from index file
	filepath = './features/0_2_person1/13.2.52.txt'
	filepath0 = './features/0_2_person1/13.4.53.txt'
	indexData=IndexPolicy.ExtractData(filepath)
	record_block['value']=str(indexData)

	#1) read token data
	token_data=mytoken.getIndexToken(record_block['id'])
	print(token_data)

	node_data=mytoken.getAuthorizedNodes();
	print(node_data)

	json_token = IndexPolicy.get_indexToken(record_block['id'])
	print(json_token)

	#2) get hash value for index
	# transfer string data to bytes block
	bytes_block = TypesUtil.string_to_bytes(record_block['value'])

	hash_value = Crypto_Hash.generate_hash(bytes_block)
	hash_str = str(hash_value)
	'''print(hash_value)
	print(hash_str)
	print(token_data[1])'''

	#3) set index token
	#mytoken.setIndexToken(record_block['id'], hash_str);

	#4) set authrozied nodes
	node_address = IndexToken.getAddress('TKB1_node1', addr_list)
	#mytoken.addAuthorizedNodes(node_address)
	#mytoken.removeAuthorizedNodes(node_address)

	#5) verify hash
	#hash_token = token_data[1]
	#print(Crypto_Hash.verify_hash(hash_value, bytes_block))

	print(IndexPolicy.verify_indexToken(record_block['id'],filepath))
Example #4
0
@TaskDescription: This module provide index authentication policy for Event-Oriented Surveillance Video Query using Blockchain
'''

import time
from utilities import DatetimeUtil, TypesUtil, FileUtil
from wrapper_pyca import Crypto_Hash
from Index_Token import IndexToken


#global variable
http_provider = 'http://localhost:8042'
contract_addr = '0xb7e549d21afa4c6fc672a37ef00bfab0ca6d81a8'
contract_config = './contracts/IndexToken.json'

#new IndexToken object
mytoken=IndexToken(http_provider, contract_addr, contract_config)


'''
Index authentication policy management
'''
class IndexPolicy(object):

	# get token data from smart contract, return json fromat
	@staticmethod
	def get_indexToken(str_index):
		token_data=mytoken.getIndexToken(str_index);
		json_token={}

		#Add status information
		json_token['id'] = token_data[0]
Example #5
0
from torchvision import transforms

from utilities import TypesUtil, FileUtil
from wrapper_pyca import Crypto_Hash
from Index_Token import IndexToken 
from Tender_RPC import Tender_RPC
from Micro_RPC import Micro_RPC 

logger = logging.getLogger(__name__)
# indextoken_logger = logging.getLogger("Index_Token")
# indextoken_logger.setLevel(logging.INFO)

# -------------------- Smart contract configuration -------------------
addr_list = "./addr_list.json"
http_provider = "http://localhost:8042"
contract_addr = IndexToken.getAddress('HashModelToken', addr_list)
contract_config = "./contracts/IndexToken.json"

# new IndexToken object
mytoken=IndexToken(http_provider, contract_addr, contract_config)

# tx commit timeout.
TX_TIMEOUT = 60

# Model
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4 * 4 * 50, 500)