Hyperledger Fabriс Research.

Part 2

Roobee
Roobee_invest
5 min readJan 14, 2020

--

We continue our series of publications about the solution developed on the Hyperledger blockchain. Data about beneficiaries will be written and read inside the Hyperledger blockchain with the help of this solution.

In the previous part, you learned:

  • Why we chose Hyperledger blockchain;
  • Details about dependencies;
  • Steps of installing dependencies;
  • How samples and containers are loaded;
  • Про запуск и тест блокчейн сети.

Launching and testing the blockchain network.

By the way, after reading it, you can try your hand at launching your own Hyperledger-based blockchain network

For today, we will take an in-depth look at the chaincode description.

Link to the documentation:

Let’s start off by describing the functions in the smartchain located in path: /chaincode/fabcar/node/fabcar.js

The smartchain contains functions/methods for writing and reading data inside the blockchain. The use case implies that we will be receiving and sending requests to peers for further recording in the state (key-value store) via an application that will utilize the SDK.

Chaincode description

1. Initialization method

  • Let’s create an empty array of keys
const arr = []
  • Now, let’s convert the array to a string
const arrString = JSON.stringify(arr);
  • Saving the array on the blockchain in the byte string format
await stub.putState('ARR', Buffer.from(arrString));
  • Finally, we send a response to the client about the successful creation of the array of keys
return getResult("CREATE_EMPTY_ARRAY_OF_KEYS_OK");                                  }

2. Method to get and invoke the class method

  • Getting an object containing the class method name to invoke, and the array of arguments to pass to the target method
let ret = stub.getFunctionAndParameters();
  • Checking if the method exists
let method = this[ret.fcn];if (!method) {…}
  • Invoking the chaincode method to pass the arguments into it
let payload = await method(stub, ret.params);

3. Method to add a beneficiary

  • Let’s get a key and convert it to a regular string
const key = (args[0] + "").toString();
  • Getting the name of a beneficiary and converting it to a regular string
const nameString = (args[1] + "").toString();
  • Let’s take retrieve an array of keys in the form of a byte string, from the blockchain
const arrBuffer = await stub.getState("ARR");
  • Converting the byte string to a regular string
let arrString = arrBuffer.toString();
  • Converting the string to an array
const arr = JSON.parse(arrString);
  • Checking if the key exists inside the array
for(let i = 0; i < arr.length; i++) {
  • If a match is found, we return an error indicating that a beneficiary with such id already exists
if(arr[i] === key) {throw new Error("BENEFICIARY_ALREADY_EXISTS"); 

}
}
  • Let’s add the key to the end of the array
arr.push(key);
  • Converting the modified array of keys to a JSON string
arrString = JSON.stringify(arr);
  • Adding the modified array in the byte string format, to the blockchain
await stub.putState('ARR', Buffer.from(arrString));
  • Let’s add the beneficiary and his key in the byte string format, to the blockchain
await stub.putState(key, Buffer.from(nameString));
  • Returning a response about the beneficiary being successfully added
return getResult("CREATE_NEW_BENEFICIARY_OK");}

4. Method to add data to the blockchain and check if a beneficiary exists

  • Let’s get a key and convert it to a regular string
const key = (args[0] + "").toString();
  • Getting the result of the check and convert it to a regular string
const checkString = (args[1] + "").toString();
  • Getting the data and converting it to a JSON object
const cheks = JSON.parse(checkString);
  • Let’s take an array of keys in the form of a byte string, from the blockchain
const arrBuffer = await stub.getState("ARR");
  • Converting the byte string to a regular string
let arrString = arrBuffer.toString();
  • Converting the string to an array
const arr = JSON.parse(arrString);
  • Checking if the key exists inside the array of keys
for(let i = 0; i < arr.length; i++) {
  • If a match is found, we take from the blockchain an array of the data we received when searching by key, in the form of a byte string,
if(arr[i] === key) {const valueBuffer = await stub.getState(key);let valueString = valueBuffer.toString();
  • Let’s add values from the received data
const addCheck = valueString + ", " + checkString;await stub.putState(key, Buffer.from(addCheck));
  • Returning a response about the successful adding.
return getResult("valueBuffer: " + valueBuffer + " checkString: " + checkString); }}
  • If NO matches were found, we show an error.
throw new Error("BENEFICIARY_NOT_EXISTS"); }

5. Method to get an array of keys

  • Let’s take an array of keys in the form of a byte string, from the blockchain
const arrBuffer = await stub.getState("ARR")
  • Converting the byte string to a regular string
const arrString = arrBuffer.toString()
  • Sending the array in the JSON format to the client
return getResult(arrString);}

6. Method to receive data by key

  • Let’s get a key and convert it to a regular string
const key = (args[0] + "").toString();
  • Taking an array of keys in the form of a byte string, from the blockchain
const arrBuffer = await stub.getState("ARR");
  • Converting the byte string to a regular string
let arrString = arrBuffer.toString();
  • Converting the string to an array
const arr = JSON.parse(arrString);
  • Checking if the key exists inside the array of keys
for (let i = 0; i < arr.length; i++) {
  • If a match is found, the data is sent
if (arr[i] === key) {const value = await stub.getState(key);return getResult(value); };};
  • If not, we generate an error
throw new Error("KEY_IS_NOT_FOUND"); };

You can find the published repository on GitHub via the link:

Subscribe to Roobee’s social media channels, so you don’t miss the next part.

Telegram | Chat | Twitter |Facebook | Reddit | Medium | Instagram | YouTube | Bitcointalk

--

--

Roobee
Roobee_invest

Roobee is a blockchain-based investment platform. Website: https://roobee.io/