You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constructor() {
// The contract name is used as the chaincode name during deployment.
super('KVContract');
}
// This function initializes the ledger with some example assets.
async initLedger(ctx) {
console.info('*** Initializing Ledger ***');
const assets = [
{ docType: 'asset', id: 'asset1', owner: 'Alice', value: '100' },
{ docType: 'asset', id: 'asset2', owner: 'Bob', value: '200' }
];
for (const asset of assets) {
// Store the asset as a JSON document
await ctx.stub.putState(asset.id, Buffer.from(JSON.stringify(asset)));
// Create a composite key to support CouchDB rich queries (for example, by asset type and owner)
const compositeKey = await ctx.stub.createCompositeKey('asset~owner', [asset.docType, asset.owner, asset.id]);
// Save the composite key with a dummy value (the actual data is stored under the asset id)
await ctx.stub.putState(compositeKey, Buffer.from('\u0000'));
}
console.info('*** Ledger Initialized ***');
}
// Create a new asset
async createAsset(ctx, id, owner, value) {
const asset = {
docType: 'asset',
id,
owner,
value
};
// Check if the asset already exists
const assetBytes = await ctx.stub.getState(id);
if (assetBytes && assetBytes.length > 0) {
throw new Error(`Asset ${id} already exists`);
}
// Store the asset in the ledger (JSON will be stored in CouchDB if configured)
await ctx.stub.putState(id, Buffer.from(JSON.stringify(asset)));
// Create composite key for CouchDB queries
const compositeKey = await ctx.stub.createCompositeKey('asset~owner', [asset.docType, asset.owner, asset.id]);
await ctx.stub.putState(compositeKey, Buffer.from('\u0000'));
return JSON.stringify(asset);
}
// Read an asset from the ledger
async readAsset(ctx, id) {
const assetBytes = await ctx.stub.getState(id);
if (!assetBytes || assetBytes.length === 0) {
throw new Error(`Asset ${id} does not exist`);
}
return assetBytes.toString();
}
// Query assets by owner using a rich query (CouchDB)
async queryAssetsByOwner(ctx, owner) {
// Build a CouchDB query to select documents with docType 'asset' and the specified owner.
const query = {
selector: {
docType: 'asset',
owner: owner
}
};
const queryString = JSON.stringify(query);
const resultsIterator = await ctx.stub.getQueryResult(queryString);
const results = [];
while (true) {
const res = await resultsIterator.next();
if (res.value && res.value.value.toString()) {
const asset = JSON.parse(res.value.value.toString('utf8'));
results.push(asset);
}
if (res.done) {
await resultsIterator.close();
break;
}
}
return JSON.stringify(results);
}
Hi, I am facing issue to store data in Couchdb ledger. Any help is appreciated.
The below error I kept receiving:
[chaincodeCmd] InitCmdFactory -> Retrieved channel (my-channel1) orderer endpoint: orderer0.group1.orderer.example.com:7030
Error: endorsement failure during invoke. response: status:500 message:"error in simulation: failed to execute transaction 58d30a1eba7a2fcc96976cd497cc2946055280968c6cdfa325ab1dc53a6026bd: could not launch chaincode chaincode1_0.0.1:439239bdb549ab09d13e372424ce44ffad866b71787c4e80cde67171b74ccc30: chaincode registration failed: container exited with 1"
make: *** [invoke] Error 1
My config file:
Chaincode:
`'use strict';
const { Contract } = require('fabric-contract-api');
class KVContract extends Contract {
}
module.exports = KVContract;
`
command I tried:
./fablo chaincode invoke "peer0.org1.example.com" "my-channel1" "chaincode1" \ '{"Args":["KVContract:initLedger"]}'
my directory in chaoncode
The text was updated successfully, but these errors were encountered: