github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/consense/dpoa/packing.go (about) 1 package dpoa 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "time" 7 8 //"github.com/ontio/ontology-crypto/keypair" 9 "github.com/sixexorg/magnetic-ring/common" 10 //"github.com/sixexorg/magnetic-ring/core/ledger" 11 //"github.com/sixexorg/magnetic-ring/store/mainchain/storages" 12 //"github.com/sixexorg/magnetic-ring/core/signature" 13 //"github.com/sixexorg/magnetic-ring/core/mainchain/types" 14 "github.com/sixexorg/magnetic-ring/consense/dpoa/comm" 15 ) 16 17 type ConsensusMsgPayload struct { 18 Type comm.MsgType `json:"type"` 19 Len uint32 `json:"len"` 20 Payload []byte `json:"payload"` 21 } 22 23 func DeserializeVbftMsg(msgPayload []byte) (comm.ConsensusMsg, error) { 24 25 m := &ConsensusMsgPayload{} 26 if err := json.Unmarshal(msgPayload, m); err != nil { 27 return nil, fmt.Errorf("unmarshal consensus msg payload: %s", err) 28 } 29 if m.Len < uint32(len(m.Payload)) { 30 return nil, fmt.Errorf("invalid payload length: %d", m.Len) 31 } 32 33 switch m.Type { 34 case comm.ConsensePrepare: 35 t := &comm.PrepareMsg{} 36 if err := json.Unmarshal(m.Payload, t); err != nil { 37 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 38 } 39 return t, nil 40 case comm.ConsensePromise: 41 t := &comm.PromiseMsg{} 42 if err := json.Unmarshal(m.Payload, t); err != nil { 43 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 44 } 45 return t, nil 46 case comm.ConsenseProposer: 47 t := &comm.ProposerMsg{} 48 if err := json.Unmarshal(m.Payload, t); err != nil { 49 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 50 } 51 return t, nil 52 case comm.ConsenseAccept: 53 t := &comm.AcceptMsg{} 54 if err := json.Unmarshal(m.Payload, t); err != nil { 55 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 56 } 57 return t, nil 58 case comm.ConsenseDone: 59 t := &comm.ConsedoneMsg{} 60 if err := json.Unmarshal(m.Payload, t); err != nil { 61 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 62 } 63 return t, nil 64 case comm.ViewTimeout: 65 t := &comm.ViewtimeoutMsg{} 66 if err := json.Unmarshal(m.Payload, t); err != nil { 67 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 68 } 69 return t, nil 70 case comm.EvilEvent: 71 t := &comm.EvilMsg{} 72 if err := json.Unmarshal(m.Payload, t); err != nil { 73 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 74 } 75 return t, nil 76 //case EarthFetchSigs: 77 // t := &EarthSigsFetchMsg{} 78 // if err := json.Unmarshal(m.Payload, t); err != nil { 79 // return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 80 // } 81 // return t, nil 82 case comm.EarthFetchRspSigs: 83 t := &comm.EarthSigsFetchRspMsg{} 84 if err := json.Unmarshal(m.Payload, t); err != nil { 85 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 86 } 87 return t, nil 88 case comm.EventFetchMessage: 89 t := &comm.EventFetchMsg{} 90 if err := json.Unmarshal(m.Payload, t); err != nil { 91 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 92 } 93 return t, nil 94 case comm.EventFetchRspMessage: 95 t := &comm.EventFetchRspMsg{} 96 if err := json.Unmarshal(m.Payload, t); err != nil { 97 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 98 } 99 return t, nil 100 case comm.EvilFetchMessage: 101 t := &comm.EvilFetchMsg{} 102 if err := json.Unmarshal(m.Payload, t); err != nil { 103 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 104 } 105 return t, nil 106 case comm.EvilFetchRspMessage: 107 t := &comm.EvilFetchRspMsg{} 108 if err := json.Unmarshal(m.Payload, t); err != nil { 109 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 110 } 111 return t, nil 112 case comm.TimeoutFetchMessage: 113 t := &comm.TimeoutFetchMsg{} 114 if err := json.Unmarshal(m.Payload, t); err != nil { 115 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 116 } 117 return t, nil 118 case comm.TimeoutFetchRspMessage: 119 t := &comm.TimeoutFetchRspMsg{} 120 if err := json.Unmarshal(m.Payload, t); err != nil { 121 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 122 } 123 return t, nil 124 case comm.PeerHandshakeMessage: 125 t := &comm.PeerHandshakeMsg{} 126 if err := json.Unmarshal(m.Payload, t); err != nil { 127 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 128 } 129 return t, nil 130 case comm.PeerHeartbeatMessage: 131 t := &comm.PeerHeartbeatMsg{} 132 if err := json.Unmarshal(m.Payload, t); err != nil { 133 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 134 } 135 return t, nil 136 case comm.BlockInfoFetchMessage: 137 t := &comm.BlockInfoFetchMsg{} 138 if err := json.Unmarshal(m.Payload, t); err != nil { 139 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 140 } 141 return t, nil 142 case comm.BlockInfoFetchRespMessage: 143 t := &comm.BlockInfoFetchRespMsg{} 144 if err := json.Unmarshal(m.Payload, t); err != nil { 145 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 146 } 147 return t, nil 148 case comm.BlockFetchMessage: 149 t := &comm.BlockFetchMsg{} 150 if err := json.Unmarshal(m.Payload, t); err != nil { 151 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 152 } 153 return t, nil 154 case comm.BlockFetchRespMessage: 155 t := &comm.BlockFetchRespMsg{} 156 if err := t.Deserialize(m.Payload); err != nil { 157 return nil, fmt.Errorf("failed to unmarshal msg (type: %d): %s", m.Type, err) 158 } 159 return t, nil 160 } 161 162 return nil, fmt.Errorf("unknown msg type: %d", m.Type) 163 } 164 165 func SerializeVbftMsg(msg comm.ConsensusMsg) ([]byte, error) { 166 167 payload, err := msg.Serialize() 168 if err != nil { 169 return nil, err 170 } 171 172 return json.Marshal(&ConsensusMsgPayload{ 173 Type: msg.Type(), 174 Len: uint32(len(payload)), 175 Payload: payload, 176 }) 177 } 178 179 func constructHandshakeMsg(self *Server) (*comm.PeerHandshakeMsg, error) { 180 blkNum := self.store.db.GetCurrentBlockHeight() 181 block, _ := self.store.getSealedBlock(blkNum) 182 if block == nil { 183 return nil, fmt.Errorf("failed to get sealed block, current block: %d", self.GetCurrentBlockNo()) 184 } 185 msg := &comm.PeerHandshakeMsg{ 186 CommittedBlockNumber: blkNum, 187 //CommittedBlockHash: blockhash, 188 //CommittedBlockLeader: block.getProposer(), 189 //ChainConfig: self.config, 190 } 191 192 return msg, nil 193 } 194 195 func constructHeartbeatMsg(store *BlockStore) (*comm.PeerHeartbeatMsg, error) { 196 197 blkNum := store.db.GetCurrentBlockHeight() 198 block, _ := store.getSealedBlock(blkNum) 199 if block == nil { 200 return nil, fmt.Errorf("failed to get sealed block, current block: %d", store.db.GetCurrentBlockHeight()) 201 } 202 //fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@@#########################", store.db.GetCurrentBlockHeight(), time.Now().String()) 203 str := time.Now().String() 204 msg := &comm.PeerHeartbeatMsg{ 205 CommittedBlockNumber: store.db.GetCurrentBlockHeight(), 206 TimeStamp: []byte(str), 207 } 208 209 return msg, nil 210 } 211 212 func constructBlockFetchMsg(blkNum uint64) (*comm.BlockFetchMsg, error) { 213 msg := &comm.BlockFetchMsg{ 214 BlockNum: blkNum, 215 } 216 return msg, nil 217 } 218 219 func constructBlockInfoFetchMsg(startBlkNum uint64) (*comm.BlockInfoFetchMsg, error) { 220 221 msg := &comm.BlockInfoFetchMsg{ 222 StartBlockNum: startBlkNum, 223 } 224 return msg, nil 225 } 226 227 func constructEventFetchRspMessage(blkNum uint64, evtimeout, evevil []interface{}) *comm.EventFetchRspMsg { 228 msg := &comm.EventFetchRspMsg{ 229 BlkNum :blkNum, 230 EvilEv :evevil, 231 TimeoutEv :evtimeout, 232 } 233 234 return msg 235 } 236 237 func constructEvilFetchMsg(blkNum uint64) *comm.EvilFetchMsg { 238 msg := &comm.EvilFetchMsg{ 239 BlkNum :blkNum, 240 } 241 242 return msg 243 } 244 245 func constructEarthSigsFetchRspMsg(blkNum uint64, pubKey string, sig []byte, blkhash common.Hash) *comm.EarthSigsFetchRspMsg { 246 return &comm.EarthSigsFetchRspMsg{ 247 BlkNum : blkNum, 248 PubKey : pubKey, 249 Sigs : sig, 250 BlkHash: blkhash, 251 } 252 } 253 254 255 func constructBlockFetchRespMsg(blkNum uint64, blk *comm.Block, blkHash common.Hash) (*comm.BlockFetchRespMsg, error) { 256 msg := &comm.BlockFetchRespMsg{ 257 BlockNumber: blkNum, 258 BlockHash: blkHash, 259 BlockData: blk, 260 } 261 return msg, nil 262 } 263 264 265 func constructBlockInfoFetchRespMsg(blockInfos []*comm.BlockInfo_) (*comm.BlockInfoFetchRespMsg, error) { 266 msg := &comm.BlockInfoFetchRespMsg{ 267 Blocks: blockInfos, 268 } 269 return msg, nil 270 }