Search code examples
pythonhyperledger-sawtooth

Sawtooth Python SDK add_event doesn't work


I have created a processor in python that work like a charm, I have also created a sync module that subscribe to some events and at this time i have problems.

my code for the sync module is the following

if __name__ == '__main__':
    # start the subscription
    event_admin_add = EventSubscription(event_type="soundchain/create")
    subscription = EventSubscription(event_type="sawtooth/state-delta")
    ctx = zmq.Context()
    socket = ctx.socket(zmq.DEALER)
    validator_url = "tcp://{}:{}".format(os.environ['VALIDATOR'], os.environ['VALIDATOR_PORT'])
    logger.info("validator url {}".format(validator_url))
    socket.connect(validator_url)

    # Construct the request
    request = ClientEventsSubscribeRequest(
       subscriptions=[event_admin_add, 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.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.CLIENT_EVENTS_SUBSCRIBE_RESPONSE:
        logger.error("unexpected message type")
        exit()
    # Parse the response
    response = ClientEventsSubscribeResponse()
    response.ParseFromString(msg.content)

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

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

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

        # Validate the response type
        if msg.message_type != Message.CLIENT_EVENTS:
            logger.error("unexpected message type")
            exit()

        # Parse the response
        events = EventList()
        events.ParseFromString(msg.content)
        logger.info("event {}".format(events))

It's a copy and paste from the Sawtooth tutorial, so nothing special.

from the processor handler i have added the following line to add an event:

    .....
    entries = {}
    payload.add_to_body('role', "admin")
    entries[address] = payload.get_body_binary()
    context.add_event("soundchain/create",
                      {
                          'creator': public_key,
                          "role": "admin"
                      }
                      )
    context.set_state(entries, timeout=2)

Now if i remove the method context.add_event and I subscribe the sync module only with sawtooth/block-commit or with sawtooth/state-delta, all work well, but when i add the method the validator is put the batches in PENDING and nothing happen, no event emitted and practically everithing crash

i do something wrong? thanks in advance for your help


Solution

  • I have solved the problem

    first of all the sawtooth documentation need a change. In the documentation is explained that the event would be launched in that way

    context.add_event(
    "xo/create", {
        'name': name,
        'creator': signer_public_key
    })
    

    and this is wrong, the correct way is:

    context.add_event(
            event_type="soundchain/create",
            attributes=[("name", "admin")])
    

    So, the attributes should be incapsulated in a List in that way everithing work well