In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use IBM Blockchain Platform extension to develop a fabric smart contract, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
IBM Blockchain Platform extension is a plug-in for VSCode, and the latest version is v1.0.17.
The extension supports the complete development workflow of Hyperledger Fabric and IBM Blockchain Platform:
Generate, edit, and package smart contracts
Deploy and debug contracts locally using a simple, pre-configured local architecture network
Connect to any Fabric environment for deployment, including IBM Blockchain Platform services (on IBM Cloud) or software (local and cloudy)
Submit and evaluate transactions and develop customer applications
This can be said to be the artifact of developing Fabric intelligent contracts, which is better than the previous local environment.
So some friends want to ask, since there is such an easy-to-use artifact, is there a simple introduction tutorial?
Don't worry, it's right down here.
Install IBM Blockchain Platform extension for VS Code
IBM Blockchain Platform extension works on VS Code. VS Code is Microsoft's open source editing tool and a very useful development tool.
If you already have VS Code, click the extension in the left sidebar of the screen. At the top, search for IBM Blockchain Platform in the expanded market. Click install, and then click reload. Then it's installed.
Note:
Fabric runs in the docker environment, and smart contracts can now be written in JavaScript, TypeScript, Java, and Go. So you need the following environment:
VS Code version 1.32 or greater
Node v8.x or greater and npm v5.x or greater
Docker version v17.06.2-ce or greater
Docker Compose v1.14.0 or greater
Create a smart contract project
IBM Blockchain Platform extension can generate an intelligent contract framework using the programming language supported by Hyperledger Fabric of your choice. It already contains simple and useful smart contracts.
In this case, we will use TypeScript as an example.
In the left sidebar, click the IBM Blockchain Platform icon (it looks like a square, and if this is the latest extension you installed, it may be at the bottom of the icon set).
Hover over the SMART CONTRACT PACKAGES panel and click "…" Menu, and then select create Smart contract Project from the drop-down list.
Choose a smart contract language. Both JavaScript,TypeScript,Java and Go are available. For this tutorial, select TypeScript.
You will then be asked if you want to name the asset in the generated contract (default is "MyAsset"), and of course you can change it to the name of the asset you want.
Select a location to save the project. Click Browse, then click New folder, and then name the project as needed (for example, "blockchainExtProject").
Click create, then select the new folder you just created, and then click Save.
Finally, select add to Workspace from the list of options.
The extension will generate a framework contract based on the language and asset name of your choice. When you are done, you can navigate to the Explorer view (most likely the top icon in the left column, which looks like the "document" icon) and open the src / my-asset-contract.ts file to view your smart contract code scaffolding.
The resulting file should look like the following figure:
Next, we'll take a look at exactly what the generated smart contract does.
Understand smart contracts
The generated smart contract code scaffold provides some common examples of operations that can be used to interact with data on the blockchain ledger. My-asset-contract.ts is the generated intelligent contract code.
/ * * SPDX-License-Identifier: Apache-2.0 * / import {Context, Contract, Info, Returns, Transaction} from 'fabric-contract-api';import {MyAsset} from'. / my-asset';@Info ({title: 'MyAssetContract', description:' My Smart Contract'}) export class MyAssetContract extends Contract {@ Transaction (false) @ Returns (ctx: Context, myAssetId: string): Promise {const buffer = await ctx.stub.getState (myAssetId) Return (! buffer & & buffer.length > 0);} @ Transaction () public async createMyAsset (ctx: Context, myAssetId: string, value: string): Promise {const exists = await this.myAssetExists (ctx, myAssetId); if (exists) {throw new Error (`The my asset ${myAssetId} already Secrets`);} const myAsset = new MyAsset (); myAsset.value = value Const buffer = Buffer.from (JSON.stringify (myAsset)); await ctx.stub.putState (myAssetId, buffer);} @ Transaction (false) @ Returns ('MyAsset') public async readMyAsset (ctx: Context, myAssetId: string): Promise {const exists = await this.myAssetExists (ctx, myAssetId); if (! exists) {throw new Error (`The my asset ${myAssetId} does not room`) } const buffer = await ctx.stub.getState (myAssetId); const myAsset = JSON.parse (buffer.toString ()) as MyAsset; return myAsset;} @ Transaction () public async updateMyAsset (ctx: Context, myAssetId: string, newValue: string): Promise {const exists = await this.myAssetExists (ctx, myAssetId); if (! exists) {throw new Error (`The my asset ${myAssetId} does not room`) } const myAsset = new MyAsset (); myAsset.value = newValue; const buffer = Buffer.from (JSON.stringify (myAsset)); await ctx.stub.putState (myAssetId, buffer);} @ Transaction () public async deleteMyAsset (ctx: Context, myAssetId: string): Promise {const exists = await this.myAssetExists (ctx, myAssetId) If (! exists) {throw new Error (`The my asset ${myAssetId} does not room`);} await ctx.stub.deleteState (myAssetId);}}
Notice the lines that start with @ Transaction: these are functions that define contract transactions-these things allow you to interact with ledgers.
Let's first look at the createMyAsset function:
@ Transaction () public async createMyAsset (ctx: Context, myAssetId: string, value: string): Promise {const exists = await this.myAssetExists (ctx, myAssetId); if (exists) {throw new Error (`The my asset ${myAssetId} already Secrets`);} const myAsset = new MyAsset (); myAsset.value = value; const buffer = Buffer.from (JSON.stringify (myAsset)) Await ctx.stub.putState (myAssetId, buffer);}
The parentheses in @ Transaction () tell you that this function modifies the contents of the ledger.
This function, called createMyAsset, accepts myAssetId and a value, both of which are strings. After this transaction is committed, a new asset is created using the keyword myAssetId and the value. For example, suppose you want to create "001", "my first asset"; then later, when you read the value of key 001, you will know that the value of that particular state is "my first asset".
Now, look at the next transaction:
@ Transaction (false) @ Returns ('MyAsset') public async readMyAsset (ctx: Context, myAssetId: string): Promise {const exists = await this.myAssetExists (ctx, myAssetId); if (! exists) {throw new Error (`The my asset ${myAssetId} does not room`);} const buffer = await ctx.stub.getState (myAssetId); const myAsset = JSON.parse (buffer.toString () as MyAsset; return myAsset) }
This starts with @ Transaction (false)-"false" indicates that this function usually does not intend to change the contents of the ledger. Such transactions are called "queries". As you can see, this function only takes myAssetId and returns the value of any state that the key points to.
You can take a closer look at other transactions in the contract. You can then continue to package and deploy the contract to use it.
Packaged Smart contract
Now that you've created a smart contract and learned about the deals in it, it's time to pack. The smart contract project is packaged into a .CDS file, which is a special type of file that can be installed on the Hyperledger Fabric node.
In the left sidebar, click the IBM Blockchain Platform icon.
Hover over the SMART CONTRACT PACKAGES panel and click "…" Menu, and then select package Smart contract Project from the drop-down list.
If all goes well, you should see a new package blockchainExtProject@0.0.1 in the list.
The package you just created can be installed on any Hyperledger Fabric peer (running in the correct version). For example, you can right-click and select Export package, and then deploy it to the cloud using the IBM Blockchain Platform operating tools console. Now you will deploy the package locally at the preconfigured runtime of the VS Code extension, so there is no need to export the package now!
Local Fabric Ops
The panel called LOCAL FABRIC OPS (in the IBM Blockchain Platform view) allows you to use Docker's simple Hyperledger Fabric runtime on your local computer. At the beginning, the Fabric should have stopped:
Local Fabric runtime is stopped. Click to start.
Click on the message and the extension will begin to extend the Docker container for you. Then, you should see the message "Local Fabric runtime is starting." When the task is complete, you will see a set of expandable / collapsible parts labeled "Smart contract", "Channel", "Node" and "Organization" respectively. "
Here is a brief description of them:
The Smart contracts section shows you the instantiated and installed contracts on this network. The next two steps in this tutorial will show you how to install and instantiate packaged smart contracts.
There is a channel called "mychannel" under the channel. In order to use a smart contract, it must be instantiated on a channel.
The nodes section contains a peer node (peer0.org1.example.com). Naming follows the Hyperledger Fabric convention, and you can see from the "org1" section that the peer is owned by Org1.
There is also a certification authority (CA) ca.org1.example.com and an order node orderer.example.com.
There is only one organization called "Org1" in this simple blockchain network. It is not realistic to use a network with only one organization in the real world, because the focus is on sharing a ledger among multiple organizations, but it is sufficient for local development purposes. Under Organization, you will see Org1MSP: this is Org1's MSP ID.
Now that you have started the local Fabric runtime, it's time to install and instantiate the smart contract.
Install Smart contract
In a real network, each organization that will support the transaction will install a smart contract on its peer node and then instantiate the contract on the channel. Now the local Fabric runtime has only one organization (Org1), one sibling (peer0.org1.example.com), and one channel (mychannel).
Therefore, you only need to install the contract on that single peer, and then you can instantiate the contract in mychannel. The methods are as follows:
In the Local FABRIC OPS panel, locate + install (under Smart contracts > installed), and then click it.
You will be asked to select a node. Select the only option, peer0.org1.example.com.
You will then be asked to select the package to install. BlockchainExtProject@0.0.1 .
You should see blockchainExtProject@0.0.1 appear under the Smart contract > installed list.
Next, you will instantiate the smart contract...
Instantiate intelligent contract
In the Local FABRIC OPS panel, find + instantiate (under Smart contract > instantiation), and then click it.
You will be asked to select a channel. Select the only option, mychannel.
You will then be asked to select a smart contract to instantiate. Select blockchainExtProject@0.0.1.
Then the system will ask you which function you want to call. If you want to use specific features during instantiation, you can enter content here. Now just press Enter to skip this step.
You will then be asked if you want to provide a private data profile. For this tutorial, just click No.
Instantiation may take longer than installation-pay attention to the success message and display blockchainExtProject@0.0.1 in the Smart contract > instantiation list to confirm that it is valid!
Now your interface should look like this:
Commit and query transactions
The Fabric gateway connects to the peer of the Hyperledger Fabric network, and client applications can use the gateway to commit transactions. When you start a local instance in LOCAL FABRIC OPS, a gateway is also automatically created for you. You can find it under FABRIC GATEWAYS. It's called "local_fabric".
To use the gateway, you also need the identity used to conduct transactions on that network. Similarly, for the local Fabric runtime, this time has been set for you. Notice that under FABRIC WALLETS there is a wallet named local_fabric_wallet that contains an ID named admin. If you hover over "local_fabric" in the "FABRIC GATEWAYS" panel, you will see that it tells you "associated wallet: local_fabric_wallet".
So, you already have a gateway and an associated wallet with a single identity, which means the gateway is ready for use.
Click local_fabric (under FABRIC GATEWAYS) to connect through this gateway.
Expand channel, then expand mychannel and blockchainExtProject@0.0.1. You will see a list of all the transactions defined in the smart contract.
Now you need to create assets. Right-click createMyAsset and select Submit Transaction. You will be asked to provide transaction parameters: try [001 "," my asset one "] (or any keys and values you like, but be sure to remember the keys you use! ).
Parameters are submitted in JSON format, so make sure that you enter the input exactly as shown, so that you can submit an array of 2 strings according to the transaction requirements.
Next, submit the updateMyAsset in a similar manner. This time, provide the same key and different values for the parameters, for example, ["001", "my asset two"]. Therefore, the value of key 001 in the ledger should now be "my asset two". Let's check it out.
ReadMyAsset is used to read rather than write to the ledger, so this time query transactions are selected. Enter ["001"] (or any key you set) as the parameter. You should see the following in the output console:
[SUCCESS] Returned value from readMyAsset: {"value": "my asset two"}
Congratulations, you have completed your first smart contract!
The above is all the contents of the article "how to use IBM Blockchain Platform extension to develop a fabric Smart contract". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.