github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/orderer/util.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 24 "github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage" 25 "github.com/hyperledger/fabric/orderer/ledger" 26 fileledger "github.com/hyperledger/fabric/orderer/ledger/file" 27 jsonledger "github.com/hyperledger/fabric/orderer/ledger/json" 28 ramledger "github.com/hyperledger/fabric/orderer/ledger/ram" 29 config "github.com/hyperledger/fabric/orderer/localconfig" 30 "github.com/hyperledger/fabric/orderer/sbft" 31 "github.com/hyperledger/fabric/orderer/sbft/backend" 32 sbftcrypto "github.com/hyperledger/fabric/orderer/sbft/crypto" 33 "github.com/hyperledger/fabric/orderer/sbft/simplebft" 34 ) 35 36 func createLedgerFactory(conf *config.TopLevel) (ledger.Factory, string) { 37 var lf ledger.Factory 38 var ld string 39 switch conf.General.LedgerType { 40 case "file": 41 ld = conf.FileLedger.Location 42 if ld == "" { 43 ld = createTempDir(conf.FileLedger.Prefix) 44 } 45 logger.Debug("Ledger dir:", ld) 46 lf = fileledger.New(ld) 47 // The file-based ledger stores the blocks for each channel 48 // in a fsblkstorage.ChainsDir sub-directory that we have 49 // to create separately. Otherwise the call to the ledger 50 // Factory's ChainIDs below will fail (dir won't exist). 51 createSubDir(ld, fsblkstorage.ChainsDir) 52 case "json": 53 ld = conf.FileLedger.Location 54 if ld == "" { 55 ld = createTempDir(conf.FileLedger.Prefix) 56 } 57 logger.Debug("Ledger dir:", ld) 58 lf = jsonledger.New(ld) 59 case "ram": 60 fallthrough 61 default: 62 lf = ramledger.New(int(conf.RAMLedger.HistorySize)) 63 } 64 return lf, ld 65 } 66 67 func createTempDir(dirPrefix string) string { 68 dirPath, err := ioutil.TempDir("", dirPrefix) 69 if err != nil { 70 logger.Panic("Error creating temp dir:", err) 71 } 72 return dirPath 73 } 74 75 func createSubDir(parentDirPath string, subDir string) (string, bool) { 76 var created bool 77 subDirPath := filepath.Join(parentDirPath, subDir) 78 if _, err := os.Stat(subDirPath); err != nil { 79 if os.IsNotExist(err) { 80 if err = os.Mkdir(subDirPath, 0755); err != nil { 81 logger.Panic("Error creating sub dir:", err) 82 } 83 created = true 84 } else { 85 logger.Debugf("Found %s sub-dir and using it", fsblkstorage.ChainsDir) 86 } 87 } 88 return subDirPath, created 89 } 90 91 // XXX The functions below need to be moved to the SBFT package ASAP 92 93 func makeSbftConsensusConfig(conf *config.TopLevel) *sbft.ConsensusConfig { 94 cfg := simplebft.Config{N: conf.Genesis.SbftShared.N, F: conf.Genesis.SbftShared.F, 95 BatchDurationNsec: uint64(conf.Genesis.DeprecatedBatchTimeout), 96 BatchSizeBytes: uint64(conf.Genesis.DeprecatedBatchSize), 97 RequestTimeoutNsec: conf.Genesis.SbftShared.RequestTimeoutNsec} 98 peers := make(map[string][]byte) 99 for addr, cert := range conf.Genesis.SbftShared.Peers { 100 peers[addr], _ = sbftcrypto.ParseCertPEM(cert) 101 } 102 return &sbft.ConsensusConfig{Consensus: &cfg, Peers: peers} 103 } 104 105 func makeSbftStackConfig(conf *config.TopLevel) *backend.StackConfig { 106 return &backend.StackConfig{ListenAddr: conf.SbftLocal.PeerCommAddr, 107 CertFile: conf.SbftLocal.CertFile, 108 KeyFile: conf.SbftLocal.KeyFile, 109 DataDir: conf.SbftLocal.DataDir} 110 }