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

implement peer snapshot client and associated methods #212

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package snapshot

import (
"context"
"fmt"

"github.com/hyperledger/fabric-admin-sdk/internal/protoutil"
"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
"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"
)

// Client is a wrapper around snapshot client
type Client struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the chaincode and discovery packages we use Peer to make it clear to the user which component of the Fabric network the are interacting with, and to which they need to supply a client connection. I think this might be better named as Peer for consistency.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I even started writing with it, but only from reading the codebase I wasn't able to understand the naming, but it makes sense to me now. Renamed it to Peer.

snapshot peer.SnapshotClient
id identity.SigningIdentity
}

func New(conn grpc.ClientConnInterface, id identity.SigningIdentity) *Client {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be perfectly named if it was creating a Snapshot, since the package is called snapshot. It might be clearer (and more consistent with other packages in this module) if it names what it is creating. For example, NewPeer.

return &Client{
snapshot: peer.NewSnapshotClient(conn),
id: id,
}
}

// SubmitSnapshotRequest from a specific peer
func (s *Client) SubmitSnapshotRequest(ctx context.Context, channelID string, blockNum uint64) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are in a nicely named snapshot package, perhaps this can be shortened to just SubmitRequest? Alternatively, we could reflect the gRPC service names and call it Generate, although that does not seem as clear to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's clearer if we remove the Snapshot portion from the client methods.
I also agree that naming it Generate is not really clear, specially since it not always reflects the behavior of what happens in the peer, given that the snapshot might be created in the future.

signedRequest, err := s.newSignedSnapshotRequest(channelID, blockNum)
if err != nil {
return fmt.Errorf("failed to create signed snapshot request: %w", err)
}

if _, err := s.snapshot.Generate(ctx, signedRequest); err != nil {
return fmt.Errorf("failed to submit generate snapshot request: %w", err)
}

return nil
}

// CancelSnapshotRequest from a specific peer
func (s *Client) CancelSnapshotRequest(ctx context.Context, channelID string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this can be called just CancelRequest or even Cancel? It should probably mirror the naming of the SubmitRequest method. If that is called SubmitRequest, this makes sense as CancelRequest. If the submit is called Generate (or Submit) then this makes more sense as the shorter Cancel.

signedRequest, err := s.newSignedSnapshotRequest(channelID, 0)
if err != nil {
return fmt.Errorf("failed to create signed snapshot request: %w", err)
}

if _, err := s.snapshot.Cancel(ctx, signedRequest); err != nil {
return fmt.Errorf("failed to cancel snapshot request: %w", err)
}

return nil
}

// ListPendingSnapshots from a specific peer
func (s *Client) ListPendingSnapshots(ctx context.Context, channelID string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can lose the Snapshots part. ListPending is fine as a name, and matches the CLI naming. I would be tempted to call it QueryPending to match the gRPC service name, and to be more consistent with the naming in the chaincode package, which uses Query... for several methods.

The big problem with this method, which needs to be fixed, is that it queries information but returns nothing. I would return a QueryPendingSnapshotsResponse object rather than the slice of block numbers that it contains, in case that protobuf message gets extended in the future.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. I was copying and pasting the outlying structure of the methods and missed it. My bad.

signedRequest, err := s.newSignedSnapshotRequest(channelID, 0)
if err != nil {
return fmt.Errorf("failed to create signed snapshot request: %w", err)
}

if _, err := s.snapshot.QueryPendings(ctx, signedRequest); err != nil {
return fmt.Errorf("failed to list pending snapshots: %w", err)
}

return nil
}

// newSignedSnapshotRequest returns a signed snapshot request for
// given channel
func (s *Client) newSignedSnapshotRequest(channelID string, blockNum uint64) (*peer.SignedSnapshotRequest, error) {
nonce, err := protoutil.CreateNonce()
if err != nil {
return nil, fmt.Errorf("failed to create nonce: %w", err)
}

request, err := proto.Marshal(&peer.SnapshotRequest{
SignatureHeader: &common.SignatureHeader{
Creator: s.id.Credentials(),
Nonce: nonce,
},
ChannelId: channelID,
BlockNumber: blockNum,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal snapshot request: %w", err)
}

signature, err := s.id.Sign(request)
if err != nil {
return nil, fmt.Errorf("failed to sign snapshot request: %w", err)
}

return &peer.SignedSnapshotRequest{
Request: request,
Signature: signature,
}, nil
}
Loading