Skip to content

Commit

Permalink
Merge "[FAB-6229] VSCC support for cfg tree cc lifecycle"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Yellick authored and Gerrit Code Review committed Dec 8, 2017
2 parents eee12ef + 8f5c24c commit c44491a
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 39 deletions.
45 changes: 35 additions & 10 deletions common/mocks/scc/sccprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,62 @@ limitations under the License.
package scc

import (
"github.com/hyperledger/fabric/common/channelconfig"
lm "github.com/hyperledger/fabric/common/mocks/ledger"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/core/common/sysccprovider"
"github.com/hyperledger/fabric/core/ledger"
)

type MocksccProviderFactory struct {
Qe *lm.MockQueryExecutor
QErr error
Qe *lm.MockQueryExecutor
QErr error
ApplicationConfigRv channelconfig.Application
ApplicationConfigBool bool
PolicyManagerRv policies.Manager
PolicyManagerBool bool
}

func (c *MocksccProviderFactory) NewSystemChaincodeProvider() sysccprovider.SystemChaincodeProvider {
return &mocksccProviderImpl{Qe: c.Qe, QErr: c.QErr}
return &MocksccProviderImpl{
Qe: c.Qe,
QErr: c.QErr,
ApplicationConfigRv: c.ApplicationConfigRv,
ApplicationConfigBool: c.ApplicationConfigBool,
PolicyManagerBool: c.PolicyManagerBool,
PolicyManagerRv: c.PolicyManagerRv,
}
}

type mocksccProviderImpl struct {
Qe *lm.MockQueryExecutor
QErr error
type MocksccProviderImpl struct {
Qe *lm.MockQueryExecutor
QErr error
ApplicationConfigRv channelconfig.Application
ApplicationConfigBool bool
PolicyManagerRv policies.Manager
PolicyManagerBool bool
}

func (c *mocksccProviderImpl) IsSysCC(name string) bool {
func (c *MocksccProviderImpl) IsSysCC(name string) bool {
return (name == "lscc") || (name == "escc") || (name == "vscc") || (name == "notext")
}

func (c *mocksccProviderImpl) IsSysCCAndNotInvokableCC2CC(name string) bool {
func (c *MocksccProviderImpl) IsSysCCAndNotInvokableCC2CC(name string) bool {
return (name == "escc") || (name == "vscc")
}

func (c *mocksccProviderImpl) IsSysCCAndNotInvokableExternal(name string) bool {
func (c *MocksccProviderImpl) IsSysCCAndNotInvokableExternal(name string) bool {
return (name == "escc") || (name == "vscc") || (name == "notext")
}

func (c *mocksccProviderImpl) GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error) {
func (c *MocksccProviderImpl) GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error) {
return c.Qe, c.QErr
}

func (c *MocksccProviderImpl) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
return c.ApplicationConfigRv, c.ApplicationConfigBool
}

func (c *MocksccProviderImpl) PolicyManager(channelID string) (policies.Manager, bool) {
return c.PolicyManagerRv, c.PolicyManagerBool
}
10 changes: 10 additions & 0 deletions core/common/sysccprovider/sysccprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package sysccprovider

import (
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/core/ledger"
)

Expand All @@ -41,6 +43,14 @@ type SystemChaincodeProvider interface {
// That's useful for system chaincodes that require unfettered
// access to the ledger
GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error)

// GetApplicationConfig returns the configtxapplication.SharedConfig for the channel
// and whether the Application config exists
GetApplicationConfig(cid string) (channelconfig.Application, bool)

// Returns the policy manager associated to the passed channel
// and whether the policy manager exists
PolicyManager(channelID string) (policies.Manager, bool)
}

var sccFactory SystemChaincodeProviderFactory
Expand Down
10 changes: 10 additions & 0 deletions core/common/sysccprovider/sysccprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package sysccprovider
import (
"testing"

"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/core/ledger"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -47,6 +49,14 @@ func (p MockChaincodeProvider) IsSysCCAndNotInvokableExternal(name string) bool
return true
}

func (p MockChaincodeProvider) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
return nil, false
}

