Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
doQueryTest = False
if doQueryTest:
    print(
        "\n>>---------------------------------------------------------------------"
    )
    res = client.getNodeVersion()
    print(
        "\n>>---------------------------------------------------------------------"
    )
    print("getClientVersion", res)
    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))
Ejemplo n.º 3
0
from worker import b
from worker import public
from client.bcosclient import BcosClient

channel = grpc.insecure_channel("localhost:26500")
stub = gateway_pb2_grpc.GatewayStub(channel)


def handleTask(stub, type, worker):
    for jobResponse in stub.ActivateJobs(
            gateway_pb2.ActivateJobsRequest(type=type,
                                            worker=worker,
                                            timeout=1000,
                                            maxJobsToActivate=32)):
        for job in jobResponse.jobs:
            if type == "task1":
                a.Task1(job, stub)
            else:
                b.Task2(job, stub)


client = BcosClient()
initHeight = client.getBlockNumber()

# handleTask(stub, "task1", "task1")
_thread.start_new_thread(handleTask, (stub, "task1", "task1"))
_thread.start_new_thread(handleTask, (stub, "task2", "task2"))
_thread.start_new_thread(public.EventEmitter, (stub, initHeight))

while 1:
    pass