Beispiel #1
0
class RegisterOnChainHandler():
    def __init__(self, config_path=my_config.CONFIG_PATH):
        self._contract_handler = ContractHandler('Register', config_path)
        self._w3 = self._contract_handler.get_w3()
        self._contract_inst = self._contract_handler.get_contract()

    def set_register(self, name, address):
        print('==== record start ====')
        tx_hash = self._contract_inst.functions.SetInst(name, address) \
                                               .transact({'from': self._w3.eth.accounts[0],
                                                          'gas': my_config.GAS_SPENT})
        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')
        print('==== record finish ====')

    def set_multiple_register(self, name_dict):
        print('==== record start ====')
        tx_hashs = []
        for name, address in name_dict.items():
            tx_hash = self._contract_inst.functions.SetInst(name, address) \
                                                   .transact({'from': self._w3.eth.accounts[0],
                                                              'gas': my_config.GAS_SPENT})
            tx_hashs.append(tx_hash)

        wait_miner(self._w3, tx_hashs)
        if check_transaction_meet_assert(self._w3, tx_hashs):
            raise IOError('assert encounter..')
        print('==== record finish ====')

    def get_register(self, name):
        print('==== get start ====')

        address = self._contract_inst.functions.GetInst(name).call()
        print('==== get end ====')
        return address

    def set_whitelist(self, address):
        print('==== record start ====')
        tx_hash = self._contract_inst.functions.SetWhitelist(address) \
                                               .transact({'from': self._w3.eth.accounts[0],
                                                          'gas': my_config.GAS_SPENT})
        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')
        print('==== record finish ====')

    def set_multiple_whitelist(self, addresses):
        print('==== record start ====')
        tx_hashs = []
        for address in addresses:
            tx_hash = self._contract_inst.functions.SetWhitelist(address) \
                                                   .transact({'from': self._w3.eth.accounts[0],
                                                              'gas': my_config.GAS_SPENT})
            tx_hashs.append(tx_hash)

        wait_miner(self._w3, tx_hashs)
        if check_transaction_meet_assert(self._w3, tx_hashs):
            raise IOError('assert encounter..')
        print('==== record finish ====')
Beispiel #2
0
    def _setup_event_emitter(self, config_path, submit_hash_callback_objs,
                             record_over_callback_objs):
        self._event_emitter = {}
        self._event_emitter[
            'submit_hash_callback_objs'] = submit_hash_callback_objs
        self._event_emitter[
            'record_over_callback_objs'] = record_over_callback_objs
        self._event_emitter['contract_handler'] = ContractHandler(
            'EventEmitter', config_path)

        self._event_emitter['submit_hash_event_filter'] = \
            self._event_emitter['contract_handler']._contract_inst.events.submit_hash.createFilter(fromBlock='latest')

        self._event_emitter['record_over_event_filter'] = \
            self._event_emitter['contract_handler']._contract_inst.events.record_over.createFilter(fromBlock='latest')
Beispiel #3
0
class RecordHashOnChainHandler():

    def __init__(self, config_path=my_config.CONFIG_PATH):
        self._contract_handler = ContractHandler('RecordHash', config_path)
        self._w3 = self._contract_handler.get_w3()
        self._contract_inst = self._contract_handler.get_contract()

    def record(self, input_hash):
        print('==== record start ====')
        tx_hash = self._contract_inst.functions.Record(convert_to_bytes(input_hash)) \
                                               .transact({'from': self._w3.eth.accounts[0],
                                                          'gas': GAS_SPENT})

        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')
        print('==== record finish ====')

    def get(self, input_hash):
        print('==== get start ====')

        exist = self._contract_inst.functions.Get(convert_to_bytes(input_hash)).call()
        print('==== get end ====')
        return exist
Beispiel #4
0
 def __init__(self, config_path=my_config.CONFIG_PATH):
     self._contract_handler = ContractHandler('RecordHash', config_path)
     self._w3 = self._contract_handler.get_w3()
     self._contract_inst = self._contract_handler.get_contract()
