How can I use two chaincodes installed in the same Fabric channel? - hyperledger-fabric

I have two chaincodes deployed in the same channel of 5 peers. The first chaincode is installed on 3 peers, the second one is installed on other 2 peers. The first chaincode inits the ledger with some data, while the second one should query the ledger to take one of the elements stored in the ledger during the initialization. It seems that the ledger associated to the second smart contract is empty. From theory, I know that there is one ledger associated with the channel, but here it seems one ledger associated with the chaincode. Where am I wrong?
Thank you in advance!

I figured out by studying the concept of namespaces and world states.

Related

Hyperledger join one org from one consortium to a channel in another consortium with already existing name. Best ways to do this

Hyperledger Fabric 2.2
Current situation. We have two separate consortiums. Both have, however, channels with the same names. If I add Org from one consortium to the channel of another consortium I get error:
Error: proposal failed (err: bad proposal response 500: cannot create ledger from genesis block: ledger [data-channel] already exists with state [ACTIVE])
What's the best way to solve this situation? Do we have to rename a channel (is it possible?) in one of the consortiums? If I make a name-update config transaction, will the Couch DB databases follow this update?
A Fabric node cannot participate in two channels with the same name. You can simply have different nodes that each participates in one of the channels.

Do I need CouchDB per peer in hyperledger if there is only one orgnization

If I'm creating 4 peers in one namespace/org in Hyperledger, do I need to create separate CouchDB for each Peer? Like for for peer0-->CouchDB0, for peer1-->CouchDB1? And why?
Regards,
Vikas
Yes, separate CouchDB is needed for each peer if CouchDB is chosen as the state DB. As for why, Fabric peer and a CouchDB has been designed to have a one-to-one relationship, and there is no guarantee that all the peers will have same state even though they are in the same org, For example, Peers in the same Org may subscribe to different channels, in which case their Ledger and State will be different.

How to access same ledger from different chaincodes?

Suppose I have 2 peers(peer0 and peer1) and 2 chaincodes(1. full rights 2.only query).
I want peer0 to have full rights of invoking but peer1 to only have query available but installing and instantiating different chaincodes is creating different ledgers hence the query from peer1 is always empty.
So How can I access same ledger from different chaincodes?
The scope of key-value data is actually channel/chaincode (meaning it is not scoped to the channel and each chaincode instance in a channel manages its own state).
So, if you want to "share" key/value state between chaincode, the only way to do this is to either always have one chaincode invoke another or you can extend this a bit and create a third chaincode which simply manages the get/put operations and have the other chaincodes always call it when dealing with the "shared" state.

How to keep a digital asset in Hyperledger Fabric?

I'm working with the BYFN example using 3 organizations, 2 channels. One channel(c12) between Org1 and Org2, another channel (c23) between Org2 and Org3. The first chaincode is an example where I'm able to transfer value between "A" and "B". When I run the code over c12, I can see I'm able to transfer an amount from A to B. But when I query the same chaincode over c23, the initialization is kept. I understand why (different ledgers) and etc.
Now I would like to introduce the following requirement. I, as an organization, need to transfer this value only if I have the amount available.
The sequence would be:
1. I'm on org2, I have 100, and I wanna transfer 60 to org1. And everything works as expected.
2. I'm on org2, I have 40 and I'll try to transfer 50 to org3 (another ledger - channel). This should fail because I have only 40.
How can I achieve this?
Where will the "shared state" be stored? MSP?
The same approach would work for a complex asset?
Extra information:
- I already have read the documentation, about the assets, account model, the examples, but usually, they are focused only one channel only. Maybe I'm losing something.
- I'm not using hyperledger composer
In fabric you have a chaincode and a ledger different for each channel. so if you want to have the business logic descripted you need to use only one channel (with 3 orgs).
You can probably use private data to make the separation you are now doing with the two channels

Hyperledger Fabric. Is it possible to deploy more than one chaincode to the same channel?

Is it possible to deploy two different chaincodes to the same channel in Hyperledger fabric
Yes. And its possible to deploy the same chaincode to 2 or more channels. You can even deploy the same chaincode to the same channel if you use a different name.
when inspect into the block structure, I find a 'Chaincode Name' field in block, and I suppose that:
One channel has one ledger(offcial doc)
One channel can have two or more chaincodes
The block generated by different chaincode(we can simplely think so) will be stored in the same one ledger
The field 'Chaincode Name' in block will distinguish it's generated from which chaincode
jworthington answer is correct but i want to clear some things from comments.
One channel means one ledger. (official doc)
Every chaincode have separated "view" on ledger (I visualize it as every key in db has chaincode name prefix and chaincode can access
only specific keys with same prefix as its name).
You can invoke read/write of second chaincode from first one and that will make only one blockchain transaction (at condition that chaincodes are on same channel/ledger, if they are not, write operation will be ignored)
I do not know how much is changed over the years, but I share information based on version 2.x.
Ledger is associated with the channel, not with the Chaincode. Even a peer can have a ledger without any Chaincode installed. https://hyperledger-fabric.readthedocs.io/en/latest/peers/peers.html#multiple-ledgers
P1 is Peer
L1 & L2 are ledgers
S1, S2, and S3 are Chaincodes
There isn’t a fixed relationship between the number of ledgers a peer has and the number of Chaincodes that can access that ledger. A peer might have many Chaincodes and many ledgers available to it.
https://hyperledger-fabric.readthedocs.io/en/latest/peers/peers.html#multiple-chaincodes
P1 is Peer
L1 & L2 are ledgers
S1, S2, and S3 are Chaincodes
Yes, we can deploy multiple chaincodes to the same channel with different chaincode ID.
Each chaincode will have separate ledger in channel, we can consider this as namespace.
One chaincode can not read/update other channel ledger directly.
However, fabric shim package provides "InvokeChaincode" functionality to call another chaincode to read/update it's ledger.
stub.InvokeChaincode("anotherCCName", chainCodeArgs, "channelName")

Resources