X

Best Practices from Oracle Development's A‑Team

Extending Oracle Blockchain Events with OCI - Part 5 (Configuring Blockchain)

Tamer Qumhieh
Master Principal Technology Evangelist

Introduction

This is Part 5 of the “Extending Oracle Blockchain Events with OCI” series. In previous blog posts I covered:

In this blog series, I’ll be customizing the “Car Dealer” chaincode that is shipped as a sample with Oracle Blockchain Platform to emit an event when a new Vehicle Part is created and added to ledger

Preparing your Chaincode

Will start with Installing and instantiating the “Car Dealer” chaincode:
From the OBP Developer Tools/Samples, install the NodeJS version of Car Dealer chaincode, then instantiate on default channel and expose through RestProxy1


Now download the Chaincode source code and unzip it

In your editor, edit the chaincode file “CarDealer/artifacts/src/github.com/node/cardealer_cc.js”. Locate the method “async initVehiclePart(stub, args, thisClass)” and at the bottom of that method add the following code to emit an event “VehiclePartCreated” when a new part is created and stored in the ledger.

let eventPayload = {
     vehiclePart: vehiclePart
 };

 const eventName = "VehiclePartCreated";
 stub.setEvent(eventName,
     Buffer.from(JSON.stringify(eventPayload)));

Save your file, and zip the content on the “CarDealer/artifacts/src/github.com/node” folder and then upgrade your chaincode in Blockchain Platform. Install a new version and upload your zip file.

Subscribing with OBP Events

As I mentioned before, Oracle Blockchain Platform provides a REST API endpoint that allows backend systems to subscribe with events by providing a callback URL that will be invoked with the event payload. Now as you have your OCI infrastructure in place, you can configure Oracle Blockchain Platform to use the Function URL as the events callback subscription endpoint.

In the previous Blog post Provisioning You Infrastructure when the terraform scripts finished executing, it printed out two values:

  • OBP_Event_Subscribe_Callback: is the API Gateway callback URL to register with OBP
  • use_to_extract_ssl_certificate: as this callback is TLS enabled, OBP requires passing the callback caCert as part of the registration process, you will use this value in the next step to extract the certificate associated with the API Gateway URL.

To extract the caCert of the callback URL, execute the below command in “cloud shell”, make sure to replace the value of USE_TO_EXTRACT_SSL_CERTIFICATE with the output value generated by the terraform script.

echo | openssl s_client -showcerts -connect USE_TO_EXTRACT_SSL_CERTIFICATE 2>/dev/null | openssl x509 -inform pem -out cert.pem;echo;echo;awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}'  cert.pem;rm cert.pem;echo;echo

This will print out the content of the caCert, copy the certificate value marked in red to use with OBP event subscription end point. 

Finally you got all the needed setup and details in place and can register your function with OBP :) The details of the OBP Event Subscription can be found here. In the below screenshot, I did subscribe my function URL and its corresponding caCert with OBP to listen for the chaincode event  "VehiclePartCreated".

Make sure your subscription returns back a successful response

You might need to try registering the endpoint several times initially, as time is needed to initially build the function container and deploy it.

Now when you invoke the "initVehiclePart" chaincode method to create a new vehicle part, your chaincode will emit "VehiclePartCreated" event, that event name will be used to create a new “Topic” in OCI Streaming Service and the event payload will be pushed to that stream. As our chaincode emits an event with the name "VehiclePartCreated"; the FN producer will create a stream named “VehiclePartCreated” and push the OBP event payload to it.

Next Step

Part 6 (Building a Consumer)

 

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha