锯齿 Python SDK add_event 不工作

Sawtooth Python SDK add_event doesn't work

我在 python 中创建了一个非常棒的处理器,我还创建了一个同步模块来订阅一些事件,但此时我遇到了问题。

我的同步模块代码如下

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))

它是 Sawtooth 教程的复制和粘贴,所以没什么特别的。

我在处理器处理程序中添加了以下行以添加事件:

    .....
    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)

现在,如果我删除方法 context.add_event 并且我仅使用 sawtooth/block-commit 或 sawtooth/state-delta 订阅同步模块,一切正常,但是当我添加方法时,验证器是将批次置于 PENDING 状态,但什么也没有发生,没有事件发出,几乎一切都崩溃了

我做错了什么? 提前感谢您的帮助

我已经解决了问题

首先锯齿文档需要修改。 在文档中解释说事件将以这种方式启动

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

这是错误的,正确的做法是:

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

所以,属性应该封装在一个List中 这样一切都很好