func (p MockChaincodeProvider) PolicyManager(channelID string) (policies.Manager, bool) {
return nil, false
}

func (f MockFactory) NewSystemChaincodeProvider() SystemChaincodeProvider {
return mockChaincodeProvider
}
Expand Down
15 changes: 15 additions & 0 deletions core/scc/sccproviderimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package scc
import (
"fmt"

"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/core/common/sysccprovider"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/peer"
Expand Down Expand Up @@ -71,3 +73,16 @@ func (c *sccProviderImpl) IsSysCCAndNotInvokableExternal(name string) bool {
// call the static method of the same name
return IsSysCCAndNotInvokableExternal(name)
}

// GetApplicationConfig returns the configtxapplication.SharedConfig for the channel
// and whether the Application config exists
func (c *sccProviderImpl) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
return peer.GetSupport().GetApplicationConfig(cid)
}

// Returns the policy manager associated to the passed channel
// and whether the policy manager exists
func (c *sccProviderImpl) PolicyManager(channelID string) (policies.Manager, bool) {
m := peer.GetPolicyManager(channelID)
return m, (m != nil)
}
47 changes: 38 additions & 9 deletions core/scc/vscc/validator_onevalidsignature.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ package vscc
import (
"fmt"

"errors"

"github.com/golang/protobuf/proto"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/common/ccprovider"
"github.com/hyperledger/fabric/core/common/sysccprovider"
Expand All @@ -36,6 +34,7 @@ import (
"github.com/hyperledger/fabric/protos/msp"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/pkg/errors"
)

var logger = flogging.MustGetLogger("vscc")
Expand Down Expand Up @@ -109,15 +108,45 @@ func (vscc *ValidatorOneValidSignature) Invoke(stub shim.ChaincodeStubInterface)
return shim.Error(err.Error())
}

// get the policy
mgr := mspmgmt.GetManagerForChain(chdr.ChannelId)
pProvider := cauthdsl.NewPolicyProvider(mgr)
policy, _, err := pProvider.NewPolicy(args[2])
if err != nil {
logger.Errorf("VSCC error: pProvider.NewPolicy failed, err %s", err)
ac, exists := vscc.sccprovider.GetApplicationConfig(chdr.ChannelId)
if !exists {
err = errors.Wrap(err, "failure while unmarshalling VSCCArgs")
logger.Errorf(err.Error())
return shim.Error(err.Error())
}

// get the policy
var policy policies.Policy
if ac.Capabilities().LifecycleViaConfig() {
vsccArgs := &pb.VSCCArgs{}
err = proto.Unmarshal(args[2], vsccArgs)
if err != nil {
err = errors.Wrap(err, "failure while unmarshalling VSCCArgs")
logger.Errorf(err.Error())
return shim.Error(err.Error())
}

mgr, exists := vscc.sccprovider.PolicyManager(chdr.ChannelId)
if !exists {
logger.Errorf("Policy manager could not be found for channel %s", chdr.ChannelId)
return shim.Error(fmt.Sprintf("Policy manager could not be found for channel %s", chdr.ChannelId))
}

policy, exists = mgr.GetPolicy(vsccArgs.GetEndorsementPolicyRef())
if !exists {
logger.Errorf("Policy '%s' could not be found for channel %s", vsccArgs.GetEndorsementPolicyRef(), chdr.ChannelId)
return shim.Error(fmt.Sprintf("Policy '%s' could not be found for channel %s", vsccArgs.GetEndorsementPolicyRef(), chdr.ChannelId))
}
} else {
mgr := mspmgmt.GetManagerForChain(chdr.ChannelId)
pProvider := cauthdsl.NewPolicyProvider(mgr)
policy, _, err = pProvider.NewPolicy(args[2])
if err != nil {
logger.Errorf("VSCC error: pProvider.NewPolicy failed, err %s", err)
return shim.Error(err.Error())
}
}

// validate the payload type
if common.HeaderType(chdr.Type) != common.HeaderType_ENDORSER_TRANSACTION {
logger.Errorf("Only Endorser Transactions are supported, provided type %d", chdr.Type)
Expand Down
Loading

0 comments on commit c44491a

Please sign in to comment.