def setup(self, request_bytes):
        to_function = ToFunction()
        to_function.ParseFromString(request_bytes)
        #
        # setup
        #
        target_address = to_function.invocation.target
        target_function = self.functions.for_type(target_address.namespace,
                                                  target_address.type)
        if target_function is None:
            raise ValueError("Unable to find a function of type ",
                             target_function)

        # for each state spec defined in target function
        #    if state name is in request -> add to Batch Context
        #    if state name is not in request -> add to missing_state_specs
        provided_state_values = self.provided_state_values(to_function)
        missing_state_specs = []
        resolved_state_values = {}
        for state_name, state_spec in target_function.registered_state_specs.items(
        ):
            if state_name in provided_state_values:
                resolved_state_values[state_name] = provided_state_values[
                    state_name]
            else:
                missing_state_specs.append(state_spec)

        self.batch = to_function.invocation.invocations
        self.context = BatchContext(target_address, resolved_state_values)
        self.target_function = target_function
        if missing_state_specs:
            self.missing_state_specs = missing_state_specs
Esempio n. 2
0
    def setup(self, request_bytes):
        to_function = ToFunction()
        to_function.ParseFromString(request_bytes)
        #
        # setup
        #
        context = BatchContext(to_function.invocation.target, to_function.invocation.state)
        target_function = self.functions.for_type(context.address.namespace, context.address.type)
        if target_function is None:
            raise ValueError("Unable to find a function of type ", target_function)

        self.batch = to_function.invocation.invocations
        self.context = context
        self.target_function = target_function
 async def handle_async(
         self, request_bytes: typing.Union[str, bytes, bytearray]) -> bytes:
     # parse
     pb_to_function = ToFunction()
     pb_to_function.ParseFromString(request_bytes)
     # target address
     pb_target_address = pb_to_function.invocation.target
     sdk_address = sdk_address_from_pb(pb_target_address)
     # target stateful function
     target_fn: StatefulFunction = self.functions.for_typename(
         sdk_address.typename)
     if not target_fn:
         raise ValueError(
             f"Unable to find a function of type {sdk_address.typename}")
     # resolve state
     res = resolve(target_fn.storage_spec, sdk_address.typename,
                   pb_to_function.invocation.state)
     if res.missing_specs:
         pb_from_function = collect_failure(res.missing_specs)
         return pb_from_function.SerializeToString()
     # invoke the batch
     ctx = UserFacingContext(sdk_address, res.storage)
     fun = target_fn.fun
     pb_batch = pb_to_function.invocation.invocations
     if target_fn.is_async:
         for pb_invocation in pb_batch:
             msg = Message(target_typename=sdk_address.typename,
                           target_id=sdk_address.id,
                           typed_value=pb_invocation.argument)
             ctx._caller = sdk_address_from_pb(pb_invocation.caller)
             # await for an async function to complete.
             # noinspection PyUnresolvedReferences
             await fun(ctx, msg)
     else:
         for pb_invocation in pb_batch:
             msg = Message(target_typename=sdk_address.typename,
                           target_id=sdk_address.id,
                           typed_value=pb_invocation.argument)
             ctx._caller = sdk_address_from_pb(pb_invocation.caller)
             # we need to call the function directly ¯\_(ツ)_/¯
             fun(ctx, msg)
     # collect the results
     pb_from_function = collect_success(ctx)
     return pb_from_function.SerializeToString()
Esempio n. 4
0
 def __call__(self, request_bytes):
     request = ToFunction()
     request.ParseFromString(request_bytes)
     reply = self.handle_invocation(request)
     return reply.SerializeToString()