Skip to content

Commit

Permalink
Switch to yaml
Browse files Browse the repository at this point in the history
change config file format from json to yaml, which is
more convenient for complex config structures.

remove unsed field tag

Signed-off-by: Jay Guo <guojiannan1101@gmail.com>
  • Loading branch information
JIANNNAN GUO authored and guoger committed Nov 6, 2020
1 parent fbee945 commit 70566ea
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 70 deletions.
37 changes: 20 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,25 @@ Clone this repo and run `go build` at root dir. This is a go module project so y

### Configure

Modify `config.json` according to your network. This is a sample:
```json
{
"peer_addrs": ["peer0.org1.example.com:7051","peer0.org2.example.com:9051"],
"orderer_addr": "orderer.example.com:7050",
"channel": "mychannel",
"chaincode": "mycc",
"version": "",
"args": ["put", "key", "value"],
"mspid": "Org1MSP",
"private_key": "wallet/priv.key",
"sign_cert": "wallet/sign.crt",
"tls_ca_certs": ["wallet/pca1.crt","wallet/pca2.crt","wallet/oca.crt"],
"num_of_conn": 20,
"client_per_conn": 40
}
Modify `config.yaml` according to your network. This is a sample:
```yaml
peer_addrs:
- localhost:7051
- localhost:9051
orderer_addr: localhost:7050
channel: mychannel
chaincode: basic
args:
- GetAllAssets
mspid: Org1MSP
private_key: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk
sign_cert: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem
tls_ca_certs:
- ./organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem
- ./organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/tlscacerts/tlsca.org2.example.com-cert.pem
- ./organizations/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
num_of_conn: 10
client_per_conn: 10
```

`peer_addrs`: peer address in IP:Port format. You may need to add peer name, i.e. `peer0.org1.example.com,peer0.org2.example.com` to your `/etc/hosts`
Expand Down Expand Up @@ -91,7 +94,7 @@ crypto-config/peerOrganizations/org1.example.com/users/User1@org1.example.com/ms

### Run

Execute `./stupid config.json 40000` to generate 40000 transactions to Fabric.
Execute `./stupid config.yaml 40000` to generate 40000 transactions to Fabric.

*Set this to integer times of batchsize, so that last block is not cut due to timeout*. For example, if you have batch size of 500, set this to 500, 1000, 40000, 100000, etc.

Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
workingDirectory: '$(System.DefaultWorkingDirectory)'

- job:
displayName: ut
displayName: unit-test
steps:
- task: GoTool@0
inputs:
Expand All @@ -43,7 +43,7 @@ jobs:
arguments: '-v ./... -cover'
workingDirectory: '$(System.DefaultWorkingDirectory)'
- job:
displayName: integrationtest
displayName: integration-test
steps:
- script: ./integration-test.sh

Expand Down
14 changes: 0 additions & 14 deletions config.json

This file was deleted.

17 changes: 17 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
peer_addrs:
- localhost:7051
- localhost:9051
orderer_addr: localhost:7050
channel: mychannel
chaincode: basic
args:
- GetAllAssets
mspid: Org1MSP
private_key: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk
sign_cert: ./organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem
tls_ca_certs:
- ./organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem
- ./organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/tlscacerts/tlsca.org2.example.com-cert.pem
- ./organizations/ordererOrganizations/example.com/msp/tlscacerts/tlsca.example.com-cert.pem
num_of_conn: 10
client_per_conn: 10
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ require (
google.golang.org/grpc v1.24.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect
gopkg.in/yaml.v2 v2.3.0
)
28 changes: 14 additions & 14 deletions infra/config.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package infra

import (
"encoding/json"
"io/ioutil"

"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/msp"
"gopkg.in/yaml.v2"
)

type Config struct {
PeerAddrs []string `json:"peer_addrs"`
OrdererAddr string `json:"orderer_addr"`
Channel string `json:"channel"`
Chaincode string `json:"chaincode"`
Version string `json:"version"`
Args []string `json:"args"`
MSPID string `json:"mspid"`
PrivateKey string `json:"private_key"`
SignCert string `json:"sign_cert"`
TLSCACerts []string `json:"tls_ca_certs"`
NumOfConn int `json:"num_of_conn"`
ClientPerConn int `json:"client_per_conn"`
PeerAddrs []string `yaml:"peer_addrs"`
OrdererAddr string `yaml:"orderer_addr"`
Channel string `yaml:"channel"`
Chaincode string `yaml:"chaincode"`
Version string `yaml:"version"`
Args []string `yaml:"args"`
MSPID string `yaml:"mspid"`
PrivateKey string `yaml:"private_key"`
SignCert string `yaml:"sign_cert"`
TLSCACerts []string `yaml:"tls_ca_certs"`
NumOfConn int `yaml:"num_of_conn"`
ClientPerConn int `yaml:"client_per_conn"`
}

func LoadConfig(f string) Config {
Expand All @@ -30,7 +30,7 @@ func LoadConfig(f string) Config {
}

config := Config{}
if err = json.Unmarshal(raw, &config); err != nil {
if err = yaml.Unmarshal(raw, &config); err != nil {
panic(err)
}

Expand Down
52 changes: 35 additions & 17 deletions infra/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,46 @@ var _ = Describe("Config", func() {
Context("config", func() {
It("successful load", func() {
var configText = `
{
"peer_addr": "peer0.org1.example.com:7051",
"orderer_addr": "orderer.example.com:7050",
"channel": "mychannel",
"chaincode": "mycc",
"args": ["invoke", "a", "b", "1"],
"mspid": "Org1MSP",
"private_key": "/path/to/private.key",
"sign_cert": "/path/to/sign.cert",
"tls_ca_certs": ["/path/to/peer/tls/ca/cert","/path/to/orderer/tls/ca/cert"],
"num_of_conn": 20,
"client_per_conn": 40
}`
f, _ := ioutil.TempFile("", "config-*.json")
peer_addrs:
- peer0.org1.example.com:7051
orderer_addr: orderer.example.com:7050
channel: mychannel
chaincode: mycc
args:
- invoke
- a
- b
- 1
mspid: Org1MSP
private_key: /path/to/private.key
sign_cert: /path/to/sign.cert
tls_ca_certs:
- /path/to/peer/tls/ca/cert
- /path/to/orderer/tls/ca/cert
num_of_conn: 20
client_per_conn: 40`

