Skip to content

Commit e9b45f7

Browse files
committed
add control peer rest api
Signed-off-by: Fedor Partanskiy <fredprtnsk@gmail.com>
1 parent e1c5a1f commit e9b45f7

File tree

81 files changed

+25999
-38
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+25999
-38
lines changed

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/go-viper/mapstructure/v2 v2.4.0
1616
github.com/gorilla/handlers v1.5.2
1717
github.com/gorilla/mux v1.8.1
18+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3
1819
github.com/hyperledger-labs/SmartBFT v0.0.0-20250503203013-eb005eef8866
1920
github.com/hyperledger/fabric-chaincode-go/v2 v2.3.0
2021
github.com/hyperledger/fabric-config v0.3.0
@@ -36,6 +37,8 @@ require (
3637
go.etcd.io/etcd/raft/v3 v3.5.16
3738
go.etcd.io/etcd/server/v3 v3.5.16
3839
go.uber.org/zap v1.27.0
40+
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4
41+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4
3942
google.golang.org/grpc v1.72.0
4043
google.golang.org/protobuf v1.36.6
4144
gopkg.in/alecthomas/kingpin.v2 v2.2.6
@@ -71,7 +74,7 @@ require (
7174
github.com/gogo/protobuf v1.3.2 // indirect
7275
github.com/golang/protobuf v1.5.4 // indirect
7376
github.com/golang/snappy v0.0.4 // indirect
74-
github.com/google/go-cmp v0.6.0 // indirect
77+
github.com/google/go-cmp v0.7.0 // indirect
7578
github.com/google/pprof v0.0.0-20241001023024-f4c0cfd0cf1d // indirect
7679
github.com/hyperledger/aries-bbs-go v0.0.0-20240528084656-761671ea73bc // indirect
7780
github.com/hyperledger/fabric-amcl v0.0.0-20230602173724-9e02669dceb2 // indirect
@@ -116,5 +119,4 @@ require (
116119
golang.org/x/sys v0.33.0 // indirect
117120
golang.org/x/text v0.26.0 // indirect
118121
golang.org/x/tools v0.33.0 // indirect
119-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
120122
)

go.sum

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,8 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
820820
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
821821
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
822822
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
823-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
824-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
823+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
824+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
825825
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
826826
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
827827
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@@ -875,6 +875,8 @@ github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWS
875875
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
876876
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
877877
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w=
878+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3 h1:5ZPtiqj0JL5oKWmcsq4VMaAW5ukBEgSGXEN89zeH1Jo=
879+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3/go.mod h1:ndYquD05frm2vACXE1nsccT4oJzjhw2arTS2cpUD1PI=
878880
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
879881
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
880882
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
@@ -1735,8 +1737,10 @@ google.golang.org/genproto v0.0.0-20230323212658-478b75c54725/go.mod h1:UUQDJDOl
17351737
google.golang.org/genproto v0.0.0-20230330154414-c0448cd141ea/go.mod h1:UUQDJDOlWu4KYeJZffbWgBkS1YFobzKbLVfK69pe0Ak=
17361738
google.golang.org/genproto v0.0.0-20230331144136-dcfb400f0633/go.mod h1:UUQDJDOlWu4KYeJZffbWgBkS1YFobzKbLVfK69pe0Ak=
17371739
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
1738-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a h1:51aaUVRocpvUOSQKM6Q7VuoaktNIaMCLuhZB6DKksq4=
1739-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a/go.mod h1:uRxBH1mhmO8PGhU89cMcHaXKZqO+OfakD8QQO0oYwlQ=
1740+
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 h1:IFnXJq3UPB3oBREOodn1v1aGQeZYQclEmvWRMN0PSsY=
1741+
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:c8q6Z6OCqnfVIqUFJkCzKcrj8eCvUrz+K4KRzSTuANg=
1742+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 h1:iK2jbkWL86DXjEx0qiHcRE9dE4/Ahua5k6V8OWFb//c=
1743+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
17401744
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
17411745
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
17421746
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package e2e
8+
9+
import (
10+
"fmt"
11+
"os"
12+
"runtime"
13+
"syscall"
14+
15+
"github.com/hyperledger/fabric/integration/restpeer"
16+
17+
docker "github.com/fsouza/go-dockerclient"
18+
"github.com/hyperledger/fabric/integration/nwo"
19+
. "github.com/onsi/ginkgo/v2"
20+
. "github.com/onsi/gomega"
21+
"github.com/tedsuo/ifrit"
22+
)
23+
24+
var _ = Describe("PeerRestAPI", func() {
25+
var (
26+
client *docker.Client
27+
tempDir string
28+
)
29+
30+
BeforeEach(func() {
31+
var err error
32+
tempDir, err = os.MkdirTemp("", "nwo")
33+
Expect(err).NotTo(HaveOccurred())
34+
35+
client, err = docker.NewClientFromEnv()
36+
Expect(err).NotTo(HaveOccurred())
37+
})
38+
39+
AfterEach(func() {
40+
err := os.RemoveAll(tempDir)
41+
Expect(err).NotTo(HaveOccurred())
42+
})
43+
44+
Describe("Rest tests", func() {
45+
var network *nwo.Network
46+
// var ordererRunner *ginkgomon.Runner
47+
var ordererProcess, peerProcess ifrit.Process
48+
49+
BeforeEach(func() {
50+
network = nwo.New(nwo.BasicEtcdRaft(), tempDir, client, StartPort(), components)
51+
52+
// Generate config and bootstrap the network
53+
network.GenerateConfigTree()
54+
network.Bootstrap()
55+
56+
// Start all the fabric processes
57+
_, ordererProcess, peerProcess = network.StartSingleOrdererNetwork("orderer")
58+
})
59+
60+
AfterEach(func() {
61+
if ordererProcess != nil {
62+
ordererProcess.Signal(syscall.SIGTERM)
63+
Eventually(ordererProcess.Wait(), network.EventuallyTimeout).Should(Receive())
64+
}
65+
66+
if peerProcess != nil {
67+
peerProcess.Signal(syscall.SIGTERM)
68+
Eventually(peerProcess.Wait(), network.EventuallyTimeout).Should(Receive())
69+
}
70+
71+
network.Cleanup()
72+
})
73+
74+
It("GET version", func() {
75+
peer := network.Peer("Org1", "peer0")
76+
infoStr := fmt.Sprintf(
77+
"peer:\n Version: latest\n Commit SHA: development build\n Go version: %s\n OS/Arch: %s\n Chaincode:\n Base Docker Label: org.hyperledger.fabric\n Docker Namespace: hyperledger\n\n",
78+
runtime.Version(),
79+
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
80+
)
81+
restpeer.Version(network, peer, infoStr)
82+
})
83+
})
84+
})
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package restpeer
8+
9+
import (
10+
"context"
11+
"encoding/json"
12+
"fmt"
13+
"io"
14+
"net/http"
15+
16+
"github.com/hyperledger/fabric/integration/nwo"
17+
"github.com/hyperledger/fabric/internal/peer/rest"
18+
"github.com/hyperledger/fabric/internal/peer/rest/pbrest"
19+
. "github.com/onsi/gomega"
20+
)
21+
22+
func Version(n *nwo.Network, p *nwo.Peer, expectedResponse string) {
23+
protocol := "http"
24+
if n.TLSEnabled {
25+
protocol = "https"
26+
}
27+
url := fmt.Sprintf("%s://127.0.0.1:%d%sversion", protocol, n.PeerPort(p, nwo.OperationsPort), rest.URLBaseV1)
28+
authClient, unauthClient := nwo.PeerOperationalClients(n, p)
29+
30+
client := unauthClient
31+
if n.TLSEnabled {
32+
client = authClient
33+
}
34+
35+
body := getBody(client, url)()
36+
verResp := &pbrest.VersionResponse{}
37+
err := json.Unmarshal([]byte(body), verResp)
38+
Expect(err).NotTo(HaveOccurred())
39+
Expect(verResp.GetInfo()).To(Equal(expectedResponse))
40+
}
41+
42+
func getBody(client *http.Client, url string) func() string {
43+
return func() string {
44+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
45+
Expect(err).NotTo(HaveOccurred())
46+
resp, err := client.Do(req)
47+
Expect(err).NotTo(HaveOccurred())
48+
bodyBytes, err := io.ReadAll(resp.Body)
49+
Expect(err).NotTo(HaveOccurred())
50+
err = resp.Body.Close()
51+
Expect(err).NotTo(HaveOccurred())
52+
return string(bodyBytes)
53+
}
54+
}

internal/peer/node/start.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import (
8888
gossipprivdata "github.com/hyperledger/fabric/gossip/privdata"
8989
gossipservice "github.com/hyperledger/fabric/gossip/service"
9090
peergossip "github.com/hyperledger/fabric/internal/peer/gossip"
91+
"github.com/hyperledger/fabric/internal/peer/rest"
9192
"github.com/hyperledger/fabric/internal/peer/version"
9293
"github.com/hyperledger/fabric/internal/pkg/comm"
9394
"github.com/hyperledger/fabric/internal/pkg/gateway"
@@ -230,11 +231,6 @@ func serve(args []string) error {
230231
platformRegistry := platforms.NewRegistry(platforms.SupportedPlatforms...)
231232

232233
opsSystem := newOperationsSystem(coreConfig)
233-
err = opsSystem.Start()
234-
if err != nil {
235-
return errors.WithMessage(err, "failed to initialize operations subsystem")
236-
}
237-
defer opsSystem.Stop()
238234

239235
metricsProvider := opsSystem.Provider
240236
logObserver := floggingmetrics.NewObserver(metricsProvider)
@@ -776,6 +772,24 @@ func serve(args []string) error {
776772

777773
logger.Infof("Deployed system chaincodes")
778774

775+
// register the rest api server
776+
opsSystem.RegisterHandler(
777+
rest.URLBaseV1,
778+
rest.NewRestAPIHandler(serverEndorser),
779+
coreConfig.OperationsTLSEnabled,
780+
)
781+
782+
err = opsSystem.Start()
783+
if err != nil {
784+
return errors.WithMessage(err, "failed to initialize operations subsystem")
785+
}
786+
defer func() {
787+
err = opsSystem.Stop()
788+
if err != nil {
789+
errors.WithMessage(err, "failed to stop operations subsystem")
790+
}
791+
}()
792+
779793
// register the lifecycleMetadataManager to get updates from the legacy
780794
// chaincode; lifecycleMetadataManager will aggregate these updates with
781795
// the ones from the new lifecycle and deliver both
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package pbrest
8+
9+
//go:generate protoc -I=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --grpc-gateway_out=. --grpc-gateway_opt=paths=source_relative --openapiv2_out=. rest.proto
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package google.api;
18+
19+
import "google/api/http.proto";
20+
import "google/protobuf/descriptor.proto";
21+
22+
option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
23+
option java_multiple_files = true;
24+
option java_outer_classname = "AnnotationsProto";
25+
option java_package = "com.google.api";
26+
option objc_class_prefix = "GAPI";
27+
28+
extend google.protobuf.MethodOptions {
29+
// See `HttpRule`.
30+
HttpRule http = 72295728;
31+
}

0 commit comments

Comments
 (0)