github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/chainbridge-core/dpos_msg/darbiter.go (about) 1 package dpos_msg 2 3 import ( 4 "bytes" 5 "io" 6 "time" 7 8 "github.com/elastos/Elastos.ELA/common" 9 "github.com/elastos/Elastos.ELA/crypto" 10 "github.com/elastos/Elastos.ELA/p2p" 11 ) 12 13 const ( 14 // maxCipherLength indicates the max length of the address cipher. 15 maxCipherLength = 256 16 ) 17 18 // Ensure BatchMsg implement p2p.Message interface. 19 var _ p2p.Message = (*DArbiter)(nil) 20 21 type DArbiter struct { 22 // The peer ID indicates who's address it is. 23 PID [33]byte 24 25 // Timestamp represents the time when this message created. 26 Timestamp time.Time 27 28 // Which peer ID is used to encode the address cipher. 29 Encode [33]byte 30 31 // The encrypted network address using the encode peer ID. 32 Cipher []byte 33 34 // Signature of the encode peer ID and cipher to proof the sender itself. 35 Signature []byte 36 37 // is current consensus producers 38 IsCurrent bool 39 } 40 41 func (m *DArbiter) CMD() string { 42 return CmdDArbiter 43 } 44 45 func (m *DArbiter) MaxLength() uint32 { 46 return 388 // 33+33+256+65 + 1 47 } 48 49 func (m *DArbiter) Serialize(w io.Writer) error { 50 var timestamp = m.Timestamp.Unix() 51 err := common.WriteElements(w, m.PID, timestamp, m.Encode) 52 if err != nil { 53 return err 54 } 55 56 if err := common.WriteVarBytes(w, m.Cipher); err != nil { 57 return err 58 } 59 60 err = common.WriteVarBytes(w, m.Signature) 61 if err != nil { 62 return err 63 } 64 if m.IsCurrent == true { 65 err = common.WriteUint8(w, 1) 66 } else { 67 err = common.WriteUint8(w, 0) 68 } 69 return err 70 } 71 72 func (m *DArbiter) Deserialize(r io.Reader) error { 73 var timestamp int64 74 err := common.ReadElements(r, &m.PID, ×tamp, &m.Encode) 75 if err != nil { 76 return err 77 } 78 m.Timestamp = time.Unix(timestamp, 0) 79 80 m.Cipher, err = common.ReadVarBytes(r, maxCipherLength, "DArbiter.Cipher") 81 if err != nil { 82 return err 83 } 84 85 m.Signature, err = common.ReadVarBytes(r, crypto.SignatureLength, 86 "DArbiter.Signature") 87 88 res, err := common.ReadUint8(r) 89 if err != nil { 90 return err 91 } 92 if res == 1 { 93 m.IsCurrent = true 94 } else { 95 m.IsCurrent = false 96 } 97 return err 98 } 99 100 func (m *DArbiter) Data() []byte { 101 b := new(bytes.Buffer) 102 var timestamp = m.Timestamp.Unix() 103 common.WriteElements(b, timestamp, m.Encode) 104 common.WriteVarBytes(b, m.Cipher) 105 if m.IsCurrent == true { 106 common.WriteUint8(b, 1) 107 } else { 108 common.WriteUint8(b, 0) 109 } 110 return b.Bytes() 111 }