f, _ := ioutil.TempFile("", "config-*.yaml")
defer os.Remove(f.Name())
f.WriteString(configText)
f.Close()

c := infra.LoadConfig(f.Name())
Expect(c.OrdererAddr).To(Equal("orderer.example.com:7050"))
Expect(c.Channel).To(Equal("mychannel"))
Expect(c.NumOfConn).To(Equal(20))

Expect(c).To(Equal(infra.Config{
PeerAddrs: []string{"peer0.org1.example.com:7051"},
OrdererAddr: "orderer.example.com:7050",
Channel: "mychannel",
Chaincode: "mycc",
Version: "",
Args: []string{"invoke", "a", "b", "1"},
MSPID: "Org1MSP",
PrivateKey: "/path/to/private.key",
SignCert: "/path/to/sign.cert",
TLSCACerts: []string{"/path/to/peer/tls/ca/cert", "/path/to/orderer/tls/ca/cert"},
NumOfConn: 20,
ClientPerConn: 40,
}))
})
})
})
8 changes: 4 additions & 4 deletions infra/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
)

type CryptoConfig struct {
MSPID string `json:"name"`
PrivKey string `json:"private_key"`
SignCert string `json:"sign_cert"`
TLSCACerts []string `json:"tls_ca_cert"`
MSPID string
PrivKey string
SignCert string
TLSCACerts []string
}

type ECDSASignature struct {
Expand Down
2 changes: 1 addition & 1 deletion integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ echo y | ./network.sh up createChannel
echo y | ./network.sh deployCC
cp -r organizations "$DIR" && cd "$DIR"
go build
STUPID_LOGLEVEL=debug ./stupid config.json 100
STUPID_LOGLEVEL=debug ./stupid config.yaml 100


2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
}
}
if len(os.Args) != 3 {
fmt.Printf("Usage: stupid config.json 500\n")
fmt.Printf("Usage: stupid config.yaml 500\n")
os.Exit(1)
}
config := infra.LoadConfig(os.Args[1])
Expand Down

0 comments on commit 70566ea

Please sign in to comment.