github.com/annchain/OG@v0.0.9/consensus/campaign/termchange.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package campaign 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/common/byteutil" 19 "github.com/annchain/OG/og/types" 20 "github.com/annchain/OG/og/types/archive" 21 22 "github.com/annchain/OG/types" 23 "strings" 24 25 "github.com/annchain/OG/common" 26 "github.com/annchain/OG/common/hexutil" 27 ) 28 29 //go:generate msgp 30 31 //msgp:tuple TermChange 32 type TermChange struct { 33 types.TxBase 34 TermID uint64 35 PkBls hexutil.Bytes 36 SigSet []*SigSet 37 Issuer *common.Address 38 } 39 40 //msgp:tuple SigSet 41 type SigSet struct { 42 PublicKey hexutil.Bytes 43 Signature hexutil.Bytes 44 } 45 46 //msgp:tuple TermChanges 47 type TermChanges []*TermChange 48 49 func (tc *TermChange) GetBase() *types.TxBase { 50 return &tc.TxBase 51 } 52 53 func (tc *TermChange) Sender() common.Address { 54 return *tc.Issuer 55 } 56 57 func (tc *TermChange) GetSender() *common.Address { 58 return tc.Issuer 59 } 60 61 func (tc *TermChange) Compare(tx types.Txi) bool { 62 switch tx := tx.(type) { 63 case *TermChange: 64 if tc.GetHash().Cmp(tx.GetHash()) == 0 { 65 return true 66 } 67 return false 68 default: 69 return false 70 } 71 } 72 73 func (tc *TermChange) IsSameTermInfo(ctc *TermChange) bool { 74 // compare term id. 75 if tc.TermID != ctc.TermID { 76 return false 77 } 78 // compare bls public key. 79 if !common.IsSameBytes(tc.PkBls, ctc.PkBls) { 80 return false 81 } 82 // compare sigset 83 if len(tc.SigSet) != len(ctc.SigSet) { 84 return false 85 } 86 oTcMap := map[string][]byte{} 87 cTcMap := map[string][]byte{} 88 for i := range tc.SigSet { 89 oss := tc.SigSet[i] 90 oTcMap[common.Bytes2Hex(oss.PublicKey)] = oss.Signature 91 css := ctc.SigSet[i] 92 cTcMap[common.Bytes2Hex(css.PublicKey)] = css.Signature 93 } 94 for pk, os := range oTcMap { 95 cs := cTcMap[pk] 96 if cs == nil { 97 return false 98 } 99 if !common.IsSameBytes(os, cs) { 100 return false 101 } 102 } 103 104 return true 105 } 106 107 func (tc *TermChange) Dump() string { 108 var phashes []string 109 for _, p := range tc.ParentsHash { 110 phashes = append(phashes, p.Hex()) 111 } 112 var sigs []string 113 for _, v := range tc.SigSet { 114 sigs = append(sigs, fmt.Sprintf("[pubkey: %x, sig: %x]", v.PublicKey, v.Signature)) 115 } 116 return fmt.Sprintf("hash: %s, pHash: [%s], issuer: %s, nonce: %d , signatute: %s, pubkey %s ,"+ 117 "PkBls: %x, sigs: [%s]", tc.Hash.Hex(), 118 strings.Join(phashes, ", "), tc.Issuer, tc.AccountNonce, hexutil.Encode(tc.Signature), hexutil.Encode(tc.PublicKey), 119 tc.PkBls, strings.Join(sigs, ", ")) 120 } 121 122 func (tc *TermChange) SignatureTargets() []byte { 123 // add parents infornmation. 124 w := byteutil.NewBinaryWriter() 125 126 w.Write(tc.AccountNonce) 127 if !archive.CanRecoverPubFromSig { 128 w.Write(tc.Issuer.Bytes) 129 } 130 w.Write(tc.PkBls) 131 for _, signatrue := range tc.SigSet { 132 w.Write(signatrue.PublicKey, signatrue.Signature) 133 } 134 return w.Bytes() 135 } 136 137 func (tc *TermChange) String() string { 138 if tc.GetSender() == nil { 139 return fmt.Sprintf("%s-[nil]-id-%d-termChange", tc.TxBase.String(), tc.TermID) 140 } 141 return fmt.Sprintf("%s-[%.10s]-id-%d-termChange", tc.TxBase.String(), tc.Issuer.String(), tc.TermID) 142 } 143 144 func (c TermChanges) String() string { 145 var strs []string 146 for _, v := range c { 147 strs = append(strs, v.String()) 148 } 149 return strings.Join(strs, ", ") 150 } 151 152 func (c *TermChange) RawTermChange() *RawTermChange { 153 if c == nil { 154 return nil 155 } 156 rc := &RawTermChange{ 157 TxBase: c.TxBase, 158 TermId: c.TermID, 159 PkBls: c.PkBls, 160 SigSet: c.SigSet, 161 } 162 return rc 163 } 164 165 func (cs TermChanges) RawTermChanges() RawTermChanges { 166 if len(cs) == 0 { 167 return nil 168 } 169 var rawTcs RawTermChanges 170 for _, v := range cs { 171 rawTc := v.RawTermChange() 172 rawTcs = append(rawTcs, rawTc) 173 } 174 return rawTcs 175 } 176 177 func (c *TermChange) RawTxi() archive.RawTxi { 178 return c.RawTermChange() 179 } 180 181 func (t *TermChange) SetSender(addr common.Address) { 182 t.Issuer = &addr 183 } 184 func (r *TermChanges) Len() int { 185 if r == nil { 186 return 0 187 } 188 return len(*r) 189 }