Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl for get block from orderer #115

Merged
merged 12 commits into from
Apr 26, 2023
92 changes: 92 additions & 0 deletions pkg/channel/getBlock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package channel

import (
"crypto/sha256"
"crypto/tls"
"fmt"

"github.com/hyperledger/fabric-admin-sdk/internal/protoutil"
"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
cb "github.com/hyperledger/fabric-protos-go-apiv2/common"
ab "github.com/hyperledger/fabric-protos-go-apiv2/orderer"
)

var Newest = &ab.SeekPosition{
SamYuan1990 marked this conversation as resolved.
Show resolved Hide resolved
Type: &ab.SeekPosition_Newest{
Newest: &ab.SeekNewest{},
},
}

func GetNewstBlock(Service ab.AtomicBroadcast_DeliverClient, channelID string, Certificate tls.Certificate, signer identity.SigningIdentity, bestEffort bool) (*cb.Block, error) {
var tlsCertHash []byte
hasher := sha256.New()
hasher.Write([]byte(Certificate.Certificate[0]))
tlsCertHash = hasher.Sum(nil)
err := seekNewest(Service, channelID, tlsCertHash, signer, bestEffort)
if err != nil {
return nil, err
}
return readBlock(Service)
}

func readBlock(Service ab.AtomicBroadcast_DeliverClient) (*cb.Block, error) {
msg, err := Service.Recv()
if err != nil {
return nil, err
}
switch t := msg.Type.(type) {
case *ab.DeliverResponse_Status:
//logger.Infof("Expect block, but got status: %v", t)
return nil, fmt.Errorf("can't read the block: %v", t)
case *ab.DeliverResponse_Block:
//logger.Infof("Received block: %v", t.Block.Header.Number)
if resp, err := Service.Recv(); err != nil { // Flush the success message
fmt.Printf("Failed to flush success message: %s", err)
} else if status := resp.GetStatus(); status != cb.Status_SUCCESS {
fmt.Printf("Expect status to be SUCCESS, got: %s", status)
}

return t.Block, nil
default:
return nil, fmt.Errorf("response error: unknown type %T", t)
}
}

func seekNewest(Service ab.AtomicBroadcast_DeliverClient, channelID string, tlsCertHash []byte, signer identity.SigningIdentity, bestEffort bool) error {
env := seekHelper(channelID, Newest, tlsCertHash, signer, bestEffort)
return Service.Send(env)
}

func seekHelper(
channelID string,
position *ab.SeekPosition,
tlsCertHash []byte,
signer identity.SigningIdentity,
bestEffort bool,
) *cb.Envelope {
seekInfo := &ab.SeekInfo{
Start: position,
Stop: position,
Behavior: ab.SeekInfo_BLOCK_UNTIL_READY,
}

if bestEffort {
seekInfo.ErrorResponse = ab.SeekInfo_BEST_EFFORT
}

env, err := protoutil.CreateSignedEnvelopeWithTLSBinding(
cb.HeaderType_DELIVER_SEEK_INFO,
channelID,
signer,
seekInfo,
int32(0),
uint64(0),
tlsCertHash,
)
if err != nil {
fmt.Printf("Error signing envelope: %s", err)
return nil
}

return env
}
19 changes: 19 additions & 0 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
"github.com/hyperledger/fabric-gateway/pkg/client"
gatewaypb "github.com/hyperledger/fabric-protos-go-apiv2/gateway"
"github.com/hyperledger/fabric-protos-go-apiv2/orderer"

"github.com/hyperledger/fabric-protos-go-apiv2/peer"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -166,6 +167,24 @@ var _ = Describe("e2e", func() {
Expect(err).NotTo(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(http.StatusCreated))

//osnURL
order := network.Node{
Addr: "localhost:7050",
TLSCACert: clientCert,
}
err = order.LoadConfig()
Expect(err).NotTo(HaveOccurred())
ordererConnection, err := network.DialConnection(order)
Expect(err).NotTo(HaveOccurred())
AtomicBroadcastClient := orderer.NewAtomicBroadcastClient(ordererConnection)
ctx, cancel := context.WithTimeout(specCtx, 2*time.Minute)
defer cancel()
DeliverClient, err := AtomicBroadcastClient.Deliver(ctx)
Expect(err).NotTo(HaveOccurred())
ordererBlock, err := channel.GetNewstBlock(DeliverClient, channelName, tlsClientCert, org1MSP, true)
Expect(err).NotTo(HaveOccurred())
Expect(ordererBlock).NotTo(BeNil())

//join peer1
err = channel.JoinChannel(
block, org1MSP, peer1Endorser,
Expand Down