Пример #1
0
    def __init__(self, root: SimpleBlock, db: Database, p2p: Network):
        self._db = db
        self._p2p = p2p
        self.root = root

        self._state = State(self._db)

        self._orphans = Orphanage(self._db)
        self.current_node_hashes = RedisSet(db, 'all_nodes')
        self._block_index = RedisHashMap(db, 'block_index', int, SimpleBlock)
        self._block_heights = RedisHashMap(db, 'block_heights', int, int)
        self._heights = RedisHashMap(db, 'heights', int)
        self._initialized = RedisFlag(db, 'initialized')

        self.head = self._get_top_block()

        self._seeker = Seeker(
            self, self._p2p
        )  # format: (total_work, block_hash) - get early blocks first
        self.currently_seeking = set()

        # todo: temp till primary chain is done in redis so queries are quick
        self._primary_chain = PrimaryChain(self._db, 'primary_chain')

        if not self._initialized.is_true:
            self._first_initialize()
            self._initialized.set_true()
Пример #2
0
	def runTest(self):

		s = Seeker('TS', 'test/unit_test.xml', 0)

		dct = s.clause_types()

		hand_dict = {'2185541': ['o:e:t:-:o:-:t:f:f', 'v:p:-:s:-:-:-:f:f', 'v:e:n:-:o:-:f:f:t']}

		for key in dct:
			self.assertEqual(dct[key], hand_dict[key])
Пример #3
0
	def runTest(self):

		s = Seeker('TS', 'test/test.xml', 0)

		dct = s.classify_discontinuous('OBJ|SBJ')

		hand_dict = {'yxxv': 0, 'xxv': 2, 'yxvx': 2, 'xvx': 2, 'vxx': 0}

		for key in dct:
			try:
				self.assertEqual(dct[key], hand_dict[key])
			except AssertionError:
				print "Generated " + key + " " + str(dct[key]) + " does not equal hand-calculated " + str(hand_dict[key] ) + "."
Пример #4
0
    def __init__(self, root: SimpleBlock, db: Database, p2p: Network):
        self._db = db
        self._p2p = p2p
        self.root = root

        self._state = State(self._db)

        self._orphans = Orphanage(self._db)
        self.current_node_hashes = RedisSet(db, 'all_nodes')
        self._block_index = RedisHashMap(db, 'block_index', int, SimpleBlock)
        self._block_heights = RedisHashMap(db, 'block_heights', int, int)
        self._heights = RedisHashMap(db, 'heights', int)
        self._initialized = RedisFlag(db, 'initialized')

        self.head = self._get_top_block()

        self._seeker = Seeker(self, self._p2p)  # format: (total_work, block_hash) - get early blocks first
        self.currently_seeking = set()

        # todo: temp till primary chain is done in redis so queries are quick
        self._primary_chain = PrimaryChain(self._db, 'primary_chain')

        if not self._initialized.is_true:
            self._first_initialize()
            self._initialized.set_true()
Пример #5
0
 def __init__(self, model):
     self.S = [Seeker(config.n, i) for i in range(config.pop_size)]
     self.g_best = None
     self.g_best_y = None
     self.model = model
     self.h=[]
     self.u_p=[]
Пример #6
0
	def runTest(self):

		s = Seeker('TS', 'test/unit_test.xml', 0)

		hand_dict = {
		'1': [[1,7,8,9,10,11,12,13,14,15,16,17,18],'1','3','OBJ','n-s---ma-','a)nh/r1',False],
		'2': [[2],'2','3','OBJ','p-s---md-','e)gw/1',True],
		'3': [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18],'3','0','PRED','v2spma---','e)ne/pw1',True],
		'4': [[4],'4','5','AuxX','u--------','comma1',True],
		'5': [[4,5,6],'5','3','ExD','n-s---fv-','*mou=sa1',True],
		'6': [[6],'6','5','AuxX','u--------','comma1',True],
		'7': [[7],'7','1','ATR','a-s---ma-','polu/tropos1',True],
		'8': [[8],'8','12','AuxX','u--------','comma1',True],
		'9': [[9],'9','12','SBJ','p-s---mn-','o(/s1',True],
		'10': [[10],'10','11','AuxZ','d--------','ma/la1',True],
		'11': [[10,11],'11','12','ADV','d--------','polu/s',True],
		'12': [[8,9,10,11,12,13,14,15,16,17,18],'12','1','ATR','v3saip---','pla/zw1',True],
		'13': [[13],'13','14','AuxX','u--------','comma1',True],
		'14': [[13,14,15,16,17,18],'14','12','AuxC','c--------','e)pei/1',True],
		'15': [[15],'15','17','ATR','n-s---fg-','*troi/a1',True],
		'16': [[16],'16','17','ATR','a-s---na-','i(ero/s1',True],
		'17': [[15,16,17],'17','18','OBJ','n-s---na-','ptoli/eqron1',True],
		'18': [[15,16,17,18],'18','14','ADV','v3saia---','pe/rqw1',True],
		'19': [[19],'19','0','AuxK','u--------','punc1',True]
		}

		for tree in s.trees.values():
			for key in tree.list_form:
				self.assertEqual(tree.list_form[key], hand_dict[key])
 def __init_players(self):
     seeker_coord, hiders_coords = self.__get_agent_coord()
     # self.__hiders = ([Hider(self.__map, self.__n, self.__m, self.__range_hide, hider_coord, seeker_coord, self.__obs)
     #     for hider_coord in hiders_coords])
     self.__obs_sign_to_hider = [None] * len(hiders_coords)
     self.__hider_status = [True] * len(hiders_coords)
     self.__hiders = ([
         Hider(self.__map, self.__n, self.__m, self.__range_hide,
               (hiders_coords[id_hider][0], hiders_coords[id_hider][1]),
               seeker_coord, self.__obs, self.__obs_sign_to_hider,
               self.__need_obs, self.__hide_place, self.__hider_status,
               self.__obs_to_cell, self.__is_generate_path, id_hider)
         for id_hider in range(len(hiders_coords))
     ])
     self.__seeker = Seeker(self.__map, self.__n, self.__m,
                            self.__range_seek, seeker_coord, self.__obs)
     self.__seeker.update_num_hiders(self.__num_hiders)
Пример #8
0
    def __init_players(self):
        self.__hiders = []
        hiders_coors = []
        seeker_coor = None
        for i in range(self.__n):
            for j in range(self.__m):
                if self.__map[i][j] == Config.SEEKER:
                    seeker_coor = i, j
                    self.__seeker = Seeker(self.__map, self.__n, self.__m,
                                           self.__range_seek, seeker_coor)
                elif self.__map[i][j] == Config.HIDER:
                    hiders_coors.append((i, j))
                    self.__num_hiders += 1

        for i in range(self.__num_hiders):
            self.__hiders.append(
                Hider(self.__map, self.__n, self.__m, self.__range_hide,
                      hiders_coors[i], seeker_coor))

        self.__seeker.update_num_hiders(self.__num_hiders)
