github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/ledger/util.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 ledger 18 19 import ( 20 "github.com/golang/protobuf/proto" 21 cb "github.com/hyperledger/fabric/protos/common" 22 ab "github.com/hyperledger/fabric/protos/orderer" 23 ) 24 25 var closedChan chan struct{} 26 27 func init() { 28 closedChan = make(chan struct{}) 29 close(closedChan) 30 } 31 32 // NotFoundErrorIterator simply always returns an error of cb.Status_NOT_FOUND, 33 // and is generally useful for implementations of the Reader interface 34 type NotFoundErrorIterator struct{} 35 36 // Next returns nil, cb.Status_NOT_FOUND 37 func (nfei *NotFoundErrorIterator) Next() (*cb.Block, cb.Status) { 38 return nil, cb.Status_NOT_FOUND 39 } 40 41 // ReadyChan returns a closed channel 42 func (nfei *NotFoundErrorIterator) ReadyChan() <-chan struct{} { 43 return closedChan 44 } 45 46 // CreateNextBlock provides a utility way to construct the next block from 47 // contents and metadata for a given ledger 48 // XXX This will need to be modified to accept marshaled envelopes 49 // to accommodate non-deterministic marshaling 50 func CreateNextBlock(rl Reader, messages []*cb.Envelope) *cb.Block { 51 var nextBlockNumber uint64 52 var previousBlockHash []byte 53 54 if rl.Height() > 0 { 55 it, _ := rl.Iterator(&ab.SeekPosition{ 56 Type: &ab.SeekPosition_Newest{ 57 &ab.SeekNewest{}, 58 }, 59 }) 60 <-it.ReadyChan() // Should never block, but just in case 61 block, status := it.Next() 62 if status != cb.Status_SUCCESS { 63 panic("Error seeking to newest block for chain with non-zero height") 64 } 65 nextBlockNumber = block.Header.Number + 1 66 previousBlockHash = block.Header.Hash() 67 } 68 69 data := &cb.BlockData{ 70 Data: make([][]byte, len(messages)), 71 } 72 73 var err error 74 for i, msg := range messages { 75 data.Data[i], err = proto.Marshal(msg) 76 if err != nil { 77 panic(err) 78 } 79 } 80 81 block := cb.NewBlock(nextBlockNumber, previousBlockHash) 82 block.Header.DataHash = data.Hash() 83 block.Data = data 84 85 return block 86 } 87 88 // GetBlock is a utility method for retrieving a single block 89 func GetBlock(rl Reader, index uint64) *cb.Block { 90 i, _ := rl.Iterator(&ab.SeekPosition{ 91 Type: &ab.SeekPosition_Specified{ 92 Specified: &ab.SeekSpecified{Number: index}, 93 }, 94 }) 95 select { 96 case <-i.ReadyChan(): 97 block, status := i.Next() 98 if status != cb.Status_SUCCESS { 99 return nil 100 } 101 return block 102 default: 103 return nil 104 } 105 }