Skip to content

Commit 85b47e6

Browse files
author
Srinivasan Muralidharan
committed
FAB-1378 beginnings of a join command
https://jira.hyperledger.org/browse/FAB-1378 "peer node start" works as before and creates the default chain. "peer node start --peer-defaultchain=false" however starts without a chain. It just creates the gossip service and starts up the peer server in a chainless, ledgerless mode. The peer is read to accept join calls. "peer node join -b <path to genesis block file>" calls CSCC with the genesis block. kvledgers is removed and the peer package is used for ledger access. CSCC is under construction but this CR allows the work to be driven via CLI (and later from SDK). Change-Id: Id581c1f04b8788f54be467f593d74ea6b1efe713 Signed-off-by: Srinivasan Muralidharan <[email protected]>
1 parent e343403 commit 85b47e6

21 files changed

+475
-335
lines changed

core/chaincode/chaincode_support.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func getTxSimulator(context context.Context) ledger.TxSimulator {
6161
if txsim, ok := context.Value(TXSimulatorKey).(ledger.TxSimulator); ok {
6262
return txsim
6363
}
64-
panic("!!!---Not Using ledgernext---!!!")
64+
//chaincode will not allow state operations
65+
return nil
6566
}
6667

6768
//CCContext pass this around instead of string of args