Пример #9
0
class Chain:
    def __init__(self, root: SimpleBlock, db: Database, p2p: Network):
        self._db = db
        self._p2p = p2p
        self.root = root

        self._state = State(self._db)

        self._orphans = Orphanage(self._db)
        self.current_node_hashes = RedisSet(db, 'all_nodes')
        self._block_index = RedisHashMap(db, 'block_index', int, SimpleBlock)
        self._block_heights = RedisHashMap(db, 'block_heights', int, int)
        self._heights = RedisHashMap(db, 'heights', int)
        self._initialized = RedisFlag(db, 'initialized')

        self.head = self._get_top_block()

        self._seeker = Seeker(self, self._p2p)  # format: (total_work, block_hash) - get early blocks first
        self.currently_seeking = set()

        # todo: temp till primary chain is done in redis so queries are quick
        self._primary_chain = PrimaryChain(self._db, 'primary_chain')

        if not self._initialized.is_true:
            self._first_initialize()
            self._initialized.set_true()

    def _first_initialize(self):
        self._heights[0] = self.root.hash
        self._block_heights[self.root.hash] = 0
        self._block_index[self.root.hash] = self.root
        self.current_node_hashes.add(self.root.hash)
        self._state.reset()
        self._apply_to_state(self.root)
        self._primary_chain.append_hashes([self.root.hash])

    def _back_up_state(self, backup_path):
        self._state.backup_to(backup_path)

    def _restore_backed_up_state(self, backup_path):
        self._state.restore_backup_from(backup_path)

    @property
    def primary_chain(self):
        return self._primary_chain.get_all()

    def _get_top_block(self):
        tb_hash = self._db.get_kv(TOP_BLOCK, int)
        if tb_hash is None:
            self._set_top_block(self.root)
            return self.root
        return self._block_index[tb_hash]

    def _set_top_block(self, top_block):
        return self._db.set_kv(TOP_BLOCK, top_block.hash)

    def seek_blocks(self, block_hashes):
        self._seeker.put(*[h for h in block_hashes if not self.has_block(h)])

    def seek_blocks(self, block_hashes):
        self._seeker.put(*[h for h in block_hashes if not self.has_block(h)])

    def seek_blocks_with_total_work(self, pairs):
        self._seeker.put_with_work(*pairs)

    def height_of_block(self, block_hash):
        return self._block_heights[block_hash]

    def has_block(self, block_hash):
        return block_hash in self.current_node_hashes or self._orphans.contains_block_hash(block_hash)

    def contains_block(self, block_hash):
        return block_hash in self.current_node_hashes

    def get_block(self, block_hash):
        return self._block_index[block_hash]

    def add_blocks(self, blocks):
        # todo : design some better sorting logic.
        # we should check if orphan chains match up with what we've added, if so add the orphan chain.
        rejects = []
        # todo: major bug - if blocks are added in the order [good, good, bad], say, and blocks 1 and 2 cause a reorg
        # then when block 3 causes an exception the state will revert but the head is still on block 2, which doesn't
        # match the state.  - I think this is fixed now
        some_path = str(random.randint(1000,1000000))
        self._back_up_state(some_path)

        total_works = [(b.total_work, b) for b in blocks]
        total_works.sort()

        most_recent_block = None

        try:
            while True:
                if len(total_works) == 0:
                    break
                tw, block = total_works.pop(0)
                most_recent_block = block
                r = self._add_block(block)
                if isinstance(r, list):
                    total_works.extend([(b.total_work, b) for b in r])
                elif isinstance(r, Encodium):
                    rejects.append(r)
            print('rejects', rejects)
            for r in rejects:
                self._orphans.add(r)
        except Exception as e:
            self._restore_backed_up_state(some_path)
            traceback.print_exc()
            print('EXCEPTION CAPTURED WHILE ADDING BLOCK', most_recent_block.to_json())

    def _add_block(self, block: SimpleBlock):
        """
        :param block: QuantaBlock instance
        :return: None on success, block if parent missing
        """
        print('_add_block', block.hash)
        if block.hash in self.current_node_hashes: return None
        if not block.acceptable_work: raise InvalidBlockException('Unacceptable work')
        if not all_true(self.contains_block, block.links):
            print('Rejecting block: don\'t have all links')
            # don't just look for children, get a primary chain
            self.seek_blocks({i for i in block.links if not self._orphans.contains_block_hash(i)})
            return block

        # success, lets add it
        self._update_metadata(block)
        block.set_block_index(self._block_index)
        if self.better_than_head(block):
            print('COINBASE _add_blk', block.coinbase)
            self._reorganize_to(block)
        self.current_node_hashes.add(block.hash)
        self._block_index[block.hash] = block
        print("Chain._add_block - processed", block.hash)
        orphaned_children = self._orphans.children_of(block.hash)
        self._orphans.remove(block)
        if len(orphaned_children) > 0:
            print([self._orphans.get(h) for h in orphaned_children])
            return [self._orphans.get(h) for h in orphaned_children]
        self._orphans.remove(block)
        return None

    def _set_height_metadata(self, block):
        height = self._block_heights[block.links[0]] + 1
        self._block_heights[block.hash] = height
        self._heights[height] = block.hash

    def _update_metadata(self, block):
        self._set_height_metadata(block)

    def _mass_primary_chain_apply(self, path):
        self._primary_chain.append_hashes([b.hash for b in path])

    def _mass_primary_chain_unapply(self, path):
        self._primary_chain.remove_hashes([b.hash for b in path])

    def _reorganize_to(self, block):
        print('reorg from %064x\nto         %064x\nheight of  %d' % (self.head.hash, block.hash, self._block_heights[block.hash]))
        pivot = self.find_pivot(self.head, block)
        unapply_path = self.order_from(pivot, self.head)
        self._mass_unapply(unapply_path)
        self._mass_primary_chain_unapply(unapply_path)
        print('COINBASE _re_org_', block.coinbase)
        apply_path = self.order_from(pivot, block)
        self._mass_apply(apply_path)
        self._mass_primary_chain_apply(apply_path)
        print('Current State')
        pp(self._state.full_state())
        self.head = block
        self._set_top_block(self.head)

    # Coin & State methods

    def get_next_state_hash(self, block):
        with self._state.lock:
            state_hash = self._get_next_state_hash_not_threadsafe(block)
        return state_hash

    def _get_next_state_hash_not_threadsafe(self, block):
        temp_path = str(random.randint(1000,1000000))
        self._back_up_state(temp_path)
        self._modify_state(block, 1)
        state_hash = self._state.hash
        self._restore_backed_up_state(temp_path)
        return state_hash

    def _valid_for_state(self, block):
        state_hash = self._get_next_state_hash_not_threadsafe(block)
        assert_equal(block.state_hash, state_hash)
        if block.tx is not None:
            assert self._state.get(block.tx.signature.pub_x) >= block.tx.total
        return True

    def _apply_to_state(self, block):
        with self._state.lock:
            print('COINBASE _aply_st', block.coinbase)
            assert self._valid_for_state(block)
            assert self._valid_for_state(block)
            self._modify_state(block, 1)

    def _unapply_to_state(self, block):
        self._modify_state(block, -1)

    def _modify_state(self, block, direction):
        assert direction in [-1, 1]
        if block.tx is not None:
            self._state.modify_balance(block.tx.recipient, direction * block.tx.value)
            self._state.modify_balance(block.tx.signature.pub_x, -1 * direction * block.tx.value)
        self._state.modify_balance(block.coinbase, direction * block.coins_generated)

    def _mass_unapply(self, path):
        for block in path[::-1]:
            self._unapply_to_state(block)

    def _mass_apply(self, path):
        print(path)
        for block in path:
            print('COINBASE _ms_aply', block.coinbase)
            self._apply_to_state(block)
            if block in self._orphans:
                self._orphans.remove(block)

    def better_than_head(self, block):
        return block.total_work > self.head.total_work

    def make_block_locator(self):
        locator = []

        h = self._block_heights[self.head.hash]
        print(h, self.head.hash)
        i = 0
        c = 0
        while h - c >= 0:
            locator.append(self.primary_chain[h - c])
            c = 2**i
            i += 1

        return locator

    def _order_from_alpha(self, early_node, late_node):
        path = []
        print(early_node.hash)
        while early_node.hash != late_node.hash:
            path = [late_node] + path
            if late_node.is_root:
                if early_node.is_root:
                    return path
                raise Exception("Root block encountered unexpectedly while ordering graph")
            late_node = self.get_block(late_node.links[0])
            #print('new_late_node')
            #print(late_node.hash)
        return path

    def _order_from_beta(self, early_node, late_node):
        pass

    def order_from(self, early_node: SimpleBlock, late_node: SimpleBlock):
        return self._order_from_alpha(early_node, late_node)

    def find_pivot(self, b1: SimpleBlock, b2: SimpleBlock):
        while b1.hash != b2.hash:
            if b1.total_work >= b2.total_work:
                b1 = self.get_block(b1.links[0])
            else:
                b2 = self.get_block(b2.links[0])
        return b1 if b1 == b2 else None
