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