Skip to content

Commit 17d079e

Browse files
committed
added to OSN new handler for fetch a block of channel
Signed-off-by: Fedor Partanskiy <fredprtnsk@gmail.com>
1 parent 824eb5a commit 17d079e

File tree

8 files changed

+369
-7
lines changed

8 files changed

+369
-7
lines changed

cmd/osnadmin/main.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"io"
1717
"net/http"
1818
"os"
19+
"strconv"
1920

2021
"github.com/hyperledger/fabric-protos-go-apiv2/common"
2122
"github.com/hyperledger/fabric/internal/osnadmin"
@@ -63,6 +64,11 @@ func executeForArgs(args []string) (output string, exit int, err error) {
6364
configUpdateEnvelopePath := update.Flag("config-update-envelope", "Path to the file containing an up-to-date config update envelope for the channel").Short('e').Required().String()
6465
tlsHandshakeTimeShift := update.Flag("tlsHandshakeTimeShift", "The amount of time to shift backwards for certificate expiration checks during TLS handshakes with the orderer endpoint").Short('t').Default("0").Duration()
6566

67+
fetch := channel.Command("fetch", "Fetch a specified block, writing it to a file.")
68+
fetchChannelID := fetch.Flag("channelID", "Channel ID").Short('c').Required().String()
69+
fetchBlockID := fetch.Flag("blockID", "Block ID - <newest|oldest|config|(number)>").Short('b').Required().String()
70+
fetchOutputFile := fetch.Flag("outputfile", "Puth to a file.").Short('o').Required().String()
71+
6672
command, err := app.Parse(args)
6773
if err != nil {
6874
return "", 1, err
@@ -141,6 +147,14 @@ func executeForArgs(args []string) (output string, exit int, err error) {
141147
resp, err = osnadmin.Remove(osnURL, *removeChannelID, caCertPool, tlsClientCert)
142148
case update.FullCommand():
143149
resp, err = osnadmin.Update(osnURL, marshaledConfigEnvelope, caCertPool, tlsClientCert, *tlsHandshakeTimeShift)
150+
case fetch.FullCommand():
151+
if *fetchBlockID != "newest" && *fetchBlockID != "oldest" && *fetchBlockID != "config" {
152+
_, err = strconv.Atoi(*fetchBlockID)
153+
if err != nil {
154+
return "", 1, fmt.Errorf("'%s' not equal <newest|oldest|config|(number)>", *fetchBlockID)
155+
}
156+
}
157+
resp, err = osnadmin.Fetch(osnURL, *fetchChannelID, *fetchBlockID, caCertPool, tlsClientCert)
144158
}
145159
if err != nil {
146160
return errorOutput(err), 1, nil
@@ -151,22 +165,28 @@ func executeForArgs(args []string) (output string, exit int, err error) {
151165
return errorOutput(err), 1, nil
152166
}
153167

154-
output, err = responseOutput(!*noStatus, resp.StatusCode, bodyBytes)
168+
output, err = responseOutput(!*noStatus, resp.StatusCode, bodyBytes, *fetchOutputFile)
155169
if err != nil {
156170
return errorOutput(err), 1, nil
157171
}
158172

159173
return output, 0, nil
160174
}
161175

162-
func responseOutput(showStatus bool, statusCode int, responseBody []byte) (string, error) {
176+
func responseOutput(showStatus bool, statusCode int, responseBody []byte, outputFile string) (string, error) {
163177
var buffer bytes.Buffer
164178
if showStatus {
165179
fmt.Fprintf(&buffer, "Status: %d\n", statusCode)
166180
}
167181
if len(responseBody) != 0 {
168-
if err := json.Indent(&buffer, responseBody, "", "\t"); err != nil {
169-
return "", err
182+
if outputFile != "" {
183+
if err := os.WriteFile(outputFile, responseBody, 0o644); err != nil {
184+
return "", err
185+
}
186+
} else {
187+
if err := json.Indent(&buffer, responseBody, "", "\t"); err != nil {
188+
return "", err
189+
}
170190
}
171191
}
172192
return buffer.String(), nil

cmd/osnadmin/mocks/channel_management.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/osnadmin/osnadmin_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type channelManagement interface {
2323
JoinChannel(channelID string, configBlock *cb.Block) (types.ChannelInfo, error)
2424
UpdateChannel(channelID string, configUpdateEnvelope *cb.Envelope) (types.ChannelInfo, error)
2525
RemoveChannel(channelID string) error
26+
FetchBlock(channelID string, blockID string) (*cb.Block, error)
2627
}
2728

2829
func TestOsnadmin(t *testing.T) {

internal/osnadmin/fetch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package osnadmin
2+
3+
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
// Fetch get block from OSN.
11+
func Fetch(osnURL, channelID string, blockID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*http.Response, error) {
12+
url := fmt.Sprintf("%s/participation/v1/channels/%s/blocks/%s", osnURL, channelID, blockID)
13+
14+
return httpGet(url, caCertPool, tlsClientCert)
15+
}

orderer/common/channelparticipation/mocks/channel_management.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)