Пример #10
0
class Chain:
    def __init__(self, root: SimpleBlock, db: Database, p2p: Network):
        self._db = db
        self._p2p = p2p
        self.root = root

        self._state = State(self._db)

        self._orphans = Orphanage(self._db)
        self.current_node_hashes = RedisSet(db, 'all_nodes')
        self._block_index = RedisHashMap(db, 'block_index', int, SimpleBlock)
        self._block_heights = RedisHashMap(db, 'block_heights', int, int)
        self._heights = RedisHashMap(db, 'heights', int)
        self._initialized = RedisFlag(db, 'initialized')

        self.head = self._get_top_block()

        self._seeker = Seeker(
            self, self._p2p
        )  # format: (total_work, block_hash) - get early blocks first
        self.currently_seeking = set()

        # todo: temp till primary chain is done in redis so queries are quick
        self._primary_chain = PrimaryChain(self._db, 'primary_chain')

        if not self._initialized.is_true:
            self._first_initialize()
            self._initialized.set_true()

    def _first_initialize(self):
        self._heights[0] = self.root.hash
        self._block_heights[self.root.hash] = 0
        self._block_index[self.root.hash] = self.root
        self.current_node_hashes.add(self.root.hash)
        self._state.reset()
        self._apply_to_state(self.root)
        self._primary_chain.append_hashes([self.root.hash])

    def _back_up_state(self, backup_path):
        self._state.backup_to(backup_path)

    def _restore_backed_up_state(self, backup_path):
        self._state.restore_backup_from(backup_path)

    @property
    def primary_chain(self):
        return self._primary_chain.get_all()

    def _get_top_block(self):
        tb_hash = self._db.get_kv(TOP_BLOCK, int)
        if tb_hash is None:
            self._set_top_block(self.root)
            return self.root
        return self._block_index[tb_hash]

    def _set_top_block(self, top_block):
        return self._db.set_kv(TOP_BLOCK, top_block.hash)

    def seek_blocks(self, block_hashes):
        self._seeker.put(*[h for h in block_hashes if not self.has_block(h)])

    def seek_blocks(self, block_hashes):
        self._seeker.put(*[h for h in block_hashes if not self.has_block(h)])

    def seek_blocks_with_total_work(self, pairs):
        self._seeker.put_with_work(*pairs)

    def height_of_block(self, block_hash):
        return self._block_heights[block_hash]

    def has_block(self, block_hash):
        return block_hash in self.current_node_hashes or self._orphans.contains_block_hash(
            block_hash)

    def contains_block(self, block_hash):
        return block_hash in self.current_node_hashes

    def get_block(self, block_hash):
        return self._block_index[block_hash]

    def add_blocks(self, blocks):
        # todo : design some better sorting logic.
        # we should check if orphan chains match up with what we've added, if so add the orphan chain.
        rejects = []
        # todo: major bug - if blocks are added in the order [good, good, bad], say, and blocks 1 and 2 cause a reorg
        # then when block 3 causes an exception the state will revert but the head is still on block 2, which doesn't
        # match the state.  - I think this is fixed now
        some_path = str(random.randint(1000, 1000000))
        self._back_up_state(some_path)

        total_works = [(b.total_work, b) for b in blocks]
        total_works.sort()

        most_recent_block = None

        try:
            while True:
                if len(total_works) == 0:
                    break
                tw, block = total_works.pop(0)
                most_recent_block = block
                r = self._add_block(block)
                if isinstance(r, list):
                    total_works.extend([(b.total_work, b) for b in r])
                elif isinstance(r, Encodium):
                    rejects.append(r)
            print('rejects', rejects)
            for r in rejects:
                self._orphans.add(r)
        except Exception as e:
            self._restore_backed_up_state(some_path)
            traceback.print_exc()
            print('EXCEPTION CAPTURED WHILE ADDING BLOCK',
                  most_recent_block.to_json())

    def _add_block(self, block: SimpleBlock):
        """
        :param block: QuantaBlock instance
        :return: None on success, block if parent missing
        """
        print('_add_block', block.hash)
        if block.hash in self.current_node_hashes: return None
        if not block.acceptable_work:
            raise InvalidBlockException('Unacceptable work')
        if not all_true(self.contains_block, block.links):
            print('Rejecting block: don\'t have all links')
            # don't just look for children, get a primary chain
            self.seek_blocks({
                i
                for i in block.links
                if not self._orphans.contains_block_hash(i)
            })
            return block

        # success, lets add it
        self._update_metadata(block)
        block.set_block_index(self._block_index)
        if self.better_than_head(block):
            print('COINBASE _add_blk', block.coinbase)
            self._reorganize_to(block)
        self.current_node_hashes.add(block.hash)
        self._block_index[block.hash] = block
        print("Chain._add_block - processed", block.hash)
        orphaned_children = self._orphans.children_of(block.hash)
        self._orphans.remove(block)
        if len(orphaned_children) > 0:
            print([self._orphans.get(h) for h in orphaned_children])
            return [self._orphans.get(h) for h in orphaned_children]
        self._orphans.remove(block)
        return None

    def _set_height_metadata(self, block):
        height = self._block_heights[block.links[0]] + 1
        self._block_heights[block.hash] = height
        self._heights[height] = block.hash

    def _update_metadata(self, block):
        self._set_height_metadata(block)

    def _mass_primary_chain_apply(self, path):
        self._primary_chain.append_hashes([b.hash for b in path])

    def _mass_primary_chain_unapply(self, path):
        self._primary_chain.remove_hashes([b.hash for b in path])

    def _reorganize_to(self, block):
        print('reorg from %064x\nto         %064x\nheight of  %d' %
              (self.head.hash, block.hash, self._block_heights[block.hash]))
        pivot = self.find_pivot(self.head, block)
        unapply_path = self.order_from(pivot, self.head)
        self._mass_unapply(unapply_path)
        self._mass_primary_chain_unapply(unapply_path)
        print('COINBASE _re_org_', block.coinbase)
        apply_path = self.order_from(pivot, block)
        self._mass_apply(apply_path)
        self._mass_primary_chain_apply(apply_path)
        print('Current State')
        pp(self._state.full_state())
        self.head = block
        self._set_top_block(self.head)

    # Coin & State methods

    def get_next_state_hash(self, block):
        with self._state.lock:
            state_hash = self._get_next_state_hash_not_threadsafe(block)
        return state_hash

    def _get_next_state_hash_not_threadsafe(self, block):
        temp_path = str(random.randint(1000, 1000000))
        self._back_up_state(temp_path)
        self._modify_state(block, 1)
        state_hash = self._state.hash
        self._restore_backed_up_state(temp_path)
        return state_hash

    def _valid_for_state(self, block):
        state_hash = self._get_next_state_hash_not_threadsafe(block)
        assert_equal(block.state_hash, state_hash)
        if block.tx is not None:
            assert self._state.get(block.tx.signature.pub_x) >= block.tx.total
        return True

    def _apply_to_state(self, block):
        with self._state.lock:
            print('COINBASE _aply_st', block.coinbase)
            assert self._valid_for_state(block)
            assert self._valid_for_state(block)
            self._modify_state(block, 1)

    def _unapply_to_state(self, block):
        self._modify_state(block, -1)

    def _modify_state(self, block, direction):
        assert direction in [-1, 1]
        if block.tx is not None:
            self._state.modify_balance(block.tx.recipient,
                                       direction * block.tx.value)
            self._state.modify_balance(block.tx.signature.pub_x,
                                       -1 * direction * block.tx.value)
        self._state.modify_balance(block.coinbase,
                                   direction * block.coins_generated)

    def _mass_unapply(self, path):
        for block in path[::-1]:
            self._unapply_to_state(block)

    def _mass_apply(self, path):
        print(path)
        for block in path:
            print('COINBASE _ms_aply', block.coinbase)
            self._apply_to_state(block)
            if block in self._orphans:
                self._orphans.remove(block)

    def better_than_head(self, block):
        return block.total_work > self.head.total_work

    def make_block_locator(self):
        locator = []

        h = self._block_heights[self.head.hash]
        print(h, self.head.hash)
        i = 0
        c = 0
        while h - c >= 0:
            locator.append(self.primary_chain[h - c])
            c = 2**i
            i += 1

        return locator

    def _order_from_alpha(self, early_node, late_node):
        path = []
        print(early_node.hash)
        while early_node.hash != late_node.hash:
            path = [late_node] + path
            if late_node.is_root:
                if early_node.is_root:
                    return path
                raise Exception(
                    "Root block encountered unexpectedly while ordering graph")
            late_node = self.get_block(late_node.links[0])
            #print('new_late_node')
            #print(late_node.hash)
        return path

    def _order_from_beta(self, early_node, late_node):
        pass

    def order_from(self, early_node: SimpleBlock, late_node: SimpleBlock):
        return self._order_from_alpha(early_node, late_node)

    def find_pivot(self, b1: SimpleBlock, b2: SimpleBlock):
        while b1.hash != b2.hash:
            if b1.total_work >= b2.total_work:
                b1 = self.get_block(b1.links[0])
            else:
                b2 = self.get_block(b2.links[0])
        return b1 if b1 == b2 else None
Пример #11
0
# OOP and OOAD Assessment
# Karina Sudnicina
# 17.12.2020
# Quidditch v 8.0

from field import Field
from seeker import Seeker
from snitch import Snitch

# import the Pygame
import random, sys, copy, os, pygame
from pygame.locals import *

# create objects
field = Field()
s1 = Seeker('1', 4, 1)
s2 = Seeker('2', 5, 3)
sn = Snitch('@', 3, 2)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FPS = 30                    # frames per second to update the screen
WINWIDTH = 800              # width and height of the program's window
WINHEIGHT = 600
HALF_WINWIDTH = int(WINWIDTH / 2)    # place things centrally
HALF_WINHEIGHT = int(WINHEIGHT / 2)

# The total width and height of each tile in pixels.
TILEWIDTH = 32
TILEHEIGHT = 32
TILEFLOORHEIGHT = 32
Пример #12
0
    queue_dis = queue.Queue()
    queue_extrac = queue.Queue()
    queue_extraced = queue.Queue()
    queue_av = queue.Queue()
    queue_hash = queue.Queue()
    queue_hashed = queue.Queue()
    queue_ext_path = queue.Queue()
    queue_csv = queue.Queue()
    queue_csved = queue.Queue()
    queue_blk = queue.Queue()
    queue_mem = queue.Queue()
    queue_memed = queue.Queue()
    queue_rslt = queue.Queue()
    queue_elastic = queue.Queue()

    see = Seeker(queue_dis, IN_DIR, BASE_NAME, CHECK_TIME)
    dis = Dispatcher(queue_dis, queue_extrac, queue_extraced, queue_ext_path,
                     queue_av, queue_hash, queue_hashed, queue_csv,
                     queue_csved, queue_blk, queue_mem, queue_memed,
                     queue_elastic, IN_DIR, WORK_DIR, OUT_DIR, DIR_OUT)
    has = Hasher(queue_hash, queue_hashed, IN_DIR, WORK_DIR, BLOCK_SIZE_HASH)
    ext = Extractor(queue_extrac, queue_extraced, queue_ext_path, IN_DIR,
                    WORK_DIR)
    csv = Csver(queue_csv, queue_csved, WORK_DIR, OUT_DIR)
    blk = Bulker(queue_blk, queue_extraced, WORK_DIR, OUT_DIR)
    mem = Memer(queue_mem, queue_extraced, IN_DIR, WORK_DIR, OUT_DIR)
    #tim = Timeliner(queue_extrac,WORK_DIR,OUT_DIR)
    avc = Avcheck(queue_av, WORK_DIR, OUT_DIR)
    #elas = Elasticer(queue_elastic,WORK_DIR,OUT_DIR)

    see.start()
