def apply(self, transaction, context): transaction_payload = TransactionPayload() transaction_payload.ParseFromString(transaction.payload) state_processor = self.get_state_processor() try: data_pb = state_processor[transaction_payload.method][PB_CLASS]() data_pb.ParseFromString(transaction_payload.data) processor = state_processor[transaction_payload.method][PROCESSOR] updated_state = processor(context, transaction.header.signer_public_key, data_pb) except KeyError: raise InvalidTransaction( 'Unknown value {} for the pub_key operation type.'.format( int(transaction_payload.method))) except ParseError: raise InvalidTransaction('Cannot decode transaction payload') addresses = context.set_state( {k: v.SerializeToString() for k, v in updated_state.items()}) if len(addresses) < len(updated_state): raise InternalError('Failed to update all of states. Updated: {}. ' 'Full list of states to update: {}.'.format( addresses, updated_state.keys())) event_name = state_processor[transaction_payload.method].get( EMIT_EVENT, None) if event_name: add_event( context, event_name, get_event_attributes(updated_state, transaction.signature))
def apply(self, transaction, context): transaction_payload = TransactionPayload() transaction_payload.ParseFromString(transaction.payload) state_processor = self.get_state_processor() try: data_pb = state_processor[transaction_payload.method]['pb_class']() data_pb.ParseFromString(transaction_payload.data) processor = state_processor[ transaction_payload.method]['processor'] updated_state = processor(context, transaction.header.signer_public_key, data_pb) except KeyError: raise InvalidTransaction( 'Unknown value {} for the pub_key operation type.'.format( int(transaction_payload.method))) except ParseError: raise InvalidTransaction('Cannot decode transaction payload') print(self._family_name) addresses = context.set_state( {k: v.SerializeToString() for k, v in updated_state.items()}) if len(addresses) < len(updated_state): raise InternalError('Failed to update all of states. Updated: {}. ' 'Full list of states to update: {}.'.format( addresses, updated_state.keys()))
async def send_raw_transaction(request): try: data = request.params['data'] except KeyError: raise RpcInvalidParamsError(message='Missed data') try: transaction = base64.b64decode(data.encode('utf-8')) except Exception: raise RpcGenericServerDefinedError( error_code=-32050, message='Decode payload of tranasaction failed' ) try: tr_pb = Transaction() tr_pb.ParseFromString(transaction) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction proto' ) try: tr_head_pb = TransactionHeader() tr_head_pb.ParseFromString(tr_pb.header) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction head proto' ) try: tr_payload_pb = TransactionPayload() tr_payload_pb.ParseFromString(tr_pb.payload) except DecodeError: raise RpcGenericServerDefinedError( error_code=-32050, message='Failed to parse transaction payload proto' ) prefix, public_key = tr_head_pb.signer_public_key[:2], \ tr_head_pb.signer_public_key[2:] if prefix in ('02', '03') and len(public_key) != 64: raise RpcGenericServerDefinedError( error_code=-32050, message='Signer public key format is not valid' ) try: handler = TP_HANDLERS[tr_head_pb.family_name] except KeyError: raise RpcGenericServerDefinedError( error_code=-32050, message='Validation handler not set for this method' ) validator = _get_proto_validator(handler, tr_payload_pb) if validator and not validator.validate(): logger.debug('Form "send_raw_transaction" validator errors: ' f'{validator.errors}') raise RpcGenericServerDefinedError( error_code=-32050, message=f'Invalid "{validator.get_pb_class().__name__}" ' 'structure' ) client = PubKeyClient() response = await client.send_raw_transaction(tr_pb) return response['data']
def apply(self, transaction, context): """ Accept transaction request that passed from validator. Validator accept not transaction request, but transaction, that have a bit another structure. The flow is: transaction -> RPC -> validator -> (transaction request) handler's apply method. Arguments: transaction (sawtooth_sdk.protobuf.processor_pb2.TpProcessRequest): transaction request. context (sawtooth_sdk.processor.context.Context): context to store and retrieve address data. References: - https://sawtooth.hyperledger.org/docs/core/releases/1.0/sdks/python_sdk/processor.html? highlight=apply#processor.handler.TransactionHandler.apply - https://sawtooth.hyperledger.org/docs/core/releases/1.0/_autogen/sdk_TP_tutorial_python.html? highlight=apply#transaction-processor-tutorial-python - https://sawtooth.hyperledger.org/docs/core/releases/1.0.1/architecture/transactions_and_batches.html - https://github.com/hyperledger/sawtooth-core/blob/10a04a56ae29b5e4b38127399dc01552db500c0a/ protos/processor.proto#L81 - https://github.com/hyperledger/sawtooth-core/blob/master/sdk/python/sawtooth_sdk/processor/context.py """ try: transaction_payload = TransactionPayload() transaction_payload.ParseFromString(transaction.payload) except DecodeError: raise InvalidTransaction('Cannot decode transaction payload.') state_processor = self.get_state_processor() try: data_pb = state_processor[transaction_payload.method][PB_CLASS]() data_pb.ParseFromString(transaction_payload.data) processor = state_processor[transaction_payload.method][PROCESSOR] validator_class = state_processor[ transaction_payload.method][VALIDATOR] except KeyError: raise InvalidTransaction( f'Invalid account method value ({transaction_payload.method}) has been set.' ) validator = validator_class.load_proto(data_pb) if not validator.validate(): raise InvalidTransaction(f'Invalid protobuf data of ' f'"{validator._pb_class.__name__}", ' f'detailed: {validator.errors}') measurement = METRICS_SENDER.get_time_measurement( f'tp.{self._family_name}.{transaction_payload.method}') context_service = CacheContextService(context=context) context_service.preload_state(transaction.header.inputs) updated_state = processor(context_service, transaction.header.signer_public_key, data_pb) context_service.set_state( {k: v.SerializeToString() for k, v in updated_state.items()}) event_name = state_processor[transaction_payload.method].get( EMIT_EVENT, None) if event_name: event_attributes = get_event_attributes(updated_state, transaction.signature) add_event(context_service, event_name, event_attributes) measurement.done()