chainHero / heroes-service

Short tutorial to build a blockchain application in Go with Hyperledger Fabric
https://chainhero.io/2018/06/tutorial-build-blockchain-app-v1-1-0/
Apache License 2.0
275 stars 147 forks source link

How can I use multiple chaincode #19

Closed RogerHsiehNCNCCU closed 6 years ago

RogerHsiehNCNCCU commented 6 years ago

main.go

fSetup := blockchain.FabricSetup{
        // Channel parameters
        ChannelID:     "chainhero",
        ChannelConfig: os.Getenv("GOPATH") + "/src/github.com/chainHero/heroes-service/fixtures/artifacts/chainhero.channel.tx",

        // Chaincode parameters
        ChainCodeID:     "heroes-service",
        ChaincodeGoPath: os.Getenv("GOPATH"),
        ChaincodePath:   "github.com/chainHero/heroes-service/chaincode/",
        OrgAdmin:        "Admin",
        OrgName:         "Org1",
        ConfigFile:      "config.yaml",

        // User parameters
        UserName: "User1",
    }

     fmt.Printf("第一個chiancode!!!")
    fmt.Println(fSetup)

    // Initialization of the Fabric SDK from the previously set properties
    err := fSetup.Initialize()
    if err != nil {
        fmt.Printf("Unable to initialize the Fabric SDK: %v\n", err)
    }

    // Install and instantiate the chaincode
    err = fSetup.InstallAndInstantiateCC()
    if err != nil {
        fmt.Printf("Unable to install and instantiate the chaincode: %v\n", err)
    }

    fmt.Printf("第二個chiancode!!!")

    fSetup2 := &blockchain.FabricSetup{
        // Channel parameters
        ChannelID:     "chainhero",
        ChannelConfig: os.Getenv("GOPATH") + "/src/github.com/chainHero/heroes-service/fixtures/artifacts/chainhero.channel.tx",

        // Chaincode parameters
        ChainCodeID:     "initLedger",
        ChaincodeGoPath: os.Getenv("GOPATH"),
        ChaincodePath:   "github.com/chainHero/heroes-service/chaincode/initLedger/",
        OrgAdmin:        "Admin",
        OrgName:         "Org1",
        ConfigFile:      "config.yaml",

        // User parameters
        UserName: "User1",
    }

    fmt.Println(*fSetup2)

    // Initialization of the Fabric SDK from the previously set properties
    /*err2 := fSetup2.Initialize2()
    if err2 != nil {
        fmt.Printf("Unable to initialize the Fabric SDK: %v\n", err2)
    }*/

    // Install and instantiate the chaincode
    err2 := fSetup2.InstallAndInstantiateCC()
    if err2 != nil {
        fmt.Printf("Unable to install and instantiate the chaincode: %v\n", err2)
    }

I try to install and instantiate multiple chaincode, but some error happened.

Environment up
Start app ...
第一個chiancode!!!{config.yaml  chainhero heroes-service false /opt/gopath//src/                                                                                                             github.com/chainHero/heroes-service/fixtures/artifacts/chainhero.channel.tx /opt                                                                                                             /gopath/ github.com/chainHero/heroes-service/chaincode/ Admin Org1 User1 <nil> <                                                                                                             nil> <nil>}
 [fabric_sdk_go] 2018/06/29 04:44:13 UTC - config.initConfig -> INFO config fabr                                                                                                             ic_sdk_go logging level is set to: INFO
Initialization Successful
Chaincode Installation & Instantiation Successful
第二個chiancode!!!{config.yaml  chainhero initLedger false /opt/gopath//src/gith                                                                                                             ub.com/chainHero/heroes-service/fixtures/artifacts/chainhero.channel.tx /opt/gop                                                                                                             ath/ github.com/chainHero/heroes-service/chaincode/initLedger/ Admin Org1 User1                                                                                                              <nil> <nil> <nil>}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x9a302d]

goroutine 1 [running]:
github.com/chainHero/heroes-service/blockchain.(*FabricSetup).InstallAndInstanti                                                                                                             ateCC(0xc420181dd0, 0x1, 0x1)
        /opt/gopath/src/github.com/chainHero/heroes-service/blockchain/setup.go:                                                                                                             119 +0x1ad
main.main()
        /opt/gopath/src/github.com/chainHero/heroes-service/main.go:74 +0x529
Makefile:29: recipe for target 'run' failed
make: *** [run] Error 2

Thanks!

RogerHsiehNCNCCU commented 6 years ago

Or the way I try to use multiple chaincode is wrong? Are there other ways to install and instantiate multiple chaincode with go sdk?

sshmaxime commented 6 years ago

Hello @a037580238,

It easier for us to help you if you can share with us your exploitation system and Go version.

However, from what I can see, you are calling err2 := fSetup2.InstallAndInstantiateCC() without initializing it (you commented the call to Initialization part).

You should keep only one FabricSetup, add a parameter to the function InstallAndInstantiateCC and pass it a structure which contain info on the chaincode you want to install and instantiate.

I hope it will help :smiley:

RogerHsiehNCNCCU commented 6 years ago

It works! Thanks a lot!