Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error to setup Couchdb #509

Open
SRshohan opened this issue Feb 12, 2025 · 0 comments
Open

Error to setup Couchdb #509

SRshohan opened this issue Feb 12, 2025 · 0 comments

Comments

@SRshohan
Copy link
Contributor

SRshohan commented Feb 12, 2025

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:

{
  "$schema": "https://github.com/hyperledger-labs/fablo/releases/download/2.1.0/schema.json",
  "global": {
    "fabricVersion": "2.5.9",
    "tls": true,
    "engine": "docker",
    "peerDevMode": false
  },
  "orgs": [
    {
      "organization": {
        "name": "Orderer",
        "domain": "orderer.example.com"
      },
      "orderers": [
        {
          "groupName": "group1",
          "type": "raft",
          "instances": 2
        }
      ],
      "tools": {}
    },
    {
      "organization": {
        "name": "Org1",
        "domain": "org1.example.com"
      },
      "peer": {
        "instances": 2,
        "db": "CouchDb"
      },
      "tools": {"explorer": true}
    }
  ],
  "channels": [
    {
      "name": "my-channel1",
      "orgs": [
        {
          "name": "Org1",
          "peers": [
            "peer0",
            "peer1"
          ]
        }
      ]
    }
  ],
  "chaincodes": [
    {
      "name": "chaincode1",
      "version": "0.0.1",
      "lang": "node",
      "channel": "my-channel1",
      "directory": "./chaincodes/chaincode-kv-node"
    }
  ]
}

Chaincode:

`'use strict';

const { Contract } = require('fabric-contract-api');

class KVContract extends Contract {

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);
}

}

module.exports = KVContract;
`

command I tried:

./fablo chaincode invoke "peer0.org1.example.com" "my-channel1" "chaincode1" \ '{"Args":["KVContract:initLedger"]}'

my directory in chaoncode

chaincode-kv-node/
├── META-INF/
│   └── statedb/
│       └── couchdb/
│           └── indexes/
│               ├── indexOwner.json
chaincode.js
.
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant