> For the complete documentation index, see [llms.txt](https://docs.catalyx.solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.catalyx.solutions/catalyx-blockchain-manager/hyperledger-fabric/version-2.4/network-and-node-management/chaincode-management.md).

# Chaincode Management

## What is Chaincode?

Chaincode is the smart contract of Hyperledger Fabric that handles the business logic of a particular application. It programmatically accesses two distinct pieces of the ledger — a blockchain that immutably records the history of all transactions, and a world state that holds a cache of the current value of these states.

Catalyst Blockchain Manager supports JavaScript, Golang, and Java for smart contract development.

### Key Concepts

**Endorsement:** Any transaction generated by the chaincode must be approved by specific organizations as defined by the endorsement policy.

**Valid Transactions:** Each peer completes validation in two phases:

1. Ensuring all parties mentioned in the endorsement policy have signed the transaction
2. Checking authenticity by matching the world state at signing time against the current state

**External Chaincodes:** With Hyperledger Fabric v2.0, chaincode can be added externally through a Docker image. Catalyst Blockchain Manager supports this — you simply reference your Docker image in an online repository.

***

## Chaincodes Lifecycle

Catalyst Blockchain Manager supports Hyperledger Fabric v2.0 capabilities with the following [chaincode lifecycle](https://hyperledger-fabric.readthedocs.io/en/release-2.2/chaincode_lifecycle.html#fabric-chaincode-lifecycle):

{% stepper %}
{% step %}

#### Add Chaincode Template

Create a Docker image and provide a link to the Docker repository when adding a chaincode template in Catalyst Blockchain Manager.

{% hint style="danger" %}
You need to add the required environment variables (`CHAINCODE_ID` and `CHAINCODE_ADDRESS`) to run chaincode in Catalyst Blockchain Manager. See [Create Your Application](file:///7571772/getting-started/create-your-application.md).
{% endhint %}
{% endstep %}

{% step %}

#### Install the Chaincode on Your Peers

Every organization that will use the chaincode to endorse a transaction or query the ledger needs to complete this step.
{% endstep %}

{% step %}

#### Approve a Chaincode Definition

Every organization that will use the chaincode needs to complete this step. The chaincode definition needs to be approved by a sufficient number of organizations to satisfy the channel's Lifecycle Endorsement policy.
{% endstep %}

{% step %}

#### Commit the Chaincode Definition

Once all necessary approvals are received, commit the definition to the channel.
{% endstep %}
{% endstepper %}

Organizations can also [upgrade](#how-to-upgrade-a-chaincode) and [delete](#how-to-remove-a-chaincode) a chaincode.

***

## How to Add a Chaincode in Catalyst Blockchain Manager?

Go to the **Chaincode** tab and click **Add chaincode**.

<figure><img src="/files/XVMFiJz77nKqfRT3cWlU" alt=""><figcaption></figcaption></figure>

Provide:

* **Chaincode name**
* **Number of replicas** — Chaincode replicas to deploy
* **Image** — Link to your chaincode Docker container at the public Docker repository

{% hint style="info" %}
For private repositories, specify an `imagePullSecret`. Create one in your Kubernetes cluster with the label `"app.kubernetes.io/name": "image-pull-secret"`.
{% endhint %}

* **Resources:** Requested CPU, CPU limit, Requested memory, Memory limit
* **Version**
* **Indexes** (optional, CouchDB only):
  * Chaincode index
  * Private index (for [private data collections](https://hyperledger-fabric.readthedocs.io/en/release-2.2/private-data/private-data.html))

{% hint style="info" %}
Indexes can only be applied to CouchDB. LevelDB does not support indexes.
{% endhint %}

Click **Save** to add the chaincode template.

<details>

<summary>Chaincode statuses</summary>

| Status      | Description                                             |
| ----------- | ------------------------------------------------------- |
| **Pending** | Ready to be installed on a peer                         |
| **Running** | Chaincode pod is running; can be committed to a channel |
| **Failed**  | All chaincode pods have failed status                   |
| **Deleted** | The chaincode was deleted                               |

</details>

***

## How to Install Chaincode on a Peer?

Go to the **Peer** tab, click on a specific peer with **Running** status, go to the **Chaincodes** tab, and click **Install chaincode**.

<figure><img src="/files/SXkSGVuT4ylY6ioN0Zdf" alt=""><figcaption></figcaption></figure>

Select the chaincode from the dropdown menu showing all chaincode templates added to the platform.

After installation, the chaincode container(s) will launch. It may take a few minutes to start.

***

## How to Approve Chaincode on a Channel?

{% stepper %}
{% step %}

#### Go to the Channels tab

Open the specific channel where you need to approve the chaincode.
{% endstep %}

{% step %}

#### Click "Approve chaincode"

Under the **Chaincodes** section, click the **Approve chaincode** button.

<figure><img src="/files/jya31CfSpZo5kqzHWXP4" alt=""><figcaption></figcaption></figure>

Fill in:

* **Peer** — A peer where the chaincode was installed
* **Chaincode** — Select from the dropdown
* **Endorsers** — Organizations that need to execute and validate transactions
* **Policy** — ANY, MAJORITY, or ALL
* **Sequence** — Automatically specified (increases every time chaincode is upgraded)
* **Package ID** — Auto-specified when you choose a peer
* **Init required** — If checked, ensures the Init function is invoked before any other function and only once
* **Private collection** (optional) — For private data between specific organizations
  {% endstep %}
  {% endstepper %}

{% hint style="danger" %}
Each Endorser organization needs to approve the **same chaincode definition** to commit it on a channel. Only the peer and Package ID differ per organization.

You cannot approve a chaincode definition twice. If you see an error, check if it was already approved.
{% endhint %}

After approval, you can see the chaincode commitment status at the Chaincodes tab. When all approvals per the channel policy are received, you can commit.

***

## How to Commit a Chaincode?

Go to the **Channels** tab, open the specific channel, and click **Commit chaincode** under the Chaincodes section. Select a peer and the chaincode, then click **Save**.

{% hint style="info" %}
Only an organization with endorser rights on the channel who approved this chaincode definition earlier can commit it.
{% endhint %}

{% hint style="info" %}
Sometimes you cannot commit a chaincode even after all endorser approvals. This can be caused by no anchor peer specified for some organizations — without an anchor peer, the organization's peers won't be returned in a discovery service request.
{% endhint %}

After successful commitment, you can test the chaincode using the **Query** and **Invoke** buttons.

***

## How to Upgrade a Chaincode?

{% stepper %}
{% step %}

#### Add a new chaincode version

Add the same chaincode name with a new version. See [How to Add a Chaincode](#how-to-add-a-chaincode-in-catalyst-blockchain-manager).
{% endstep %}

{% step %}

#### Install the new chaincode on your peers

{% endstep %}

{% step %}

#### Approve the new chaincode definition

{% endstep %}

{% step %}

#### Commit the definition to the channel

{% endstep %}
{% endstepper %}

{% hint style="info" %}
To only update the chaincode policies (without changing binaries), skip steps 1 and 2 — just approve and commit the same chaincode with a new policy.
{% endhint %}

See [Upgrading a Chaincode](https://hyperledger-fabric.readthedocs.io/en/release-2.0/chaincode_lifecycle.html?highlight=lifecycle%20chaincode#upgrade-a-chaincode) in the Hyperledger Fabric documentation.

***

## How to Remove a Chaincode?

Click the **Remove** button in the Actions column of the Chaincodes table.

{% hint style="danger" %}
Removing a chaincode will delete all chaincode deployments. Do not remove a chaincode if you want it to remain running, committed to a channel, and used by business applications.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.catalyx.solutions/catalyx-blockchain-manager/hyperledger-fabric/version-2.4/network-and-node-management/chaincode-management.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
