Skip to content

Instantly share code, notes, and snippets.

@danintel
Created June 21, 2018 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danintel/c9a2ee89c18223e1dafe879f19d6d46f to your computer and use it in GitHub Desktop.
Save danintel/c9a2ee89c18223e1dafe879f19d6d46f to your computer and use it in GitHub Desktop.
Patch to add Protobuf (Protocol Buffer) serializatio to Sawtooth Cookiejar application
13 pyclient/cookiejar_client.py
@@ -33,6 +33,10 @@
from sawtooth_sdk.protobuf.batch_pb2 import BatchHeader
from sawtooth_sdk.protobuf.batch_pb2 import Batch
+from sys import path
+path.append('../proto')
+from cookiejar_pb2 import CookiejarTransaction
+
# The Transaction Family Name
FAMILY_NAME = 'cookiejar'
# TF Prefix is first 6 characters of SHA-512("cookiejar"), a4d219
@@ -141,10 +145,11 @@ def _wrap_and_send(self, action, amount):
Called by bake() and eat().
'''
- # Generate a CSV UTF-8 encoded string as the payload.
- raw_payload = action
- raw_payload = ",".join([raw_payload, str(amount)])
- payload = raw_payload.encode() # Convert Unicode to bytes
+ # Generate a Protobuf encoded string as the payload.
+ transaction = CookiejarTransaction()
+ transaction.action = action
+ transaction.amount = int(amount)
+ payload = transaction.SerializeToString()
# Construct the address where we'll store our state.
# We just have one input and output address (the same one).
1 pyprocessor/Dockerfile
@@ -23,6 +23,7 @@ RUN \
apt-transport-https \
build-essential \
ca-certificates \
+ python3-protobuf \
python3-sawtooth-sdk \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
13 pyprocessor/cookiejar_tp.py
@@ -24,6 +24,10 @@
from sawtooth_sdk.processor.exceptions import InternalError
from sawtooth_sdk.processor.core import TransactionProcessor
+from sys import path
+path.append('../proto')
+from cookiejar_pb2 import CookiejarTransaction
+
# hard-coded for simplicity (otherwise get the URL from the args in main):
#DEFAULT_URL = 'tcp://localhost:4004'
# For Docker:
@@ -87,11 +91,12 @@ def apply(self, transaction, context):
# Get the payload and extract the cookiejar-specific information.
# It has already been converted from Base64, but needs deserializing.
- # It was serialized with CSV: action, value
+ # It was serialized with Protobuf: string action, uint32 amount
header = transaction.header
- payload_list = transaction.payload.decode().split(",")
- action = payload_list[0]
- amount = payload_list[1]
+ cookie = CookiejarTransaction()
+ cookie.ParseFromString(transaction.payload)
+ action = cookie.action
+ amount = str(cookie.amount)
# Get the signer's public key, sent in the header from the client.
from_key = header.signer_public_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment