run client with fabric2.0 sdk go

development env

first

mkdir -p ~/fabric
cd ~/fabric
export  FABRIC_CFG_PATH=$PWD
cp $GOPATH/src/github.com/hyperledger/fabric/integration/chaincode $GOPATH/src/github.com/hyperledger/fabric/integration/externalbuilders ./ -rp
generate crypto config

Sdk need its membership(record in msp), but sampleconfig/msp is not enough because it has not user, which is essential to sdk. So we should generate it firstly.

crypto-config.yaml

OrdererOrgs:
  - Name: SampleOrg
    Domain: example.com
    Specs:
      - Hostname: orderer
      - Hostname: peer0
    Users:
      Count: 1

generate msp

cryptogen generate --config=./crypto-config.yaml
modify local msp location etc

Although sampleconfig/msp is unusable in the case, but configtx.yaml is ok, core.yaml and orderer.yaml is also ok after a little modification.

cp $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/* ./ -replaced
rm -rf msp

step1: modify orderer.yaml

BootstrapFile: channel-artifacts/genesis.block
Location: production/orderer
LocalMSPDir: crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp

step2: modify core.yaml

fileSystemPath: production/peer
endpoint:
mspConfigPath: crypto-config/ordererOrganizations/example.com/orderers/peer0.example.com/msp

externalBuilders:
- path: /home/ubuntu/fabric/externalbuilders/golang
  name: external-golang
  environmentWhitelist:
  - GOPROXY
  - GOCACHE
  - GOPATH

Note that externalBuilders.path should be replaced by your externalBuilders’ path, which should be a absolute path.

create sdk config

Finally, the config file of sdk is also essential.

config_sdk.yaml

version: 1.0.0
client:
  organization: SampleOrg
  logging:
    level: info
  cryptoconfig:
    path: $CUR/crypto-config
  BCCSP:
    security:
     enabled: true
     default:
      provider: "SW"
     hashAlgorithm: "SHA2"
     softVerify: true
     level: 256
channels:
  mychan1:
    peers:
      peer0.example.com:
        endorsingPeer: true
        chaincodeQuery: true
        ledgerQuery: true
        eventSource: true
organizations:
  SampleOrg:
    mspid: SampleOrg
    cryptoPath:  ordererOrganizations/example.com/users/Admin@example.com/msp
    peers:
      - peer0.example.com
orderers:
  orderer.example.com:
    url: 127.0.0.1:7050
    grpcOptions:
      allow-insecure: true
peers:
  peer0.example.com:
    url: 127.0.0.1:7051
    grpcOptions:
      allow-insecure: true
generate channel.tx
configtxgen -profile SampleSingleMSPChannel -channelID mychan1 -outputCreateChannelTx ./channel-artifacts/channel.tx

run orderer and peer

orderer start
GOCACHE=(go env GOCACHE) peer node start

code

// Package resmgmt enables creation and update of resources on a Fabric network.
// It allows administrators to create and/or update channnels, and for peers to join channels.
// Administrators can also perform chaincode related operations on a peer, such as
// installing, instantiating, and upgrading chaincode.
//
//  Basic Flow:
//  1) Prepare client context
//  2) Create resource managememt client
//  3) Create new channel
//  4) Peer(s) join channel
//  5) Install chaincode onto peer(s) filesystem
//  6) Instantiate chaincode on channel
//  7) Query peer for channels, installed/instantiated chaincodes etc.
step1: new sdk

ConfigFile is config_sdk.yaml above.

sdk, err := fabsdk.New(config.FromFile(ConfigFile))
step2: Create resource managememt client

As setup above, OrgAdmin is “Admin” and OrgName is “SampleOrg”.

clientContext := sdk.Context(fabsdk.WithUser(info.OrgAdmin), fabsdk.WithOrg(info.OrgName))

// New returns a resource management client instance.
resMgmtClient, err := resmgmt.New(clientContext)
step3: Create new channel

As setup above, ChannelID is “mychan1”, ChannelConfig is “network/channel-artifacts/mychan2.tx”, and OrdererOrgName is “orderer.example.com”.

// New creates a new Client instance
mspClient, err := mspclient.New(sdk.Context(), mspclient.WithOrg(info.OrgName))


//  Returns: signing identity
adminIdentity, err := mspClient.GetSigningIdentity(info.OrgAdmin)


// SaveChannelRequest holds parameters for save channel request
channelReq := resmgmt.SaveChannelRequest{ChannelID:info.ChannelID, ChannelConfigPath:info.ChannelConfig, SigningIdentities:[]msp.SigningIdentity{adminIdentity}}
// save channel response with transaction ID
 _, err = resMgmtClient.SaveChannel(channelReq, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint(info.OrdererOrgName))
step4: Peer(s) join channel
resMgmtClient.JoinChannel(info.ChannelID, resmgmt.WithRetry(retry.DefaultResMgmtOpts), resmgmt.WithOrdererEndpoint(info.OrdererOrgName))

Final

It seems that fabric sdk go have not supprt chaincode lifecycle in fabric v2.0. we can’t install or instantiate in fabric2.0 network like before. So my study end for the moment.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值