github.com/Gessiux/neatchain@v1.3.1/chain/consensus/neatcon/types/neatcon.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/Gessiux/go-merkle" 8 "github.com/Gessiux/go-wire" 9 neatTypes "github.com/Gessiux/neatchain/chain/core/types" 10 "github.com/Gessiux/neatchain/utilities/common/hexutil" 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"` // commit from validators from the last block 21 ValidatorsHash []byte `json:"validators_hash"` // validators for the current block 22 SeenCommit *Commit `json:"seen_commit"` 23 EpochBytes []byte `json:"epoch_bytes"` 24 } 25 26 /* 27 // EncodeRLP serializes ist into the Ethereum RLP format. 28 func (te *NeatConExtra) EncodeRLP(w io.Writer) error { 29 return rlp.Encode(w, []interface{}{ 30 te.ChainID, te.Height, te.Time, te.LastBlockID, 31 te.SeenCommitHash, te.ValidatorsHash, 32 te.SeenCommit, 33 }) 34 } 35 36 // DecodeRLP implements rlp.Decoder, and load the istanbul fields from a RLP stream. 37 func (te *NeatConExtra) DecodeRLP(s *rlp.Stream) error { 38 var ncExtra NeatConExtra 39 if err := s.Decode(&ncExtra); err != nil { 40 return err 41 } 42 te.ChainID, te.Height, te.Time, te.LastBlockID, 43 te.SeenCommitHash, te.ValidatorsHash, 44 te.SeenCommit = ncExtra.ChainID, ncExtra.Height, ncExtra.Time, ncExtra.LastBlockID, 45 ncExtra.SeenCommitHash, ncExtra.ValidatorsHash, 46 ncExtra.SeenCommit 47 return nil 48 } 49 */ 50 51 //be careful, here not deep copy because just reference to SeenCommit 52 func (te *NeatConExtra) Copy() *NeatConExtra { 53 //fmt.Printf("State.Copy(), s.LastValidators are %v\n",s.LastValidators) 54 //debug.PrintStack() 55 56 return &NeatConExtra{ 57 ChainID: te.ChainID, 58 Height: te.Height, 59 Time: te.Time, 60 NeedToSave: te.NeedToSave, 61 NeedToBroadcast: te.NeedToBroadcast, 62 EpochNumber: te.EpochNumber, 63 SeenCommitHash: te.SeenCommitHash, 64 ValidatorsHash: te.ValidatorsHash, 65 SeenCommit: te.SeenCommit, 66 EpochBytes: te.EpochBytes, 67 } 68 } 69 70 // NOTE: hash is nil if required fields are missing. 71 func (te *NeatConExtra) Hash() []byte { 72 if len(te.ValidatorsHash) == 0 { 73 return nil 74 } 75 return merkle.SimpleHashFromMap(map[string]interface{}{ 76 "ChainID": te.ChainID, 77 "Height": te.Height, 78 "Time": te.Time, 79 "NeedToSave": te.NeedToSave, 80 "NeedToBroadcast": te.NeedToBroadcast, 81 "EpochNumber": te.EpochNumber, 82 "Validators": te.ValidatorsHash, 83 "EpochBytes": te.EpochBytes, 84 }) 85 } 86 87 // ExtractNeatConExtra extracts all values of the NeatConExtra from the header. It returns an 88 // error if the length of the given extra-data is less than 32 bytes or the extra-data can not 89 // be decoded. 90 func ExtractNeatConExtra(h *neatTypes.Header) (*NeatConExtra, error) { 91 92 if len(h.Extra) == 0 { 93 return &NeatConExtra{}, nil 94 } 95 96 var ncExtra = NeatConExtra{} 97 err := wire.ReadBinaryBytes(h.Extra[:], &ncExtra) 98 //err := rlp.DecodeBytes(h.Extra[:], &ncExtra) 99 if err != nil { 100 return nil, err 101 } 102 return &ncExtra, nil 103 } 104 105 func (te *NeatConExtra) String() string { 106 str := fmt.Sprintf(`NeatConExtra: { 107 ChainID: %s 108 EpochNumber: %v 109 Height: %v 110 Time: %v 111 112 EpochBytes: length %v 113 } 114 `, te.ChainID, te.EpochNumber, te.Height, te.Time, len(te.EpochBytes)) 115 return str 116 } 117 118 func DecodeExtraData(extra string) (ncExtra *NeatConExtra, err error) { 119 ncExtra = &NeatConExtra{} 120 extraByte, err := hexutil.Decode(extra) 121 if err != nil { 122 return nil, err 123 } 124 125 err = wire.ReadBinaryBytes(extraByte, ncExtra) 126 if err != nil { 127 return nil, err 128 } 129 return ncExtra, nil 130 }