From 805dc2f7910b5abb9a8854996fbb0166e25a82db Mon Sep 17 00:00:00 2001 From: dimaxgl Date: Tue, 14 Oct 2025 19:07:59 +0300 Subject: [PATCH 1/6] add fetch and update helper functions Signed-off-by: dimaxgl --- internal/osnadmin/fetch.go | 34 +++++++++++++++++++++++++++ internal/osnadmin/update.go | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 internal/osnadmin/fetch.go create mode 100644 internal/osnadmin/update.go diff --git a/internal/osnadmin/fetch.go b/internal/osnadmin/fetch.go new file mode 100644 index 0000000..67cd76e --- /dev/null +++ b/internal/osnadmin/fetch.go @@ -0,0 +1,34 @@ +package osnadmin + +import ( + "crypto/tls" + "crypto/x509" + "fmt" + "io" + + "github.com/hyperledger/fabric-protos-go-apiv2/common" + "google.golang.org/protobuf/proto" +) + +func Fetch(osnURL, channelID string, blockID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*common.Block, error) { + url := fmt.Sprintf("%s/participation/v1/channels/%s/blocks/%s", osnURL, channelID, blockID) + + resp, err := httpGet(url, caCertPool, tlsClientCert) + if err != nil { + return nil, fmt.Errorf("process request: %w", err) + } + if resp.StatusCode != 200 { + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("read response body: %w", err) + } + + var block common.Block + if err := proto.Unmarshal(body, &block); err != nil { + return nil, fmt.Errorf("unmarshal response body: %w", err) + } + return &block, nil +} diff --git a/internal/osnadmin/update.go b/internal/osnadmin/update.go new file mode 100644 index 0000000..8b16ca7 --- /dev/null +++ b/internal/osnadmin/update.go @@ -0,0 +1,47 @@ +package osnadmin + +import ( + "bytes" + "crypto/tls" + "crypto/x509" + "fmt" + "mime/multipart" + "net/http" +) + +// Update channel configuration using presented config envelope. +func Update(osnURL, channelID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate, configEnvelope []byte) (*http.Response, error) { + url := fmt.Sprintf("%s/participation/v1/channels/%s", osnURL, channelID) + + req, err := createUpdateRequest(url, configEnvelope) + if err != nil { + return nil, fmt.Errorf("create update request: %w", err) + } + + return httpDo(req, caCertPool, tlsClientCert) +} + +func createUpdateRequest(url string, configEnvelope []byte) (*http.Request, error) { + joinBody := new(bytes.Buffer) + writer := multipart.NewWriter(joinBody) + part, err := writer.CreateFormFile("config-update-envelope", "config_update.pb") + if err != nil { + return nil, err + } + _, err = part.Write(configEnvelope) + if err != nil { + return nil, err + } + err = writer.Close() + if err != nil { + return nil, err + } + + req, err := http.NewRequest(http.MethodPut, url, joinBody) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", writer.FormDataContentType()) + + return req, nil +} From 99448415f41b2f82b58617c3ce8f03c52de3d4f6 Mon Sep 17 00:00:00 2001 From: dimaxgl Date: Tue, 14 Oct 2025 19:09:15 +0300 Subject: [PATCH 2/6] add fetch and update helper functions Signed-off-by: dimaxgl --- internal/osnadmin/fetch.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/osnadmin/fetch.go b/internal/osnadmin/fetch.go index 67cd76e..21a2e9b 100644 --- a/internal/osnadmin/fetch.go +++ b/internal/osnadmin/fetch.go @@ -5,6 +5,7 @@ import ( "crypto/x509" "fmt" "io" + "net/http" "github.com/hyperledger/fabric-protos-go-apiv2/common" "google.golang.org/protobuf/proto" @@ -17,10 +18,12 @@ func Fetch(osnURL, channelID string, blockID string, caCertPool *x509.CertPool, if err != nil { return nil, fmt.Errorf("process request: %w", err) } - if resp.StatusCode != 200 { + if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("read response body: %w", err) From f043058719d09594960df844abdf9f63323569bc Mon Sep 17 00:00:00 2001 From: dimaxgl Date: Wed, 15 Oct 2025 14:08:54 +0300 Subject: [PATCH 3/6] add fetch and update helper functions Signed-off-by: dimaxgl --- internal/osnadmin/update.go | 9 +++++---- pkg/channel/channel.go | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/osnadmin/update.go b/internal/osnadmin/update.go index 8b16ca7..1bf429f 100644 --- a/internal/osnadmin/update.go +++ b/internal/osnadmin/update.go @@ -2,6 +2,7 @@ package osnadmin import ( "bytes" + "context" "crypto/tls" "crypto/x509" "fmt" @@ -10,10 +11,10 @@ import ( ) // Update channel configuration using presented config envelope. -func Update(osnURL, channelID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate, configEnvelope []byte) (*http.Response, error) { +func Update(ctx context.Context, osnURL, channelID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate, configEnvelope []byte) (*http.Response, error) { url := fmt.Sprintf("%s/participation/v1/channels/%s", osnURL, channelID) - req, err := createUpdateRequest(url, configEnvelope) + req, err := createUpdateRequest(ctx, url, configEnvelope) if err != nil { return nil, fmt.Errorf("create update request: %w", err) } @@ -21,7 +22,7 @@ func Update(osnURL, channelID string, caCertPool *x509.CertPool, tlsClientCert t return httpDo(req, caCertPool, tlsClientCert) } -func createUpdateRequest(url string, configEnvelope []byte) (*http.Request, error) { +func createUpdateRequest(ctx context.Context, url string, configEnvelope []byte) (*http.Request, error) { joinBody := new(bytes.Buffer) writer := multipart.NewWriter(joinBody) part, err := writer.CreateFormFile("config-update-envelope", "config_update.pb") @@ -37,7 +38,7 @@ func createUpdateRequest(url string, configEnvelope []byte) (*http.Request, erro return nil, err } - req, err := http.NewRequest(http.MethodPut, url, joinBody) + req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, joinBody) if err != nil { return nil, err } diff --git a/pkg/channel/channel.go b/pkg/channel/channel.go index db1bdf8..0c47fac 100644 --- a/pkg/channel/channel.go +++ b/pkg/channel/channel.go @@ -13,9 +13,9 @@ import ( "github.com/hyperledger/fabric-admin-sdk/internal/protoutil" "github.com/hyperledger/fabric-admin-sdk/pkg/identity" "github.com/hyperledger/fabric-admin-sdk/pkg/internal/proposal" - "github.com/hyperledger/fabric-protos-go-apiv2/peer" cb "github.com/hyperledger/fabric-protos-go-apiv2/common" + "github.com/hyperledger/fabric-protos-go-apiv2/peer" "google.golang.org/grpc" "google.golang.org/protobuf/proto" ) @@ -110,3 +110,8 @@ func ListChannelOnPeer(ctx context.Context, connection grpc.ClientConnInterface, } return channelQueryResponse.GetChannels(), nil } + +func UpdateChannel(ctx context.Context, osnURL string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate, channelName string, env *cb.Envelope) (*http.Response, error) { + envelopeBytes := protoutil.MarshalOrPanic(env) + return osnadmin.Update(ctx, osnURL, channelName, caCertPool, tlsClientCert, envelopeBytes) +} From 9e6721fc132e314cb10a63a1c2d9db7c0b1608d5 Mon Sep 17 00:00:00 2001 From: dimaxgl Date: Wed, 15 Oct 2025 14:20:22 +0300 Subject: [PATCH 4/6] add fetch and update helper functions Signed-off-by: dimaxgl --- internal/osnadmin/fetch.go | 9 +++++++-- pkg/channel/block.go | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/osnadmin/fetch.go b/internal/osnadmin/fetch.go index 21a2e9b..b43fcba 100644 --- a/internal/osnadmin/fetch.go +++ b/internal/osnadmin/fetch.go @@ -1,6 +1,7 @@ package osnadmin import ( + "context" "crypto/tls" "crypto/x509" "fmt" @@ -11,10 +12,14 @@ import ( "google.golang.org/protobuf/proto" ) -func Fetch(osnURL, channelID string, blockID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*common.Block, error) { +func Fetch(ctx context.Context, osnURL, channelID string, blockID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*common.Block, error) { url := fmt.Sprintf("%s/participation/v1/channels/%s/blocks/%s", osnURL, channelID, blockID) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, fmt.Errorf("create request: %w", err) + } - resp, err := httpGet(url, caCertPool, tlsClientCert) + resp, err := httpDo(req, caCertPool, tlsClientCert) if err != nil { return nil, fmt.Errorf("process request: %w", err) } diff --git a/pkg/channel/block.go b/pkg/channel/block.go index aac92ca..e4b6678 100644 --- a/pkg/channel/block.go +++ b/pkg/channel/block.go @@ -2,8 +2,11 @@ package channel import ( "context" + "crypto/tls" + "crypto/x509" "fmt" + "github.com/hyperledger/fabric-admin-sdk/internal/osnadmin" "github.com/hyperledger/fabric-admin-sdk/pkg/identity" "github.com/hyperledger/fabric-admin-sdk/pkg/internal/proposal" @@ -61,3 +64,7 @@ func getSignedProposal(ctx context.Context, connection grpc.ClientConnInterface, return proposalResp, nil } + +func GetBlock(ctx context.Context, osnURL, channelID, blockID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate) (*cb.Block, error) { + return osnadmin.Fetch(ctx, osnURL, channelID, blockID, caCertPool, tlsClientCert) +} From dbd475bfa1c710b42b79092cd70e742acea645c5 Mon Sep 17 00:00:00 2001 From: dimaxgl Date: Wed, 22 Oct 2025 17:47:51 +0300 Subject: [PATCH 5/6] fix update method signature Signed-off-by: dimaxgl --- internal/osnadmin/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/osnadmin/update.go b/internal/osnadmin/update.go index 1bf429f..2ef1f35 100644 --- a/internal/osnadmin/update.go +++ b/internal/osnadmin/update.go @@ -12,7 +12,7 @@ import ( // Update channel configuration using presented config envelope. func Update(ctx context.Context, osnURL, channelID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate, configEnvelope []byte) (*http.Response, error) { - url := fmt.Sprintf("%s/participation/v1/channels/%s", osnURL, channelID) + url := fmt.Sprintf("%s/participation/v1/channels", osnURL) req, err := createUpdateRequest(ctx, url, configEnvelope) if err != nil { From 2226d112fa6f6c439476fe6fc235021ff7256447 Mon Sep 17 00:00:00 2001 From: dimaxgl Date: Fri, 31 Oct 2025 23:25:23 +0300 Subject: [PATCH 6/6] fix lint for perfsprint Signed-off-by: dimaxgl --- internal/osnadmin/update.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/osnadmin/update.go b/internal/osnadmin/update.go index 2ef1f35..df8e987 100644 --- a/internal/osnadmin/update.go +++ b/internal/osnadmin/update.go @@ -12,7 +12,7 @@ import ( // Update channel configuration using presented config envelope. func Update(ctx context.Context, osnURL, channelID string, caCertPool *x509.CertPool, tlsClientCert tls.Certificate, configEnvelope []byte) (*http.Response, error) { - url := fmt.Sprintf("%s/participation/v1/channels", osnURL) + url := osnURL + "/participation/v1/channels" req, err := createUpdateRequest(ctx, url, configEnvelope) if err != nil {