github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/gossip/state/metastate.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package state
     8  
     9  import (
    10  	"bytes"
    11  	"encoding/binary"
    12  )
    13  
    14  // NodeMetastate information to store the information about current
    15  // height of the ledger (last accepted block sequence number).
    16  type NodeMetastate struct {
    17  
    18  	// Actual ledger height
    19  	LedgerHeight uint64
    20  }
    21  
    22  // NewNodeMetastate creates new meta data with given ledger height148.69
    23  func NewNodeMetastate(height uint64) *NodeMetastate {
    24  	return &NodeMetastate{height}
    25  }
    26  
    27  // Bytes decodes meta state into byte array for serialization
    28  func (n *NodeMetastate) Bytes() ([]byte, error) {
    29  	buffer := new(bytes.Buffer)
    30  	// Explicitly specify byte order for write into the buffer
    31  	// to provide cross platform support, note the it consistent
    32  	// with FromBytes function
    33  	err := binary.Write(buffer, binary.BigEndian, *n)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return buffer.Bytes(), nil
    38  }
    39  
    40  // Height returns ledger height from the state
    41  func (n *NodeMetastate) Height() uint64 {
    42  	return n.LedgerHeight
    43  }
    44  
    45  // Update state with new ledger height
    46  func (n *NodeMetastate) Update(height uint64) {
    47  	n.LedgerHeight = height
    48  }
    49  
    50  // FromBytes - encode from byte array into meta data structure
    51  func FromBytes(buf []byte) (*NodeMetastate, error) {
    52  	state := NodeMetastate{}
    53  	reader := bytes.NewReader(buf)
    54  	// As bytes are written in the big endian to keep supporting
    55  	// cross platforming and for consistency reasons read also
    56  	// done using same order
    57  	err := binary.Read(reader, binary.BigEndian, &state)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	return &state, nil
    62  }