def EventEmitter(stub, initHeight):
    data_parser = DatatypeParser()
    try:
        client = BcosClient()
        lastHeight = initHeight
        while 1:
            currHeight = client.getBlockNumber()
            if currHeight > lastHeight:
                for height in range(lastHeight + 1, currHeight + 1):
                    result = client.getBlockByNumber(
                        height, _includeTransactions=False)
                for hash in result["transactions"]:
                    receipt = client.getTransactionReceipt(hash)
                    logs = data_parser.parse_event_logs(receipt["logs"])
                    for log in logs:
                        print(log)
                        if log["eventname"] == "Task1Finished":
                            stub.PublishMessage(
                                gateway_pb2.PublishMessageRequest(
                                    name="task1",
                                    correlationKey="task1",
                                    variables=json.dumps(
                                        {"contract": receipt["to"]})))
                        elif log["eventname"] == "Task2Finished":
                            stub.PublishMessage(
                                gateway_pb2.PublishMessageRequest(
                                    name="task2",
                                    correlationKey=receipt["to"]))
    except BcosException as e:
        print("execute demo_transaction failed for: {}".format(e))
    except BcosError as e:
        print("execute demo_transaction failed for: {}".format(e))
Beispiel #2
0
 print(
     "\n>>---------------------------------------------------------------------"
 )
 try:
     res = client.getBlockNumber()
     print("getBlockNumber", res)
 except BcosError as e:
     print("bcos client error,", e.info())
 print(
     "\n>>---------------------------------------------------------------------"
 )
 print("getPeers", client.getPeers())
 print(
     "\n>>---------------------------------------------------------------------"
 )
 print("getBlockByNumber", client.getBlockByNumber(50))
 print(
     "\n>>---------------------------------------------------------------------"
 )
 print("getBlockHashByNumber", client.getBlockHashByNumber(50))
 print(
     "\n>>---------------------------------------------------------------------"
 )
 print(
     "getBlockByHash",
     client.getBlockByHash(
         "0xe7588bf4ee5a6fb5aae9bdcc2c4f3c58cf7a789b15a4daa6617ed594b5ba3951"
     ))
 print(
     "\n>>---------------------------------------------------------------------"
 )
Beispiel #3
0
 print(
     "\n>>---------------------------------------------------------------------"
 )
 try:
     res = client.getBlockNumber()
     print("getBlockNumber", json.dumps(res, indent=4))
 except BcosError as e:
     print("bcos client error,", e.info())
 print(
     "\n>>---------------------------------------------------------------------"
 )
 print("getPeers", client.getPeers())
 print(
     "\n>>---------------------------------------------------------------------"
 )
 print("getBlockByNumber", json.dumps(client.getBlockByNumber(1)))
 print(
     "\n>>---------------------------------------------------------------------"
 )
 blockhash = client.getBlockHashByNumber(1)
 print("getBlockHashByNumber", blockhash)
 print(
     "\n>>---------------------------------------------------------------------"
 )
 block = client.getBlockByHash(blockhash)
 print("getBlockByHash", block)
 if isinstance(block, dict) and "transactions" in block.keys():
     txhash = block["transactions"][0]["hash"]
     print(
         "\n>>---------------------------------------------------------------------"
     )