Пример #13
0
def main():
    level = 3
    #init board
    board = Board()
    catchingHider = False
    pathToCurrentHider = []

    checkingAnnounceArea = False
    pathToCurrentOptimalPoint = []
    hiderPos = []
    #init seeker
    seeker = Seeker()
    seeker.build_visitMap(mapInfo[0])
    seeker.update(mapInfo[1][0], mapInfo[0])

    #init seeker move
    seekerNewMove = [seeker.Sx, seeker.Sy]
    seekerOldMove = []
    seekerOldMove.append(seekerNewMove)

    #init hider
    hiderList = []
    for i in range(len(mapInfo[2])):
        hiderList.append(Hider())
        hiderList[i].update(mapInfo[2][i], mapInfo[0])
    numberOfHiders = len(hiderList)
    #init game
    FPS = 60
    WIN = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Map")

    running = True
    clock = pygame.time.Clock()

    #init draw
    board.draw_board(WIN, mapInfo[0])
    seeker.drawVison(WIN, mapInfo[0])
    seeker.drawSeeker(WIN)

    for i in range(len(hiderList)):
        hiderList[i].drawHider(WIN)

    pygame.display.update()
    pygame.time.wait(20)

    #start game loop
    turn_count = 0
    hider_time = 5
    while running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    pause()

        #move
        #seekerOldMove.append(seekerNewMove)
        #if len(seekerOldMove) > 4:
        #    seekerOldMove.pop(0)
        turn_count = turn_count + 1

        if level >= 3:
            for i in range(len(hiderList)):
                danger = 0
                for curX in range(hiderList[i].Sx - 2, hiderList[i].Sx + 3):
                    for curY in range(hiderList[i].Sy - 2,
                                      hiderList[i].Sy + 3):
                        if (curX < 1 or curX > ROW - 2 or curY < 1
                                or curY > COL - 2):
                            continue
                        if (hiderList[i].valueInVision(curX, curY) == 3):
                            danger = 1
                if (danger):
                    new_move = hiderList[i].run(mapInfo[0])
                    randVal = randint(0, 10000)
                    if (randVal % 2 == 0):
                        new_move = (hiderList[i].Sx, hiderList[i].Sy)
                    hiderList[i].update(new_move, mapInfo[0])

                    continue

                new_move = hiderList[i].get_goal(mapInfo[0], 1000)
                hiderList[i].update(new_move, mapInfo[0])

        if turn_count > hider_time:
            if len(seeker.hiderPositionList) == 0 and len(
                    seeker.announcePositionList) == 0:
                seekerNewMove = seeker.randomMove(mapInfo[0])

            elif len(seeker.hiderPositionList) == 0 and len(
                    seeker.announcePositionList) > 0:
                if checkingAnnounceArea == False:
                    announcePos = seeker.announcePositionList[0]
                    seeker.findAnnounceArea(mapInfo[0], announcePos)

                    optimal_point = seeker.findOptimalPoint(mapInfo[0])
                    if optimal_point is None:
                        seekerNewMove = seeker.randomMove(mapInfo[0])
                    pathToCurrentOptimalPoint = seeker.FromStartToEnd(
                        list([seeker.Sx, seeker.Sy]), optimal_point,
                        mapInfo[0])
                    checkingAnnounceArea = True
                if len(seeker.announceArea) > 0:
                    if pathToCurrentOptimalPoint is None:
                        seekerNewMove = seeker.randomMove(mapInfo[0])
                    elif len(pathToCurrentOptimalPoint) > 0:
                        seekerNewMove = pathToCurrentOptimalPoint.pop(0)
                    else:
                        optimal_point = seeker.findOptimalPoint(mapInfo[0])
                        pathToCurrentOptimalPoint = seeker.FromStartToEnd(
                            list([seeker.Sx, seeker.Sy]), optimal_point,
                            mapInfo[0])
                elif len(seeker.announceArea) == 0:
                    seeker.announcePositionList.pop(0)
                    checkingAnnounceArea = False

            elif len(seeker.hiderPositionList) > 0:
                hiderPos = seeker.hiderPositionList[0]
                if catchingHider == False:
                    pathToCurrentHider = seeker.FromStartToEnd(
                        list([seeker.Sx, seeker.Sy]), hiderPos, mapInfo[0])
                    print("hider position:", hiderPos)
                    catchingHider = True
                seekerNewMove = pathToCurrentHider.pop(0)
                if seekerNewMove[0] == hiderPos[0] and seekerNewMove[
                        1] == hiderPos[1]:
                    numberOfHiders = numberOfHiders - 1
                    catchingHider = False
                    mapInfo[0][hiderPos[0]][hiderPos[1]] = 0
                    seeker.visionScopeUpdate(mapInfo[0])
                    seeker.visibleUpdate()
                    seeker.hiderPositionList.pop(0)
                    print('need to pop: ', seekerNewMove)
                    for i in range(len(hiderList)):
                        print(hiderList[i].Sx, hiderList[i].Sy)

                    for k in range(0, len(hiderList)):
                        if hiderList[k].Sx == seekerNewMove[0] and hiderList[
                                k].Sy == seekerNewMove[1]:
                            temp = hiderList[k]
                            temp.cleanUpAnnounce(mapInfo[0])

                            hiderList.pop(k)
                            break

                    if len(hiderList) == 0:
                        print("Find all hiders, game over")
                        return

            #seekerNewMove = seeker.randomMove(mapInfo[0])
            if (seekerNewMove is None):
                seeker.build_visitMap(mapInfo[0])
                seekerNewMove = (seeker.Sx, seeker.Sy)

            seeker.update(seekerNewMove, mapInfo[0])
            seeker.updateAnnounceArea()
            seeker.findHider()

            seeker.findAnnounce()

        #draw
        #draw board
        board.draw_board(WIN, mapInfo[0])
        #draw vision
        seeker.drawVison(WIN, mapInfo[0])

        for i in range(len(hiderList)):
            hiderList[i].drawVison(WIN, mapInfo[0], seeker)

        #draw agents
        seeker.drawSeeker(WIN)
        for i in range(len(hiderList)):
            hiderList[i].drawHider(WIN)
        for i in hiderList:
            if turn_count >= 30:
                i.announce(WIN, mapInfo[0])
            i.drawAnnounce(WIN)
        #for i in seeker.vision:
        #    print(i)
        #print("X of seeker:", seeker.Sx)
        #print("Y of seeker:", seeker.Sy, "\n")
        #print hider location
        #print(" hider position list that seeker found", seeker.hiderPositionList)

        pygame.display.update()
        pygame.time.wait(30)

    pygame.quit()
Пример #14
0
    def __init__(self, account_id, private_key, provider, dev=False):
        self.plugin = PluginManager()
        self.plugin.load(PLUGINS_PATH)
        self.plugin.set_default_solverclass('gcs_solver.py')

        self.dev = dev
        self.account_id = account_id
        self.web3 = Web3(provider)
        self.interest = ''
        self.trusted_users = []
        self.web3.eth.defaultAccount = account_id

        # PoA であれば geth_poa_middleware を利用
        try:
            self.web3.eth.getBlock("latest")
        except ExtraDataLengthError:
            self.web3.middleware_onion.inject(geth_poa_middleware, layer=0)

        if private_key:
            self.web3.middleware_onion.add(
                construct_sign_and_send_raw_middleware(private_key))
        self.deploy_erc1820()

        self.__observer = None
        self.__state = None
        self.assets = None
        # Wallet の情報
        self.wallet = Wallet(self.web3, self.account_id)

        # オペレータ(トークンの交換などを担当)のコントラクト
        self.operator_address = None
        self.load_config()

        self.operator_address = self._fix_config_address(
            self.config['operator']['address'])
        if self.config['operator']['solver_pluginfile']:
            self.plugin.set_solverclass(
                self.operator_address,
                self.config['operator']['solver_pluginfile'])

        self.contracts = Contracts(self.web3)
        self.deploy_metemcyberutil()

        self.fetch_trusted_users()

        self.event_listener = BasicEventListener('')
        self.event_listener.start()

        # inventory (トークン・カタログの管理)のインスタンス生成
        catalog_address = self._fix_config_address(
            self.config['catalog']['address'])
        broker_address = self._fix_config_address(
            self.config['broker']['address'])
        self.inventory = Inventory(self.contracts, self.account_id,
                                   self.event_listener, catalog_address,
                                   broker_address)

        # Seeker (チャレンジの依頼者)のインスタンス
        self.seeker = Seeker(self.contracts)

        # Solver (チャレンジの受領者)としてのインスタンス
        if self.operator_address:
            solverclass = self.plugin.get_solverclass(self.operator_address)
            self.solver = solverclass(self.contracts, self.account_id,
                                      self.operator_address)
        else:
            self.solver = None

        # MISP設定のinsert
        self.load_misp_config(MISP_INI_FILEPATH)
Пример #15
0
            content = "media"
        elif args.markdown:
            content = "markdown"
        elif args.broadcast:
            content = "broadcast"
        elif args.object:
            content = "file"
        elif args.delete:
            content = "shred"
        else:
            content = "tag-search"

        Publisher().process(content, args.file)

    elif args.mode == 'seeker':
        seeker = Seeker()

        if args.search:
            if not (args.search_tag or args.search_name or args.search_hash):
                print(
                    colored.red(
                        "[!] Atleast one of tag, name or file hash is require for search"
                    ))
                exit(0)
            data = {}
            if args.search_hash:
                data["hash"] = args.search_hash
            else:
                data["hash"] = None

            if args.search_name:
