Join a new organization in a channel. Hyperledger Fabric Node-sdk - hyperledger-fabric

I am trying to join a new organization in a channel of an existing network.
I understood the flow to join an org to a channel from fabric documentation but they did use docker.
I want to accomplish the same using node-sdk.
To get the latest config block of a channel I am using these methods:
<async> getChannelConfig(target, timeout)
<async> getChannelConfigFromOrderer()
https://fabric-sdk-node.github.io/release-1.4/Channel.html#getChannelConfig__anchor
Both are returning the same result. And its type is common.ConfigEnvelope.
It is not common.Block.
It is returning an object according to documentation and I am saving it as a json file.
When I went through it, It doesn't have the below fields which are required for the purpose.
{"channel_group":{"groups":{"Application":{"groups": {
I am attaching the latest block file which I got.
Please tell where I am making the mistake and if there is any reference for this please mention.

Actually, the latest config blocks are protobuf object.
So, when I am trying to save it as json, it was not properly formatted for json.
To do that, first, it has to convert into buffer and save.
// return latest config block as protobuf object
const latestConfig = await getChannelConfigFromOrderer();
const latestConfigBuffer = latestConfig.toBuffer();
// save it in .pb format
fs.writeFileSync("latest-config.pb", latestConfigBuffer);
Then using configtxlator convert it to json and made changes accordingly.

Related

Unable to insert data through EntityRepository.create or EntityRepository.persist #MikroOrm #NestJS

:)
I am trying to test my Entity operations using the code in the file.
I am creating a userRepository object as follows:
image
When I console.log find{} from the repository, it fetches the previously stored records:
image
I create a dummy object using faker and it works fine but as soon as I try to create it in DB or persist it, it does not seem to work:
enter image description here
I also tried orm.em.persist. Let me know if more details are required.
Just for future readers, this has been asked & asnwered on the GH.
https://github.com/mikro-orm/mikro-orm/discussions/1571

Why can't I store a PriorityQueue into MongoDB

Recently I have decided to replace arrays with priority queues for storing my list of jobs for a user into MongoDB. I use NodeJS and ExpressJS for backend. The priority queue I attempted to store is from an external package which can be installed by running the following command in terminal:
yarn add js-priority-queue
For some reason the priority queue works perfectly prior to storing it into MongoDB. However, the next time I attempt to take it out of MongoDB and use it, its functionality is missing. I declare its type as Schema.Types.Mixed in the Schema. Am I doing something wrong or is it not possible to store instantiated class objects into MongoDB?
As far as I know, when you store things in MongoDB they are stored as extended JSON (EJSON) in binary format (BSON)
const { EJSON } = require('bson');
const test = EJSON.stringify({a: new Date(), foo:function(){console.log('foo');}})
console.log(test) // "{"a":{"$date":"2020-07-07T14:45:49.475Z"}}"
So any sort of function is lost.

HyperLedger Fabric history transaction with key

I'm using the fabcar project: https://github.com/IBM/blockchain-application-using-fabric-java-sdk
I would like to know if there is a way to get the transaction history with a certain key (Not just querying a simple car o listing all of them.
Thank you.
Yes, you have an API called GetHistoryForKey()
You can read more here : How to fetch asset modification history in hyperledger fabric
Here is the process which I have tried and got the result. I have implemented below code in my chaincode.
private Response getQueryHistory(ChaincodeStub chaincodeStub, List<String> args) {
// method for getting the history for key
QueryResultsIterator<KeyModification> queryResults = chaincodeStub.getHistoryForKey(args.get(0));
return newSuccessResponse(prepareJsonFromQueryResult(queryResults));
}
private String prepareJsonFromQueryResult(QueryResultsIterator<KeyModification> queryResults) {
// here build your json object from query result
return "your json object";
}
There are several already implemented method of ChaincodeStub
For more information of method, have look https://hyperledger.github.io/fabric-chaincode-java/release-1.4/api/org/hyperledger/fabric/shim/ChaincodeStub.html

How retrieve all block transactions in the Hyperledger fabric?

As you know there is chance to have multiple transaction in one block , depend on the batch size and orderer configuration.
I need to make only one call to return all transaction inside the block not one by one.
I could retrieve one transaction with queryTransaction by using fabric SDK.
like
let response_payload = await channel.queryTransaction(trxnID, peer);
First Approach: implement a chanincode function and pass the block number which comes from eventHub along the method then inside the chaincode retrieve all transaction Ids and then make a query to find all transaction then stitch all together as result.
Second Approach:
retrieve the block inside with fabric sdk then parse all signed proposal in the payload of the block content.
Third Approach:
retrieve the block inside with fabric sdk then retrieve the transaction ids or keys in the payload and then make a couch db query to retrieve all content .
Which approach do you think is more reasonable if not what is your suggestion?
If you have a your client set up correctly, there should be a
LedgerClient which has a function like
QueryBlock(blockNumber uint64, options ...ledger.RequestOption) (*common.Block, error)
Once you have the block, you can pull the data out of it
block, _ := QueryBlock(37)
data := block.GetData().GetData()
data is a [][]byte, and each entry is one transaction.

Set metadata in REST request to put blob in AZURE

i am able to upload file to azure blob using REST api provided by Azure.
i want to set metadata at the time i am doing request for put blob, when i am setting it into header as shown here i am unble to upload file and getting following exception org.apache.http.client.ClientProtocolException.
from the last line of the code below
HttpPut req = new HttpPut(uri);
req.setHeader("x-ms-blob-type", blobType);
req.setHeader("x-ms-date", date);
req.setHeader("x-ms-version", storageServiceVersion);
req.setHeader("x-ms-meta-Cat", user);
req.setHeader("Authorization", authorizationHeader);
HttpEntity entity = new InputStreamEntity(is,blobLength);
req.setEntity(entity);
HttpResponse response = httpClient.execute(req);
regarding the same, i have two questions.
can setting different metadata, avoid overwriting of file? See my question for the same here
if Yes for first question, how to set metadata in REST request to put blob into Azure?
please help
So a few things are going here.
Regarding the error you're getting, it is because you're not adding your metadata header when calculating authorization header. Please read Constructing the Canonicalized Headers String section here: http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx.
Based on this, you would need to change the following line of code (from your blog post)
String canonicalizedHeaders = "x-ms-blob-type:"+blobType+"\nx-ms-date:"+date+"\nx-ms-version:"+storageServiceVersion;
to
String canonicalizedHeaders = "x-ms-blob-type:"+blobType+"\nx-ms-date:"+date+"\nx-ms-meta-cat"+user+"\nx-ms-version:"+storageServiceVersion;
(Note: I have just made these changes in Notepad so they may not work. Please go to the link I mentioned above for correctly creating the canonicalized headers string.
can setting different metadata, avoid overwriting of file?
Not sure what you mean by this. You can update metadata of a blob by performing Set Blob Metadata operation on a blog.

Resources