github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/pss/types.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package pss 26 27 import ( 28 "encoding/json" 29 "fmt" 30 "sync" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/common/hexutil" 34 "github.com/ethereum/go-ethereum/p2p" 35 "github.com/ethereum/go-ethereum/rlp" 36 "github.com/ethereum/go-ethereum/swarm/storage" 37 whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" 38 ) 39 40 const ( 41 defaultWhisperTTL = 6000 42 ) 43 44 const ( 45 pssControlSym = 1 46 pssControlRaw = 1 << 1 47 ) 48 49 var ( 50 topicHashMutex = sync.Mutex{} 51 topicHashFunc = storage.MakeHashFunc("SHA256")() 52 rawTopic = Topic{} 53 ) 54 55 // 56 type Topic whisper.TopicType 57 58 func (t *Topic) String() string { 59 return hexutil.Encode(t[:]) 60 } 61 62 // 63 func (t Topic) MarshalJSON() (b []byte, err error) { 64 return json.Marshal(t.String()) 65 } 66 67 // 68 func (t *Topic) UnmarshalJSON(input []byte) error { 69 topicbytes, err := hexutil.Decode(string(input[1 : len(input)-1])) 70 if err != nil { 71 return err 72 } 73 copy(t[:], topicbytes) 74 return nil 75 } 76 77 // 78 type PssAddress []byte 79 80 // 81 func (a PssAddress) MarshalJSON() ([]byte, error) { 82 return json.Marshal(hexutil.Encode(a[:])) 83 } 84 85 // 86 func (a *PssAddress) UnmarshalJSON(input []byte) error { 87 b, err := hexutil.Decode(string(input[1 : len(input)-1])) 88 if err != nil { 89 return err 90 } 91 for _, bb := range b { 92 *a = append(*a, bb) 93 } 94 return nil 95 } 96 97 // 98 type pssDigest [digestLength]byte 99 100 // 101 type msgParams struct { 102 raw bool 103 sym bool 104 } 105 106 func newMsgParamsFromBytes(paramBytes []byte) *msgParams { 107 if len(paramBytes) != 1 { 108 return nil 109 } 110 return &msgParams{ 111 raw: paramBytes[0]&pssControlRaw > 0, 112 sym: paramBytes[0]&pssControlSym > 0, 113 } 114 } 115 116 func (m *msgParams) Bytes() (paramBytes []byte) { 117 var b byte 118 if m.raw { 119 b |= pssControlRaw 120 } 121 if m.sym { 122 b |= pssControlSym 123 } 124 paramBytes = append(paramBytes, b) 125 return paramBytes 126 } 127 128 // 129 type PssMsg struct { 130 To []byte 131 Control []byte 132 Expire uint32 133 Payload *whisper.Envelope 134 } 135 136 func newPssMsg(param *msgParams) *PssMsg { 137 return &PssMsg{ 138 Control: param.Bytes(), 139 } 140 } 141 142 // 143 func (msg *PssMsg) isRaw() bool { 144 return msg.Control[0]&pssControlRaw > 0 145 } 146 147 // 148 func (msg *PssMsg) isSym() bool { 149 return msg.Control[0]&pssControlSym > 0 150 } 151 152 // 153 func (msg *PssMsg) serialize() []byte { 154 rlpdata, _ := rlp.EncodeToBytes(struct { 155 To []byte 156 Payload *whisper.Envelope 157 }{ 158 To: msg.To, 159 Payload: msg.Payload, 160 }) 161 return rlpdata 162 } 163 164 // 165 func (msg *PssMsg) String() string { 166 return fmt.Sprintf("PssMsg: Recipient: %x", common.ToHex(msg.To)) 167 } 168 169 // 170 // 171 // 172 type Handler func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error 173 174 // 175 // 176 type stateStore struct { 177 values map[string][]byte 178 } 179 180 func newStateStore() *stateStore { 181 return &stateStore{values: make(map[string][]byte)} 182 } 183 184 func (store *stateStore) Load(key string) ([]byte, error) { 185 return nil, nil 186 } 187 188 func (store *stateStore) Save(key string, v []byte) error { 189 return nil 190 } 191 192 // 193 func BytesToTopic(b []byte) Topic { 194 topicHashMutex.Lock() 195 defer topicHashMutex.Unlock() 196 topicHashFunc.Reset() 197 topicHashFunc.Write(b) 198 return Topic(whisper.BytesToTopic(topicHashFunc.Sum(nil))) 199 }