class Game:
    def __init__(self, gui, is_debug):
        self.__map = self.__n = self.__m = None
        self.__range_seek = Config.RANGE_SEEKER
        self.__range_hide = Config.RANGE_HIDER
        self.__gui = gui
        self.__num_hiders = 0
        self.__hiders = []
        self.__obs = []
        self.__obs_sign_to_hider = [None]
        self.__need_obs = []
        self.__obs_to_cell = []
        self.__hide_place = [None, None]
        self.__hider_status = []
        self.__is_generate_path = [False]

    def read_input(self, map_name, is_debug):
        fin = open("../map/" + map_name + ".txt", "r")
        self.__n, self.__m = [int(x) for x in fin.readline().split(" ")]
        self.__read_map(fin)
        self.__read_obstacles(fin)
        if not is_debug:
            self.__gui.read_config(self.__map)
        self.__init_players()
        fin.close()

    def __get_agent_coord(self):
        seeker_coord, hider_coords = None, []
        for i in range(self.__n):
            for j in range(self.__m):
                if self.__map[i][j] == Config.SEEKER:
                    seeker_coord = (i, j)
                elif self.__map[i][j] == Config.HIDER:
                    hider_coords.append((i, j))
                    self.__num_hiders += 1
        return seeker_coord, hider_coords

    def __init_players(self):
        seeker_coord, hiders_coords = self.__get_agent_coord()
        # self.__hiders = ([Hider(self.__map, self.__n, self.__m, self.__range_hide, hider_coord, seeker_coord, self.__obs)
        #     for hider_coord in hiders_coords])
        self.__obs_sign_to_hider = [None] * len(hiders_coords)
        self.__hider_status = [True] * len(hiders_coords)
        self.__hiders = ([
            Hider(self.__map, self.__n, self.__m, self.__range_hide,
                  (hiders_coords[id_hider][0], hiders_coords[id_hider][1]),
                  seeker_coord, self.__obs, self.__obs_sign_to_hider,
                  self.__need_obs, self.__hide_place, self.__hider_status,
                  self.__obs_to_cell, self.__is_generate_path, id_hider)
            for id_hider in range(len(hiders_coords))
        ])
        self.__seeker = Seeker(self.__map, self.__n, self.__m,
                               self.__range_seek, seeker_coord, self.__obs)
        self.__seeker.update_num_hiders(self.__num_hiders)

    def __read_map(self, fin):
        self.__map = [[int(x) for x in fin.readline().split(" ")]
                      for i in range(self.__n)]

    def __read_obstacles(self, fin):
        line = fin.readline()
        while (line != ""):
            x_tl, y_tl, x_br, y_br = [int(x) for x in line.split(" ")]
            new_obs = []
            for i in range(x_tl, x_br + 1):
                for j in range(y_tl, y_br + 1):
                    self.__map[i][j] = Config.OBS
                    new_obs.append((i, j))
            self.__obs.append(new_obs)
            line = fin.readline()

    def __is_seeker_turn(self):
        return self.__turn % (self.__num_hiders + 1) == 1

    def __is_turn_of_hider_number(self):
        return (self.__turn %
                (self.__num_hiders + 1) - 2) % (self.__num_hiders + 1)

    def __compute_seeker_turn(self):
        return (self.__turn - 1) // (self.__num_hiders + 1) + 1

    def __compute_hider_turn(self, index):
        res = (self.__turn - (index + 2)) // (self.__num_hiders + 1) + 1
        return res

    def __hiders_found(self):
        for i in range(len(self.__hiders)):
            if self.__hiders[i] != None:
                return False
        return True

    def make_seeker_move(self):
        x, y = self.__seeker.move(self.__compute_seeker_turn(), self.__hiders)
        # if (self.__seeker.cur_x, self.__seeker.cur_y) == (5, 3):
        #     print("YAFSDFADFAF")
        self.__point -= int(x != 0 or y != 0)
        self.__map[self.__seeker.cur_x - x][self.__seeker.cur_y -
                                            y] = Config.EMPTY
        self.__map[self.__seeker.cur_x][self.__seeker.cur_y] = Config.SEEKER
        for i in range(len(self.__hiders)):
            if self.__hiders[i] != None:
                self.__hiders[i].map[self.__seeker.cur_x -
                                     x][self.__seeker.cur_y - y] = Config.EMPTY
                self.__hiders[i].map[self.__seeker.cur_x][
                    self.__seeker.cur_y] = Config.SEEKER

    def make_hider_move(self, is_debug):
        index_hider_move = self.__is_turn_of_hider_number()
        current_hider = self.__hiders[index_hider_move]
        if current_hider != None:
            x, y = current_hider.move(
                self.__compute_hider_turn(index_hider_move))
            # print(f/"hider {index_hider_move}: {self.__hiders[index_hider_move].cur_x} {self.__hiders[index_hider_move].cur_y}")
            self.__seeker.update_hider_pos(current_hider.cur_x,
                                           current_hider.cur_y, x, y)
            if not self.overlap_hider(
                    self.__hiders[index_hider_move].cur_x - x,
                    self.__hiders[index_hider_move].cur_y - y,
                    index_hider_move):
                self.__map[self.__hiders[index_hider_move].cur_x -
                           x][self.__hiders[index_hider_move].cur_y -
                              y] = Config.EMPTY
            self.__map[self.__hiders[index_hider_move].cur_x][
                self.__hiders[index_hider_move].cur_y] = Config.HIDER
            if current_hider.should_announced(
                    self.__compute_hider_turn(index_hider_move)):
                x, y = current_hider.announce()
                self.__seeker.signal_announce(x, y)
                if not is_debug:
                    self.__gui.send_signal_announce((x, y), self.__turn)

    def __is_time_out(self):
        return self.__turn >= (self.__num_hiders + 1) * (self.__n * self.__m +
                                                         Config.PREGAME_TURN)

    def operate(self, is_debug):
        self.__turn, self.__point = (1, 0)
        self.__winner = Config.HIDER
        message = ""
        while True:
            if self.__hiders_found() and (self.__turn -
                                          1) % (self.__num_hiders + 1) == 1:
                self.__winner = Config.SEEKER
                break
            if self.__is_time_out():
                message = "Time out"
                break
            # if self.__seeker.visited_all():
            #     message = "Seeker gives up"
            #     break
            if self.__is_seeker_turn():
                self.make_seeker_move()
                # print(f"seeker {self.__seeker.cur_x} {self.__seeker.cur_y}")
            else:
                self.make_hider_move(is_debug)
            self.update_game_map()
            self.update_game_info(is_debug)
        if not is_debug:
            self.__gui.visualize()
        if (self.__winner == Config.SEEKER):
            print("Seeker win")
        else:
            print(message + ", hiders win")
        print("Point: {:d}".format(self.__point))
        print("Turns taken: {}".format(self.__turn))

    def update_game_map(self):
        for i in range(self.__n):
            for j in range(self.__m):
                if self.__map[i][j] == Config.OBS:
                    self.__map[i][j] = Config.EMPTY
        for obs in self.__obs:
            for x, y in obs:
                self.__map[x][y] = Config.OBS
        for hider in self.__hiders:
            if hider != None:
                self.__map[hider.cur_x][hider.cur_y] = Config.HIDER
        self.__map[self.__seeker.cur_x][self.__seeker.cur_y] = Config.SEEKER

    def obs_push(self, obs_id, direction):
        x, y = direction
        for ox, oy in self.__obs[obs_id]:
            nx, ny = ox + x, oy + y
            if self.__map[nx][ny] in [
                    Config.WALL, Config.OBS, Config.SEEKER, Config.HIDER
            ]:
                return False  # not pushable
        for ox, oy in self.__obs[obs_id]:
            nx, ny = ox + x, oy + y
            self.__map[ox][oy] = Config.EMPTY
            self.__map[nx][ny] = Config.OBS
            self.__obs[obs_id] = (nx, ny)
        return True

    def update_game_info(self, is_debug):
        if not is_debug:
            self.__gui.update_map(self.__map)
        self.__turn += 1
        self.__check_met_hider(is_debug)

    def __notify_hiders(self):
        for hider in self.__hiders:
            if hider == None:
                continue
            is_regconized = False
            for hider_x, hider_y in self.__seeker.list_notify:
                if hider.cur_x == hider_x and hider.cur_y == hider_y:
                    is_regconized = True
                    hider.update_seeker_pos(self.__seeker.cur_x,
                                            self.__seeker.cur_y)
            hider.is_regconized = is_regconized

    def overlap_hider(self, i, j, index):
        for k in range(len(self.__hiders)):
            if self.__hiders[k] != None:
                if k != index:
                    if (self.__hiders[k].cur_x, self.__hiders[k].cur_y) == (i,
                                                                            j):
                        return True
        return False

    def __check_met_hider(self, is_debug):
        found_somehider = False
        for i in range(len(self.__hiders)):
            hider = self.__hiders[i]
            if hider != None:
                if self.__seeker.meet(hider):
                    if is_debug:
                        print("found hider at {} {}".format(
                            self.__seeker.cur_x, self.__seeker.cur_y))
                        print(self.__turn)
                    self.__point += 20
                    x, y = hider.cur_x, hider.cur_y
                    if not self.overlap_hider(x, y, i):
                        self.__map[x][y] = Config.EMPTY
                        self.__seeker.map[x][y] = Config.VERIFIED
                    self.__hiders[i] = None
                    self.__obs_sign_to_hider[i] = None
                    self.__hider_status[i] = False
                    found_somehider = True
        if found_somehider:
            self.reset_seeker_info()

    def reset_seeker_info(self):
        self.__seeker.detected_coord = None
        self.__seeker.radar_path = []
        self.__seeker.announce = None
        self.__seeker.init_heuristic_map()
        self.__seeker.reset_verified_map()

    def check_observable(self, i, j):
        print(self.__seeker.is_observable(i, j))