class ProvedDBOnChainHandler():
    def __init__(self, config_path=my_config.CONFIG_PATH):
        self._contract_handler = ContractHandler('ProvedDB', config_path)
        self._w3 = self._contract_handler.get_w3()
        self._contract_inst = self._contract_handler.get_contract()

    def hash_entry(self, input_vals):
        return calculate_entry_hash(input_vals)

    def create(self, key, val):
        print('==== create start ====')
        tx_hash = self._contract_inst.functions.Create(str(key), str(val)) \
                                     .transact({'from': self._w3.eth.accounts[0],
                                                'gas': my_config.GAS_SPENT})

        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')

        print('==== create finish ====')

    def update(self, key, val):
        print('==== update start ====')
        tx_hash = self._contract_inst.functions.Update(str(key), str(val)) \
                                               .transact({'from': self._w3.eth.accounts[0],
                                                          'gas': my_config.GAS_SPENT})

        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')

        print('==== update finish ====')

    def retrieve(self, key):
        print('==== retrieve start ====')

        exist, data = self._contract_inst.functions.Retrieve(str(key)).call()
        print('==== retrieve end ====')
        return (exist, Web3.toHex(data))

    def delete(self, key):
        print('==== deletestart ====')
        tx_hash = self._contract_inst.functions.Delete(str(key)) \
                                               .transact({'from': self._w3.eth.accounts[0],
                                                          'gas': my_config.GAS_SPENT})

        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')

        print('==== delete finish ====')

    def check_entry(self, key, val):
        print('==== check_entry start ====')
        ret = self._contract_inst.functions.CheckEntry(str(key),
                                                       str(val)).call()
        print('==== check_entry end ====')
        return ret

    def get_keys_length(self):
        print('==== get_keys_length start ====')
        ret = self._contract_inst.functions.GetKeysLength().call()
        print('==== get_keys_length end ====')
        return ret

    def get_key(self, idx):
        print('==== get_key start ====')
        ret = self._contract_inst.functions.GetKey(idx).call()
        print('==== get_key end ====')
        return ret

    def get_finalise_entries_length(self, hash_sum):
        print('==== get_finalise_entries_length start ====')
        hash_arg = convert_to_bytes(hash_sum)
        existed, finalised, length = self._contract_inst.functions.GetFinaliseEntriesLength(
            hash_arg).call()
        print('==== get_finalise_entries_length end ====')
        return existed, finalised, length

    def get_finalise_entry(self, hash_sum, idx):
        print('==== get_finalise_entries_length start ====')
        hash_arg = convert_to_bytes(hash_sum)
        ret = self._contract_inst.functions.GetFinaliseEntry(hash_arg,
                                                             idx).call()
        print('==== get_finalise_entries_length end ====')
        return Web3.toHex(ret)

    def finalise(self, hash_sum):
        print('==== finalise start ====')
        hash_arg = convert_to_bytes(hash_sum)
        tx_hash = self._contract_inst.functions.Finalise(hash_arg) \
                                               .transact({'from': self._w3.eth.accounts[0],
                                                         'gas': my_config.GAS_SPENT})

        wait_miner(self._w3, tx_hash)
        if check_transaction_meet_assert(self._w3, tx_hash):
            raise IOError('assert encounter..')

        print('==== dfinalise finish ====')

    def get_finalised_group_entries_length(self, hash_sum):
        print('==== get_finalised_group_entries_length start ====')
        hash_arg = convert_to_bytes(hash_sum)
        existed, length = self._contract_inst.functions.GetFinalisedGroupEntriesLength(
            hash_arg).call()
        print('==== get_finalised_group_entries_length end ====')
        return existed, length

    def get_finalised_group_entry(self, hash_sum, idx):
        print('==== get_finalised_group_entry start ====')
        hash_arg = convert_to_bytes(hash_sum)
        ret = self._contract_inst.functions.GetFinalisedGroupEntry(
            hash_arg, idx).call()
        print('==== get_finalised_group_entry end ====')
        return Web3.toHex(ret)