Skip to content

Commit

Permalink
FAB-1378 beginnings of a join command
Browse files Browse the repository at this point in the history
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 <muralisr@us.ibm.com>
  • Loading branch information
Srinivasan Muralidharan committed Dec 15, 2016
1 parent e343403 commit 85b47e6
Show file tree
Hide file tree
Showing 21 changed files with 475 additions and 335 deletions.
3 changes: 2 additions & 1 deletion core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func getTxSimulator(context context.Context) ledger.TxSimulator {
if txsim, ok := context.Value(TXSimulatorKey).(ledger.TxSimulator); ok {
return txsim
}
panic("!!!---Not Using ledgernext---!!!")
//chaincode will not allow state operations
return nil
}

//CCContext pass this around instead of string of args
Expand Down
19 changes: 10 additions & 9 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/hyperledger/fabric/core/container"
"github.com/hyperledger/fabric/core/container/ccintf"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/util"
pb "github.com/hyperledger/fabric/protos/peer"
Expand Down Expand Up @@ -60,6 +59,9 @@ func getNowMillis() int64 {
func initPeer(chainIDs ...string) (net.Listener, error) {
//start clean
finitPeer(nil, chainIDs...)

peer.MockInitialize()

var opts []grpc.ServerOption
if viper.GetBool("peer.tls.enabled") {
creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file"))
Expand All @@ -70,10 +72,6 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
}
grpcServer := grpc.NewServer(opts...)

ledgerPath := viper.GetString("peer.fileSystemPath")

kvledger.Initialize(ledgerPath)

peerAddress, err := peer.GetLocalAddress()
if err != nil {
return nil, fmt.Errorf("Error obtaining peer address: %s", err)
Expand All @@ -93,7 +91,10 @@ func initPeer(chainIDs ...string) (net.Listener, error) {
RegisterSysCCs()

for _, id := range chainIDs {
kvledger.CreateLedger(id)
if err = peer.MockCreateChain(id); err != nil {
closeListenerAndSleep(lis)
return nil, err
}
DeploySysCCs(id)
}

Expand All @@ -106,7 +107,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
if lis != nil {
for _, c := range chainIDs {
deRegisterSysCCs(c)
if lgr := kvledger.GetLedger(c); lgr != nil {
if lgr := peer.GetLedger(c); lgr != nil {
lgr.Close()
}
}
Expand All @@ -119,7 +120,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
}

func startTxSimulation(ctxt context.Context, chainID string) (context.Context, ledger.TxSimulator, error) {
lgr := kvledger.GetLedger(chainID)
lgr := peer.GetLedger(chainID)
txsim, err := lgr.NewTxSimulator()
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -161,7 +162,7 @@ func endTxSimulationCIS(chainID string, txid string, txsim ledger.TxSimulator, p

func endTxSimulation(chainID string, txsim ledger.TxSimulator, payload []byte, commit bool, prop *pb.Proposal) error {
txsim.Done()
if lgr := kvledger.GetLedger(chainID); lgr != nil {
if lgr := peer.GetLedger(chainID); lgr != nil {
if commit {
var txSimulationResults []byte
var err error
Expand Down
37 changes: 31 additions & 6 deletions core/chaincode/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,18 @@ func (handler *Handler) afterGetState(e *fsm.Event, state string) {
handler.handleGetState(msg)
}

// is this a txid for which there is a valid txsim
func (handler *Handler) isValidTxSim(txid string, fmtStr string, args ...interface{}) (*transactionContext, *pb.ChaincodeMessage) {
txContext := handler.getTxContext(txid)
if txContext == nil || txContext.txsimulator == nil {
// Send error msg back to chaincode. No ledger context
errStr := fmt.Sprintf(fmtStr, args...)
chaincodeLogger.Errorf(errStr)
return nil, &pb.ChaincodeMessage{Type: pb.ChaincodeMessage_ERROR, Payload: []byte(errStr), Txid: txid}
}
return txContext, nil
}

// Handles query to ledger to get state
func (handler *Handler) handleGetState(msg *pb.ChaincodeMessage) {
// The defer followed by triggering a go routine dance is needed to ensure that the previous state transition
Expand Down Expand Up @@ -509,8 +521,13 @@ func (handler *Handler) handleGetState(msg *pb.ChaincodeMessage) {

var res []byte
var err error
var txContext *transactionContext

txContext, serialSendMsg = handler.isValidTxSim(msg.Txid, "[%s]No ledger context for GetState. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR)
if txContext == nil {
return
}

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

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

iterID := util.GenerateUUID()
txContext := handler.getTxContext(msg.Txid)

var txContext *transactionContext

txContext, serialSendMsg = handler.isValidTxSim(msg.Txid, "[%s]No ledger context for RangeQueryState. Sending %s", shorttxid(msg.Txid), pb.ChaincodeMessage_ERROR)
if txContext == nil {
return
}
chaincodeID := handler.ChaincodeID.Name

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

var txContext *transactionContext

txContext, triggerNextStateMsg = handler.isValidTxSim(msg.Txid, "[%s]No ledger context for %s. Sending %s", shorttxid(msg.Txid), msg.Type.String(), pb.ChaincodeMessage_ERROR)
if txContext == nil {
return
}

if msg.Type.String() == pb.ChaincodeMessage_PUT_STATE.String() {
putStateInfo := &pb.PutStateInfo{}
unmarshalErr := proto.Unmarshal(msg.Payload, putStateInfo)
Expand All @@ -874,13 +903,10 @@ func (handler *Handler) enterBusyState(e *fsm.Event, state string) {
return
}

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

txContext := handler.getTxContext(msg.Txid)
ctxt := context.Background()
ctxt = context.WithValue(ctxt, TXSimulatorKey, txContext.txsimulator)

Expand Down
73 changes: 51 additions & 22 deletions core/chaincode/importsysccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,36 @@ import (
//see systemchaincode_test.go for an example using "sample_syscc"
var systemChaincodes = []*SystemChaincode{
{
Enabled: true,
Name: "lccc",
Path: "github.com/hyperledger/fabric/core/chaincode",
InitArgs: [][]byte{[]byte("")},
Chaincode: &LifeCycleSysCC{},
ChainlessCC: true,
Enabled: true,
Name: "cscc",
Path: "github.com/hyperledger/fabric/core/system_chaincode/cscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &cscc.PeerConfiger{},
},
{
Enabled: true,
Name: "escc",
Path: "github.com/hyperledger/fabric/core/system_chaincode/escc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &escc.EndorserOneValidSignature{},
ChainlessCC: false,
Enabled: true,
Name: "lccc",
Path: "github.com/hyperledger/fabric/core/chaincode",
InitArgs: [][]byte{[]byte("")},
Chaincode: &LifeCycleSysCC{},
},
{
Enabled: true,
Name: "vscc",
Path: "github.com/hyperledger/fabric/core/system_chaincode/vscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &vscc.ValidatorOneValidSignature{},
ChainlessCC: false,
Enabled: true,
Name: "escc",
Path: "github.com/hyperledger/fabric/core/system_chaincode/escc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &escc.EndorserOneValidSignature{},
},
{
Enabled: true,
Name: "cscc",
Path: "github.com/hyperledger/fabric/core/system_chaincode/cscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &cscc.PeerConfiger{},
ChainlessCC: false,
Enabled: true,
Name: "vscc",
Path: "github.com/hyperledger/fabric/core/system_chaincode/vscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &vscc.ValidatorOneValidSignature{},
}}

//RegisterSysCCs is the hook for system chaincodes where system chaincodes are registered with the fabric
Expand All @@ -66,7 +70,19 @@ func RegisterSysCCs() {
//note the chaincode must still be deployed and launched like a user chaincode will be
func DeploySysCCs(chainID string) {
for _, sysCC := range systemChaincodes {
DeploySysCC(chainID, sysCC)
if !sysCC.ChainlessCC {
deploySysCC(chainID, sysCC)
}
}
}

//DeployChainlessSysCCs is the hook for deploying chainless system chaincodes
//these chaincodes cannot make any ledger calls
func DeployChainlessSysCCs() {
for _, sysCC := range systemChaincodes {
if sysCC.ChainlessCC {
deploySysCC("", sysCC)
}
}
}

Expand All @@ -75,7 +91,9 @@ func DeploySysCCs(chainID string) {
//in the same process
func deRegisterSysCCs(chainID string) {
for _, sysCC := range systemChaincodes {
deregisterSysCC(chainID, sysCC)
if !sysCC.ChainlessCC {
deregisterSysCC(chainID, sysCC)
}
}
}

Expand All @@ -89,3 +107,14 @@ func IsSysCC(name string) bool {
}
return false
}

//IsChainlessSysCC returns true if the name matches a chainless system chaincode's
//system chaincode names are system, chain wide
func IsChainlessSysCC(name string) bool {
for _, sysCC := range systemChaincodes {
if sysCC.Name == name && sysCC.ChainlessCC {
return true
}
}
return false
}
4 changes: 2 additions & 2 deletions core/chaincode/lccc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/util"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/op/go-logging"
Expand Down Expand Up @@ -277,7 +277,7 @@ func (lccc *LifeCycleSysCC) deploy(stub shim.ChaincodeStubInterface, chainname s
// 1) don't allow state initialization on deploy
// 2) combine both LCCC and the called chaincodes into one RW set
// 3) just drop the second
lgr := kvledger.GetLedger(chainname)
lgr := peer.GetLedger(chainname)

var dummytxsim ledger.TxSimulator
var err error
Expand Down
35 changes: 24 additions & 11 deletions core/chaincode/sysccapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/container/inproccontroller"
"github.com/hyperledger/fabric/core/ledger"
"github.com/hyperledger/fabric/core/ledger/kvledger"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/util"
"github.com/op/go-logging"
"github.com/spf13/viper"
Expand All @@ -38,6 +38,10 @@ var sysccLogger = logging.MustGetLogger("sysccapi")
// when the fabric comes up. SystemChaincodes are installed by adding an
// entry in importsysccs.go
type SystemChaincode struct {
//Global, once only not tied to chains. Such chaincodes cannot
//save state in the ledger. CSCC is an example
ChainlessCC bool

// Enabled a convenient switch to enable/disable system chaincode without
// having to remove entry from importsysccs.go
Enabled bool
Expand Down Expand Up @@ -76,28 +80,37 @@ func RegisterSysCC(syscc *SystemChaincode) error {
return err
}

// DeploySysCC deploys the given system chaincode on a chain
func DeploySysCC(chainID string, syscc *SystemChaincode) error {
// deploySysCC deploys the given system chaincode on a chain
func deploySysCC(chainID string, syscc *SystemChaincode) error {
if !syscc.Enabled || !isWhitelisted(syscc) {
sysccLogger.Info(fmt.Sprintf("system chaincode (%s,%s) disabled", syscc.Name, syscc.Path))
return nil
}

if chainID == "" && !syscc.ChainlessCC {
return fmt.Errorf("cannot deploy system chaincode %s without chain id", syscc.Name)
} else if chainID != "" && syscc.ChainlessCC {
return fmt.Errorf("cannot deploy chainless system chaincode %s with chain id %s", syscc.Name, chainID)
}

var err error

lgr := kvledger.GetLedger(chainID)
var txsim ledger.TxSimulator
if txsim, err = lgr.NewTxSimulator(); err != nil {
return err
}
ctxt := context.Background()
if !syscc.ChainlessCC {
lgr := peer.GetLedger(chainID)
var txsim ledger.TxSimulator
if txsim, err = lgr.NewTxSimulator(); err != nil {
return err
}

ctxt = context.WithValue(ctxt, TXSimulatorKey, txsim)

defer txsim.Done()
defer txsim.Done()
}

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

ctxt := context.WithValue(context.Background(), TXSimulatorKey, txsim)

// First build and get the deployment spec
chaincodeDeploymentSpec, err := buildSysCC(ctxt, spec)

Expand Down

0 comments on commit 85b47e6

Please sign in to comment.