github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/consensus/neatcon/types/neatcon.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	neatTypes "github.com/neatlab/neatio/chain/core/types"
     8  	"github.com/neatlab/neatio/utilities/common/hexutil"
     9  	"github.com/neatlib/merkle-go"
    10  	"github.com/neatlib/wire-go"
    11  )
    12  
    13  type NeatConExtra struct {
    14  	ChainID         string    `json:"chain_id"`
    15  	Height          uint64    `json:"height"`
    16  	Time            time.Time `json:"time"`
    17  	NeedToSave      bool      `json:"need_to_save"`
    18  	NeedToBroadcast bool      `json:"need_to_broadcast"`
    19  	EpochNumber     uint64    `json:"epoch_number"`
    20  	SeenCommitHash  []byte    `json:"last_commit_hash"`
    21  	ValidatorsHash  []byte    `json:"validators_hash"`
    22  	SeenCommit      *Commit   `json:"seen_commit"`
    23  	EpochBytes      []byte    `json:"epoch_bytes"`
    24  }
    25  
    26  func (te *NeatConExtra) Copy() *NeatConExtra {
    27  
    28  	return &NeatConExtra{
    29  		ChainID:         te.ChainID,
    30  		Height:          te.Height,
    31  		Time:            te.Time,
    32  		NeedToSave:      te.NeedToSave,
    33  		NeedToBroadcast: te.NeedToBroadcast,
    34  		EpochNumber:     te.EpochNumber,
    35  		SeenCommitHash:  te.SeenCommitHash,
    36  		ValidatorsHash:  te.ValidatorsHash,
    37  		SeenCommit:      te.SeenCommit,
    38  		EpochBytes:      te.EpochBytes,
    39  	}
    40  }
    41  
    42  func (te *NeatConExtra) Hash() []byte {
    43  	if len(te.ValidatorsHash) == 0 {
    44  		return nil
    45  	}
    46  	return merkle.SimpleHashFromMap(map[string]interface{}{
    47  		"ChainID":         te.ChainID,
    48  		"Height":          te.Height,
    49  		"Time":            te.Time,
    50  		"NeedToSave":      te.NeedToSave,
    51  		"NeedToBroadcast": te.NeedToBroadcast,
    52  		"EpochNumber":     te.EpochNumber,
    53  		"Validators":      te.ValidatorsHash,
    54  		"EpochBytes":      te.EpochBytes,
    55  	})
    56  }
    57  
    58  func ExtractNeatConExtra(h *neatTypes.Header) (*NeatConExtra, error) {
    59  
    60  	if len(h.Extra) == 0 {
    61  		return &NeatConExtra{}, nil
    62  	}
    63  
    64  	var ncExtra = NeatConExtra{}
    65  	err := wire.ReadBinaryBytes(h.Extra[:], &ncExtra)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return &ncExtra, nil
    70  }
    71  
    72  func (te *NeatConExtra) String() string {
    73  	str := fmt.Sprintf(`Network info: {
    74  ChainID:     %s
    75  EpochNumber: %v
    76  Height:      %v
    77  Time:        %v
    78  
    79  EpochBytes: length %v
    80  }
    81  `, te.ChainID, te.EpochNumber, te.Height, te.Time, len(te.EpochBytes))
    82  	return str
    83  }
    84  
    85  func DecodeExtraData(extra string) (ncExtra *NeatConExtra, err error) {
    86  	ncExtra = &NeatConExtra{}
    87  	extraByte, err := hexutil.Decode(extra)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	err = wire.ReadBinaryBytes(extraByte, ncExtra)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return ncExtra, nil
    97  }