Пример #17
0
class Game:
    def __init__(self, gui, is_debug, level):
        self.__map = self.__n = self.__m = None
        self.__range_seek = Config.RANGE_SEEKER
        self.__range_hide = Config.RANGE_HIDER
        self.__gui = gui
        self.__num_hiders = 0
        self.__level = level

    def read_input(self, map_name, is_debug):
        fin = open("../map/" + map_name + ".txt", "r")
        self.__n, self.__m = [int(x) for x in fin.readline().split(" ")]
        self.__read_map(fin)
        self.__read_obstacles(fin)
        if not is_debug:
            self.__gui.read_config(self.__map)
        self.__init_players()
        fin.close()

    def __init_players(self):
        self.__hiders = []
        hiders_coors = []
        seeker_coor = None
        for i in range(self.__n):
            for j in range(self.__m):
                if self.__map[i][j] == Config.SEEKER:
                    seeker_coor = i, j
                    self.__seeker = Seeker(self.__map, self.__n, self.__m,
                                           self.__range_seek, seeker_coor)
                elif self.__map[i][j] == Config.HIDER:
                    hiders_coors.append((i, j))
                    self.__num_hiders += 1

        for i in range(self.__num_hiders):
            self.__hiders.append(
                Hider(self.__map, self.__n, self.__m, self.__range_hide,
                      hiders_coors[i], seeker_coor))

        self.__seeker.update_num_hiders(self.__num_hiders)

    def __read_map(self, fin):
        self.__map = [[int(x) for x in fin.readline().split(" ")]
                      for i in range(self.__n)]

    def __read_obstacles(self, fin):
        line = fin.readline()
        while (line != ""):
            x_tl, y_tl, x_br, y_br = [int(x) for x in line.split(" ")]
            for i in range(x_tl, x_br + 1):
                for j in range(y_tl, y_br + 1):
                    self.__map[i][j] = Config.OBS
            line = fin.readline()

    def __is_seeker_turn(self):
        return self.__turn % (self.__num_hiders + 1) == 1

    def __is_turn_of_hider_number(self):
        return (self.__turn %
                (self.__num_hiders + 1) - 2) % (self.__num_hiders + 1)

    def __compute_seeker_turn(self):
        return (self.__turn - 1) // (self.__num_hiders + 1) + 1

    def __compute_hider_turn(self, index):
        return (self.__turn - (index + 2)) // (self.__num_hiders + 1) + 1

    def __hiders_found(self):
        for i in range(len(self.__hiders)):
            if self.__hiders[i] != None:
                return False
        return True

    def make_seeker_move(self):
        x, y = self.__seeker.move(self.__compute_seeker_turn())
        self.__point -= int(x != 0 or y != 0)
        self.__map[self.__seeker.cur_x - x][self.__seeker.cur_y -
                                            y] = Config.EMPTY
        self.__map[self.__seeker.cur_x][self.__seeker.cur_y] = Config.SEEKER
        for i in range(len(self.__hiders)):
            if self.__hiders[i] != None:
                self.__hiders[i].map[self.__seeker.cur_x -
                                     x][self.__seeker.cur_y - y] = Config.EMPTY
                self.__hiders[i].map[self.__seeker.cur_x][
                    self.__seeker.cur_y] = Config.SEEKER

    def make_hider_move(self, is_debug):
        index_hider_move = self.__is_turn_of_hider_number()
        current_hider = self.__hiders[index_hider_move]
        if current_hider != None:
            if self.__level > 2:
                x, y = current_hider.move(
                    self.__compute_hider_turn(index_hider_move))
                self.__seeker.update_hider_pos(current_hider.cur_x,
                                               current_hider.cur_y, x, y)

                if not self.overlap_hider(
                        self.__hiders[index_hider_move].cur_x - x,
                        self.__hiders[index_hider_move].cur_y - y,
                        index_hider_move):
                    self.__map[self.__hiders[index_hider_move].cur_x -
                               x][self.__hiders[index_hider_move].cur_y -
                                  y] = Config.EMPTY
                self.__map[self.__hiders[index_hider_move].cur_x][
                    self.__hiders[index_hider_move].cur_y] = Config.HIDER

            if current_hider.should_announced(
                    self.__compute_hider_turn(index_hider_move)):
                x, y = current_hider.announce()
                self.__seeker.signal_announce(x, y)
                if not is_debug:
                    self.__gui.send_signal_announce((x, y), self.__turn)

    def operate(self, is_debug):
        self.__turn, self.__point = (1, 0)
        self.__winner = Config.HIDER
        while True:
            if self.__hiders_found() and (self.__turn -
                                          1) % (self.__num_hiders + 1) == 1:
                self.__winner = Config.SEEKER
                break
            if self.__seeker.visited_all():
                break
            if self.__is_seeker_turn():
                self.make_seeker_move()
            else:
                self.make_hider_move(is_debug)
            self.update_game_info(is_debug)
        if not is_debug:
            self.__gui.visualize()
        if (self.__winner == Config.SEEKER):
            print("Seeker win")
        else:
            print("Hiders win")
        print("Point: {:d}".format(self.__point))
        print("Turns taken: {}".format(self.__turn))

    def update_game_info(self, is_debug):
        if not is_debug:
            self.__gui.update_map(self.__map)
        self.__turn += 1
        self.__check_met_hider(is_debug)
        #self.__notify_hiders()

    def __notify_hiders(self):
        for hider in self.__hiders:
            if hider == None:
                continue
            is_regconized = False
            for hider_x, hider_y in self.__seeker.list_notify:
                if hider.cur_x == hider_x and hider.cur_y == hider_y:
                    is_regconized = True
                    hider.update_seeker_pos(self.__seeker.cur_x,
                                            self.__seeker.cur_y)
            hider.is_regconized = is_regconized
        #print("Seeker notify x: {:d}, y: {:d}".format(self.__seeker.cur_x, self.__seeker.cur_y))

    def overlap_hider(self, i, j, index):
        for k in range(len(self.__hiders)):
            if self.__hiders[k] != None:
                if k != index:
                    if (self.__hiders[k].cur_x, self.__hiders[k].cur_y) == (i,
                                                                            j):
                        return True
        return False

    def __check_met_hider(self, is_debug):
        found_somehider = False
        for i in range(len(self.__hiders)):
            hider = self.__hiders[i]
            if hider != None:
                if self.__seeker.meet(hider):
                    if is_debug:
                        print("found hider at {} {}".format(
                            self.__seeker.cur_x, self.__seeker.cur_y))
                        print(self.__turn)
                    self.__point += 20
                    x, y = hider.cur_x, hider.cur_y
                    if not self.overlap_hider(x, y, i):
                        self.__map[x][y] = Config.EMPTY
                        self.__seeker.map[x][y] = Config.VERIFIED
                    self.__hiders[i] = None
                    found_somehider = True
        if found_somehider:
            self.reset_seeker_info()

    def reset_seeker_info(self):
        self.__seeker.detected_coord = None
        self.__seeker.radar_path = []
        self.__seeker.announce = None
        self.__seeker.init_heuristic_map()
        self.__seeker.reset_verified_map()

    def check_observable(self, i, j):
        print(self.__seeker.is_observable(i, j))
Пример #18
0
from seeker import Seeker
from mail_module import send_mail

s = Seeker()
file_name, second_file_name = s.seek()
mail_text = 'DevExpress dependency report.'
mail_title = 'DevExpress dependency report'
send_to = ['*****@*****.**']
send_from = '*****@*****.**'

