github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/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  // Close does nothing
    47  func (nfei *NotFoundErrorIterator) Close() {}
    48  
    49  // CreateNextBlock provides a utility way to construct the next block from
    50  // contents and metadata for a given ledger
    51  // XXX This will need to be modified to accept marshaled envelopes
    52  //     to accommodate non-deterministic marshaling
    53  func CreateNextBlock(rl Reader, messages []*cb.Envelope) *cb.Block {
    54  	var nextBlockNumber uint64
    55  	var previousBlockHash []byte
    56  
    57  	if rl.Height() > 0 {
    58  		it, _ := rl.Iterator(&ab.SeekPosition{
    59  			Type: &ab.SeekPosition_Newest{
    60  				Newest: &ab.SeekNewest{},
    61  			},
    62  		})
    63  		<-it.ReadyChan() // Should never block, but just in case
    64  		block, status := it.Next()
    65  		if status != cb.Status_SUCCESS {
    66  			panic("Error seeking to newest block for chain with non-zero height")
    67  		}
    68  		nextBlockNumber = block.Header.Number + 1
    69  		previousBlockHash = block.Header.Hash()
    70  	}
    71  
    72  	data := &cb.BlockData{
    73  		Data: make([][]byte, len(messages)),
    74  	}
    75  
    76  	var err error
    77  	for i, msg := range messages {
    78  		data.Data[i], err = proto.Marshal(msg)
    79  		if err != nil {
    80  			panic(err)
    81  		}
    82  	}
    83  
    84  	block := cb.NewBlock(nextBlockNumber, previousBlockHash)
    85  	block.Header.DataHash = data.Hash()
    86  	block.Data = data
    87  
    88  	return block
    89  }
    90  
    91  // GetBlock is a utility method for retrieving a single block
    92  func GetBlock(rl Reader, index uint64) *cb.Block {
    93  	i, _ := rl.Iterator(&ab.SeekPosition{
    94  		Type: &ab.SeekPosition_Specified{
    95  			Specified: &ab.SeekSpecified{Number: index},
    96  		},
    97  	})
    98  	select {
    99  	case <-i.ReadyChan():
   100  		block, status := i.Next()
   101  		if status != cb.Status_SUCCESS {
   102  			return nil
   103  		}
   104  		return block
   105  	default:
   106  		return nil
   107  	}
   108  }