Skip to content

Commit f9ea6ac

Browse files
committed
splitting the peer into a client and a server
Signed-off-by: Fedor Partanskiy <[email protected]>
1 parent cdd7bea commit f9ea6ac

22 files changed

+565
-350
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ GO_TAGS ?=
8686
RELEASE_EXES = orderer $(TOOLS_EXES)
8787
RELEASE_IMAGES = baseos ccenv orderer peer
8888
RELEASE_PLATFORMS = darwin-amd64 darwin-arm64 linux-amd64 linux-arm64 windows-amd64
89-
TOOLS_EXES = configtxgen configtxlator cryptogen discover ledgerutil osnadmin peer
89+
TOOLS_EXES = configtxgen configtxlator cryptogen discover ledgerutil osnadmin peer cli
9090

9191
pkgmap.configtxgen := $(PKGNAME)/cmd/configtxgen
9292
pkgmap.configtxlator := $(PKGNAME)/cmd/configtxlator
@@ -96,6 +96,7 @@ pkgmap.ledgerutil := $(PKGNAME)/cmd/ledgerutil
9696
pkgmap.orderer := $(PKGNAME)/cmd/orderer
9797
pkgmap.osnadmin := $(PKGNAME)/cmd/osnadmin
9898
pkgmap.peer := $(PKGNAME)/cmd/peer
99+
pkgmap.cli := $(PKGNAME)/cmd/cli
99100

100101
.DEFAULT_GOAL := all
101102

cmd/cli/main.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package main
8+
9+
import (
10+
_ "net/http/pprof"
11+
"os"
12+
"strings"
13+
14+
"github.com/hyperledger/fabric-lib-go/bccsp/factory"
15+
"github.com/hyperledger/fabric/internal/peer/chaincode"
16+
"github.com/hyperledger/fabric/internal/peer/channel"
17+
"github.com/hyperledger/fabric/internal/peer/common"
18+
"github.com/hyperledger/fabric/internal/peer/lifecycle"
19+
"github.com/hyperledger/fabric/internal/peer/snapshot"
20+
"github.com/hyperledger/fabric/internal/peer/version"
21+
"github.com/spf13/cobra"
22+
"github.com/spf13/viper"
23+
)
24+
25+
// The main command describes the service and
26+
// defaults to printing the help message.
27+
var mainCmd = &cobra.Command{Use: "cli"}
28+
29+
func main() {
30+
setEnvConfig(viper.GetViper())
31+
32+
// Define command-line flags that are valid for all cli commands and
33+
// subcommands.
34+
mainFlags := mainCmd.PersistentFlags()
35+
36+
mainFlags.String("logging-level", "", "Legacy logging level flag")
37+
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
38+
mainFlags.MarkHidden("logging-level")
39+
40+
cryptoProvider := factory.GetDefault()
41+
42+
mainCmd.AddCommand(version.Cmd())
43+
mainCmd.AddCommand(chaincode.Cmd(nil, cryptoProvider))
44+
mainCmd.AddCommand(channel.Cmd(nil))
45+
mainCmd.AddCommand(lifecycle.Cmd(cryptoProvider))
46+
mainCmd.AddCommand(snapshot.Cmd(cryptoProvider))
47+
48+
// On failure Cobra prints the usage message and error string, so we only
49+
// need to exit with a non-0 status
50+
if mainCmd.Execute() != nil {
51+
os.Exit(1)
52+
}
53+
}
54+
55+
func setEnvConfig(v *viper.Viper) {
56+
v.SetEnvPrefix(common.CmdRoot)
57+
v.AllowEmptyEnv(true)
58+
v.AutomaticEnv()
59+
replacer := strings.NewReplacer(".", "_")
60+
v.SetEnvKeyReplacer(replacer)
61+
}

