def add(quorum_id=None): req = get_json(request, app) neighbours = req[NEIGHBOURS] # try to access peer object (if peer is inaccessible then peer has not started) try: # check and make sure peer is in quorum if not app.config[PBFT_INSTANCES].in_committee(quorum_id): app.logger.error( "Peer not in committee {} can not join PBFT".format( quorum_id)) return ROUTE_EXECUTION_FAILED.format( msg="Peer not in committee {} can not join PBFT".format( 'quorum_id')) except AttributeError as e: app.logger.error( "Peer not started request /start/<quorum_id_a>/<quorum_id_b> first" ) app.logger.error(e) return ROUTE_EXECUTION_FAILED.format(msg="") + "Peer not started request " \ "/start/<quorum_id_a>/<quorum_id_b> first \n\n\n{}".format( e) app.logger.info("Adding quorum ID {q} with neighbours {n}".format( q=quorum_id, n=neighbours)) # store neighbour info in app app.config[QUORUMS][quorum_id] = neighbours return ROUTE_EXECUTED_CORRECTLY
def join(quorum_id=None): req = get_json(request, app) neighbours = req[NEIGHBOURS] # try to access peer object (if peer is inaccessible then peer has not started) try: # check and make sure peer is in quorum if not app.config[PBFT_INSTANCES].in_committee(quorum_id): app.logger.error( "Peer not in committee {} can not join PBFT".format( quorum_id)) return ROUTE_EXECUTION_FAILED.format( msg="Peer not in committee {} can not join PBFT".format( 'quorum_id')) except AttributeError as e: app.logger.error( "Peer not started request /start/<quorum_id_a>/<quorum_id_b> first" ) app.logger.error(e) return ROUTE_EXECUTION_FAILED.format(msg="") + "Peer not started request " \ "/start/<quorum_id_a>/<quorum_id_b> first \n\n\n{}".format(e) app.logger.info("Joining {q} with neighbours {n}".format(q=quorum_id, n=neighbours)) # store neighbour info in app app.config[QUORUMS][quorum_id] = neighbours # get sawtooth container ip address ips = [n.pop(DOCKER_IP) for n in app.config[QUORUMS][quorum_id]] ips.append(app.config[PBFT_INSTANCES].ip(quorum_id)) app.config[PBFT_INSTANCES].peer_join( quorum_id, ips) # use sawtooth container ip to start sawtooth return ROUTE_EXECUTED_CORRECTLY
def forward(app, url_subdirectory: str, quorum_id: str, json_data): neighbour__in_target_quorum = [] app.logger.info('Looking for neighbour to forward request to') app.logger.debug('request:') app.logger.debug(json_data) for check_quorum_id in list(app.config[QUORUMS].keys()): for intersecting_quorum in app.config[QUORUMS][check_quorum_id]: intersecting_quorum_id = intersecting_quorum[QUORUM_ID] if intersecting_quorum_id == quorum_id: neighbour__in_target_quorum.append(intersecting_quorum) forwarding_to_neighbour = random.choice(neighbour__in_target_quorum) url = URL_REQUEST.format(hostname=forwarding_to_neighbour[API_IP], port=forwarding_to_neighbour[PORT]) url += url_subdirectory app.logger.info( "request in quorum this peer is not a member of forwarding to " "{}".format(url)) try: forwarding_request = requests.post(url, json=json_data) forwarding_request = get_plain_text(forwarding_request) app.logger.info( "response form forward is {}".format(forwarding_request)) return forwarding_request except ConnectionError as e: app.logger.error("{host}:{port} unreachable".format( host=forwarding_to_neighbour[API_IP], port=forwarding_to_neighbour[PORT])) app.logger.error(e) return ROUTE_EXECUTION_FAILED.format( msg="forward to {} failed".format(url))
def get_json(request, app): # try and parse json try: req = request.get_json(force=True) except KeyError as e: app.logger.error(e) return ROUTE_EXECUTION_FAILED.format(msg=e) return req
def forward(app, url_subdirectory: str, quorum_id: str, json_data): for check_quorum_id in list(app.config[QUORUMS].keys()): for intersecting_quorum in app.config[QUORUMS][check_quorum_id]: intersecting_quorum_id = intersecting_quorum[QUORUM_ID] if intersecting_quorum_id == quorum_id: url = URL_REQUEST.format(hostname=intersecting_quorum[API_IP], port=intersecting_quorum[PORT]) url += url_subdirectory app.logger.info( "request in quorum this peer is not a member of forwarding to " "{}".format(url)) try: forwarding_request = requests.post(url, json=json_data) forwarding_request = get_plain_text(forwarding_request) app.logger.info("response form forward is {}".format( forwarding_request)) return forwarding_request except ConnectionError as e: app.logger.error("{host}:{port} unreachable".format( host=intersecting_quorum[API_IP], port=intersecting_quorum[PORT])) app.logger.error(e) return ROUTE_EXECUTION_FAILED.format( msg="forward to {} failed".format(url))
def ip(quorum_id): if app.config[PBFT_INSTANCES].in_committee(quorum_id): return app.config[PBFT_INSTANCES].ip(quorum_id) else: return ROUTE_EXECUTION_FAILED.format( msg="peer not in quorum {}".format(quorum_id))