IBM Blockchain - Simple Application

2020-02-16 02:24发布

问题:

Alright, I need to develop a simple application in an IBM Blockchain(starter plan). But I can't get it done, after almost 1 month of trial and error and thousands of tutorials. So, my case is really simple(I guess): I have a models.cto file:

namespace com.test.models

/**
 * A company asset.
 */
asset Company identified by company_id {
  o String company_id
  o String document_key
  o String document_value
  o String name
  o String telephone
  o String email
}

/**
 * A person_in_charge asset.
 */
asset PersonInCharge identified by person_in_charge_id {
  o String person_in_charge_id
  o String company_id
  o String name
  o String document_key
  o String document_value
  o String email
  o String language
  o String created_date_time
}

I tried to make it as participant instead. I tried to make transaction and then do something with .js file but there are only use cases and tutorials about complex examples using assets, participants, etc. I just want to insert "PersonInCharge" and "Company" separately and query (each Company or by id whatever) also separately.

I was able to made a example using Swagger api generator, but it didn't worked as expected, even thought it created all the methods I wanted, like this:

For summarize, I wanted to deploy my chaincode in a peer, inside of a channel at IBM Blockchain. I did deployed a marble example and it did worked perfectly.

Really self describing error, not with a general message:

I am following a "how to" guide for develop locally and deploy to IBM. Therefore, I am creating a bna file composer archive create -t dir -n . and then creating a card with the downloaded connection settings(from IBM) and else using this tutorial Deploying a business network on Starter Plan and I am failing at this step: composer network start -c adminCard -n bna-blockchain -V 0.0.1 -A admin -C ./credentials/admin-pub.pem -f delete_me.card. perhaps beucause I'm not being able to instantiate my chaincode on IBM Blockchain.

Sorry if it looks confunsing, just ask me if you have any questions about my issue.

Thanks in advance and any help would be awesome!

回答1:

Before starting the network, you need to install it:

composer network install -c adminCard -a vehicle-manufacture-network.bna


回答2:

I took a break in the development and came back after a month. In that time, I had help of a person inside IBM who could help me to get it done.

And I'm here so point out, some failures too.

The IBM guy(I won't say his name, of course) itself said that the service to upload a chaincode(channels/chaincode/Install chaincode) is broken and does not work well. Also, I asked an easy to answer question in the IBM Blockchain paid forum but had no help at all(they did not even answer the question), even after almost 3 months. And you can't find a tutorial(besides marbles samples) that works from start to end.

Sorry if it looks like I'm raging for nothing in here, but I had a really hard time to put it all together to work 100%.

I will not show how to create a model file or transactional scripts(because there are a plenty of it on examples) but the commands you need to execute in order to run the chaincode on IBM and generate a loopback API. With that said...

Let's go to what's important: the code

First things first, Environment settings:

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.6 LTS
Release:        16.04
Codename:       xenial

IMPORTANT: IBM Blockchain(until now 2019-03-23) won't run under virtualized machines like virtual box or hyperv because of a couple npm dependencies. But a hardware virtual machine(HVM) like amazon ones will do just fine.

STEPS

Install node and npm(follow the versions strictly as listed)

node -v
v8.15.1

npm -v
6.4.1

curl -O https://hyperledger.github.io/composer/unstable/prereqs-ubuntu.sh
chmod u+x prereqs-ubuntu.sh
./prereqs-ubuntu.sh

npm install -g --unsafe-perm composer-cli@0.20.5
npm install -g --unsafe-perm composer-rest-server@0.20.5

composer-rest-server -v
v0.20.5

composer -v
v0.20.5

Create a project using hyperledger composer-playground. Export it and navigate through the root directory of the project you just downloaded(the project can be developed locally too).

Download the connection settings file at IBM Blockchain(Overview/Connection Profile/Download) and name it as connection-profile.json. Find the node registrar, you'll use it to create the cards.

Run npm install. If anything goes wrong while installing the dependencies, check if you're using exactly the same versions that I am using. If you're not, uninstall it all and start again from the beginning.

tricky zone

composer card create -f ca.card -p connection-profile.json -u admin -s *your-password*

composer card import -f ca.card -c ca

composer identity request --card ca --path ./credentials -u admin -s *your-password*

You'll see that the credentials folder were generated. Inside this folder, find the file admin-pub.pem (credentials/admin-pub.pem) copy the whole content of it, and then upload it to IBM Blockchain(Members/Certificates/Add Certificate). You'll be prompted to restart peers, click "yes"

After the peers come back online, in the same Certificates tab, find the certificate you just add, in the ACTION column click at the menu, and then choose the option "Sync Certificate".

 composer archive create -t dir -n .


 composer card create -f adminCard.card -p connection-profile.json -u admin -c ./credentials/admin-pub.pem -k ./credentials/admin-priv.pem --role PeerAdmin --role ChannelAdmin

 composer card import -f ./adminCard.card  -c  adminCard


 composer network install -c adminCard -a bna-name@version.bna



 composer network start -c adminCard -n *bna-name* -V *bna-version* -A admin -C ./credentials/admin-pub.pem -f delete_me.card (Output: Successfully created business network card: Filename: delete_me.card)

 composer card create -n *bna-name* -p connection-profile.json -u admin -c ./credentials/admin-pub.pem -k ./credentials/admin-priv.pem (Output file: admin@*bna-name*.card)

 composer card import -f ./admin@*bna-name*.card (Card file: ./admin@*bna-name*.card Card name: admin@*bna-name*)

Now you'll generate the rest api using (automatically generated using swagger)

 composer-rest-server -c admin@*bna-name* -n never -w true -p 8080

Enjoy!