Skip to content

Commit

Permalink
try to fix #106 (#108)
Browse files Browse the repository at this point in the history
I suppose we need to provide function to support approve sign for
chaincode level and channel level.
this PR is just a draft as POC.

---------

Signed-off-by: Sam Yuan <[email protected]>
  • Loading branch information
SamYuan1990 authored Apr 4, 2023
1 parent f193c96 commit ae751cf
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 18 deletions.
6 changes: 5 additions & 1 deletion pkg/chaincode/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ func Approve(ctx context.Context, connection grpc.ClientConnInterface, id identi
if err != nil {
return err
}
validationParameter, err := chaincodeDef.getApplicationPolicyBytes()
if err != nil {
return err
}
approveArgs := &lifecycle.ApproveChaincodeDefinitionForMyOrgArgs{
Name: chaincodeDef.Name,
Version: chaincodeDef.Version,
Sequence: chaincodeDef.Sequence,
EndorsementPlugin: chaincodeDef.EndorsementPlugin,
ValidationPlugin: chaincodeDef.ValidationPlugin,
ValidationParameter: chaincodeDef.ValidationParameter,
ValidationParameter: validationParameter,
Collections: chaincodeDef.Collections,
InitRequired: chaincodeDef.InitRequired,
Source: newChaincodeSource(chaincodeDef.PackageID),
Expand Down
6 changes: 5 additions & 1 deletion pkg/chaincode/checkcommitreadiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import (
// CheckCommitReadiness for a chaincode and return all approval records. The connection may be to any Gateway peer that
// is a member of the channel.
func CheckCommitReadiness(ctx context.Context, connection grpc.ClientConnInterface, id identity.SigningIdentity, chaincodeDef *Definition) (*lifecycle.CheckCommitReadinessResult, error) {
validationParameter, err := chaincodeDef.getApplicationPolicyBytes()
if err != nil {
return nil, err
}
args := &lifecycle.CheckCommitReadinessArgs{
Name: chaincodeDef.Name,
Version: chaincodeDef.Version,
Sequence: chaincodeDef.Sequence,
EndorsementPlugin: chaincodeDef.EndorsementPlugin,
ValidationPlugin: chaincodeDef.ValidationPlugin,
ValidationParameter: chaincodeDef.ValidationParameter,
ValidationParameter: validationParameter,
Collections: chaincodeDef.Collections,
InitRequired: chaincodeDef.InitRequired,
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/chaincode/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ func Commit(ctx context.Context, connection grpc.ClientConnInterface, id identit
if err != nil {
return err
}
validationParameter, err := chaincodeDef.getApplicationPolicyBytes()
if err != nil {
return err
}
commitArgs := &lifecycle.CommitChaincodeDefinitionArgs{
Name: chaincodeDef.Name,
Version: chaincodeDef.Version,
Sequence: chaincodeDef.Sequence,
EndorsementPlugin: chaincodeDef.EndorsementPlugin,
ValidationPlugin: chaincodeDef.ValidationPlugin,
ValidationParameter: chaincodeDef.ValidationParameter,
ValidationParameter: validationParameter,
Collections: chaincodeDef.Collections,
InitRequired: chaincodeDef.InitRequired,
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/chaincode/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"

"github.com/hyperledger/fabric-protos-go-apiv2/peer"
"google.golang.org/protobuf/proto"
)

const (
Expand Down Expand Up @@ -57,9 +58,9 @@ type Definition struct {
// track of chaincode upgrades.
Sequence int64

// ValidationParameter defines the endorsement policy for the chaincode. This can be an explicit endorsement policy
// string or reference a policy in the channel configuration.
ValidationParameter []byte
// ApplicationPolicy defines the endorsement policy for the chaincode. This can be an explicit endorsement policy
// it follows fabric-protos-go-apiv2/peer format and it will convert to validationParameter during validation phase.
ApplicationPolicy *peer.ApplicationPolicy

// InitRequired is true only if the chaincode defines an Init function using the low-level shim API, which must be
// invoked before other transaction functions may be invoked; otherwise false. It is not recommended to rely on
Expand All @@ -85,3 +86,15 @@ func (d *Definition) validate() error {
}
return nil
}

// getApplicationPolicyBytes is to convert ApplicationPolicy to Bytes for proto usage
func (d *Definition) getApplicationPolicyBytes() ([]byte, error) {
if d.ApplicationPolicy != nil {
AppPolicydata, err := proto.Marshal(d.ApplicationPolicy)
if err != nil {
return nil, err
}
return AppPolicydata, nil
}
return nil, nil
}
Loading

0 comments on commit ae751cf

Please sign in to comment.