def __init__(self, **kwargs): self.chain = Chain() self.walkers = [] self.times = np.zeros(1) self.dt = 1e-7 self.update_time_unit_(**kwargs) self.error = 1e-7 self.update_error_(**kwargs) pop = None dat = None try: pop = kwargs['node_population'] except: KeyError try: dat = kwargs['node_data'] except: KeyError self.memory_safe = False #self.population = [] if 'memory_safe' in kwargs.keys(): self.memory_safe = True self.add_nodes_(pop, dat, **kwargs)
def wrapper(self, *args, **kwargs): self.elapsed, id, cash, chain = time.time(), True if len( args) == 4 else False, self.get_cash(), Chain( args[0]) if not len(args) == 4 else None option = chain.get_option( args[1], args[2], args[3]) if not id else ClientMethods.fetch_by_id( self.client, args[1]) self.direction = 'debit' if args[ 4 if not id else 2] == 'buy' else 'credit' self.legs = [{ 'option': option['url'], 'side': args[4 if not id else 2], 'position_effect': args[5 if not id else 3], 'ratio_quantity': kwargs.get('ratio_quantity', 1) }] self.price = option[ 'high_fill_rate_buy_price' if args[4 if not id else 2] == 'buy' else 'high_fill_rate_sell_price'] if not kwargs.get( 'price') else kwargs['price'] self.quantity = kwargs.get( 'quantity', 1) if not kwargs.get('max_quantity') else ( math.floor(cash / self.price) if math.floor( cash / self.price) <= kwargs['max_quantity'] else 1) return func(self, *args, **kwargs)
def __init__(self, genesis_block, miners, start_timestamp, end_timestamp, cutoff, undercutter_power, honest_power=0, avg_block_time = avg_block_time, avoidance=False): self.block_times = [] self.txs_set = set() self.block_size = block_size self.avoidance = avoidance self.undercutter_power = undercutter_power self.honest_power = honest_power self.rational_power = 1 - undercutter_power - honest_power self.conf_blocks = 6 self.avg_block_time = avg_block_time self.start_time = int(start_timestamp) self.current_time = int(start_timestamp) self.end_time = int(end_timestamp) self.experiment_end_flag = False self.D = cutoff self.gamma = 0 fork_conditions = {"1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0} self.fork_status ={"forks": 0, "failed_forks": 0 , "fork_len": [0], "main_len":[0], "conditions": fork_conditions, "rational_changing_to_fork": 0, "rational_changing_to_main": 0} self.genesis_block = genesis_block self.chains = [Chain(miners, genesis_block)] self.chains[-1].next_block_time = self.current_time self.chains[-1].prev_time = self.current_time-1 self.miners = miners for miner in self.miners: if isinstance(miner, UndercutMiner): # considering only one undercutter, for multiple change to list or map self.undercutter = miner self.mempool_stat = {'size':[], 'fees': []}
def getSimulator(self, simulator): if simulator == "logistics": return Logistics(start=True) elif simulator == "pong": return Pong(start=True) elif simulator == "tetris": return Tetris(start=True) elif simulator == "wumpus": return Wumpus(start=True) elif simulator == "blocks": return Blocks_world(start=True) elif simulator == "blackjack": return Game(start=True) elif simulator == "50chain": return Chain(start=True) elif simulator == "net_admin": return Admin(start=True) else: print('{} is not a supported simulation'.format(simulator)) raise NameError()
def form_chains(self): self.chains = [] cboard = self.current_board for color in (BLACK, WHITE): for block in cboard.iterate_blocks(color): block.chain = None for block1 in cboard.iterate_blocks(color): if block1.chain: continue chain = Chain() self.chains.append(chain) chain.add_block(block1) block_added = True while block_added: block_added = False for block2 in cboard.iterate_blocks(color): if block2.chain: continue for block3 in chain.blocks.values(): if len( cboard.block_connection_status( block3.get_origin(), block2.get_origin())) >= 2: chain.add_block(block2) block_added = True break
def test_many_transaction_in_genesis_block_fails(self): b = self._create_genesis_block(self.w1) b.add_transaction(self.w1.send_funds(self.w2.address, 1)) c = Chain() c.difficulty = 2 c.add_block(b) self.assertFalse(c.is_valid())
def sync_overall(save=False, sync_es=False): local_chain = sync_local(sync_es) NODES = nodes.get_list_node(mongo_conn) for peer in NODES: endpoint = peer + '/blockchain' try: r = requests.get(endpoint, timeout=5) chain_data = r.json() peer_blocks = [Block(block_dict) for block_dict in chain_data] peer_chain = Chain(peer_blocks) # check valid , if valid and longer, sync local if peer_chain.is_valid() and len(peer_chain) > len(local_chain): ## insert new block to elasticsearch ## find a new block ## m is length of local_chains ## n la so block moi can them m = len(local_chain) n = len(peer_chain) - len(local_chain) peer_blocks = peer_chain.blocks local_blocks = local_chain.blocks for i in range(0, n): block = local_blocks[i + m] block_dict = block.dict() es.insert_document(block_dict) local_chain = peer_chain except Exception as e: pass if save: local_chain.self_save() return local_chain
def main(): # create a blockchain chain = Chain() # initialize set of nodes nodes = [] for i in range(NUM_NODES): nodes.append(Node(chain, 1)) # actions while chain.time <= MAX_TIME: for i in range(NUM_NODES): cur_node = random.choice(nodes) cur_node.make_block() cur_node.commit_block() chain.inc_time() chain.print_main_chain_head() nodes[0].found_blocks.append(Block(chain.blocks[6], chain.time, nodes[0].ID)) nodes[0].commit_block() chain.print_main_chain_head() for i in range(11, 25): nodes[0].found_blocks.append(Block(chain.blocks[i], chain.time, nodes[0].ID)) nodes[0].commit_block() chain.print_main_chain_head() chain.print_chain() for i in range(chain.main_chain_max_height): chain.print_chain_converge_at_height(i)
def test_load_2(self): '''Test chaining of two files.''' tmpfile = testfname.replace('test_tree', 'test_tree_2_tmp') shutil.copyfile(testfname, tmpfile) chain = Chain(testfname.replace('.root', '*.root'), 'test_tree') self.assertEqual(len(chain), 200) os.remove(tmpfile)
def draw_lane_edge(self, lane_edge, tool_box): color = QtCore.Qt.blue group = tool_box.lane_edge_type(lane_edge.type) info = "lane-edge section=%d index=%d type=%s" \ % (lane_edge.section_id, lane_edge.index, lane_edge.type) balls = list() for i, point in enumerate(lane_edge.polyline): ball = Ball(point.x, point.y, color) if i: ball.info = info else: ball.info = info + " polyline-first-point" ball.road_item = lane_edge ball.lane_edge = lane_edge ball.point_index = i ball.setZValue(1) ball.is_a_line = False ball.is_selectable = True self.addItem(ball) balls.append(ball) group.items.append(ball) ball0 = balls[0] for ball in balls: chain = Chain(ball0, ball, color) ball0 = ball chain.info = info chain.road_item = lane_edge chain.lane_edge = lane_edge chain.setZValue(1) chain.is_a_line = True chain.is_selectable = True self.addItem(chain) group.items.append(chain)
def train(disease_group_id): """ Use data extracted from request JSON to create structure in a training phase. """ # Extract training data from JSON try: data = request.json['points'] except KeyError: return _log_response('Invalid JSON', disease_group_id, 400) # Initialise empty structures predictor = Chain() feed_classes = {} # Train predictor (if enough data) and save structures if len(data) >= 50: try: feed_classes = _construct_feed_classes(data) X = _convert_training_data_to_matrix(data, feed_classes) y = _convert_expert_weighting_to_labels(data) except KeyError: return _log_response('Invalid JSON', disease_group_id, 400) else: predictor.train(X, y) _save_pickles(disease_group_id, predictor, feed_classes) return _log_response('Trained predictor saved', disease_group_id, 200) else: _save_pickles(disease_group_id, predictor, feed_classes) return _log_response('Insufficient training data - empty predictor saved', disease_group_id, 200)
def setUp(self): super(ChainTest, self).setUp() self.executor = MagicMock() self.strategy = MagicMock() self.chain = Chain(self.executor, self.strategy, test_util.genesis_hash) self.chain.strategy = MagicMock()
def draw_balls_and_chains(self, lane_edge_or_center_line, info, group, color): balls = list() for i, point in enumerate(lane_edge_or_center_line.polyline): ball = Ball(point.x, point.y, color) if i: ball.info = info else: ball.info = info + "polyline-first-point" ball.road_item = lane_edge_or_center_line ball.lane_edge = lane_edge_or_center_line ball.point_index = i ball.setZValue(1) ball.is_a_line = False ball.is_selectable = True self.addItem(ball) balls.append(ball) group.items.append(ball) ball0 = balls[0] for ball in balls: chain = Chain(ball0, ball, color) ball0 = ball chain.info = info chain.road_item = lane_edge_or_center_line chain.lane_edge = lane_edge_or_center_line chain.setZValue(1) chain.is_a_line = True chain.is_selectable = True self.addItem(chain) group.items.append(chain)
def run(self): # The main ETL processing log.info("START") t1 = Util.start_timer("total ETL") # Get the ETL Chain pipeline config strings # Default is to use the section [etl], but may be overidden on cmd line config_section = self.options_dict.get('config_section') if config_section is None: config_section = 'etl' chains_str = self.configdict.get(config_section, 'chains') if not chains_str: raise ValueError('ETL chain entry not defined in section [etl]') # Multiple Chains may be specified in the config chains_str_arr = chains_str.split(',') for chain_str in chains_str_arr: # Build single Chain of components and let it run chain = Chain(chain_str, self.configdict) chain.assemble() # Run the ETL for this Chain chain.run() Util.end_timer(t1, "total ETL") log.info("ALL DONE")
def train(self): Flow().defaults() m = Model().make_fcn() m.summary() c = Chain().make() c.transform_summary() c.determinant_summary() print( '_________________________________________________________________\n\n' ) o = Adam(lr=1.0e-3) m.compile(optimizer=o, loss='mse') mc = ModelCheckpoint(filepath='model_weights.h5', verbose=True, save_best_only=True) m.fit_generator(generator=Generator().make(20), validation_data=Generator().make(10), steps_per_epoch=100, epochs=10, validation_steps=3, callbacks=[mc])
def sync_overall(peers, save=False): best_chain = local_chain for peer in peers: # try to connect to peer peer_chain_url = peer + '/chain' try: r = requests.get(peer_chain_url, timeout=5) peer_chain_dict = r.json() peer_blocks = [Block(block_dict) for block_dict in peer_chain_dict] peer_chain = Chain(peer_blocks) if peer_chain.is_valid() and peer_chain > best_chain: best_chain = peer_chain except requests.exceptions.ConnectionError: print("Peer at %s not running. Continuing to next peer." % peer) except requests.exceptions.Timeout: print("Timeout when connecting peer at %s." % peer) else: print( "Peer at %s is running. Gathered their blochchain for analysis." % peer) print("Longest blockchain has %s blocks" % len(best_chain)) # for now, save the new blockchain over whatever was there if save: best_chain.save() return best_chain
def sync_overall(save=False): best_chain = sync_local() for peer in PEERS: # try to connect to peer peer_blockchain_url = peer + 'blockchain.json' try: r = requests.get(peer_blockchain_url) peer_blockchain_dict = r.json() peer_blocks = [Block(bdict) for bdict in peer_blockchain_dict] peer_chain = Chain(peer_blocks) if peer_chain.is_valid() and peer_chain > best_chain: best_chain = peer_chain except requests.exceptions.ConnectionError: print("Peer at %s not running. Continuing to next peer." % peer) else: print( "Peer at %s is running. Gathered their blockchain for analysis." % peer) print("Longest blockchain is %s blocks" % len(best_chain)) if save: best_chain.self_save() return best_chain
def test(self): """ Gerenal test. """ # Creates a new chain chain = Chain() # Puts some blocks into the chain block_1 = Block("apple", "0") chain.add(block_1) block_1.mine() block_2 = Block("banana", block_1.hash) chain.add(block_2) block_2.mine() block_3 = Block("cat", block_2.hash) chain.add(block_3) # Check if the chain is "not valid" if the last block is not mined self.assertFalse(chain.is_valid) block_3.mine() # Checks if the chain is valid self.assertTrue(chain.is_valid)
def __init__(self, graphics_item, slot, use_this_polyline=None): self.graphics_item = graphics_item self.graphics_item.setVisible(False) road_item = graphics_item.road_item graphics_scene = graphics_item.scene() self.balls = list() if use_this_polyline: polyline = use_this_polyline else: polyline = road_item.polyline for i, point in enumerate(polyline): ball = Ball(point.x, point.y, Observer(slot, i)) ball.is_selectable = False graphics_scene.addItem(ball) self.balls.append(ball) self.chains = list() for i in range(len(self.balls) - 1): ball1 = self.balls[i] ball2 = self.balls[i + 1] chain = Chain(ball1, ball2) chain.is_selectable = False graphics_scene.addItem(chain) self.chains.append(chain)
def __add_floating_block(self, block): """ Add a floating block to be tracked by the miner. This is a block from a chain with a greater or equal cost. If the floating block can be added to an existing chain undergoing chain resolution then it will be; otherwise, a new chain will be created to undergo chain resolution. :param block: The floating block to be added. :return: None if the floating block was added to an already tracked chain; otherwise, the newly created chain to undergo chain resolution. """ for chain in self.floating_chains: cur = chain.blocks[-1] if block.is_valid(cur.hash()): logging.debug("Add to existing floating chain") block.set_previous_hash(cur.hash()) chain.add(block) if chain.is_complete(): self.__receive_complete_chain(chain) return None elif block in chain.blocks: return None logging.debug("Create new floating chain") chain = Chain() chain.add(block) self.floating_chains.append(chain) return chain
def __init__(self, table, base_t=None, tool_t=None): self.table = table if base_t is None: self.base_t = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] else: self.base_t = base_t if tool_t is None: self.tool_t = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] else: self.tool_t = tool_t self.joints = get_joints_from_table(self.table) # List of actuated joints self.ajoints = self.get_ajoints() # List of passive joints self.pjoints = self.get_pjoints() # Actuated joints' variables self.q = self.get_q() # Passive joints' variables self.qp = self.get_qp() # Chains self.chain = Chain(self.joints) # Tuples (root, end) for each loop self.loops = get_loops(self.joints) # List of loop solvers from loop_solver import LoopSolver self.loopsolvers = [LoopSolver(self.joints, *l) for l in self.loops]
def run_attacks(algos, pools, expected_blocks, fork_block): attack_settings = [ hash_rent_11, hash_rent_8, mining_pool_ethermine, mining_pool_ethermine70, mining_pool_ethermine_and_hashrent, mining_pool_ethermine_nanopool # protocol incentivized # mining_pool_40_incentivized, # mining_pool_40_and_hashrent_incentivized ] total_seconds = expected_blocks * 14 results = defaultdict(list) for chain_rule, algo_name in algos: for attack in attack_settings: mainnet = Chain('mainnet', pools, parent_chain_blocks=None, chain_score_fn=chain_rule) attacker_chain = None active_chains = [mainnet] lost_at_block = None forked = False for seconds in range(total_seconds): for _chain in active_chains: if _chain.mines_a_block(seconds): _chain.create_block(seconds) if _chain.name == 'mainnet' and _chain.height == fork_block and not forked: print "Chain block time and pools before fork:" print " - mainnet: block_time:{}, pools:{}".format( mainnet.block_time, mainnet.pools) attacker_chain = mainnet.fork(attack.name, attack.pools) mainnet = mainnet.remove_pools(attack.pools) active_chains = [] active_chains.append(mainnet) active_chains.append(attacker_chain) forked = True print "Chain block time and pools after fork:" print " - {}: block_time:{}, pools:{}".format( attacker_chain.name, attacker_chain.block_time, attacker_chain.pools) print " - mainnet: block_time:{}, pools:{}".format( mainnet.block_time, mainnet.pools) # Check if the mainnet lost. We lost when the attacker is more than # 30 blocks ahead and has a higher score if lost_at_block is None and has_attacker_won( active_chains): lost_at_block = active_chains[0].height print "Lost to attacker at block: {}".format( lost_at_block) # Print results after simulation with the chain score over a range of blocks print_winner(algo_name, active_chains, expected_blocks, fork_block) results[algo_name].append( Result(algo_name, active_chains[0], active_chains[1], lost_at_block, attack)) return results
def syncLocalChain(self): localChain = Chain([]) for block in self.database["blockchain"].find({}, { "_id": 0 }).sort([("index", 1)]): localBlock = Block(block) localChain.addBlock(localBlock) return localChain
def __init__(self): self.server = pyo.Server(nchnls=1).boot() self.server.setAmp(0.3) self.inp = pyo.Input() self.extractor = Extractor(self.inp) self.chain = Chain(self.inp) self.player = ChainWaver(self.chain, self.extractor).out()
def __init__(self, file_lst): # Instantiate parse file on instantion self.file_lst = file_lst self.tempo = None self.tpb = None self.chain = Chain() self._parse()
def test(): test_chain = Chain() test_num_blocks = 10 for i in range(1, test_num_blocks + 1): token = "".join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(16)) test_chain.next_block(test_chain.tail, token) print(test_chain)
def test_invalid_chainshots(chainshotter, monkeypatch, moto): # patch to not have complains about the instance missing in aws monkeypatch.setattr('chainmanager.RegionInstancePair.instance', MagicMock()) chain = Chain([RegionInstancePair(DEFAULT_REGION, 'no-instance')]) # volumes filter returns empty (no ethermint_volume) with pytest.raises(IndexError): chainshotter.chainshot("Test", chain) # UnauthorizedOperation when creating a snapshot - how to simulate? pass
def new_game(self, handler): """Creates a new Game and returns the game id """ game_id = self._get_next_game_id() self.games[game_id] = { "handlers": [handler] } game = self.get_game(game_id) game["chain"] = Chain() # code to send message to the client return game_id
def set_ball_and_chain(self): if len(self.polyline) == 1: point = self.polyline[0] self.ball = Ball(point.x, point.y) self.ball.is_selectable = False #self.graphics_scene.addItem(self.ball) self.chain = Chain(self.ball, None) self.chain.is_selectable = False self.graphics_scene.addItem(self.chain) return self.set_fixed_end(self.polyline[-1])
def thaw(self, chainshot): """ Unfreezes a network from a chainshot file For each snapshot/instance in the file, it restarts the instance (by creating a new instance with the same parameters) and attaches the snapshot as volume and mounts it :param chainshot: the config created by chainshot() :return: Chain object """ instances = [] for snapshot_info in chainshot["instances"]: ec2 = boto3.resource( 'ec2', region_name=snapshot_info["instance"]["region"]) new_instance = self.instance_creator.create_ec2s_from_json( [snapshot_info["instance"]])[0] logger.info("Created new instance {} from AMI {}".format( new_instance.id, snapshot_info["instance"]["ami"])) snapshot = ec2.Snapshot(snapshot_info["snapshot"]["id"]) volume = ec2.create_volume( SnapshotId=snapshot.id, AvailabilityZone=new_instance.placement["AvailabilityZone"]) wait_for_available_volume( volume, get_region_name(new_instance.placement["AvailabilityZone"])) new_instance.attach_volume(VolumeId=volume.id, Device=DEFAULT_DEVICE) logger.info( "Attached volume {} containing snapshot {} to instance {}". format(volume.id, snapshot.id, new_instance.id)) instances.append(new_instance) run_sh_script("shell_scripts/mount_snapshot.sh", snapshot_info["instance"]["key_name"], new_instance.public_ip_address) for instance in instances: logger.info("Instance ID: {} unfreezed from chainshot".format( instance.id)) chain = Chain(map(RegionInstancePair.from_boto, instances)) run_ethermint(chain) logger.info("Done starting ethermint on instances") return chain