def main(): """Entry point function for the client CLI.""" ctx = zmq.Context() socket = ctx.socket(zmq.DEALER) socket.connect(DEFAULT_VALIDATOR_URL) filters = [events_pb2.EventFilter(key="address", match_string=NOTARY_TP_ADDRESS_PREFIX + ".*", filter_type=events_pb2.EventFilter.REGEX_ANY)] listen_to_events(socket, delta_filters=filters)
def main(): print('INIT') filters = [ events_pb2.EventFilter(key="address", match_string=COOKIEJAR_TP_ADDRESS_PREFIX + ".*", filter_type=events_pb2.EventFilter.REGEX_ANY) ] try: # To listen to all events, pass delta_filters=None : #listen_to_events(delta_filters=None) listen_to_events(delta_filters=None) except KeyboardInterrupt: pass except SystemExit as err: raise err except BaseException as err: traceback.print_exc(file=sys.stderr) sys.exit(1)
def main(): '''Entry point function for the client CLI.''' filters = [ events_pb2.EventFilter(key="address", match_string=COOKIEJAR_TP_ADDRESS_PREFIX + ".*", filter_type=events_pb2.EventFilter.SIMPLE_ALL) ] try: # To listen to all events, pass delta_filters=None : # listen_to_events(delta_filters=None) listen_to_events(delta_filters=filters) except KeyboardInterrupt: pass except SystemExit as err: raise err except BaseException as err: traceback.print_exc(file=sys.stderr) sys.exit(1)
def start(self, known_ids=None): """Subscribes to state delta events, and then waits to receive deltas. Sends any events received to delta handlers. """ self._stream.wait_for_ready() LOGGER.debug("Subscribing to client state events") request = client_event_pb2.ClientEventsSubscribeRequest( last_known_block_ids=known_ids, subscriptions=[ events_pb2.EventSubscription( event_type="sawtooth/block-commit"), events_pb2.EventSubscription( event_type="sawtooth/state-delta", filters=[ events_pb2.EventFilter( key="address", match_string="^" + addresser.NAMESPACE + ".*", filter_type=events_pb2.EventFilter.REGEX_ANY, ) ], ), ], ) response_future = self._stream.send( Message.CLIENT_EVENTS_SUBSCRIBE_REQUEST, request.SerializeToString()) response = client_event_pb2.ClientEventsSubscribeResponse() response.ParseFromString(response_future.result().content) # Forked all the way back to genesis, restart with no known_ids if (known_ids and response.status == client_event_pb2.ClientEventsSubscribeResponse.UNKNOWN_BLOCK): return self.start() if response.status != client_event_pb2.ClientEventsSubscribeResponse.OK: raise RuntimeError("Subscription failed with status: {}".format( client_event_pb2.ClientEventsSubscribeResponse.Status.Name( response.status))) self._is_active = True LOGGER.debug("Successfully subscribed to state delta events") while self._is_active: message_future = self._stream.receive() msg = message_future.result() if msg.message_type == Message.CLIENT_EVENTS: event_list = events_pb2.EventList() event_list.ParseFromString(msg.content) events = list(event_list.events) event = StateDeltaEvent(events) delta_count = len(event.state_changes) if delta_count > 0: LOGGER.debug("Received %d deltas for block: %s", delta_count, event.block_id) for handler in self._delta_handlers: handler(event)
def _wrap_and_send(self, action, amount, wait=None): '''Create a transaction, then wrap it in a batch. Even single transactions must be wrapped into a batch. Called by bake() and eat(). ''' amount=str(amount) amount=amount.replace('[','') amount=amount.replace(']','') print(amount) payload = str(amount).encode('utf-8') # Construct the address where we'll store our state. # We just have one input and output address (the same one). input_and_output_address_list = [self._address] # Create a TransactionHeader. header = TransactionHeader( signer_public_key=self._public_key, family_name=FAMILY_NAME, family_version="1.0", inputs=input_and_output_address_list, outputs=input_and_output_address_list, dependencies=[], payload_sha512=_hash(payload), batcher_public_key=self._public_key, nonce=random.random().hex().encode() ).SerializeToString() # Create a Transaction from the header and payload above. transaction = Transaction( header=header, payload=payload, header_signature=self._signer.sign(header) ) transaction_list = [transaction] # Create a BatchHeader from transaction_list above. header = BatchHeader( signer_public_key=self._public_key, transaction_ids=[txn.header_signature for txn in transaction_list] ).SerializeToString() # Create Batch using the BatchHeader and transaction_list above. batch = Batch( header=header, transactions=transaction_list, header_signature=self._signer.sign(header)) # Create a Batch List from Batch above batch_list = BatchList(batches=[batch]) batch_id = batch_list.batches[0].header_signature # Send batch_list to the REST API result = self._send_to_rest_api("batches", batch_list.SerializeToString(), 'application/octet-stream') filters = [events_pb2.EventFilter(key="address", match_string= TP_ADDRESS_PREFIX + ".*", filter_type=events_pb2. EventFilter.REGEX_ANY)] try: # To listen to all events, pass delta_filters=None : #listen_to_events(delta_filters=None) notification=listen_to_events(delta_filters=filters) #print(notification) except KeyboardInterrupt: pass except SystemExit as err: raise err except BaseException as err: traceback.print_exc(file=sys.stderr) sys.exit(1) # Wait until transaction status is COMMITTED, error, or timed out return self._wait_for_status(batch_id, wait, result),notification