Skip to content

Commit

Permalink
[FAB-8023] Channel Config from orderer
Browse files Browse the repository at this point in the history
Change-Id: I64c32e4921452a301749e1ae947e362c326238df
Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask committed Feb 1, 2018
1 parent e8a79bb commit cdb34d5
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 956 deletions.
3 changes: 0 additions & 3 deletions api/apifabclient/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ type Channel interface {
txn.ProposalSender

Name() string
Initialize(data []byte) error
IsInitialized() bool
LoadConfigUpdateEnvelope(data []byte) error
ChannelConfig() (*common.ConfigEnvelope, error)
SendInstantiateProposal(chaincodeName string, args [][]byte, chaincodePath string, chaincodeVersion string, chaincodePolicy *common.SignaturePolicyEnvelope,
collConfig []*common.CollectionConfig, targets []txn.ProposalProcessor) ([]*txn.TransactionProposalResponse, txn.TransactionID, error)
Expand Down
13 changes: 7 additions & 6 deletions api/apifabclient/chconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
type ChannelConfig interface {

// Query channel configuration
Query() (*ChannelCfg, error)
Query() (ChannelCfg, error)
}

// ChannelCfg contains channel configuration
type ChannelCfg struct {
Msps []*msp.MSPConfig
AnchorPeers []*OrgAnchorPeer
Orderers []string
Versions *Versions
type ChannelCfg interface {
Name() string
Msps() []*msp.MSPConfig
AnchorPeers() []*OrgAnchorPeer
Orderers() []string
Versions() *Versions
}

// Versions ...
Expand Down
44 changes: 40 additions & 4 deletions pkg/fabric-client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/errors"
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/orderer"
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
)

Expand All @@ -33,22 +34,57 @@ type Channel struct {
// name: used to identify different channel instances. The naming of channel instances
// is enforced by the ordering service and must be unique within the blockchain network.
// client: Provides operational context such as submitting User etc.
func New(ctx fab.Context, name string) (*Channel, error) {
if name == "" {
func New(ctx fab.Context, cfg fab.ChannelCfg) (*Channel, error) {
if cfg.Name() == "" {
return nil, errors.Errorf("name is required")
}
if ctx == nil {
return nil, errors.Errorf("client is required")
}
p := make(map[string]fab.Peer)
o := make(map[string]fab.Orderer)

c := Channel{
name: name,
name: cfg.Name(),
peers: p,
orderers: o,
clientContext: ctx,
mspManager: msp.NewMSPManager(),
}

mspManager := msp.NewMSPManager()
if len(cfg.Msps()) > 0 {
msps, err := loadMSPs(cfg.Msps(), ctx.CryptoSuite())
if err != nil {
return nil, errors.WithMessage(err, "load MSPs from config failed")
}

if err := mspManager.Setup(msps); err != nil {
return nil, errors.WithMessage(err, "MSPManager Setup failed")
}
}

c.mspManager = mspManager
c.anchorPeers = cfg.AnchorPeers()

// Add orderer if specified in config
for _, name := range cfg.Orderers() {

// Figure out orderer configuration
oCfg, err := ctx.Config().OrdererConfig(name)

// Check if retrieving orderer configuration went ok
if err != nil || oCfg == nil {
return nil, errors.Errorf("failed to retrieve orderer config: %s", err)
}

o, err := orderer.New(ctx.Config(), orderer.FromOrdererConfig(oCfg))
if err != nil {
return nil, errors.WithMessage(err, "failed to create new orderer from config")
}

c.orderers[o.URL()] = o
}

logger.Debugf("Constructed channel instance for channel %s: %v", c.name, c)

return &c, nil
Expand Down
134 changes: 8 additions & 126 deletions pkg/fabric-client/channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ oG5kQQIgQAe4OOKYhJdh3f7URaKfGTf492/nmRmtK+ySKjpHSrU=
func TestChannelMethods(t *testing.T) {
user := mocks.NewMockUser("test")
ctx := mocks.NewMockContext(user)
channel, err := New(ctx, "testChannel")
channel, err := New(ctx, mocks.NewMockChannelCfg("testChannel"))
if err != nil {
t.Fatalf("New return error[%s]", err)
}
if channel.Name() != "testChannel" {
t.Fatalf("New create wrong channel")
}

_, err = New(ctx, "")
_, err = New(ctx, mocks.NewMockChannelCfg(""))
if err == nil {
t.Fatalf("New didn't return error")
}
if err.Error() != "name is required" {
t.Fatalf("New didn't return right error")
}

_, err = New(nil, "testChannel")
_, err = New(nil, mocks.NewMockChannelCfg("testChannel"))
if err == nil {
t.Fatalf("NewChannel didn't return error")
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestAddRemoveOrderer(t *testing.T) {
}
}

func TestAnchorAndRemovePeers(t *testing.T) {
func TestAddAndRemovePeers(t *testing.T) {
//Setup channel
channel, _ := setupTestChannel()

Expand All @@ -113,12 +113,10 @@ func TestAnchorAndRemovePeers(t *testing.T) {

//Add the Peer again
channel.AddPeer(&peer)

channel.Initialize(nil)
if len(channel.AnchorPeers()) != 0 {
//Currently testing only for empty anchor list
t.Fatal("Anchor peer list is incorrect")
if len(channel.Peers()) != 1 {
t.Fatal("Add Peer failed")
}

}

func TestPrimaryPeer(t *testing.T) {
Expand Down Expand Up @@ -170,122 +168,6 @@ func TestPrimaryPeer(t *testing.T) {

}

func TestChannelInitializeFromOrderer(t *testing.T) {
org1MSPID := "ORG1MSP"
org2MSPID := "ORG2MSP"

channel, _ := setupTestChannel()
builder := &mocks.MockConfigBlockBuilder{
MockConfigGroupBuilder: mocks.MockConfigGroupBuilder{
ModPolicy: "Admins",
MSPNames: []string{
org1MSPID,
org2MSPID,
},
OrdererAddress: "localhost:7054",
RootCA: validRootCA,
},
Index: 0,
LastConfigIndex: 0,
}
orderer := mocks.NewMockOrderer("", nil)
orderer.(mocks.MockOrderer).EnqueueForSendDeliver(builder.Build())
orderer.(mocks.MockOrderer).EnqueueForSendDeliver(builder.Build())
err := channel.AddOrderer(orderer)
if err != nil {
t.Fatalf("Error adding orderer: %v", err)
}

err = channel.Initialize(nil)
if err != nil {
t.Fatalf("channel Initialize failed : %v", err)
}
if !channel.IsInitialized() {
t.Fatalf("channel Initialize failed : channel initialized flag not set")
}

mspManager := channel.MSPManager()
if mspManager == nil {
t.Fatalf("nil MSPManager on new channel")
}
msps, err := mspManager.GetMSPs()
if err != nil || len(msps) == 0 {
t.Fatalf("At least one MSP expected in MSPManager")
}
msp, ok := msps[org1MSPID]
if !ok {
t.Fatalf("Could not find %s", org1MSPID)
}
if identifier, _ := msp.GetIdentifier(); identifier != org1MSPID {
t.Fatalf("Expecting MSP identifier to be %s but got %s", org1MSPID, identifier)
}
msp, ok = msps[org2MSPID]
if !ok {
t.Fatalf("Could not find %s", org2MSPID)
}
if identifier, _ := msp.GetIdentifier(); identifier != org2MSPID {
t.Fatalf("Expecting MSP identifier to be %s but got %s", org2MSPID, identifier)
}

channel.SetMSPManager(nil)
if channel.MSPManager() != nil {
t.Fatal("Set MSPManager is not working as expected")
}

}

func TestOrganizationUnits(t *testing.T) {
org1MSPID := "ORG1MSP"
org2MSPID := "ORG2MSP"

channel, _ := setupTestChannel()
orgUnits, err := channel.OrganizationUnits()

if len(orgUnits) > 0 {
t.Fatalf("Returned non configured organizational unit : %v", err)
}
builder := &mocks.MockConfigBlockBuilder{
MockConfigGroupBuilder: mocks.MockConfigGroupBuilder{
ModPolicy: "Admins",
MSPNames: []string{
channel.Name(),
org1MSPID,
org2MSPID,
},
OrdererAddress: "localhost:7054",
RootCA: validRootCA,
},
Index: 0,
LastConfigIndex: 0,
}
orderer := mocks.NewMockOrderer("", nil)
orderer.(mocks.MockOrderer).EnqueueForSendDeliver(builder.Build())
orderer.(mocks.MockOrderer).EnqueueForSendDeliver(builder.Build())
err = channel.AddOrderer(orderer)
if err != nil {
t.Fatalf("Error adding orderer: %v", err)
}

err = channel.Initialize(nil)
if err != nil {
t.Fatalf("channel Initialize failed : %v", err)
}
orgUnits, err = channel.OrganizationUnits()
if err != nil {
t.Fatalf("CANNOT retrieve organizational units : %v", err)
}
if !isValueInList(channel.Name(), orgUnits) {
t.Fatalf("Could not find %s in the list of organizations", channel.Name())
}
if !isValueInList(org1MSPID, orgUnits) {
t.Fatalf("Could not find %s in the list of organizations", org1MSPID)
}
if !isValueInList(org2MSPID, orgUnits) {
t.Fatalf("Could not find %s in the list of organizations", org2MSPID)
}

}

func isValueInList(value string, list []string) bool {
for _, v := range list {
if v == value {
Expand All @@ -298,7 +180,7 @@ func isValueInList(value string, list []string) bool {
func setupTestChannel() (*Channel, error) {
user := mocks.NewMockUser("test")
ctx := mocks.NewMockContext(user)
return New(ctx, "testChannel")
return New(ctx, mocks.NewMockChannelCfg("testChannel"))
}

func setupMassiveTestChannel(numberOfPeers int, numberOfOrderers int) (*Channel, error) {
Expand Down
Loading

0 comments on commit cdb34d5

Please sign in to comment.