Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Latest commit

 

History

History
122 lines (86 loc) · 4.25 KB

compile-hyperledger-fabric-proto-files.md

File metadata and controls

122 lines (86 loc) · 4.25 KB

Compile Hyperledger-fabric proto files for PHP

Authors: Jitendra Singh Dikhit & Ashish Kumar

Introduction

In this blog tutorial, we are going to setup protocol buffer compiler and gRPC extension for PHP language. After this, you will be able to generate proto buffer files with gRPC support for Hyperledger-Fabric proto files in PHP language.

What are protocol Buffers?

Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data, useful in developing languages to communicate with each other. Protocol buffers are designed to emphasize simplicity and performance.

Read more about Google protobuf

What is gRPC?

gRPC is a client application, which can directly call methods on a server application on a different machine as if it was a local object, making it easier for you to create distributed applications and services.

Read more about gRPC

What is Hyperledger-Fabric

It is a business blockchain framework hosted by the Linux Foundation intended as a foundation for developing blockchain applications or solutions with a modular architecture. It is a platform for distributed ledger solutions with high degree of confidentiality, resiliency, flexibility, and scalability.

Read more about Hyperledger

Why PHP?

PHP is a great option for many reasons; here are some reasons why the language may be right for you or your project:

  1. It's free and simple.
  2. Powerful, flexible, and scalable.
  3. Fast loading.
  4. Large open source support.
  5. Extensions and add-ons.
  6. Exceptional performance.

Installation

This installation showcase is performed on a Mac machine, which has brew installed. We are going to install PHP version 7.1 because protocol buffers only support up to version 7.1 thusfar.

Install and verify PHP 7.1

brew install php71
php -v

If not php71, unlink your current version, link php71, and check the version again. For example:

brew unlink php56
brew link php71
php -v

Install and verify php-grpc extension

brew install grpc php71-grpc
which grpc_php_plugin

Install and verify protobuf compiler

brew install protobuf php71-protobuf
protoc --version

Hello, World!

Create a directory named protos and download helloworld.proto file inside it; download sample proto file from helloworld.proto:

mkdir protos
touch protos/helloworld.proto
mkdir php

Generate PHP classes from proto file:

protoc --proto_path=protos/ --php_out=php/ --grpc_out=php/ --plugin=protoc-gen-grpc=`which grpc_php_plugin` protos/helloworld.proto

Additional commands on the PHP/gRPC site.

Check your generated folder structure:

tree php/

Output:

php/
├── GPBMetadata
│   └── Helloworld.php
└── Helloworld
    ├── GreeterClient.php
    ├── HelloReply.php
    └── HelloRequest.php

Now, you are all setup for generating PHP files for Hyperledger-fabric proto files.

Compile Hyperledger-fabric proto files

Hyperledger-fabric provides proto files to generate gRPC files in the supported language. We will download standard proto files given by Hyperledger community and compile them.

  1. Get the proto folder.
  2. Add/adjust local copy php_namespace (and other options) as necessary, until they are updated upstream.
  3. Run above with protoc or create a bash file with the code to generate.
find ./protobuf/protos -name "*.proto" -exec protoc --proto_path=protobuf/protos/ --php_out=protobuf/dist/ --grpc_out=protobuf/dist/ --plugin=protoc-gen-grpc=`which grpc_php_plugin` {} \;

Conclusion

Check generated files with tree protobuf/dist/

All done!!! These files are ready to include and communicate with Hyperledger-fabric server.

References