cmd/cli/main_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package main
8+
9+
import (
10+
"testing"
11+
12+
"github.com/spf13/viper"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestSetEnvConfig(t *testing.T) {
18+
vp := viper.New()
19+
t.Setenv("CORE_FRUIT", "Apple")
20+
t.Setenv("CORE_COLOR", "")
21+
err := vp.BindEnv("Fruit")
22+
require.NoError(t, err)
23+
err = vp.BindEnv("Color")
24+
require.NoError(t, err)
25+
vp.SetDefault("Color", "Green")
26+
27+
setEnvConfig(vp)
28+
29+
assert.Equal(t, "Apple", vp.Get("Fruit"))
30+
assert.Equal(t, "", vp.Get("Color"))
31+
}

cmd/peer/main.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ import (
1111
"os"
1212
"strings"
1313

14-
"github.com/hyperledger/fabric-lib-go/bccsp/factory"
15-
"github.com/hyperledger/fabric/internal/peer/chaincode"
16-
"github.com/hyperledger/fabric/internal/peer/channel"
1714
"github.com/hyperledger/fabric/internal/peer/common"
18-
"github.com/hyperledger/fabric/internal/peer/lifecycle"
1915
"github.com/hyperledger/fabric/internal/peer/node"
20-
"github.com/hyperledger/fabric/internal/peer/snapshot"
2116
"github.com/hyperledger/fabric/internal/peer/version"
2217
"github.com/spf13/cobra"
2318
"github.com/spf13/viper"
@@ -38,14 +33,8 @@ func main() {
3833
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
3934
mainFlags.MarkHidden("logging-level")
4035

41-
cryptoProvider := factory.GetDefault()
42-
4336
mainCmd.AddCommand(version.Cmd())
4437
mainCmd.AddCommand(node.Cmd())
45-
mainCmd.AddCommand(chaincode.Cmd(nil, cryptoProvider))
46-
mainCmd.AddCommand(channel.Cmd(nil))
47-
mainCmd.AddCommand(lifecycle.Cmd(cryptoProvider))
48-
mainCmd.AddCommand(snapshot.Cmd(cryptoProvider))
4938

5039
// On failure Cobra prints the usage message and error string, so we only
5140
// need to exit with a non-0 status

docs/source/command_ref.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ Commands Reference
55
:maxdepth: 1
66

77
commands/peercommand.md
8-
commands/peerchaincode.md
9-
commands/peerlifecycle.md
10-
commands/peerchannel.md
11-
commands/peersnapshot.md
12-
commands/peerversion.md
138
commands/peernode.md
9+
commands/peerversion.md
10+
commands/clicommand.md
11+
commands/clichaincode.md
12+
commands/clilifecycle.md
13+
commands/clichannel.md
14+
commands/clisnapshot.md
15+
commands/cliversion.md
1416
commands/osnadminchannel.md
1517
commands/configtxgen.md
1618
commands/configtxlator.md
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
Please make changes to preamble and postscript wrappers as appropriate.
44
--->
55

6-
# peer chaincode
6+
# cli chaincode
77

8-
The `peer chaincode` command allows users to invoke and query chaincode.
8+
The `cli chaincode` command allows users to invoke and query chaincode.
99

1010
## Syntax
1111

12-
The `peer chaincode` command has the following subcommands:
12+
The `cli chaincode` command has the following subcommands:
1313

1414
* invoke
1515
* query
@@ -26,14 +26,14 @@ Args is an array of arguments of the function. For instance,
2626
`{"Args":["GetAllAssets"]}` is equivalent to
2727
`{"Function":"GetAllAssets", "Args":[]}`.
2828

29-
Each peer chaincode subcommand is described together with its options in its own
29+
Each cli chaincode subcommand is described together with its options in its own
3030
section in this topic.
3131

3232
## Flags
3333

34-
Each `peer chaincode` subcommand has both a set of flags specific to an
34+
Each `cli chaincode` subcommand has both a set of flags specific to an
3535
individual subcommand, as well as a set of global flags that relate to all
36-
`peer chaincode` subcommands. Not all subcommands would use these flags.
36+
`cli chaincode` subcommands. Not all subcommands would use these flags.
3737
For instance, the `query` subcommand does not need the `--orderer` flag.
3838

3939
The individual flags are described with the relevant subcommand. The global
@@ -75,12 +75,12 @@ values. For example, you will use `--peerAddresses localhost:9051
7575
--peerAddresses localhost:7051` rather than `--peerAddresses "localhost:9051
7676
localhost:7051"`.
7777

78-
## peer chaincode invoke
78+
## cli chaincode invoke
7979
```
8080
Invoke the specified chaincode. It will try to commit the endorsed transaction to the network.
8181
8282
Usage:
83-
peer chaincode invoke [flags]
83+
cli chaincode invoke [flags]
8484
8585
Flags:
8686
-C, --channelID string The channel on which this command should be executed
@@ -108,12 +108,12 @@ Global Flags:
108108
```
109109

110110

111-
## peer chaincode query
111+
## cli chaincode query
112112
```
113113
Get endorsed result of chaincode function call and print it. It won't generate transaction.
114114
115115
Usage:
116-
peer chaincode query [flags]
116+
cli chaincode query [flags]
117117
118118
Flags:
119119
-C, --channelID string The channel on which this command should be executed
@@ -141,17 +141,17 @@ Global Flags:
141141

142142
## Example Usage
143143

144-
### peer chaincode invoke example
144+
### cli chaincode invoke example
145145

146-
Here is an example of the `peer chaincode invoke` command:
146+
Here is an example of the `cli chaincode invoke` command:
147147

148148
* Invoke the chaincode named `mycc` at version `1.0` on channel `mychannel`
149149
on `peer0.org1.example.com:7051` and `peer0.org2.example.com:9051` (the
150150
peers defined by `--peerAddresses`), requesting to move 10 units from
151151
variable `a` to variable `b`:
152152

153153
```
154-
peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:9051 -c '{"Args":["invoke","a","b","10"]}'
154+
cli chaincode invoke -o orderer.example.com:7050 -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:9051 -c '{"Args":["invoke","a","b","10"]}'
155155
156156
2018-02-22 16:34:27.069 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
157157
2018-02-22 16:34:27.069 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
@@ -176,28 +176,28 @@ Here is an example of the `peer chaincode invoke` command:
176176
successfully. The transaction will then be added to a block and, finally, validated
177177
or invalidated by each peer on the channel.
178178
179-
Here is an example of how to format the `peer chaincode invoke` command when the chaincode package includes multiple smart contracts.
179+
Here is an example of how to format the `cli chaincode invoke` command when the chaincode package includes multiple smart contracts.
180180
181181
* If you are using the [contract-api](https://www.npmjs.com/package/fabric-contract-api), the name you pass to `super("MyContract")` can be used as a prefix.
182182
183183
```
184-
peer chaincode invoke -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{ "Args": ["MyContract:methodName", "{}"] }'
184+
cli chaincode invoke -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{ "Args": ["MyContract:methodName", "{}"] }'
185185
186-
peer chaincode invoke -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{ "Args": ["MyOtherContract:methodName", "{}"] }'
186+
cli chaincode invoke -C $CHANNEL_NAME -n $CHAINCODE_NAME -c '{ "Args": ["MyOtherContract:methodName", "{}"] }'
187187
188188
```
189189
190-
### peer chaincode query example
190+
### cli chaincode query example
191191
192-
Here is an example of the `peer chaincode query` command, which queries the
192+
Here is an example of the `cli chaincode query` command, which queries the
193193
peer ledger for the chaincode named `mycc` at version `1.0` for the value of
194194
variable `a`:
195195
196196
* You can see from the output that variable `a` had a value of 90 at the time of
197197
the query.
198198
199199
```
200-
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
200+
cli chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
201201
202202
2018-02-22 16:34:30.816 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
203203
2018-02-22 16:34:30.816 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc

0 commit comments

Comments
 (0)