#send_mail(send_from, send_to, mail_title, mail_text, [str(file_name), str(second_file_name)])
Пример #19
0
class Player():
    def __init__(self, account_id, private_key, provider, dev=False):
        self.plugin = PluginManager()
        self.plugin.load(PLUGINS_PATH)
        self.plugin.set_default_solverclass('gcs_solver.py')

        self.dev = dev
        self.account_id = account_id
        self.web3 = Web3(provider)
        self.interest = ''
        self.trusted_users = []
        self.web3.eth.defaultAccount = account_id

        # PoA であれば geth_poa_middleware を利用
        try:
            self.web3.eth.getBlock("latest")
        except ExtraDataLengthError:
            self.web3.middleware_onion.inject(geth_poa_middleware, layer=0)

        if private_key:
            self.web3.middleware_onion.add(
                construct_sign_and_send_raw_middleware(private_key))
        self.deploy_erc1820()

        self.__observer = None
        self.__state = None
        self.assets = None
        # Wallet の情報
        self.wallet = Wallet(self.web3, self.account_id)

        # オペレータ(トークンの交換などを担当)のコントラクト
        self.operator_address = None
        self.load_config()

        self.operator_address = self._fix_config_address(
            self.config['operator']['address'])
        if self.config['operator']['solver_pluginfile']:
            self.plugin.set_solverclass(
                self.operator_address,
                self.config['operator']['solver_pluginfile'])

        self.contracts = Contracts(self.web3)
        self.deploy_metemcyberutil()

        self.fetch_trusted_users()

        self.event_listener = BasicEventListener('')
        self.event_listener.start()

        # inventory (トークン・カタログの管理)のインスタンス生成
        catalog_address = self._fix_config_address(
            self.config['catalog']['address'])
        broker_address = self._fix_config_address(
            self.config['broker']['address'])
        self.inventory = Inventory(self.contracts, self.account_id,
                                   self.event_listener, catalog_address,
                                   broker_address)

        # Seeker (チャレンジの依頼者)のインスタンス
        self.seeker = Seeker(self.contracts)

        # Solver (チャレンジの受領者)としてのインスタンス
        if self.operator_address:
            solverclass = self.plugin.get_solverclass(self.operator_address)
            self.solver = solverclass(self.contracts, self.account_id,
                                      self.operator_address)
        else:
            self.solver = None

        # MISP設定のinsert
        self.load_misp_config(MISP_INI_FILEPATH)

    def deploy_erc1820(self):
        # ERC777を利用するにはERC1820が必要
        # https://github.com/ConsenSys/ERC1400/blob/master/migrations/2_erc1820_registry.js
        deployer_address = '0xa990077c3205cbDf861e17Fa532eeB069cE9fF96'
        contract_address = Web3.toChecksumAddress(
            '0x1820a4b7618bde71dce8cdc73aab6c95905fad24')
        code = self.web3.eth.getCode(contract_address)
        LOGGER.debug('erc1820_address has %s', code)

        if not code:
            #指定のアドレスへ送金
            tx_hash = self.web3.eth.sendTransaction({
                'from':
                self.account_id,
                'to':
                deployer_address,
                'value':
                self.web3.toWei('0.1', 'ether')
            })
            tx_receipt = self.web3.eth.waitForTransactionReceipt(tx_hash)
            GASLOG.info('erc1820.sendTransaction: gasUsed=%d',
                        tx_receipt['gasUsed'])
            LOGGER.debug(tx_receipt)
            #ERC1820のデプロイ
            with open(ERC1820_RAW_TX_FILEPATH, 'r') as fin:
                raw_tx = fin.read().strip()
            tx_hash = self.web3.eth.sendRawTransaction(raw_tx)
            tx_receipt = self.web3.eth.waitForTransactionReceipt(tx_hash)
            GASLOG.info('erc1820.sendRawTransaction: gasUsed=%d',
                        tx_receipt['gasUsed'])
            LOGGER.debug(tx_receipt)

    def deploy_metemcyberutil(self):
        metemcyber_util = self.contracts.accept(MetemcyberUtil())
        if not self.config['metemcyber_util']['address']:
            self.config['metemcyber_util']['address'] = \
                metemcyber_util.new().contract_address
        placeholder = metemcyber_util.register_library(
            self.config['metemcyber_util']['address'],
            self.config['metemcyber_util']['placeholder'])
        if placeholder != self.config['metemcyber_util']['placeholder']:
            self.config['metemcyber_util']['placeholder'] = placeholder
            self.save_config()

    def _fix_config_address(self, target):
        if self.web3.isChecksumAddress(target):
            return target
        if self.web3.isAddress(target):
            return self.web3.toChecksumAddress(target)
        return None

    def load_config(self):
        # コントラクトのアドレスを設定ファイルから読み込む
        fname = CONFIG_INI_FILEPATH
        config = configparser.ConfigParser()
        config.add_section('catalog')
        config.set('catalog', 'address', '')
        config.add_section('broker')
        config.set('broker', 'address', '')
        config.add_section('operator')
        config.set('operator', 'address', '')
        config.set('operator', 'owner', '')
        config.set('operator', 'solver_pluginfile', '')
        config.add_section('metemcyber_util')
        config.set('metemcyber_util', 'address', '')
        config.set('metemcyber_util', 'placeholder', '')
        if not os.path.exists(fname):
            self.config = config
            return

        config.read(fname)
        self.config = config
        LOGGER.info('[load config]')
        LOGGER.info('catalog address: %s', config['catalog']['address'])

    def save_config(self):
        if hasattr(self, 'inventory') and self.inventory:
            catalog = 'catalog'
            if not self.config.has_section(catalog):
                self.config.add_section(catalog)
            self.config.set(catalog, 'address', self.inventory.catalog_address)

            broker = 'broker'
            if not self.config.has_section(broker):
                self.config.add_section(broker)
            self.config.set(broker, 'address', self.inventory.broker_address)

        operator = 'operator'
        if not self.config.has_section(operator):
            self.config.add_section(operator)
        self.config.set(operator, 'address',
                        self.operator_address if self.operator_address else '')
        self.config.set(operator, 'owner',
                        self.account_id if self.account_id else '')
        if self.plugin and self.operator_address:
            fname = self.plugin.get_plugin_filename(self.operator_address)
            self.config.set(operator, 'solver_pluginfile',
                            fname if fname else '')

        with open(CONFIG_INI_FILEPATH, 'w') as fout:
            self.config.write(fout)
            LOGGER.info('update config.')

    def load_misp_config(self, fname):
        # MISPに関する設定を設定ファイルから読み取る
        self.default_price = -1
        self.default_quantity = -1
        self.default_num_consign = -1
        if not os.path.exists(fname):
            return
        config = configparser.ConfigParser()
        config.read(fname)
        try:
            self.default_price = config["MISP"].getint("defaultprice")
            self.default_quantity = config["MISP"].getint("defaultquantity")
            self.default_num_consign = config["MISP"].getint(
                "default_num_consign")
            LOGGER.info('[load MISP config]')
        except KeyError as err:
            LOGGER.warning('MISP configファイルの読み込みに失敗しました')
            LOGGER.warning(err)

    @staticmethod
    def uuid_to_filepath(uuid):
        return os.path.abspath('{}/{}.json'.format(MISP_DATAFILE_PATH, uuid))

    @staticmethod
    def tokenaddress_to_filepath(address):
        return os.path.abspath('{}/{}'.format(FILESERVER_ASSETS_PATH, address))

    def add_observer(self, observer):
        self.__observer = observer

    def notify_observer(self):
        self.__observer.update(self)

    @property
    def state(self):
        return self.__state

    @state.setter
    def state(self, state):
        self.__state = state
        # Stateと同じ名前の関数があれば自動実行
        if state in dir(self):
            getattr(self, state)()
        self.notify_observer()

    def setup_inventory(self,
                        catalog_address='',
                        broker_address='',
                        is_private=False):
        if catalog_address == '':
            catalog_address = self.contracts.accept(CTICatalog()).\
                new(is_private).contract_address
            LOGGER.info('deployed CTICatalog. address: %s', catalog_address)
        if broker_address == '':
            broker_address = self.contracts.accept(CTIBroker()).\
                new().contract_address
            LOGGER.info('deployed CTIBroker. address: %s', broker_address)

        if self.inventory:
            self.inventory.switch_catalog(catalog_address)
            self.inventory.switch_broker(broker_address)
        else:
            # inventory インスタンスの作成
            self.inventory = Inventory(self.contracts, self.account_id,
                                       catalog_address, broker_address)

        self.save_config()

    def create_token(self, initial_supply, default_operators=None):
        # CTIトークンとなるERC777トークンの発行
        ctitoken = self.contracts.accept(CTIToken()).new(
            initial_supply, default_operators if default_operators else [])
        return ctitoken.contract_address

    def accept_as_solver(self, view=None):
        LOGGER.info('accept as solver')
        if not self.inventory or not self.solver:
            return
        own_tokens = self.inventory.list_own_tokens(self.account_id)
        if len(own_tokens) == 0:
            return
        self.solver.accept_challenges(own_tokens, view=view)
        self.solver.reemit_pending_tasks()

    def setup_operator(self,
                       operator_address='',
                       solver_pluginfile='',
                       view=None):
        if operator_address == self.operator_address:
            return

        if solver_pluginfile and \
                not self.plugin.is_pluginfile(solver_pluginfile):
            raise Exception('invalid plugin file: ' + solver_pluginfile)

        old_operator_address = self.operator_address
        if operator_address == '':
            # オペレータのデプロイ
            ctioperator = self.contracts.accept(CTIOperator())
            operator_address = ctioperator.new().contract_address
            ctioperator.set_recipient()

        if solver_pluginfile:
            self.plugin.set_solverclass(operator_address, solver_pluginfile)

        if operator_address != old_operator_address:
            if self.solver:
                self.solver.destroy()
            if operator_address:
                solverclass = self.plugin.get_solverclass(operator_address)
                self.solver = solverclass(self.contracts, self.account_id,
                                          operator_address)
            else:
                self.solver = None

        self.operator_address = operator_address
        self.save_config()

        if self.solver:
            self.accept_as_solver(view)

    def buy(self, token_address):
        # トークンの購買処理の実装
        self.inventory.buy(token_address, allow_cheaper=True)

    def disseminate_token_from_mispdata(self, default_pirce, default_quantity,
                                        default_num_consign, view):
        # mispオブジェクトファイルの一覧をtokenとして公開する

        # 登録済みのtokenを取得
        registered_token = self.fetch_registered_token()
        registered_uuid = [token.get('uuid') for token in registered_token]
        for obj_path in Path(MISP_DATAFILE_PATH).glob("./*.json"):
            # UUID (ファイル名から拡張子を省いた部分) を取得
            uuid = obj_path.stem
            if uuid in registered_uuid:
                continue
            metadata = {}
            with open(obj_path) as fin:
                misp = json.load(fin)
            try:
                view.vio.print('disseminating CTI: \n'
                               '  UUID: ' + uuid + '\n'
                               '  TITLE: ' + misp['Event']['info'] + '\n')
                metadata['uuid'] = uuid
                metadata['title'] = misp['Event']['info']
                metadata['price'] = default_pirce
                metadata['operator'] = self.operator_address
                metadata['quantity'] = default_quantity
                self.disseminate_new_token(metadata, default_num_consign)
            except KeyError:
                LOGGER.warning('There is no Event info in %s', misp)

    def disseminate_new_token(self, cti_metadata, num_consign=0):
        # ERC20/777 トークンを、独自トークンとして発行する
        # CTIトークンを作成
        token_address = self.create_token(cti_metadata['quantity'])

        # カタログに登録して詳細をアップデート
        self.disseminate_token(token_address, cti_metadata)

        if num_consign > 0:
            self.inventory.consign(token_address, num_consign)

        return token_address

    def disseminate_token(self, token_address, cti_metadata):
        # トークンをカタログに登録

        cti_metadata['tokenAddress'] = token_address
        self.create_asset_content(cti_metadata)
        self.register_catalog(token_address, cti_metadata)
        self.save_registered_token(cti_metadata)

    def create_asset_content(self, cti_metadata):
        misp_filepath = self.uuid_to_filepath(cti_metadata['uuid'])
        dist_linkpath = self.tokenaddress_to_filepath(
            cti_metadata['tokenAddress'])

        ## create a simple placeholder if MISP file does not exist.
        if not os.path.isfile(misp_filepath):
            LOGGER.warning('MISP file does not exist: %s', misp_filepath)
            os.makedirs(os.path.dirname(misp_filepath), exist_ok=True)
            ## simple placeholder with title. is this redundant?
            j = json.loads('{"Event":{"info": ""}}')
            j['Event']['info'] = cti_metadata['title']
            with open(misp_filepath, 'w') as fout:
                json.dump(j, fout, indent=2, ensure_ascii=False)
            LOGGER.warning('created a simple placeholder. '
                           'please overwrite the file above.')

        dist_dir = os.path.dirname(dist_linkpath)
        if not os.path.isdir(dist_dir):
            os.makedirs(dist_dir)
            LOGGER.warning('created missing directory for disseminate: %s',
                           dist_dir)
        try:
            os.symlink(misp_filepath, dist_linkpath)
        except FileExistsError:
            LOGGER.error('disseminate link already exists: %s', dist_linkpath)

    def register_catalog(self, token_address, cti_metadata):
        self.inventory.register_token(self.account_id, token_address,
                                      cti_metadata)

    def unregister_catalog(self, token_address):
        self.inventory.unregister_token(token_address)

    def update_catalog(self, token_address, cti_metadata):
        self.inventory.modify_token(token_address, cti_metadata)

    @staticmethod
    def save_registered_token(cti_metadata):
        # cticatalog コントラクトに登録したtokenのmetadataを保存する
        fieldnames = [
            'uuid', 'tokenAddress', 'title', 'price', 'operator', 'quantity'
        ]

        is_empty = not os.path.isfile(REGISTERED_TOKEN_TSV)

        with open(REGISTERED_TOKEN_TSV, 'a', newline='') as tsvfile:
            writer = csv.DictWriter(tsvfile,
                                    fieldnames=fieldnames,
                                    extrasaction='ignore',
                                    delimiter='\t')
            if is_empty:
                writer.writeheader()
            writer.writerow(cti_metadata)

    @staticmethod
    def fetch_registered_token():
        # 登録済みトークンのfetch
        registered_tokens = []
        try:
            with open(REGISTERED_TOKEN_TSV, newline='') as tsvfile:
                tsv = csv.DictReader(tsvfile, delimiter='\t')
                for row in tsv:
                    registered_tokens.append(row)
                return registered_tokens
        except FileNotFoundError:
            pass
        except Exception as err:
            LOGGER.error(err)
        return registered_tokens

    def consign(self, token_address, amount):
        self.inventory.consign(token_address, amount)

    def takeback(self, token_address, amount):
        self.inventory.takeback(token_address, amount)

    def watch_token_start(self, token_address, callback):
        ctitoken = self.contracts.accept(CTIToken()).get(token_address)
        argument_filters = dict()
        argument_filters['from'] = self.operator_address
        argument_filters['to'] = self.account_id
        event_filter = ctitoken.event_filter('Sent',
                                             fromBlock='latest',
                                             argument_filters=argument_filters)
        self.event_listener.add_event_filter('Sent:' + token_address,
                                             event_filter, callback)

    def watch_token_stop(self, token_address):
        self.event_listener.remove_event_filter_in_callback('Sent:' +
                                                            token_address)

    def request_challenge(self, token_address, data=''):
        # token_address のトークンに対してチャレンジを実行
        if self.seeker.challenge(self.operator_address,
                                 token_address,
                                 data=data):
            # トークン送付したので情報更新する
            self.inventory.update_balanceof_myself(token_address)

    @staticmethod
    def receive_challenge_answer(data):
        try:
            # data is generated at Solver.webhook().
            download_url = data['download_url']
            token_address = data['token_address']
            if len(download_url) == 0 or len(token_address) == 0:
                raise Exception('received empty data')
        except:
            msg = '受信データの解析不能: ' + str(data)
            return False, msg

        msg = ''
        msg += '受信 URL: ' + download_url + '\n'
        msg += 'トークン: ' + token_address + '\n'

        try:
            request = Request(download_url, method="GET")
            with urlopen(request) as response:
                rdata = response.read()
        except Exception as err:
            LOGGER.error(err)
            msg += \
                'チャレンジ結果を受信しましたが、受信URLからの' + \
                'ダウンロードに失敗しました: ' + str(err) + '\n'
            msg += '手動でダウンロードしてください\n'
            return True, msg

        try:
            jdata = json.loads(rdata)
            title = jdata['Event']['info']
        except:
            title = '(解析できませんでした)'
        msg += '取得データタイトル: ' + title

        try:
            if not os.path.isdir(DOWNLOADED_CTI_PATH):
                os.makedirs(DOWNLOADED_CTI_PATH)
            filepath = '{}/{}.json'.format(DOWNLOADED_CTI_PATH, token_address)
            with open(filepath, 'wb') as fout:
                fout.write(rdata)
            msg += '\n取得データを保存しました: ' + filepath
        except Exception as err:
            msg += '\n取得データの保存に失敗しました: ' + str(err)
            msg += '\n手動で再取得してください'
        return True, msg

    def cancel_challenge(self, task_id):
        assert self.operator_address
        self.seeker.cancel_challenge(self.operator_address, task_id)

    def fetch_task_id(self, token_address):
        token_related_task = self.contracts.accept(CTIOperator()).\
            get(self.operator_address).history(token_address, MAX_HISTORY_NUM)
        # 最新の一つのみを表示
        task_id = token_related_task[0]
        return task_id

    def fetch_trusted_users(self):
        # 信頼済みユーザのfetch
        trusted_users = []
        try:
            with open(TRUSTED_USERS_TSV, newline='') as tsvfile:
                tsv = csv.DictReader(tsvfile, delimiter='\t')
                for row in tsv:
                    try:
                        row['id'] = Web3.toChecksumAddress(row['id'])
                    except:
                        continue
                    if row['id'] == self.account_id:
                        continue
                    trusted_users.append(row['id'])
                self.trusted_users = trusted_users
        except FileNotFoundError:
            pass
        except Exception as err:
            LOGGER.error(err)

    def interest_assets(self):
        if not self.interest:
            return self.inventory.catalog_tokens

        filtered_assets = filter(lambda x: self.interest in x[1]['title'],
                                 self.inventory.catalog_tokens.items())

        return dict(filtered_assets)

    def accept_challenge(self, token_address, view=None):
        LOGGER.info('accept_challenge token: %s', token_address)
        self.solver.accept_challenges([token_address], view=view)

    def refuse_challenge(self, token_address):
        LOGGER.info('refuse_challenge token: %s', token_address)
        self.solver.refuse_challenges([token_address])

    def like_cti(self, token_address):
        assert self.inventory
        self.inventory.like_cti(token_address)

    def get_like_users(self):
        try:
            return self.inventory.like_users
        except:
            return dict()

    def send_token(self, token_address, target_address, amount):
        assert token_address and target_address and amount > 0
        try:
            self.contracts.accept(CTIToken()).get(token_address).\
                send_token(target_address, amount)
            # ブローカー経由でないためイベントは飛ばない。手動で反映する。
            self.inventory.update_balanceof_myself(token_address)
            return True
        except Exception as err:
            LOGGER.exception(err)
        return False

    def burn_token(self, token_address, amount, data=''):
        assert token_address and amount > 0
        try:
            self.contracts.accept(CTIToken()).get(token_address).\
                burn_token(amount, data)
            # Burned イベントが飛ぶがキャッチしていない。手動で反映する。
            self.inventory.update_balanceof_myself(token_address)
            return True
        except Exception as err:
            LOGGER.exception(err)
        return False