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.