core/chaincode/exectransaction_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/hyperledger/fabric/core/container"
3232
"github.com/hyperledger/fabric/core/container/ccintf"
3333
"github.com/hyperledger/fabric/core/ledger"
34-
"github.com/hyperledger/fabric/core/ledger/kvledger"
3534
"github.com/hyperledger/fabric/core/peer"
3635
"github.com/hyperledger/fabric/core/util"
3736
pb "github.com/hyperledger/fabric/protos/peer"
@@ -60,6 +59,9 @@ func getNowMillis() int64 {
6059
func initPeer(chainIDs ...string) (net.Listener, error) {
6160
//start clean
6261
finitPeer(nil, chainIDs...)
62+
63+
peer.MockInitialize()
64+
6365
var opts []grpc.ServerOption
6466
if viper.GetBool("peer.tls.enabled") {
6567
creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file"))
@@ -70,10 +72,6 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
7072
}
7173
grpcServer := grpc.NewServer(opts...)
7274

73-
ledgerPath := viper.GetString("peer.fileSystemPath")
74-
75-
kvledger.Initialize(ledgerPath)
76-
7775
peerAddress, err := peer.GetLocalAddress()
7876
if err != nil {
7977
return nil, fmt.Errorf("Error obtaining peer address: %s", err)
@@ -93,7 +91,10 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
9391
RegisterSysCCs()
9492

9593
for _, id := range chainIDs {
96-
kvledger.CreateLedger(id)
94+
if err = peer.MockCreateChain(id); err != nil {
95+
closeListenerAndSleep(lis)
96+
return nil, err
97+
}
9798
DeploySysCCs(id)
9899
}
99100

@@ -106,7 +107,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
106107
if lis != nil {
107108
for _, c := range chainIDs {
108109
deRegisterSysCCs(c)
109-
if lgr := kvledger.GetLedger(c); lgr != nil {
110+
if lgr := peer.GetLedger(c); lgr != nil {
110111
lgr.Close()
111112
}
112113
}
@@ -119,7 +120,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
119120
}
120121

121122
func startTxSimulation(ctxt context.Context, chainID string) (context.Context, ledger.TxSimulator, error) {
122-
lgr := kvledger.GetLedger(chainID)
123+
lgr := peer.GetLedger(chainID)
123124
txsim, err := lgr.NewTxSimulator()
124125
if err != nil {
125126
return nil, nil, err
@@ -161,7 +162,7 @@ func endTxSimulationCIS(chainID string, txid string, txsim ledger.TxSimulator, p
161162

162163
func endTxSimulation(chainID string, txsim ledger.TxSimulator, payload []byte, commit bool, prop *pb.Proposal) error {
163164
txsim.Done()
164-
if lgr := kvledger.GetLedger(chainID); lgr != nil {
165+
if lgr := peer.GetLedger(chainID); lgr != nil {
165166
if commit {
166167
var txSimulationResults []byte
167168
var err error

core/chaincode/handler.go

+31-6
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ func (handler *Handler) afterGetState(e *fsm.Event, state string) {
480480
handler.handleGetState(msg)
481481
}
482482

483+
// is this a txid for which there is a valid txsim
484+
func (handler *Handler) isValidTxSim(txid string, fmtStr string, args ...interface{}) (*transactionContext, *pb.ChaincodeMessage) {
485+
txContext := handler.getTxContext(txid)
486+
if txContext == nil || txContext.txsimulator == nil {
487+
// Send error msg back to chaincode. No ledger context
488+
errStr := fmt.Sprintf(fmtStr, args...)
489+
chaincodeLogger.Errorf(errStr)
490+
return nil, &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(errStr), Txid: txid}
491+
}
492+
return txContext, nil
493+
}
494+
483495
// Handles query to ledger to get state
484496
func (handler *Handler) handleGetState(msg *pb.ChaincodeMessage) {
485497
// The defer followed by triggering a go routine dance is needed to ensure that the previous state transition
@@ -509,8 +521,13 @@ func (handler *Handler) handleGetState(msg *pb.ChaincodeMessage) {
509521

510522
var res []byte
511523
var err error
524+
var txContext *transactionContext
525+
526+
txContext, serialSendMsg = handler.isValidTxSim(msg.Txid, "[%s]No ledger context for GetState. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR)
527+
if txContext == nil {
528+
return
529+
}
512530

513-
txContext := handler.getTxContext(msg.Txid)
514531
res, err = txContext.txsimulator.GetState(chaincodeID, key)
515532

516533
if err != nil {
@@ -579,8 +596,13 @@ func (handler *Handler) handleRangeQueryState(msg *pb.ChaincodeMessage) {
579596
}
580597

581598
iterID := util.GenerateUUID()
582-
txContext := handler.getTxContext(msg.Txid)
583599

600+
var txContext *transactionContext
601+
602+
txContext, serialSendMsg = handler.isValidTxSim(msg.Txid, "[%s]No ledger context for RangeQueryState. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR)
603+
if txContext == nil {
604+
return
605+
}
584606
chaincodeID := handler.ChaincodeID.Name
585607

586608
rangeIter, err := txContext.txsimulator.GetStateRangeScanIterator(chaincodeID, rangeQueryState.StartKey, rangeQueryState.EndKey)
@@ -864,6 +886,13 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
864886
var err error
865887
var res []byte
866888

889+
var txContext *transactionContext
890+
891+
txContext, triggerNextStateMsg = handler.isValidTxSim(msg.Txid, "[%s]No ledger context for %s. Sending %s", shorttxid(msg.Txid), msg.Type.String(), pb.ChaincodeMessage_ERROR)
892+
if txContext == nil {
893+
return
894+
}
895+
867896
if msg.Type.String() == pb.ChaincodeMessage_PUT_STATE.String() {
868897
putStateInfo := &pb.PutStateInfo{}
869898
unmarshalErr := proto.Unmarshal(msg.Payload, putStateInfo)
@@ -874,13 +903,10 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
874903
return
875904
}
876905

877-
// Invoke ledger to put state
878-
txContext := handler.getTxContext(msg.Txid)
879906
err = txContext.txsimulator.SetState(chaincodeID, putStateInfo.Key, putStateInfo.Value)
880907
} else if msg.Type.String() == pb.ChaincodeMessage_DEL_STATE.String() {
881908
// Invoke ledger to delete state
882909
key := string(msg.Payload)
883-
txContext := handler.getTxContext(msg.Txid)
884910
err = txContext.txsimulator.DeleteState(chaincodeID, key)
885911
} else if msg.Type.String() == pb.ChaincodeMessage_INVOKE_CHAINCODE.String() {
886912
//check and prohibit C-call-C for CONFIDENTIAL txs
@@ -902,7 +928,6 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
902928
newChaincodeID := chaincodeSpec.ChaincodeID.Name
903929
chaincodeLogger.Debugf("[%s] C-call-C %s", shorttxid(msg.Txid), newChaincodeID)
904930

905-
txContext := handler.getTxContext(msg.Txid)
906931
ctxt := context.Background()
907932
ctxt = context.WithValue(ctxt, TXSimulatorKey, txContext.txsimulator)
908933

core/chaincode/importsysccs.go

+51-22
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,36 @@ import (
2626
//see systemchaincode_test.go for an example using "sample_syscc"
2727
var systemChaincodes = []*SystemChaincode{
2828
{
29-
Enabled: true,
30-
Name: "lccc",
31-
Path: "github.com/hyperledger/fabric/core/chaincode",
32-
InitArgs: [][]byte{[]byte("")},
33-
Chaincode: &LifeCycleSysCC{},
29+
ChainlessCC: true,
30+
Enabled: true,
31+
Name: "cscc",
32+
Path: "github.com/hyperledger/fabric/core/system_chaincode/cscc",
33+
InitArgs: [][]byte{[]byte("")},
34+
Chaincode: &cscc.PeerConfiger{},
3435
},
3536
{
36-
Enabled: true,
37-
Name: "escc",
38-
Path: "github.com/hyperledger/fabric/core/system_chaincode/escc",
39-
InitArgs: [][]byte{[]byte("")},
40-
Chaincode: &escc.EndorserOneValidSignature{},
37+
ChainlessCC: false,
38+
Enabled: true,
39+
Name: "lccc",
40+
Path: "github.com/hyperledger/fabric/core/chaincode",
41+
InitArgs: [][]byte{[]byte("")},
42+
Chaincode: &LifeCycleSysCC{},
4143
},
4244
{
43-
Enabled: true,
44-
Name: "vscc",
45-
Path: "github.com/hyperledger/fabric/core/system_chaincode/vscc",
46-
InitArgs: [][]byte{[]byte("")},
47-
Chaincode: &vscc.ValidatorOneValidSignature{},
45+
ChainlessCC: false,
46+
Enabled: true,
47+
Name: "escc",
48+
Path: "github.com/hyperledger/fabric/core/system_chaincode/escc",
49+
InitArgs: [][]byte{[]byte("")},
50+
Chaincode: &escc.EndorserOneValidSignature{},
4851
},
4952
{
50-
Enabled: true,
51-
Name: "cscc",
52-
Path: "github.com/hyperledger/fabric/core/system_chaincode/cscc",
53-
InitArgs: [][]byte{[]byte("")},
54-
Chaincode: &cscc.PeerConfiger{},
53+
ChainlessCC: false,
54+
Enabled: true,
55+
Name: "vscc",
56+
Path: "github.com/hyperledger/fabric/core/system_chaincode/vscc",
57+
InitArgs: [][]byte{[]byte("")},
58+
Chaincode: &vscc.ValidatorOneValidSignature{},
5559
}}
5660

5761
//RegisterSysCCs is the hook for system chaincodes where system chaincodes are registered with the fabric
@@ -66,7 +70,19 @@ func RegisterSysCCs() {
6670
//note the chaincode must still be deployed and launched like a user chaincode will be
6771
func DeploySysCCs(chainID string) {
6872
for _, sysCC := range systemChaincodes {
69-
DeploySysCC(chainID, sysCC)
73+
if !sysCC.ChainlessCC {
74+
deploySysCC(chainID, sysCC)
75+
}
76+
}
77+
}
78+
79+
//DeployChainlessSysCCs is the hook for deploying chainless system chaincodes
80+
//these chaincodes cannot make any ledger calls
81+
func DeployChainlessSysCCs() {
82+
for _, sysCC := range systemChaincodes {
83+
if sysCC.ChainlessCC {
84+
deploySysCC("", sysCC)
85+
}
7086
}
7187
}
7288

@@ -75,7 +91,9 @@ func DeploySysCCs(chainID string) {
7591
//in the same process
7692
func deRegisterSysCCs(chainID string) {
7793
for _, sysCC := range systemChaincodes {
78-
deregisterSysCC(chainID, sysCC)
94+
if !sysCC.ChainlessCC {
95+
deregisterSysCC(chainID, sysCC)
96+
}
7997
}
8098
}
8199

@@ -89,3 +107,14 @@ func IsSysCC(name string) bool {
89107
}
90108
return false
91109
}
110+
111+
//IsChainlessSysCC returns true if the name matches a chainless system chaincode's
112+
//system chaincode names are system, chain wide
113+
func IsChainlessSysCC(name string) bool {
114+
for _, sysCC := range systemChaincodes {
115+
if sysCC.Name == name && sysCC.ChainlessCC {
116+
return true
117+
}
118+
}
119+
return false
120+
}

core/chaincode/lccc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/golang/protobuf/proto"
2424
"github.com/hyperledger/fabric/core/chaincode/shim"
2525
"github.com/hyperledger/fabric/core/ledger"
26-
"github.com/hyperledger/fabric/core/ledger/kvledger"
26+
"github.com/hyperledger/fabric/core/peer"
2727
"github.com/hyperledger/fabric/core/util"
2828
pb "github.com/hyperledger/fabric/protos/peer"
2929
"github.com/op/go-logging"
@@ -277,7 +277,7 @@ func (lccc *LifeCycleSysCC) deploy(stub shim.ChaincodeStubInterface, chainname s
277277
// 1) don't allow state initialization on deploy
278278
// 2) combine both LCCC and the called chaincodes into one RW set
279279
// 3) just drop the second
280-
lgr := kvledger.GetLedger(chainname)
280+
lgr := peer.GetLedger(chainname)
281281

282282
var dummytxsim ledger.TxSimulator
283283
var err error

core/chaincode/sysccapi.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/hyperledger/fabric/core/chaincode/shim"
2525
"github.com/hyperledger/fabric/core/container/inproccontroller"
2626
"github.com/hyperledger/fabric/core/ledger"
27-
"github.com/hyperledger/fabric/core/ledger/kvledger"
27+
"github.com/hyperledger/fabric/core/peer"
2828
"github.com/hyperledger/fabric/core/util"
2929
"github.com/op/go-logging"
3030
"github.com/spf13/viper"
@@ -38,6 +38,10 @@ var sysccLogger = logging.MustGetLogger("sysccapi")
3838
// when the fabric comes up. SystemChaincodes are installed by adding an
3939
// entry in importsysccs.go
4040
type SystemChaincode struct {
41+
//Global, once only not tied to chains. Such chaincodes cannot
42+
//save state in the ledger. CSCC is an example
43+
ChainlessCC bool
44+
4145
// Enabled a convenient switch to enable/disable system chaincode without
4246
// having to remove entry from importsysccs.go
4347
Enabled bool
@@ -76,28 +80,37 @@ func RegisterSysCC(syscc *SystemChaincode) error {
7680
return err
7781
}
7882

79-
// DeploySysCC deploys the given system chaincode on a chain
80-
func DeploySysCC(chainID string, syscc *SystemChaincode) error {
83+
// deploySysCC deploys the given system chaincode on a chain
84+
func deploySysCC(chainID string, syscc *SystemChaincode) error {
8185
if !syscc.Enabled || !isWhitelisted(syscc) {
8286
sysccLogger.Info(fmt.Sprintf("system chaincode (%s,%s) disabled", syscc.Name, syscc.Path))
8387
return nil
8488
}
8589

90+
if chainID == "" && !syscc.ChainlessCC {
91+
return fmt.Errorf("cannot deploy system chaincode %s without chain id", syscc.Name)
92+
} else if chainID != "" && syscc.ChainlessCC {
93+
return fmt.Errorf("cannot deploy chainless system chaincode %s with chain id %s", syscc.Name, chainID)
94+
}
95+
8696
var err error
8797

88-
lgr := kvledger.GetLedger(chainID)
89-
var txsim ledger.TxSimulator
90-
if txsim, err = lgr.NewTxSimulator(); err != nil {
91-
return err
92-
}
98+
ctxt := context.Background()
99+
if !syscc.ChainlessCC {
100+
lgr := peer.GetLedger(chainID)
101+
var txsim ledger.TxSimulator
102+
if txsim, err = lgr.NewTxSimulator(); err != nil {
103+
return err
104+
}
105+
106+
ctxt = context.WithValue(ctxt, TXSimulatorKey, txsim)
93107

94-
defer txsim.Done()
108+
defer txsim.Done()
109+
}
95110

96111
chaincodeID := &pb.ChaincodeID{Path: syscc.Path, Name: syscc.Name}
97112
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]), ChaincodeID: chaincodeID, CtorMsg: &pb.ChaincodeInput{Args: syscc.InitArgs}}
98113

99-
ctxt := context.WithValue(context.Background(), TXSimulatorKey, txsim)
100-
101114
// First build and get the deployment spec
102115
chaincodeDeploymentSpec, err := buildSysCC(ctxt, spec)
103116

0 commit comments

Comments
 (0)