Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Fabric2.0 Java SDK to realize contract transaction

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use Fabric2.0 Java SDK to achieve contract trading", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Fabric2.0 Java SDK to achieve contract trading" this article.

1.Gateway

Before putting it into practice, there is a relatively new concept to understand: Gateway. In order to cope with the consequences caused by frequent changes in the Fabric network, the original SDK of Fabric2.0 is covered with a layer of gateway Gateway, which is used to reduce the burden of applications.

The specific topology is as follows:

MagnetoCorp and DigiBank applications (distribution and purchase) delegate their respective network interactions to their gateways. Each gateway understands the network channel topology, including multiple peers and order of two organizations, MagnetoCorp and DigiBank, to focus the application on business logic. Nodes can use the gossip protocol to interact with each other in consensus within and between organizations.

two。 Environment preparation system tools version remarks Window10

Fabric2.0 has deployed mychannel channel and mycc contract to two organization nodes Java1.8

Maven3.5.2

Fabric network structure | Node type | Node name | Organization | ip | Service port |-| orderer | orderer.example.com |-| 7050 | peer | peer0.org1.example.com | org1 | 192.168.2.104 | 7051 | peer | peer1.org1.example.com | org1 | 192.168.2.104 | 8051 | peer | peer0.org2.example.com | org2 | 192.168.2.104 | 9051 | peer | peer1org2.example.com | org2 | 192.168.2. | 10051 | ca | ca1.org1.example.com | org1 | 192.168.2.104 | 7054 | ca | ca2.org1.example.com | org2 | 192.168.2.104 | 8054

3. Create a foundation project

Create a new Maven project and add the following dependencies:

Org.hyperledger.fabric fabric-gateway-java 2.0.04. Create connectionProfile

ConnectionProfile is used to create a connected network object. If you have friends who have run through first-network, compared to remembering a command from ccp.sh, you can generate files like connection-org1.json and connection-org1.yaml. In fact, these are the connectionProfile we need and can be used directly. Of course, you can also write it by yourself. Please refer to the official template.

4.1 profile structure description

The overall connectionProfile structure is as follows

Contains the object description:

Parameter name description name Custom Network name version Custom Network version client client related Information channels Network contains Channel Information organizations Network Organization Information orderers sorting Node Information peerpee Node Information certificateAuthoritiesca Node Information 4.1.1 client

4.1.2 channels

There are 4 roles defined in the node object | role | description | |-- |-- | endorsingPeer | node with endorsement permission | chaincodeQuery | node with contract query permission | ledgerQuery | node with account query permission | eventSource | event hub node

4.1.3 organizations

Admin private key and admin signature certificate support path and file content. The path uses the parameter path, the corresponding value enters the path, the file content uses the parameter pem, and the corresponding value enters the private key or certificate content.

4.1.4 orderers

4.1.5 peer

Similar to sort nodes, tlsCACerts must be configured if it is a tls

4.1.6 certificateAuthorities

Similar to sort nodes, tlsCACerts must be configured if it is a tls

5. Description of JAVA project catalogue

The new project directory is as follows

Src/main/java: store the demo main program class src/main/resources/connection.json: the newly created connectionProfilesrc/main/resources/crypto-config above: store the fabric network certificate content (choose what to use) 6. Practice 6.1 creating a gateway account

The gateway account is the fabric user object that connects to the fabric network.

/ use user1 in org1 to initialize a gateway wallet account to connect to the network Wallet wallet = Wallets.newInMemoryWallet (); Path certificatePath = credentialPath.resolve (Paths.get ("signcerts", "User1@org1.example.com-cert.pem")); certificate = readX509Certificate (certificatePath); Path privateKeyPath = credentialPath.resolve ("keystore", "priv_sk")) PrivateKey = getPrivateKey (privateKeyPath); / / put in wallet wallet.put ("user", Identities.newX509Identity ("Org1MSP", certificate,privateKey))

Account objects can be stored in wallet for easy access. The certificate object uses org1's user private key, certificate.

6.2 create a gateway

Create a gateway through connectionProfile and gateway account

/ / obtain Fabric network connection object GatewayImpl.Builder builder = (GatewayImpl.Builder) Gateway.createBuilder (); builder.identity (wallet, "user") .networkConfig (NETWORK_CONFIG_PATH) according to connection-org1.json

NETWORK_CONFIG_PATH: connectionProfile file path

6.3 connection gateway / / connection gateway gateway = builder.connect (); / / obtain mychannel tunnel Network network = gateway.getNetwork ("mychannel"); / / obtain contract object Contract contract = network.getContract ("mycc")

GetNetwork after gateway connection: you can obtain the Fabric specific tunnel network network.getContract according to the channel name: you can obtain the intelligent contract objects deployed to the corresponding channel according to the contract name

6.4 transaction

Based on the smart contract in the previous section, there is an addTen transaction, and the result is that the object is added by 10

First of all, we query the current value of contract object a.

/ / query contract object evaluateTransaction byte [] queryAResultBefore = contract.evaluateTransaction ("query", "a"); System.out.println ("before transaction:" + new String (queryAResultBefore, StandardCharsets.UTF_8))

Then call addTen to do axi10.

/ / create and submit the transaction byte [] invokeResult = contract.createTransaction ("addTen") .setEndorsingPeers (network.getChannel () .getPeers (EnumSet.of (Peer.PeerRole.ENDORSING_PEER) .submit ("a"); System.out.println (new String (invokeResult, StandardCharsets.UTF_8))

Here setEndorsingPeers sets the endorsement node. Here, the collection of endorsement permission nodes in the channel is selected.

Inquire again after the transaction is completed.

/ / query contract object evaluateTransaction byte [] queryAResultAfter = contract.evaluateTransaction ("query", "a"); System.out.println ("after transaction:" + new String (queryAResultAfter, StandardCharsets.UTF_8))

Finally, the console output:

The deal was successful.

The above is all the contents of the article "how to use Fabric2.0 Java SDK to achieve contract transactions". 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report