Example #1
0
    def mining(self):
        print('starting to mine')
        transactions = []
        if len(self.tx_pool) == 0:
            return 1
        elif len(self.tx_pool) <= 6:
            transactions = copy.deepcopy(self.tx_pool)
        else:
            q = Queue.PriorityQueue()
            s = set()
            while len(s) < 6:
                s.add(random.randint(0, len(self.tx_pool) - 1))
            for x in s:
                q.put(self.tx_pool[x])
            while len(transactions) < 5:
                transactions.append(q.get())

        status = 'invalid'
        # print('invalid')
        detected = False
        while status == 'invalid' and len(transactions) > 0:
            result = self.double_spending_check(transactions)
            status = result[0]
            if status == 'invalid':
                detected = True
            transactions = result[1]

        if len(transactions) == 0:
            return 2

        difficulty = self.get_difficulty()
        # print(difficulty)
        leading_zeros = ''
        leading_zeros = leading_zeros.rjust(difficulty, '0')
        nonce = 0
        # print leading_zeros
        # print difficulty
        time_stamp = time.time()
        prev_hash = self.block_json_to_obj(
            self.blockchain[-1]).calculate_hash()
        # print(prev_hash)
        newblock = block(difficulty, time_stamp, transactions, prev_hash,
                         nonce)
        hash_val = newblock.calculate_hash()
        bin_hash_val = (bin(int(hash_val, 16))[2:]).zfill(256)
        # print(bin_hash_val[:difficulty])
        while bin_hash_val[:difficulty] != leading_zeros:
            nonce += 1
            newblock = block(difficulty, time.time(), transactions, prev_hash,
                             nonce)
            hash_val = newblock.calculate_hash()
            bin_hash_val = (bin(int(hash_val, 16))[2:]).zfill(256)
        # print(bin_hash_val)
        return (detected, newblock)
Example #2
0
def create_block(game, blocks, block_number, row_number):
    Block = block(game)
    Block_width = Block.rect.width
    Block.x = Block_width + 1.5 * Block_width * block_number
    Block.rect.x = Block.x
    Block.rect.y = Block.rect.height + 2 * Block.rect.height * row_number
    blocks.add(Block)
Example #3
0
 def resetPlayer(self, position):
     self.head = block(position)
     self.playerBody = []
     self.playerBody.append(self.head)
     self.headTurns = {}
     self.dx = 1
     self.dy = 0
Example #4
0
 def reset_snake(self, pos):
     self.start = block(pos)
     self.snake_body = []
     self.snake_body.append(self.start)
     self.direct_x = 1
     self.direct_y = 0
     self.direction = dict()
Example #5
0
 def block_json_to_obj(self, newblock):
     """
     receive a new block from the net and create a new block object before add it to the block_pool
     """
     newblock = json.loads(newblock)
     new_block = block(newblock["difficulty"], newblock["time_stamp"],
                       newblock["transactions"], newblock["prev_hash"],
                       newblock["Nonce"])
     return new_block
Example #6
0
    def increasePlayerLength(self):
        tail = self.playerBody[-1]
        currdx = tail.dx
        currdy = tail.dy

        if currdx == 0 and currdy == 1:
            self.playerBody.append(
                block((tail.position[0], tail.position[1] - 1)))
        elif currdx == 0 and currdy == -1:
            self.playerBody.append(
                block((tail.position[0], tail.position[1] + 1)))
        elif currdx == 1 and currdy == 0:
            self.playerBody.append(
                block((tail.position[0] - 1, tail.position[1])))
        elif currdx == -1 and currdy == 0:
            self.playerBody.append(
                block((tail.position[0] + 1, tail.position[1])))

        self.playerBody[-1].dx = currdx
        self.playerBody[-1].dy = currdy
Example #7
0
    def build_snake(self):
        last_part = self.snake_body[-1]
        last_x = last_part.direct_x
        last_y = last_part.direct_y

        if last_x == 1 and last_y == 0:
            self.snake_body.append(
                block((last_part.pos[0] - 1, last_part.pos[1])))
        elif last_x == -1 and last_y == 0:
            self.snake_body.append(
                block((last_part.pos[0] + 1, last_part.pos[1])))
        elif last_x == 0 and last_y == 1:
            self.snake_body.append(
                block((last_part.pos[0], last_part.pos[1] - 1)))
        elif last_x == 0 and last_y == -1:
            self.snake_body.append(
                block((last_part.pos[0], last_part.pos[1] + 1)))

        self.snake_body[-1].direct_x = last_x
        self.snake_body[-1].direct_y = last_y
Example #8
0
    def __init__(self, position, playerColor):
        self.playerBody = []
        self.headTurns = {}
        self.blocks = []
        self.playerColor = playerColor
        self.head = block(position)
        self.pos = position
        self.playerBody.append(self.head)

        self.dx = 0
        self.dy = 1
        self.eventType = ""
Example #9
0
def initial_prior(network_data, internetwork_data, edge_prob):
    """ Generate the initial prior system to start the MCMC
    Input:
        network_data: the data of the three networks
        inter_network_data: the data of the four internetworks
        edge_prob: the edge probability to sample edges obtained from shelby county networks
    """
    block_water = block(network_data[0], edge_prob[0])
    block_power = block(network_data[1], edge_prob[1])
    block_gas = block(network_data[2], edge_prob[2])

    block_networks = [block_water, block_power, block_gas]
    for i in range(len(block_networks)):
        block_networks[i].adj_matrix()
        block_networks[i].edge_failure_matrix()
        block_networks[i].edgeprobmatrix()

    #Initialize the four interdependent networks
    block_inter_wd2ps = interblock(internetwork_data[0], block_networks)
    block_inter_gd2ps = interblock(internetwork_data[1], block_networks)
    block_inter_pd2w = interblock(internetwork_data[2], block_networks)
    block_inter_pd2g = interblock(internetwork_data[3], block_networks)

    block_internetworks = [
        block_inter_wd2ps, block_inter_gd2ps, block_inter_pd2w,
        block_inter_pd2g
    ]
    for i in range(len(block_internetworks)):
        block_internetworks[i].adj_matrix()
        block_internetworks[i].failpropmatrix()
        block_internetworks[i].edgeprobmatrix()

    block_system = block_intersystem(block_networks, block_internetworks)
    block_system.adj_matrix()
    block_system.edge_failure_matrix()
    block_system.edgeprobmatrix()
    return block_system, block_networks, block_internetworks
Example #10
0
 def __init__(self):
     pygame.init()
     self.screen = pygame.display.set_mode((800, 600))
     self.Paddle = paddle(self)
     self.Block = block(self)
     self.blocks = Group()
     self.balls = Group()
     self.scorefont = pygame.font.SysFont("monospace", 16)
     self.gameoverfont = pygame.font.SysFont("monospace", 120)
     self.highscorefont = pygame.font.SysFont("monospace", 30)
     self.score = 0
     with open("highscore.txt") as f:
         contents = f.read()
     self.high = int(contents)
     b.create_wall(self, self.blocks)
     self.Ball = ball(self, self.Paddle, self.blocks)
     self.balls.add(self.Ball)
     self.game_over = False
     self.pause = False
Example #11
0
 def __init__(self, color, pos):
     self.color = color
     self.start = block(pos)
     self.snake_body.append(self.start)
     self.direct_y = 1
     self.direct_x = 0
Example #12
0
 def create_genesis_block(self):
     creation_date = time.mktime(datetime.datetime.strptime(self.chain_creation_date, "%d/%m/%Y").timetuple())
     return block(self.mining_difficulty, creation_date, [], 'this is the first block', 0)