github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/committer/committer_impl.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 committer 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/core/committer/txvalidator" 23 "github.com/hyperledger/fabric/core/ledger" 24 "github.com/hyperledger/fabric/events/producer" 25 "github.com/hyperledger/fabric/protos/common" 26 "github.com/op/go-logging" 27 ) 28 29 //--------!!!IMPORTANT!!-!!IMPORTANT!!-!!IMPORTANT!!--------- 30 // This is used merely to complete the loop for the "skeleton" 31 // path so we can reason about and modify committer component 32 // more effectively using code. 33 34 var logger *logging.Logger // package-level logger 35 36 func init() { 37 logger = logging.MustGetLogger("committer") 38 } 39 40 // LedgerCommitter is the implementation of Committer interface 41 // it keeps the reference to the ledger to commit blocks and retreive 42 // chain information 43 type LedgerCommitter struct { 44 ledger ledger.PeerLedger 45 validator txvalidator.Validator 46 } 47 48 // NewLedgerCommitter is a factory function to create an instance of the committer 49 func NewLedgerCommitter(ledger ledger.PeerLedger, validator txvalidator.Validator) *LedgerCommitter { 50 return &LedgerCommitter{ledger: ledger, validator: validator} 51 } 52 53 // Commit commits block to into the ledger 54 // Note, it is important that this always be called serially 55 func (lc *LedgerCommitter) Commit(block *common.Block) error { 56 // Validate and mark invalid transactions 57 logger.Debug("Validating block") 58 if err := lc.validator.Validate(block); err != nil { 59 return err 60 } 61 62 if err := lc.ledger.Commit(block); err != nil { 63 return err 64 } 65 66 // send block event *after* the block has been committed 67 if err := producer.SendProducerBlockEvent(block); err != nil { 68 logger.Errorf("Error sending block event %s", err) 69 return fmt.Errorf("Error sending block event %s", err) 70 } 71 72 return nil 73 } 74 75 // LedgerHeight returns recently committed block sequence number 76 func (lc *LedgerCommitter) LedgerHeight() (uint64, error) { 77 var info *common.BlockchainInfo 78 var err error 79 if info, err = lc.ledger.GetBlockchainInfo(); err != nil { 80 logger.Errorf("Cannot get blockchain info, %s\n", info) 81 return uint64(0), err 82 } 83 84 return info.Height, nil 85 } 86 87 // GetBlocks used to retrieve blocks with sequence numbers provided in the slice 88 func (lc *LedgerCommitter) GetBlocks(blockSeqs []uint64) []*common.Block { 89 var blocks []*common.Block 90 91 for _, seqNum := range blockSeqs { 92 if blck, err := lc.ledger.GetBlockByNumber(seqNum); err != nil { 93 logger.Errorf("Not able to acquire block num %d, from the ledger skipping...\n", seqNum) 94 continue 95 } else { 96 logger.Debug("Appending next block with seqNum = ", seqNum, " to the resulting set") 97 blocks = append(blocks, blck) 98 } 99 } 100 101 return blocks 102 } 103 104 // Close the ledger 105 func (lc *LedgerCommitter) Close() { 106 lc.ledger.Close() 107 }