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