github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/protos/common/block.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 common
    18  
    19  import (
    20  	"encoding/asn1"
    21  	"fmt"
    22  	"math"
    23  
    24  	"github.com/hyperledger/fabric/common/util"
    25  )
    26  
    27  // NewBlock construct a block with no data and no metadata.
    28  func NewBlock(seqNum uint64, previousHash []byte) *Block {
    29  	block := &Block{}
    30  	block.Header = &BlockHeader{}
    31  	block.Header.Number = seqNum
    32  	block.Header.PreviousHash = previousHash
    33  	block.Data = &BlockData{}
    34  
    35  	var metadataContents [][]byte
    36  	for i := 0; i < len(BlockMetadataIndex_name); i++ {
    37  		metadataContents = append(metadataContents, []byte{})
    38  	}
    39  	block.Metadata = &BlockMetadata{Metadata: metadataContents}
    40  
    41  	return block
    42  }
    43  
    44  type asn1Header struct {
    45  	Number       int64
    46  	PreviousHash []byte
    47  	DataHash     []byte
    48  }
    49  
    50  // Bytes returns the ASN.1 marshaled representation of the block header.
    51  func (b *BlockHeader) Bytes() []byte {
    52  	asn1Header := asn1Header{
    53  		PreviousHash: b.PreviousHash,
    54  		DataHash:     b.DataHash,
    55  	}
    56  	if b.Number > uint64(math.MaxInt64) {
    57  		panic(fmt.Errorf("Golang does not currently support encoding uint64 to asn1"))
    58  	} else {
    59  		asn1Header.Number = int64(b.Number)
    60  	}
    61  	result, err := asn1.Marshal(asn1Header)
    62  	if err != nil {
    63  		// Errors should only arise for types which cannot be encoded, since the
    64  		// BlockHeader type is known a-priori to contain only encodable types, an
    65  		// error here is fatal and should not be propogated
    66  		panic(err)
    67  	}
    68  	return result
    69  }
    70  
    71  // Hash returns the hash of the block header.
    72  // XXX This method will be removed shortly to allow for confgurable hashing algorithms
    73  func (b *BlockHeader) Hash() []byte {
    74  	return util.ComputeSHA256(b.Bytes())
    75  }
    76  
    77  // Bytes returns a deterministically serialized version of the BlockData
    78  // eventually, this should be replaced with a true Merkle tree construction,
    79  // but for the moment, we assume a Merkle tree of infinite width (uint32_max)
    80  // which degrades to a flat hash
    81  func (b *BlockData) Bytes() []byte {
    82  	return util.ConcatenateBytes(b.Data...)
    83  }
    84  
    85  // Hash returns the hash of the marshaled representation of the block data.
    86  func (b *BlockData) Hash() []byte {
    87  	return util.ComputeSHA256(b.Bytes())
    88  }