github.com/annchain/OG@v0.0.9/og/types/sequencer.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "github.com/annchain/OG/arefactor/og/types" 6 "github.com/annchain/OG/common" 7 "github.com/annchain/OG/common/byteutil" 8 "github.com/annchain/OG/common/hexutil" 9 "strings" 10 ) 11 12 // Sequencer is the block generated by consensus to confirm the graph 13 type Sequencer struct { 14 // graph structure info 15 Hash types.Hash 16 ParentsHash types.Hashes 17 Height uint64 18 MineNonce uint64 19 AccountNonce uint64 20 Issuer common.Address 21 Signature hexutil.Bytes 22 PublicKey hexutil.Bytes 23 StateRoot types.Hash 24 //Proposing bool `msg:"-"` // is the sequencer is proposal ,did't commit yet ,use this flag to avoid bls sig verification failed 25 26 // derived properties 27 Weight uint64 28 invalid bool 29 } 30 31 func (s *Sequencer) SetMineNonce(v uint64) { 32 s.MineNonce = v 33 } 34 35 func (s *Sequencer) SetParents(hashes types.Hashes) { 36 s.ParentsHash = hashes 37 } 38 39 func (s *Sequencer) SetWeight(weight uint64) { 40 s.Weight = weight 41 } 42 43 func (s *Sequencer) SetValid(b bool) { 44 s.invalid = !b 45 } 46 47 func (s *Sequencer) Valid() bool { 48 return !s.invalid 49 } 50 51 func (s *Sequencer) SetSender(addr common.Address) { 52 s.Issuer = addr 53 } 54 55 func (s *Sequencer) SetHash(h types.Hash) { 56 s.Hash = h 57 } 58 59 func (s *Sequencer) GetNonce() uint64 { 60 return s.AccountNonce 61 } 62 63 func (s *Sequencer) Sender() common.Address { 64 return s.Issuer 65 } 66 67 func (s *Sequencer) SetHeight(height uint64) { 68 s.Height = height 69 } 70 71 func (s *Sequencer) Dump() string { 72 var phashes []string 73 for _, p := range s.ParentsHash { 74 phashes = append(phashes, p.Hex()) 75 } 76 return fmt.Sprintf("pHash:[%s], Issuer : %s , Height: %d, nonce : %d , blspub: %s, signatute : %s, pubkey: %s root: %s", 77 strings.Join(phashes, " ,"), 78 s.Issuer.Hex(), 79 s.Height, 80 s.AccountNonce, 81 s.PublicKey, 82 hexutil.Encode(s.PublicKey), 83 hexutil.Encode(s.Signature), 84 s.StateRoot.Hex(), 85 ) 86 } 87 88 89 90 func (s *Sequencer) SignatureTargets() []byte { 91 w := byteutil.NewBinaryWriter() 92 93 w.Write(s.PublicKey, s.AccountNonce) 94 w.Write(s.Issuer.Bytes) 95 96 //w.Write(s.Height, s.Weight, s.StateRoot.KeyBytes) 97 w.Write(s.Height, s.StateRoot.Bytes) 98 for _, parent := range s.GetParents() { 99 w.Write(parent.Bytes) 100 } 101 return w.Bytes() 102 } 103 104 func (s *Sequencer) GetType() TxBaseType { 105 return TxBaseTypeSequencer 106 } 107 108 func (s *Sequencer) GetHeight() uint64 { 109 return s.Height 110 } 111 112 func (s *Sequencer) GetWeight() uint64 { 113 if s.Weight == 0 { 114 panic("implementation error: weight not initialized") 115 } 116 return s.Weight 117 } 118 119 func (s *Sequencer) GetHash() types.Hash { 120 return s.Hash 121 } 122 123 func (s *Sequencer) GetParents() types.Hashes { 124 return s.ParentsHash 125 } 126 127 func (s *Sequencer) String() string { 128 return fmt.Sprintf("Sq-[%.10s]:%d", s.Issuer.String(), s.AccountNonce) 129 //if s.Issuer == nil { 130 // return fmt.Sprintf("Sq-[nil]-%d", s.AccountNonce) 131 //} else { 132 // 133 //} 134 } 135 136 func (s *Sequencer) CalculateWeight(parents Txis) uint64 { 137 var maxWeight uint64 138 for _, p := range parents { 139 if p.GetWeight() > maxWeight { 140 maxWeight = p.GetWeight() 141 } 142 } 143 return maxWeight + 1 144 } 145 146 func (s *Sequencer) Compare(tx Txi) bool { 147 switch tx := tx.(type) { 148 case *Sequencer: 149 if s.GetHash().Cmp(tx.GetHash()) == 0 { 150 return true 151 } 152 return false 153 default: 154 return false 155 } 156 }