Skip to content

Commit 718924c

Browse files
author
Srinivasan Muralidharan
committed
FAB-1256 remove anchor of DefaultChain from peer
https://jira.hyperledger.org/browse/FAB-1256 DefaultChain was the chain that was holding together the flows through the fabric and was a place holder for full fledged chains implementation. We now remove that . force chainID to be passed through the system, top down . use the chainID as the ledgername (note the panic in GetLedger) Pending "join" support, the system still require a single ledger to be created but that is now created by specifying the chain at the top level (unit tests, CLI in "peer node start", etc). Once "join" is added, this scaffolding can be removed from CLI (the chaincode unit tests will likely continue to use it) when the complete chain initialization will be "peer join" configuration driven Change-Id: Ie3b64db4b9030a4cb695fb3e1075822b55a129d1 Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent 6cd7be2 commit 718924c

19 files changed

+353
-317
lines changed

core/chaincode/chaincode_support.go

+37-46
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ import (
3838
pb "github.com/hyperledger/fabric/protos/peer"
3939
)
4040

41-
// ChainName is the name of the chain to which this chaincode support belongs to.
42-
type ChainName string
43-
4441
const (
45-
// DefaultChain is the name of the default chain.
46-
DefaultChain ChainName = "default"
4742
// DevModeUserRunsChaincode property allows user to run chaincode in development environment
4843
DevModeUserRunsChaincode string = "dev"
4944
chaincodeStartupTimeoutDefault int = 5000
@@ -54,13 +49,12 @@ const (
5449
TXSimulatorKey string = "txsimulatorkey"
5550
)
5651

57-
// chains is a map between different blockchains and their ChaincodeSupport.
58-
//this needs to be a first class, top-level object... for now, lets just have a placeholder
59-
var chains map[ChainName]*ChaincodeSupport
60-
61-
func init() {
62-
chains = make(map[ChainName]*ChaincodeSupport)
63-
}
52+
//this is basically the singleton that supports the
53+
//entire chaincode framework. It does NOT know about
54+
//chains. Chains are per-proposal entities that are
55+
//setup as part of "join" and go through this object
56+
//via calls to Execute and Deploy chaincodes.
57+
var theChaincodeSupport *ChaincodeSupport
6458

6559
//use this for ledger access and make sure TXSimulator is being used
6660
func getTxSimulator(context context.Context) ledger.TxSimulator {
@@ -84,9 +78,8 @@ type runningChaincodes struct {
8478
chaincodeMap map[string]*chaincodeRTEnv
8579
}
8680

87-
// GetChain returns the chaincode support for a given chain
88-
func GetChain(name ChainName) *ChaincodeSupport {
89-
return chains[name]
81+
func GetChain() *ChaincodeSupport {
82+
return theChaincodeSupport
9083
}
9184

9285
//call this under lock
@@ -106,48 +99,47 @@ func (chaincodeSupport *ChaincodeSupport) chaincodeHasBeenLaunched(chaincode str
10699
}
107100

108101
// NewChaincodeSupport creates a new ChaincodeSupport instance
109-
func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration) *ChaincodeSupport {
102+
func NewChaincodeSupport(getPeerEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration) *ChaincodeSupport {
110103
pnid := viper.GetString("peer.networkId")
111104
pid := viper.GetString("peer.id")
112105

113-
s := &ChaincodeSupport{name: chainname, runningChaincodes: &runningChaincodes{chaincodeMap: make(map[string]*chaincodeRTEnv)}, peerNetworkID: pnid, peerID: pid}
106+
theChaincodeSupport = &ChaincodeSupport{runningChaincodes: &runningChaincodes{chaincodeMap: make(map[string]*chaincodeRTEnv)}, peerNetworkID: pnid, peerID: pid}
114107

115108
//initialize global chain
116-
chains[chainname] = s
117109

118110
peerEndpoint, err := getPeerEndpoint()
119111
if err != nil {
120112
chaincodeLogger.Errorf("Error getting PeerEndpoint, using peer.address: %s", err)
121-
s.peerAddress = viper.GetString("peer.address")
113+
theChaincodeSupport.peerAddress = viper.GetString("peer.address")
122114
} else {
123-
s.peerAddress = peerEndpoint.Address
115+
theChaincodeSupport.peerAddress = peerEndpoint.Address
124116
}
125-
chaincodeLogger.Infof("Chaincode support using peerAddress: %s\n", s.peerAddress)
117+
chaincodeLogger.Infof("Chaincode support using peerAddress: %s\n", theChaincodeSupport.peerAddress)
126118
//peerAddress = viper.GetString("peer.address")
127-
if s.peerAddress == "" {
128-
s.peerAddress = peerAddressDefault
119+
if theChaincodeSupport.peerAddress == "" {
120+
theChaincodeSupport.peerAddress = peerAddressDefault
129121
}
130122

131-
s.userRunsCC = userrunsCC
123+
theChaincodeSupport.userRunsCC = userrunsCC
132124

133-
s.ccStartupTimeout = ccstartuptimeout
125+
theChaincodeSupport.ccStartupTimeout = ccstartuptimeout
134126

135127
//TODO I'm not sure if this needs to be on a per chain basis... too lowel and just needs to be a global default ?
136-
s.chaincodeInstallPath = viper.GetString("chaincode.installpath")
137-
if s.chaincodeInstallPath == "" {
138-
s.chaincodeInstallPath = chaincodeInstallPathDefault
128+
theChaincodeSupport.chaincodeInstallPath = viper.GetString("chaincode.installpath")
129+
if theChaincodeSupport.chaincodeInstallPath == "" {
130+
theChaincodeSupport.chaincodeInstallPath = chaincodeInstallPathDefault
139131
}
140132

141-
s.peerTLS = viper.GetBool("peer.tls.enabled")
142-
if s.peerTLS {
143-
s.peerTLSCertFile = viper.GetString("peer.tls.cert.file")
144-
s.peerTLSKeyFile = viper.GetString("peer.tls.key.file")
145-
s.peerTLSSvrHostOrd = viper.GetString("peer.tls.serverhostoverride")
133+
theChaincodeSupport.peerTLS = viper.GetBool("peer.tls.enabled")
134+
if theChaincodeSupport.peerTLS {
135+
theChaincodeSupport.peerTLSCertFile = viper.GetString("peer.tls.cert.file")
136+
theChaincodeSupport.peerTLSKeyFile = viper.GetString("peer.tls.key.file")
137+
theChaincodeSupport.peerTLSSvrHostOrd = viper.GetString("peer.tls.serverhostoverride")
146138
}
147139

148140
kadef := 0
149141
if ka := viper.GetString("chaincode.keepalive"); ka == "" {
150-
s.keepalive = time.Duration(kadef) * time.Second
142+
theChaincodeSupport.keepalive = time.Duration(kadef) * time.Second
151143
} else {
152144
t, terr := strconv.Atoi(ka)
153145
if terr != nil {
@@ -157,7 +149,7 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
157149
chaincodeLogger.Debugf("Turn off keepalive(value %s)", ka)
158150
t = kadef
159151
}
160-
s.keepalive = time.Duration(t) * time.Second
152+
theChaincodeSupport.keepalive = time.Duration(t) * time.Second
161153
}
162154

163155
viper.SetEnvPrefix("CORE")
@@ -169,13 +161,13 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
169161
chaincodeLogLevel, err := logging.LogLevel(chaincodeLogLevelString)
170162

171163
if err == nil {
172-
s.chaincodeLogLevel = chaincodeLogLevel.String()
164+
theChaincodeSupport.chaincodeLogLevel = chaincodeLogLevel.String()
173165
} else {
174166
chaincodeLogger.Warningf("Chaincode logging level %s is invalid; defaulting to %s", chaincodeLogLevelString, flogging.DefaultLoggingLevel().String())
175-
s.chaincodeLogLevel = flogging.DefaultLoggingLevel().String()
167+
theChaincodeSupport.chaincodeLogLevel = flogging.DefaultLoggingLevel().String()
176168
}
177169

178-
return s
170+
return theChaincodeSupport
179171
}
180172

181173
// // ChaincodeStream standard stream for ChaincodeMessage type.
@@ -186,7 +178,6 @@ func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEn
186178

187179
// ChaincodeSupport responsible for providing interfacing with chaincodes from the Peer.
188180
type ChaincodeSupport struct {
189-
name ChainName
190181
runningChaincodes *runningChaincodes
191182
peerAddress string
192183
ccStartupTimeout time.Duration
@@ -270,7 +261,7 @@ func (chaincodeSupport *ChaincodeSupport) deregisterHandler(chaincodehandler *Ha
270261
}
271262

272263
// Based on state of chaincode send either init or ready to move to ready state
273-
func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Context, txid string, prop *pb.Proposal, chaincode string, initArgs [][]byte, timeout time.Duration) error {
264+
func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Context, chainID string, txid string, prop *pb.Proposal, chaincode string, initArgs [][]byte, timeout time.Duration) error {
274265
chaincodeSupport.runningChaincodes.Lock()
275266
//if its in the map, there must be a connected stream...nothing to do
276267
var chrte *chaincodeRTEnv
@@ -284,7 +275,7 @@ func (chaincodeSupport *ChaincodeSupport) sendInitOrReady(context context.Contex
284275

285276
var notfy chan *pb.ChaincodeMessage
286277
var err error
287-
if notfy, err = chrte.handler.initOrReady(context, txid, prop, initArgs); err != nil {
278+
if notfy, err = chrte.handler.initOrReady(context, chainID, txid, prop, initArgs); err != nil {
288279
return fmt.Errorf("Error sending %s: %s", pb.ChaincodeMessage_INIT, err)
289280
}
290281
if notfy != nil {
@@ -442,7 +433,7 @@ func (chaincodeSupport *ChaincodeSupport) Stop(context context.Context, cds *pb.
442433
}
443434

444435
// Launch will launch the chaincode if not running (if running return nil) and will wait for handler of the chaincode to get into FSM ready state.
445-
func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid string, prop *pb.Proposal, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error) {
436+
func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, chainID string, txid string, prop *pb.Proposal, spec interface{}) (*pb.ChaincodeID, *pb.ChaincodeInput, error) {
446437
//build the chaincode
447438
var cID *pb.ChaincodeID
448439
var cMsg *pb.ChaincodeInput
@@ -499,7 +490,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid s
499490
var depPayload []byte
500491

501492
//hopefully we are restarting from existing image and the deployed transaction exists
502-
depPayload, err = GetCDSFromLCCC(context, txid, prop, string(DefaultChain), chaincode)
493+
depPayload, err = GetCDSFromLCCC(context, txid, prop, chainID, chaincode)
503494
if err != nil {
504495
return cID, cMsg, fmt.Errorf("Could not get deployment transaction from LCCC for %s - %s", chaincode, err)
505496
}
@@ -530,7 +521,7 @@ func (chaincodeSupport *ChaincodeSupport) Launch(context context.Context, txid s
530521

531522
if err == nil {
532523
//send init (if (args)) and wait for ready state
533-
err = chaincodeSupport.sendInitOrReady(context, txid, prop, chaincode, initargs, chaincodeSupport.ccStartupTimeout)
524+
err = chaincodeSupport.sendInitOrReady(context, chainID, txid, prop, chaincode, initargs, chaincodeSupport.ccStartupTimeout)
534525
if err != nil {
535526
chaincodeLogger.Errorf("sending init failed(%s)", err)
536527
err = fmt.Errorf("Failed to init chaincode(%s)", err)
@@ -618,7 +609,7 @@ func createTransactionMessage(txid string, cMsg *pb.ChaincodeInput) (*pb.Chainco
618609
}
619610

620611
// Execute executes a transaction and waits for it to complete until a timeout value.
621-
func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, chaincode string, msg *pb.ChaincodeMessage, timeout time.Duration, prop *pb.Proposal) (*pb.ChaincodeMessage, error) {
612+
func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, chainID string, chaincode string, msg *pb.ChaincodeMessage, timeout time.Duration, prop *pb.Proposal) (*pb.ChaincodeMessage, error) {
622613
chaincodeSupport.runningChaincodes.Lock()
623614
//we expect the chaincode to be running... sanity check
624615
chrte, ok := chaincodeSupport.chaincodeHasBeenLaunched(chaincode)
@@ -631,7 +622,7 @@ func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, chaincod
631622

632623
var notfy chan *pb.ChaincodeMessage
633624
var err error
634-
if notfy, err = chrte.handler.sendExecuteMessage(ctxt, msg, prop); err != nil {
625+
if notfy, err = chrte.handler.sendExecuteMessage(ctxt, chainID, msg, prop); err != nil {
635626
return nil, fmt.Errorf("Error sending %s: %s", msg.Type.String(), err)
636627
}
637628
var ccresp *pb.ChaincodeMessage

core/chaincode/chaincodeexec.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ func createCIS(ccname string, args [][]byte) (*pb.ChaincodeInvocationSpec, error
3636

3737
// GetCDSFromLCCC gets chaincode deployment spec from LCCC
3838
func GetCDSFromLCCC(ctxt context.Context, txid string, prop *pb.Proposal, chainID string, chaincodeID string) ([]byte, error) {
39-
payload, _, err := ExecuteChaincode(ctxt, txid, prop, string(DefaultChain), "lccc", [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
39+
payload, _, err := ExecuteChaincode(ctxt, chainID, txid, prop, "lccc", [][]byte{[]byte("getdepspec"), []byte(chainID), []byte(chaincodeID)})
4040
return payload, err
4141
}
4242

4343
// ExecuteChaincode executes a given chaincode given chaincode name and arguments
44-
func ExecuteChaincode(ctxt context.Context, txid string, prop *pb.Proposal, chainname string, ccname string, args [][]byte) ([]byte, *pb.ChaincodeEvent, error) {
44+
func ExecuteChaincode(ctxt context.Context, chainID string, txid string, prop *pb.Proposal, ccname string, args [][]byte) ([]byte, *pb.ChaincodeEvent, error) {
4545
var spec *pb.ChaincodeInvocationSpec
4646
var err error
4747
var b []byte
4848
var ccevent *pb.ChaincodeEvent
4949

5050
spec, err = createCIS(ccname, args)
51-
b, ccevent, err = Execute(ctxt, GetChain(ChainName(chainname)), txid, prop, spec)
51+
b, ccevent, err = Execute(ctxt, chainID, txid, prop, spec)
5252
if err != nil {
5353
return nil, nil, fmt.Errorf("Error deploying chaincode: %s", err)
5454
}

core/chaincode/exectransaction.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
//Execute - execute proposal
31-
func Execute(ctxt context.Context, chain *ChaincodeSupport, txid string, prop *pb.Proposal, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
31+
func Execute(ctxt context.Context, chainID string, txid string, prop *pb.Proposal, spec interface{}) ([]byte, *pb.ChaincodeEvent, error) {
3232
var err error
3333
var cds *pb.ChaincodeDeploymentSpec
3434
var ci *pb.ChaincodeInvocationSpec
@@ -39,18 +39,18 @@ func Execute(ctxt context.Context, chain *ChaincodeSupport, txid string, prop *p
3939
}
4040

4141
if cds != nil {
42-
_, err := chain.Deploy(ctxt, cds)
42+
_, err := theChaincodeSupport.Deploy(ctxt, cds)
4343
if err != nil {
4444
return nil, nil, fmt.Errorf("Failed to deploy chaincode spec(%s)", err)
4545
}
4646

47-
_, _, err = chain.Launch(ctxt, txid, prop, cds)
47+
_, _, err = theChaincodeSupport.Launch(ctxt, chainID, txid, prop, cds)
4848
if err != nil {
4949
return nil, nil, fmt.Errorf("%s", err)
5050
}
5151
} else {
5252
//will launch if necessary (and wait for ready)
53-
cID, cMsg, err := chain.Launch(ctxt, txid, prop, ci)
53+
cID, cMsg, err := theChaincodeSupport.Launch(ctxt, chainID, txid, prop, ci)
5454
if err != nil {
5555
return nil, nil, fmt.Errorf("Failed to launch chaincode spec(%s)", err)
5656
}
@@ -75,7 +75,7 @@ func Execute(ctxt context.Context, chain *ChaincodeSupport, txid string, prop *p
7575
return nil, nil, fmt.Errorf("Failed to transaction message(%s)", err)
7676
}
7777

78-
resp, err := chain.Execute(ctxt, chaincode, ccMsg, timeout, prop)
78+
resp, err := theChaincodeSupport.Execute(ctxt, chainID, chaincode, ccMsg, timeout, prop)
7979
if err != nil {
8080
// Rollback transaction
8181
return nil, nil, fmt.Errorf("Failed to execute transaction (%s)", err)

0 commit comments

Comments
 (0)