🎨
Learn hyperledger fabric 2 with golang
  • Learn programming smart contracts in Hyperledger Fabric v2
  • Setting up the Fabric Business Network
    • Step 1 - Bring up the Fabric CA
    • Step 2 - Enroll organisations with Fabric CA
    • Step 3 - Bring up peer nodes
    • Step 4 - Configure a channel
    • Step 5 - Execute the script
  • Create a chain code
    • Deploy and invoke the chain code
    • What just happened
  • Create application side code to invoke smartcode
  • Recap
Powered by GitBook
On this page

Was this helpful?

Create application side code to invoke smartcode

We created the castvote.go as a client to invoke castvote contract in voternetchaincode.

  • First create a wallet for any registered user (user1 in this case). a wallet is nothing but a file containing the users certs,msp info which will be used by the client app to invoke transaction .It represents the identity which invokes the transaction.

  • gateway is responsible for setting up the network configuration for the transaction . Amongst other things it uses service discovery to get all the endorsers info for submitting the transaction.

  • The minimal network info is provided in the config file connection.yaml

  • finally get the contract from the channel and invoke the transaction

castvote.go

wallet, err := gateway.NewFileSystemWallet("wallet")

gw, err := gateway.Connect(
   gateway.WithConfig(config.FromFile(configFilePath)),
   gateway.WithIdentity(wallet, "user"),
)

network, err := gw.GetNetwork("votingchannel")

contract := network.GetContract("voternetchaincode")
result, err := contract.SubmitTransaction("Cast", voter, time.Now().String(), candidate, uuid.New().String())

cd application  // else the filepaths will be wrong

 application % go run castvote.go user2 candidate1
 [fabsdk/core] 2021/06/22 09:59:01 UTC - cryptosuite.GetDefault -> INFO No default cryptosuite found, using default SW implementation
2021/06/22 15:29:03 

application % go run castvote.go user2 candidate1
 [fabsdk/core] 2021/06/22 09:59:11 UTC - cryptosuite.GetDefault -> INFO No default cryptosuite found, using default SW implementation
2021/06/22 15:29:11 couldnt execute transaction: Failed to submit: Multiple errors occurred: - Transaction processing for endorser [localhost:7051]: Chaincode status Code: (500) UNKNOWN. Description: voter already voted - Transaction processing for endorser [localhost:9051]: Chaincode status Code: (500) UNKNOWN. Description: voter already voted
exit status 1
PreviousWhat just happenedNextRecap

Last updated 3 years ago

Was this helpful?