github.com/annchain/OG@v0.0.9/consensus/campaign/message.go (about) 1 package campaign 2 3 import ( 4 "fmt" 5 "github.com/annchain/OG/arefactor/og_interface" 6 "github.com/annchain/OG/consensus/vrf" 7 "github.com/annchain/OG/og/protocol/ogmessage/archive" 8 "github.com/annchain/OG/og/types" 9 "github.com/annchain/OG/types/msg" 10 "strings" 11 ) 12 13 //go:generate msgp 14 15 const ( 16 MessageTypeCampaign msg.BinaryMessageType = iota + 300 17 MessageTypeTermChange 18 MessageTypeTermChangeRequest 19 MessageTypeTermChangeResponse 20 ) 21 22 //msgp:tuple RawCampaign 23 type RawCampaign struct { 24 types.TxBase 25 DkgPublicKey []byte 26 Vrf vrf.VrfInfo 27 } 28 29 //msgp:tuple RawTermChange 30 type RawTermChange struct { 31 types.TxBase 32 TermId uint64 33 PkBls []byte 34 SigSet []*SigSet 35 } 36 37 func (t *RawTermChange) String() string { 38 return fmt.Sprintf("%s-%d_%d-RawTC", t.TxBase.String(), t.AccountNonce, t.Height) 39 } 40 41 func (t *RawCampaign) String() string { 42 return fmt.Sprintf("%s-%d_%d-RawCP", t.TxBase.String(), t.AccountNonce, t.Height) 43 } 44 45 func (rc *RawCampaign) Campaign() *Campaign { 46 if rc == nil { 47 return nil 48 } 49 cp := &Campaign{ 50 TxBase: rc.TxBase, 51 DkgPublicKey: rc.DkgPublicKey, 52 Vrf: rc.Vrf, 53 } 54 if !archive.CanRecoverPubFromSig { 55 addr := og_interface.Signer.AddressFromPubKeyBytes(rc.PublicKey) 56 cp.Issuer = &addr 57 } 58 return cp 59 } 60 61 func (r *RawTermChange) TermChange() *TermChange { 62 if r == nil { 63 return nil 64 } 65 t := &TermChange{ 66 TxBase: r.TxBase, 67 PkBls: r.PkBls, 68 SigSet: r.SigSet, 69 TermID: r.TermId, 70 } 71 if !archive.CanRecoverPubFromSig { 72 addr := og_interface.Signer.AddressFromPubKeyBytes(r.PublicKey) 73 t.Issuer = &addr 74 } 75 return t 76 } 77 func (t *RawTermChange) Txi() types.Txi { 78 return t.TermChange() 79 } 80 81 func (t *RawCampaign) Txi() types.Txi { 82 return t.Campaign() 83 } 84 85 //msgp:tuple RawCampaigns 86 type RawCampaigns []*RawCampaign 87 88 //msgp:tuple RawTermChanges 89 type RawTermChanges []*RawTermChange 90 91 func (r RawCampaigns) Campaigns() Campaigns { 92 if len(r) == 0 { 93 return nil 94 } 95 var cs Campaigns 96 for _, v := range r { 97 c := v.Campaign() 98 cs = append(cs, c) 99 } 100 return cs 101 } 102 103 func (r RawTermChanges) TermChanges() TermChanges { 104 if len(r) == 0 { 105 return nil 106 } 107 var cs TermChanges 108 for _, v := range r { 109 c := v.TermChange() 110 cs = append(cs, c) 111 } 112 return cs 113 } 114 115 func (r RawTermChanges) String() string { 116 var strs []string 117 for _, v := range r { 118 strs = append(strs, v.String()) 119 } 120 return strings.Join(strs, ", ") 121 } 122 123 func (r RawCampaigns) String() string { 124 var strs []string 125 for _, v := range r { 126 strs = append(strs, v.String()) 127 } 128 return strings.Join(strs, ", ") 129 } 130 131 func (r RawTermChanges) Txis() types.Txis { 132 if len(r) == 0 { 133 return nil 134 } 135 var cs types.Txis 136 for _, v := range r { 137 c := v.TermChange() 138 cs = append(cs, c) 139 } 140 return cs 141 } 142 143 func (r RawCampaigns) Txis() types.Txis { 144 if len(r) == 0 { 145 return nil 146 } 147 var cs types.Txis 148 for _, v := range r { 149 c := v.Campaign() 150 cs = append(cs, c) 151 } 152 return cs 153 } 154 155 func (r *RawCampaigns) Len() int { 156 if r == nil { 157 return 0 158 } 159 return len(*r) 160 } 161 162 func (r *RawTermChanges) Len() int { 163 if r == nil { 164 return 0 165 } 166 return len(*r) 167 } 168 169 //msgp:tuple MessageCampaign 170 type MessageCampaign struct { 171 RawCampaign *RawCampaign 172 } 173 174 func (m *MessageCampaign) GetType() msg.BinaryMessageType { 175 return MessageTypeCampaign 176 } 177 178 func (m *MessageCampaign) GetData() []byte { 179 b, err := m.MarshalMsg(nil) 180 if err != nil { 181 panic(err) 182 } 183 return b 184 } 185 186 func (m *MessageCampaign) ToBinary() msg.BinaryMessage { 187 return msg.BinaryMessage{ 188 Type: m.GetType(), 189 Data: m.GetData(), 190 } 191 } 192 193 func (m *MessageCampaign) FromBinary(bs []byte) error { 194 _, err := m.UnmarshalMsg(bs) 195 return err 196 } 197 198 func (m *MessageCampaign) String() string { 199 return m.RawCampaign.String() 200 } 201 202 //msgp:tuple MessageTermChange 203 type MessageTermChange struct { 204 RawTermChange *RawTermChange 205 } 206 207 func (m *MessageTermChange) GetType() msg.BinaryMessageType { 208 return MessageTypeTermChange 209 } 210 211 func (m *MessageTermChange) GetData() []byte { 212 b, err := m.MarshalMsg(nil) 213 if err != nil { 214 panic(err) 215 } 216 return b 217 } 218 219 func (m *MessageTermChange) ToBinary() msg.BinaryMessage { 220 return msg.BinaryMessage{ 221 Type: m.GetType(), 222 Data: m.GetData(), 223 } 224 } 225 226 func (m *MessageTermChange) FromBinary(bs []byte) error { 227 _, err := m.UnmarshalMsg(bs) 228 return err 229 } 230 231 func (m *MessageTermChange) String() string { 232 return m.RawTermChange.String() 233 } 234 235 //msgp:tuple MessageTermChangeRequest 236 type MessageTermChangeRequest struct { 237 Id uint32 238 } 239 240 func (m *MessageTermChangeRequest) GetType() msg.BinaryMessageType { 241 return MessageTypeTermChangeRequest 242 } 243 244 func (m *MessageTermChangeRequest) GetData() []byte { 245 b, err := m.MarshalMsg(nil) 246 if err != nil { 247 panic(err) 248 } 249 return b 250 } 251 252 func (m *MessageTermChangeRequest) ToBinary() msg.BinaryMessage { 253 return msg.BinaryMessage{ 254 Type: m.GetType(), 255 Data: m.GetData(), 256 } 257 } 258 259 func (m *MessageTermChangeRequest) FromBinary(bs []byte) error { 260 _, err := m.UnmarshalMsg(bs) 261 return err 262 } 263 264 func (m *MessageTermChangeRequest) String() string { 265 return fmt.Sprintf("requst id %d ", m.Id) 266 } 267 268 //msgp:tuple MessageTermChangeResponse 269 type MessageTermChangeResponse struct { 270 TermChange *TermChange 271 Id uint32 272 } 273 274 func (m *MessageTermChangeResponse) GetType() msg.BinaryMessageType { 275 return MessageTypeTermChangeResponse 276 } 277 278 func (m *MessageTermChangeResponse) GetData() []byte { 279 b, err := m.MarshalMsg(nil) 280 if err != nil { 281 panic(err) 282 } 283 return b 284 } 285 286 func (m *MessageTermChangeResponse) ToBinary() msg.BinaryMessage { 287 return msg.BinaryMessage{ 288 Type: m.GetType(), 289 Data: m.GetData(), 290 } 291 } 292 293 func (m *MessageTermChangeResponse) FromBinary(bs []byte) error { 294 _, err := m.UnmarshalMsg(bs) 295 return err 296 } 297 298 func (m *MessageTermChangeResponse) String() string { 299 return fmt.Sprintf("requst id %d , %v ", m.Id, m.TermChange) 300 }