Search code examples
python-3.xpublish-subscribehyperledger-sawtooth

How to subscribe to events in sawtooth hyperledger?


I am following the steps given in sawtooth hyperledger api doc to create a delta event subscriber.

For some reason its not working and I see following in sawtooth logs

[2019-07-18 18:14:41.003 INFO dispatch] received a message of type CLIENT_EVENTS_SUBSCRIBE_REQUEST from fc9a2db054180e53269ec4c0cad18482afe6f4307251ca7d673f8b723c3293abc5b1c2c07b827705cdf0d9a145f8f44cdf546971c05fd67244a01e4cb1ccc9c2 but have no handler for that type [2019-07-18 18:16:46.474 INFO interconnect] No response from fc9a2db054180e53269ec4c0cad18482afe6f4307251ca7d673f8b723c3293abc5b1c2c07b827705cdf0d9a145f8f44cdf546971c05fd67244a01e4cb1ccc9c2 in 125.4719786643982 seconds - removing connection.

Also I tried using Sawtooth Simple Supply but I still see the same error. Can somebody tell me what I might be doing wrong?

Sawtooth : 1.1.5

Python : 3.6.8

Tried using different examples. Code:

import zmq
from sawtooth_sdk.protobuf.events_pb2 import EventSubscription, EventFilter, EventList
from sawtooth_sdk.protobuf.client_event_pb2 import ClientEventsSubscribeRequest, ClientEventsSubscribeResponse
from sawtooth_sdk.protobuf import block_pb2, events_pb2, client_event_pb2
from sawtooth_sdk.protobuf import state_context_pb2
from sawtooth_sdk.protobuf import transaction_receipt_pb2
from sawtooth_sdk.protobuf.validator_pb2 import Message
from sawtooth_sdk.processor.core import TransactionProcessor
import time


AUTH_KEY_NAMESPACE = '7f1029.*'
URL='tcp://192.168.17.185:8800'

subscription = EventSubscription(
    event_type="sawtooth/state-delta",
    filters=[
        # Filter to only addresses in the "xo" namespace using a regex
        EventFilter(
            key="address",
            match_string=AUTH_KEY_NAMESPACE,
            filter_type=EventFilter.REGEX_ANY)
    ])

ctx = zmq.Context()
socket = ctx.socket(zmq.DEALER)
socket.connect(URL)

# Construct the request
request = ClientEventsSubscribeRequest(
    subscriptions=[subscription]).SerializeToString()

# Construct the message wrapper
correlation_id = "123" # This must be unique for all in-process requests
msg = Message(
    correlation_id=correlation_id,
    message_type=Message.MessageType.CLIENT_EVENTS_SUBSCRIBE_REQUEST,
    content=request)

# Send the request
socket.send_multipart([msg.SerializeToString()])


# Receive the response
resp = socket.recv_multipart()[-1]

# Parse the message wrapper
msg = Message()
msg.ParseFromString(resp)

# Validate the response type
if msg.message_type != Message.MessageType.CLIENT_EVENTS_SUBSCRIBE_RESPONSE:
    print("Unexpected message type")
    exit(1)

print("Got Subscribe Response")

# Parse the response
msg = Message()
msg.ParseFromString(resp)

# Validate the response type
if msg.message_type != Message.MessageType.CLIENT_EVENTS_SUBSCRIBE_RESPONSE:
    print("Unexpected message type")
    exit(1)

print("Got Subscribe Response")

# Parse the response
response = ClientEventsSubscribeResponse()
response.ParseFromString(msg.content)

# Validate the response status
if response.status != ClientEventsSubscribeResponse.OK:
    print("Subscription failed: {}".format(response.response_message))
    exit(1)

print("Subscribe response is ok")

while True:
  resp = socket.recv_multipart()[-1]

  # Parse the message wrapper
  msg = Message()
  msg.ParseFromString(resp)

  # Validate the response type
  if msg.message_type != CLIENT_EVENTS:
    print("Unexpected message type")
    exit(1)

  # Parse the response
  events = EventList()
  events.ParseFromString(msg.content)

  for event in events:
    print(event)

Solution

  • I have a working example in Python3 of a Hyperledger Sawtooth event handler. I also have one written in Go. See:

    https://github.com/danintel/sawtooth-cookiejar/tree/master/events

    Get rid of the EventFilter and see if it works.

    I never had luck with using ZMQ. I use the Sawtooth Stream interface to send my requests.