github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/orderer/mocks/blockcutter/blockcutter.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 mocks 18 19 import ( 20 "github.com/hyperledger/fabric/orderer/common/filter" 21 cb "github.com/hyperledger/fabric/protos/common" 22 ) 23 24 import ( 25 "github.com/op/go-logging" 26 ) 27 28 var logger = logging.MustGetLogger("orderer/mocks/blockcutter") 29 30 // Receiver mocks the blockcutter.Receiver interface 31 type Receiver struct { 32 // IsolatedTx causes Ordered returns [][]{curBatch, []{newTx}}, true, false when set to true 33 IsolatedTx bool 34 35 // CutAncestors causes Ordered returns [][]{curBatch, []{newTx}}, true, true when set to true 36 CutAncestors bool 37 38 // CutNext causes Ordered returns [][]{append(curBatch, newTx)}, true, false when set to true 39 CutNext bool 40 41 // CurBatch is the currently outstanding messages in the batch 42 CurBatch []*cb.Envelope 43 44 // Block is a channel which is read from before returning from Ordered, it is useful for synchronization 45 // If you do not wish synchronization for whatever reason, simply close the channel 46 Block chan struct{} 47 } 48 49 // NewReceiver returns the mock blockcutter.Receiver implemenation 50 func NewReceiver() *Receiver { 51 return &Receiver{ 52 IsolatedTx: false, 53 CutAncestors: false, 54 CutNext: false, 55 Block: make(chan struct{}), 56 } 57 } 58 59 func noopCommitters(size int) []filter.Committer { 60 res := make([]filter.Committer, size) 61 for i := range res { 62 res[i] = filter.NoopCommitter 63 } 64 return res 65 } 66 67 // Ordered will add or cut the batch according to the state of Receiver, it blocks reading from Block on return 68 func (mbc *Receiver) Ordered(env *cb.Envelope) ([][]*cb.Envelope, [][]filter.Committer, bool, bool) { 69 defer func() { 70 <-mbc.Block 71 }() 72 73 if mbc.IsolatedTx { 74 logger.Debugf("Receiver: Returning dual batch") 75 res := [][]*cb.Envelope{mbc.CurBatch, []*cb.Envelope{env}} 76 mbc.CurBatch = nil 77 return res, [][]filter.Committer{noopCommitters(len(res[0])), noopCommitters(len(res[1]))}, true, false 78 } 79 80 if mbc.CutAncestors { 81 logger.Debugf("Receiver: Returning current batch and appending newest env") 82 res := [][]*cb.Envelope{mbc.CurBatch} 83 mbc.CurBatch = []*cb.Envelope{env} 84 return res, [][]filter.Committer{noopCommitters(len(res))}, true, true 85 } 86 87 mbc.CurBatch = append(mbc.CurBatch, env) 88 89 if mbc.CutNext { 90 logger.Debugf("Returning regular batch") 91 res := [][]*cb.Envelope{mbc.CurBatch} 92 mbc.CurBatch = nil 93 return res, [][]filter.Committer{noopCommitters(len(res))}, true, false 94 } 95 96 logger.Debugf("Appending to batch") 97 return nil, nil, true, true 98 } 99 100 // Cut terminates the current batch, returning it 101 func (mbc *Receiver) Cut() ([]*cb.Envelope, []filter.Committer) { 102 logger.Debugf("Cutting batch") 103 res := mbc.CurBatch 104 mbc.CurBatch = nil 105 return res, noopCommitters(len(res)) 106 }