tencent cloud

Tencent Cloud Blockchain as a Service

Release Notes
Product Introduction
Overview
Product Features
Underlying Engine
Strengths
Scenarios
Basic Concepts
Purchase Guide
Purchase Page Description
Overdue Payment Instructions
Pricing and Specification Description
Getting Started
Hyperledger Fabric
Operation Guide
Consortium
Event Center
Network Overview
Connection Management
Contract Management
Organization Management
Node Management
Blockchain Browser
Certificate management
Audit Log
Node Monitoring and Logs
Development Guide
Hyperledger Fabric
Application System Integration
API Documentation
History
Introduction
API Category
Making API Requests
Hyperledger Fabric 2.3 APIs
Data Types
Error Codes
FAQs
Product Policy
Data Processing And Security Agreement
Contact Us

Introduction to Smart Contracts

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2025-04-10 00:40:56

Overview

In Fabric, Chaincode is also known as a smart contract. It is a piece of code written in an advanced language, primarily used for reading and manipulating data on the ledger. A smart contract serves as a bridge between user applications and the Fabric ledger. User applications can directly interact with smart contracts, and smart contracts can directly interact with the Fabric ledger.
A complete Fabric blockchain application consists of two parts: the user's application and the smart contract written by the user. The user's application calls the smart contract through the Peer Node of the blockchain network. The user's smart contract operates on the ledger data through the Peer Node of the blockchain network. As shown in the figure below:

In this architecture, the smart contract is primarily responsible for encapsulating the process of directly interacting with the ledger (including storing and querying ledger data according to the user-specified logical storage), for the invocation by the user's application. The user's application is primarily responsible for the following responsibilities:
User applications are responsible for generating the data that needs to be stored on the blockchain network according to business logic.
The user's application obtains related data from the blockchain network according to business logic and performs business operations.
In Fabric, the smart contracts written by users run in an isolated sandbox environment, currently mainly presented as independent docker containers, installed on the Peer Nodes in the blockchain network. The Peer Node starts the docker container to run the smart contract. After the smart contract container starts, when the smart contract is called, it establishes interaction with the Peer Node that started the smart contract container, to implement operations on the ledger-related data of the corresponding Peer Node. Different smart contracts have different namespaces, and their data are also independent. The smart contract can only access the key-value pairs belonging to its own namespace. If a smart contract wants to access the data of other smart contracts, it can be achieved by internally calling other smart contracts.
The Peer Node in Fabric provides the service API for calling smart contracts. The user's application can interact with the Fabric Peer by using this service. By interacting with the smart contract container, the interaction between the application and the smart contract is completed. The user's application can use the services provided by the Fabric Peer in the following two ways:
Use the SDK provided by Fabric to interact with the Fabric Peer. For details, see Fabric SDK.
Use TencentCloud SDK to call related TBaaS cloud API to interact with Fabric Peer in TBaaS.

Must-Knows

When writing a smart contract, pay attention to the following items:
Smart contract processes non-deterministic code. In Fabric transactions, transaction requests are sent to multiple nodes of multiple organizations for endorsement confirmation according to user configuration. If the smart contract contains non-deterministic code (for example, a random number), it will cause inconsistent results on each endorsing node and fail to reach consensus. When writing a smart contract, it is advisable not to include random functions in it.
Statelessness in smart contract operation. When writing a smart contract, it is advisable not to use global variables within the smart contract and design it to be stateless. In Fabric transactions, transaction requests are sent to multiple nodes of multiple organizations for endorsement confirmation according to user configuration. If the business logic of the smart contract depends on global variables, it may lead to inconsistent results on each endorsing node and fail to reach consensus.
Initialization code for smart contracts. When writing a smart contract, you need to implement the Init function. This function will be executed once by default during the instantiation and upgrade operations of the smart contract. If the content in the Init function can only be executed once, the upgraded smart contract cannot contain this part of the Init code. For example, during normal use, the initial key-value pair KEY1/VALUE1 set in the Init function has been modified to KEY1/VALUE1_NEW and written to the ledger. However, when the upgraded smart contract calls the Init function by default, the KEY1 in the ledger is updated to VALUE1. To avoid similar issues, it is recommended to write the related Init content as an independent function for Invoke to call. When upgrading the smart contract, there is no need to change the Init code.

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백