def do_load(args): with open(args.filename, mode='rb') as fd: batches = batch_pb2.BatchList() batches.ParseFromString(fd.read()) stream = Stream(args.url) futures = [] start = time.time() for batch_list in _split_batch_list(batches): future = stream.send( message_type=Message.CLIENT_BATCH_SUBMIT_REQUEST, content=batch_list.SerializeToString()) futures.append(future) for future in futures: result = future.result() try: assert result.message_type == Message.CLIENT_BATCH_SUBMIT_RESPONSE except ValidatorConnectionError as vce: LOGGER.warning("the future resolved to %s", vce) stop = time.time() print("batches: {} batch/sec: {}".format( str(len(batches.batches)), len(batches.batches) / (stop - start))) stream.close()
def listen_to_events(delta_filters=None): '''Listen to cookiejar state-delta events.''' # Subscribe to events block_commit_subscription = events_pb2.EventSubscription( event_type="sawtooth/block-commit") state_delta_subscription = events_pb2.EventSubscription( event_type="sawtooth/state-delta", filters=delta_filters) fine_subscription = events_pb2.EventSubscription( event_type="AC is in good condition") problem_subscription = events_pb2.EventSubscription( event_type="AC is malfunctioning") fixed_subscription = events_pb2.EventSubscription( event_type="Maintenance fixed the AC") notfixed_subscription = events_pb2.EventSubscription( event_type="Maintenance hasn't fixed the AC yet") request = client_event_pb2.ClientEventsSubscribeRequest( subscriptions=[fine_subscription,problem_subscription,fixed_subscription,notfixed_subscription]) # Send the subscription request stream = Stream(DEFAULT_VALIDATOR_URL) msg = stream.send(message_type=Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST, content=request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_SUBSCRIBE_RESPONSE # Parse the subscription response response = client_event_pb2.ClientEventsSubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsSubscribeResponse.OK # Listen for events in an infinite loop print("Listening to events.") msg = stream.receive().result() assert msg.message_type == Message.CLIENT_EVENTS # Parse the response event_list = events_pb2.EventList() event_list.ParseFromString(msg.content) print("Received the following events: ----------") notification=[] for event in event_list.events: notification.append((event.event_type,event.attributes)) #server_socket(notification) # Unsubscribe from events request = client_event_pb2.ClientEventsUnsubscribeRequest() msg = stream.send(Message.CLIENT_EVENTS_UNSUBSCRIBE_REQUEST, request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_UNSUBSCRIBE_RESPONSE # Parse the unsubscribe response response = client_event_pb2.ClientEventsUnsubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsUnsubscribeResponse.OK return notification
def start(self, endpoint): self._stream = Stream(endpoint) startup_state = self._register() # Validators version 1.1 send startup info with the registration # response; newer versions will send an activation message with the # startup info if startup_state is None: startup_state = self._wait_until_active() self._updates = Queue() driver_thread = Thread( target=self._driver_loop) driver_thread.start() try: self._engine.start( self._updates, ZmqService( stream=self._stream, timeout=SERVICE_TIMEOUT), startup_state) except Exception: # pylint: disable=broad-except LOGGER.exception("Uncaught engine exception") self.stop() driver_thread.join()
def main(): stream = None try: opts = parse_args(sys.argv[1:]) stream = Stream(opts.stream_url) log_config = get_log_config(filename="rest_api_log_config.toml") if log_config is not None: log_configuration(log_config=log_config) else: log_dir = get_log_dir() log_configuration(log_dir=log_dir, name="sawtooth_rest_api") init_console_logging(verbose_level=opts.verbose) start_rest_api( opts.host, int(opts.port), stream, int(opts.timeout)) # pylint: disable=broad-except except Exception as e: print("Error: {}".format(e), file=sys.stderr) sys.exit(1) finally: if stream is not None: stream.close()
def start(self, endpoint): self._stream = Stream(endpoint) (chain_head, peers) = self._register() self._updates = Queue() engine_thread = Thread( target=self._engine.start, args=(self._updates, ZmqService(stream=self._stream, timeout=SERVICE_TIMEOUT, name=self._engine.name(), version=self._engine.version()), chain_head, peers)) engine_thread.start() while True: if self._exit: self._engine.stop() engine_thread.join() break try: message = self._stream.receive().result(0.1) except concurrent.futures.TimeoutError: continue result = self._process(message) self._updates.put(result)
def __init__(self, url): """ Args: url (string): The URL of the validator """ self._stream = Stream(url) self._url = url self._handlers = []
def __init__(self, url): """ Args: url (string): The URL of the validator """ self._stream = Stream(url) self._url = url self._handlers = [] self._highest_sdk_feature_requested = \ self._FeatureVersion.FEATURE_UNUSED self._header_style = TpRegisterRequest.HEADER_STYLE_UNSET
def do_subscribe(opts): opts_config = SubscriberConfig( connect=opts.connect, database_name=opts.database_name, database_host=opts.database_host, database_port=opts.database_port, database_user=opts.database_user, database_password=opts.database_password) subscriber_config = load_subscriber_config(opts_config) subscriber = None stream = None connection = None # pylint: disable=broad-except try: url = None if "tcp://" not in subscriber_config.connect: url = "tcp://" + subscriber_config.connect else: url = subscriber_config.connect stream = Stream(url) connection = psycopg2.connect( dbname=subscriber_config.database_name, host=subscriber_config.database_host, port=subscriber_config.database_port, user=subscriber_config.database_user, password=subscriber_config.database_password) subscriber = Subscriber(stream, connection) log_config = get_log_config( filename="supplychain_sds_log_config.toml") if log_config is not None: log_configuration(log_config=log_config) else: log_dir = get_log_dir() # use the stream zmq identity for filename log_configuration( log_dir=log_dir, name="supplychain-sds-" + str(stream.zmq_id)[2:-1]) subscriber.start() except KeyboardInterrupt: pass except Exception as e: print('Error: {}'.format(e), file=sys.stderr) finally: if subscriber is not None: subscriber.shutdown() if stream is not None: stream.close() if connection is not None: connection.close()
def listen_to_events(delta_filters=None): '''Listen to cookiejar state-delta events.''' # Subscribe to events block_commit_subscription = events_pb2.EventSubscription( event_type="sawtooth/block-commit") state_delta_subscription = events_pb2.EventSubscription( event_type="sawtooth/state-delta", filters=delta_filters) bake_subscription = events_pb2.EventSubscription( event_type="cookiejar/bake") eat_subscription = events_pb2.EventSubscription(event_type="cookiejar/eat") request = client_event_pb2.ClientEventsSubscribeRequest(subscriptions=[ block_commit_subscription, state_delta_subscription, bake_subscription, eat_subscription ]) # Send the subscription request stream = Stream(DEFAULT_VALIDATOR_URL) msg = stream.send(message_type=Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST, content=request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_SUBSCRIBE_RESPONSE # Parse the subscription response response = client_event_pb2.ClientEventsSubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsSubscribeResponse.OK # Listen for events in an infinite loop print("Listening to events.") while True: msg = stream.receive().result() assert msg.message_type == Message.CLIENT_EVENTS # Parse the response event_list = events_pb2.EventList() event_list.ParseFromString(msg.content) print("Received the following events: ----------") for event in event_list.events: print(event) # Unsubscribe from events request = client_event_pb2.ClientEventsUnsubscribeRequest() msg = stream.send(Message.CLIENT_EVENTS_UNSUBSCRIBE_REQUEST, request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_UNSUBSCRIBE_RESPONSE # Parse the unsubscribe response response = client_event_pb2.ClientEventsUnsubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsUnsubscribeResponse.OK
def unsubscribe_from_events(): # Unsubscribe from events stream = Stream(DEFAULT_VALIDATOR_URL) request = client_event_pb2.ClientEventsUnsubscribeRequest() msg = stream.send(Message.CLIENT_EVENTS_UNSUBSCRIBE_REQUEST, request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_UNSUBSCRIBE_RESPONSE # Parse the unsubscribe response response = client_event_pb2.ClientEventsUnsubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsUnsubscribeResponse.OK
def __init__(self, service, component_endpoint, config_dir, data_dir, key_dir): self._config_dir = config_dir self._data_dir = data_dir self._signer = _load_identity_signer(key_dir, 'validator') self._validator_id = self._signer.get_public_key().as_hex() stream = Stream(component_endpoint) self._block_cache = _BlockCacheProxy(service, stream) self._state_view_factory = _StateViewFactoryProxy(service) self._batch_publisher = _BatchPublisherProxy(stream, self._signer) self._publisher = None
def __init__(self, family_handler, test_helper=None, keyfile=PRIV_KEY_FILE): self.url = REST_API_URL self._family_handler = family_handler self.test_helper = test_helper self._stream = Stream(ZMQ_URL) try: self._signer = self.get_signer_priv_key_from_file(keyfile) except ClientException as e: LOGGER.warn('Could not set up signer from file, detailed: %s', e) self._signer = self.generate_signer(keyfile)
def listen_to_events(): # Listen for events in an infinite loop stream = Stream(DEFAULT_VALIDATOR_URL) print("Listening to events.") while True: msg = stream.receive().result() assert msg.message_type == Message.CLIENT_EVENTS # Parse the response event_list = events_pb2.EventList() event_list.ParseFromString(msg.content) print("Received the following events: ----------") for event in event_list.events: print(event)
def __init__(self, url, benchmark, expected_event_count): self._event = '{}-{}'.format(benchmark, EVENT_SUFFIX) self._url = url self.counter = 0 self._is_active = False self._expected_event_count = expected_event_count self._stream = Stream('tcp://{}:4004'.format(self._url)) date = datetime.date.today() logging_file_name = '{}-{}-benchmark-events.csv'.format( str(date), benchmark) self._event_log_file = path.join(DEFAULT_MONITORING_FOLDER, logging_file_name)
def main(): stream = None try: opts = parse_args(sys.argv[1:]) opts_config = RestApiConfig(bind=opts.bind, connect=opts.connect, timeout=opts.timeout) rest_api_config = load_rest_api_config(opts_config) if "tcp://" not in rest_api_config.connect: stream = Stream("tcp://" + rest_api_config.connect) else: stream = Stream(rest_api_config.connect) log_config = get_log_config(filename="rest_api_log_config.toml") if log_config is not None: log_configuration(log_config=log_config) else: log_dir = get_log_dir() log_configuration(log_dir=log_dir, name="sawtooth_rest_api") init_console_logging(verbose_level=opts.verbose) try: host, port = rest_api_config.bind[0].split(":") port = int(port) except ValueError as e: print("Unable to parse binding {}: Must be in the format" " host:port".format(rest_api_config.bind[0])) sys.exit(1) start_rest_api(host, port, stream, int(rest_api_config.timeout)) # pylint: disable=broad-except except Exception as e: print("Error: {}".format(e), file=sys.stderr) sys.exit(1) finally: if stream is not None: stream.close()
def main(): stream = None try: opts = parse_args(sys.argv[1:]) stream = Stream(opts.stream_url) start_rest_api( opts.host, int(opts.port), stream, int(opts.timeout)) # pylint: disable=broad-except except Exception as e: print("Error: {}".format(e), file=sys.stderr) sys.exit(1) finally: if stream is not None: stream.close()
def __init__(self, family_handler, test_helper=None, keyfile=None): config = load_toml_with_defaults( '/config/remme-client-config.toml')['remme']['client'] self.url = config['validator_rest_api_url'] self._family_handler = family_handler self.test_helper = test_helper self._stream = Stream( f'tcp://{ config["validator_ip"] }:{ config["validator_port"] }') if keyfile is None: keyfile = PRIV_KEY_FILE try: self._signer = self.get_signer_priv_key_from_file(keyfile) except ClientException as e: LOGGER.warn('Could not set up signer from file, detailed: %s', e) self._signer = self.generate_signer(keyfile)
def __init__(self, service, component_endpoint, config_dir, data_dir, key_dir): self._config_dir = config_dir self._data_dir = data_dir LOGGER.debug('BgtOracle: Stream key_dir=%s', key_dir) self._signer = _load_identity_signer(key_dir, 'validator') self._validator_id = self._signer.get_public_key().as_hex() self._service = service LOGGER.debug('BgtOracle: Stream component_endpoint=%s', component_endpoint) stream = Stream(component_endpoint) self._block_cache = _BlockCacheProxy(service, stream) self._state_view_factory = _StateViewFactoryProxy(service) self._batch_publisher = _BatchPublisherProxy(stream, self._signer) self._publisher = None self._can_fail_block = False #True LOGGER.debug('BgtOracle:validator=%s init DONE', _short_id(self._validator_id))
def start(self, endpoint): self._stream = Stream(endpoint) startup_state = self._register() self._updates = Queue() driver_thread = Thread(target=self._driver_loop) driver_thread.start() try: self._engine.start( self._updates, ZmqService(stream=self._stream, timeout=SERVICE_TIMEOUT), startup_state) except Exception: # pylint: disable=broad-except LOGGER.exception("Uncaught engine exception") self.stop() driver_thread.join()
def start(self, endpoint): LOGGER.debug('ZmqDriver: start endpoint=%s', endpoint) self._stream = Stream(endpoint) startup_state = self._register() self._updates = Queue() driver_thread = Thread(target=self._driver_loop) driver_thread.start() try: self._engine.start( self._updates, ZmqService(stream=self._stream, timeout=SERVICE_TIMEOUT, name=self._engine.name(), version=self._engine.version()), startup_state) except Exception as ex: # pylint: disable=broad-except LOGGER.exception("Uncaught engine exception(%s)", ex) self.stop() driver_thread.join()
def __init__(self, service, component_endpoint, config_dir, data_dir, key_dir): self._config_dir = config_dir self._data_dir = data_dir self._service = service LOGGER.debug('PbftOracle: Stream key_dir=%s', key_dir) self._signer = _load_identity_signer(key_dir, 'validator') self._validator_id = self._signer.get_public_key().as_hex() LOGGER.debug('PbftOracle: Stream component_endpoint=%s ', component_endpoint) stream = Stream(component_endpoint) self._block_cache = _BlockCacheProxy(service, stream) self._state_view_factory = _StateViewFactoryProxy(service) self._batch_publisher = _BatchPublisherProxy(stream, self._signer) self._publisher = None self._consensus_state_store = ConsensusStateStore( data_dir=self._data_dir, validator_id=self._validator_id) LOGGER.debug('PbftOracle: _validator_id=%s init DONE', self._validator_id)
def subscribe_to_events(delta_filters=None): '''Listen to attestation state-delta events.''' # Subscribe to events trust_path_subscription = events_pb2.EventSubscription( event_type="attestation/trustpath", filters=delta_filters) trust_entry_subscription = events_pb2.EventSubscription( event_type="attestation/entrypoint", filters=delta_filters) request = client_event_pb2.ClientEventsSubscribeRequest( subscriptions=[trust_path_subscription, trust_entry_subscription]) # Send the subscription request stream = Stream(DEFAULT_VALIDATOR_URL) msg = stream.send(message_type=Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST, content=request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_SUBSCRIBE_RESPONSE # Parse the subscription response response = client_event_pb2.ClientEventsSubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsSubscribeResponse.OK
def __init__(self, validatorUrl): self.stream = Stream(validatorUrl)
def on_validator_discovered(self, url): stream = Stream(url) self._streams.append(stream)
def __init__(self, validator_url): LOGGER.info("Connecting to validator: %s", validator_url) self._stream = Stream(validator_url) self._delta_handlers = [] self._is_active = False
def __init__(self, url=None): self._stream = Stream(url or VALIDATOR_URL) self._handlers = []
def setUp(self): self.url = "tcp://validator:4004" self.stream = Stream(self.url)
def listen_to_events(delta_filters=None): BenLogDB.set_table('beneficiary_log', '(id,beneficiary_id,beneficiary_type_id)') '''Listen to vaccination state-delta events.''' # Subscribe to events add_beneficiary_subscription = events_pb2.EventSubscription( event_type="Beneficiary/Add_Beneficiary", filters=delta_filters) block_commit_subscription = events_pb2.EventSubscription( event_type="sawtooth/block-commit", filters=delta_filters) #Create subscription request requestBen = client_event_pb2.ClientEventsSubscribeRequest( subscriptions=[ block_commit_subscription, add_beneficiary_subscription ], last_known_block_ids=['0000000000000000']) # Send the subscription request streamBen = Stream(BEN_VALIDATOR_URL) msgBen = streamBen.send( message_type=Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST, content=requestBen.SerializeToString()).result() assert msgBen.message_type == Message.CLIENT_EVENTS_SUBSCRIBE_RESPONSE # Parse the subscription response responseBen = client_event_pb2.ClientEventsSubscribeResponse() responseBen.ParseFromString(msgBen.content) assert responseBen.status == client_event_pb2.ClientEventsSubscribeResponse.OK # Listen for events in an infinite loop while True: msgBen = streamBen.receive().result() assert msgBen.message_type == Message.CLIENT_EVENTS # Parse the response event_list_ben = events_pb2.EventList() event_list_ben.ParseFromString(msgBen.content) # Log each Beneficiary event into the DB for event in event_list_ben.events: if event.event_type == "Beneficiary/Add_Beneficiary": print("Received the beneficiry event", flush=True) print("Beneficiary ID : ", event.attributes[0].value, flush=True) print("Beneficiary Type : ", event.attributes[1].value, flush=True) BenLogDB.insert_data( uuid.uuid4(), #uuid event.attributes[0].value, #beneficiaryId event.attributes[1].value) #beneficiaryType # Unsubscribe from events request = client_event_pb2.ClientEventsUnsubscribeRequest() msg = stream.send(Message.CLIENT_EVENTS_UNSUBSCRIBE_REQUEST, request.SerializeToString()).result() assert msg.message_type == Message.CLIENT_EVENTS_UNSUBSCRIBE_RESPONSE # Parse the unsubscribe response response = client_event_pb2.ClientEventsUnsubscribeResponse() response.ParseFromString(msg.content) assert response.status == \ client_event_pb2.ClientEventsUnsubscribeResponse.OK
def __init__(self, url): self._stream = Stream(url) self._url = url self._handlers = []
def __init__(self, validator_url): LOGGER.info('Connecting to validator: %s', validator_url) self._stream = Stream(validator_url) self._event_handlers = [] self._is_active = False