def run(options, design): options = parser.parse_args() logger.from_argparse(logger, options) logger.set_level(logging.FATAL, 'http') logger.set_level(logging.FATAL, 'transport') filename = options.design if not pathlib.Path(filename).exists(): parser.error('file, `%s` does not exists.' % filename) if not pathlib.Path(filename).is_file(): parser.error('file, `%s` is not valid file.' % filename) design = load_design(filename) log = logger.get_logger(__name__) log_state = logger.get_logger('consensus.state') log.info('design loaded:\n%s', pprint.pformat(convert_namedtuple_to_dict(design), width=1)) log.debug('trying to run %d / %d (faulty nodes / all nodes)', len(design.faulties), len(design.nodes)) run_func = run_default if options.case is not None: try: os.chdir(pathlib.Path(options.case).absolute()) sys.path.insert(0, '.') run_func = getattr(get_module('main'), 'run', None) except (FileNotFoundError, AttributeError): log.debug('failed to load case, %s', options.case) else: log.debug('loaded case from %s', options.case) blockchains = run_func(options, design) prev = None while True: time.sleep(1) now = set( map(lambda x: (x.consensus.node.name, x.consensus), blockchains)) if now == prev: continue prev = now log_current_state(now, design, log_state) return
def __init__(self, blockchain, loop, waiting=3000, time_limit=INFINITE): self.consensus = blockchain.consensus self.loop = loop self.checkpoint = INIT self.waiting = waiting * MS self.time_limit = time_limit * MS self.validator_names = set( map(lambda x: x.name, self.consensus.validator_candidates)) self.log = logger.get_logger(self._log_name(), node=self.consensus.node.name)
def __init__(self, blockchain, loop, audit_waiting, audit_time_limit=INFINITE): self.blockchain = blockchain self.loop = loop self.audit_waiting = audit_waiting self.audit_time_limit = audit_time_limit self.log = logger.get_logger('audit.faulty-node', node=self.blockchain.consensus.node.name)
def _send_message_multiple_one(queue, message, endpoint): log = logger.get_logger('client') try: response = requests.post( endpoint.join('/send_message'), data=message.serialize(to_string=True), ) response.raise_for_status() log.debug('sent message, %s to %s', message, endpoint) except Exception as e: log.error("failed to send message, %s to %s", message, endpoint) queue.put(False) return queue.put(True) return
def send_message(message_info): assert isinstance(message_info, MessageInfo) log = logger.get_logger('client') log.debug('loaded message: %s', message_info) endpoint = 'http://%s:%s' % (message_info.ip, message_info.port) try: message = Message(message_info.id, message_info.message) response = requests.post( urllib.parse.urljoin(endpoint, '/send_message'), data=message.serialize(to_string=True), ) response.raise_for_status() log.debug('message sent!') except Exception as e: log.error("ConnectionError occurred during client send message to '%s'!" % endpoint) return return message
help= 'endpoints with the number of endpoints\'s messages want to send; ex) http://localhost:80?m=5 http://localhost:80?m=10', # noqa type=str, ) parser.add_argument( 'message', nargs='?', help='Messages you want to send to the server', type=str, ) if __name__ == '__main__': options = parser.parse_args() logger.from_argparse(logger, options) log = logger.get_logger(__name__) log.debug('options: %s', options) message = None endpoints = list() for n, i in enumerate(options.endpoints): try: endpoints.append(Endpoint.from_uri(i)) except AssertionError as e: if n < len(options.endpoints) - 1: parser.error('invalid endpoint: %s' % i) sys.exit(1) message = Message.new(i)
def start(self, blockchain, *a, **kw): self.log_faulty = logger.get_logger( 'transport.faulty', node=blockchain.consensus.node.name) return super(StateRegressionTransport, self).start(blockchain, *a, **kw)
if __name__ == '__main__': options = parser.parse_args() logger.from_argparse(logger, options) logger.set_level(logging.FATAL, 'http') logger.set_level(logging.FATAL, 'transport') if not pathlib.Path(options.design).exists(): parser.error('file, `%s` does not exists.' % options.design) if not pathlib.Path(options.design).is_file(): parser.error('file, `%s` is not valid file.' % options.design) design = load_design(options.design) log = logger.get_logger(__name__) log_state = logger.get_logger('consensus.state') log.debug('options: %s', options) log.info('design loaded:\n%s', pprint.pformat(convert_namedtuple_to_dict(design), width=1)) if options.action in ('run', ): run(options, design) elif options.action in ('check', ): check(options, design) sys.exit(0)
def __init__(self, node_name, *a, **kw): self.log_node_unreachable = logger.get_logger('http.node-unreachable', node=node_name) super(NodeUnreachableBOSNetHTTPServerRequestHandler, self).__init__(node_name, *a, **kw)
def run(options, design): log_state = logger.get_logger('consensus.state') network_module = get_network_module('local_socket') loop = asyncio.get_event_loop() blockchains = list() nodes = dict() servers = dict() auditors = dict() audit_waiting = design.common.audit_waiting audit_time_limit = design.common.audit_time_limit for node_design in design.nodes: node = node_design.node nodes[node.name] = node consensus = CONSENSUS_MODULE.Consensus( node, node_design.quorum.threshold, node_design.quorum.validators, ) for validator in node_design.quorum.validators: consensus.add_to_validator_connected(validator) transport = network_module.Transport(node, loop) faulties = getattr(design.faulties, node.name, list()) if len(faulties) > 0: blockchain = FaultyBlockchain(faulties, consensus, transport) else: blockchain = Blockchain(consensus, transport) blockchains.append(blockchain) servers[node.name] = network_module.Server(blockchain) auditors[node.name] = FaultyNodeAuditor(blockchain, loop, audit_waiting, audit_time_limit) for server in servers.values(): server.start() try: coros = list() for auditor in auditors.values(): coros.extend(auditor.get_coroutines()) loop.run_until_complete( asyncio.gather( asyncio.gather(*tuple(coros)), send_bulk_message_coro(design, nodes, list(servers.values())[0]), )) except (KeyboardInterrupt, SystemExit): log.debug('exception occured!') finally: loop.close() log_nodes_state(blockchains, design, log_state) check_safety(blockchains) return