github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/pss/types.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:49</date>
    10  //</624342679001501696>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package pss
    29  
    30  import (
    31  	"encoding/json"
    32  	"fmt"
    33  	"sync"
    34  
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/common/hexutil"
    37  	"github.com/ethereum/go-ethereum/p2p"
    38  	"github.com/ethereum/go-ethereum/rlp"
    39  	"github.com/ethereum/go-ethereum/swarm/storage"
    40  	whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
    41  )
    42  
    43  const (
    44  	defaultWhisperTTL = 6000
    45  )
    46  
    47  const (
    48  	pssControlSym = 1
    49  	pssControlRaw = 1 << 1
    50  )
    51  
    52  var (
    53  	topicHashMutex = sync.Mutex{}
    54  	topicHashFunc  = storage.MakeHashFunc("SHA256")()
    55  	rawTopic       = Topic{}
    56  )
    57  
    58  //
    59  type Topic whisper.TopicType
    60  
    61  func (t *Topic) String() string {
    62  	return hexutil.Encode(t[:])
    63  }
    64  
    65  //
    66  func (t Topic) MarshalJSON() (b []byte, err error) {
    67  	return json.Marshal(t.String())
    68  }
    69  
    70  //
    71  func (t *Topic) UnmarshalJSON(input []byte) error {
    72  	topicbytes, err := hexutil.Decode(string(input[1 : len(input)-1]))
    73  	if err != nil {
    74  		return err
    75  	}
    76  	copy(t[:], topicbytes)
    77  	return nil
    78  }
    79  
    80  //
    81  type PssAddress []byte
    82  
    83  //
    84  func (a PssAddress) MarshalJSON() ([]byte, error) {
    85  	return json.Marshal(hexutil.Encode(a[:]))
    86  }
    87  
    88  //
    89  func (a *PssAddress) UnmarshalJSON(input []byte) error {
    90  	b, err := hexutil.Decode(string(input[1 : len(input)-1]))
    91  	if err != nil {
    92  		return err
    93  	}
    94  	for _, bb := range b {
    95  		*a = append(*a, bb)
    96  	}
    97  	return nil
    98  }
    99  
   100  //
   101  type pssDigest [digestLength]byte
   102  
   103  //
   104  type msgParams struct {
   105  	raw bool
   106  	sym bool
   107  }
   108  
   109  func newMsgParamsFromBytes(paramBytes []byte) *msgParams {
   110  	if len(paramBytes) != 1 {
   111  		return nil
   112  	}
   113  	return &msgParams{
   114  		raw: paramBytes[0]&pssControlRaw > 0,
   115  		sym: paramBytes[0]&pssControlSym > 0,
   116  	}
   117  }
   118  
   119  func (m *msgParams) Bytes() (paramBytes []byte) {
   120  	var b byte
   121  	if m.raw {
   122  		b |= pssControlRaw
   123  	}
   124  	if m.sym {
   125  		b |= pssControlSym
   126  	}
   127  	paramBytes = append(paramBytes, b)
   128  	return paramBytes
   129  }
   130  
   131  //
   132  type PssMsg struct {
   133  	To      []byte
   134  	Control []byte
   135  	Expire  uint32
   136  	Payload *whisper.Envelope
   137  }
   138  
   139  func newPssMsg(param *msgParams) *PssMsg {
   140  	return &PssMsg{
   141  		Control: param.Bytes(),
   142  	}
   143  }
   144  
   145  //
   146  func (msg *PssMsg) isRaw() bool {
   147  	return msg.Control[0]&pssControlRaw > 0
   148  }
   149  
   150  //
   151  func (msg *PssMsg) isSym() bool {
   152  	return msg.Control[0]&pssControlSym > 0
   153  }
   154  
   155  //
   156  func (msg *PssMsg) serialize() []byte {
   157  	rlpdata, _ := rlp.EncodeToBytes(struct {
   158  		To      []byte
   159  		Payload *whisper.Envelope
   160  	}{
   161  		To:      msg.To,
   162  		Payload: msg.Payload,
   163  	})
   164  	return rlpdata
   165  }
   166  
   167  //
   168  func (msg *PssMsg) String() string {
   169  	return fmt.Sprintf("PssMsg: Recipient: %x", common.ToHex(msg.To))
   170  }
   171  
   172  //
   173  //
   174  //
   175  type Handler func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error
   176  
   177  //
   178  //
   179  type stateStore struct {
   180  	values map[string][]byte
   181  }
   182  
   183  func newStateStore() *stateStore {
   184  	return &stateStore{values: make(map[string][]byte)}
   185  }
   186  
   187  func (store *stateStore) Load(key string) ([]byte, error) {
   188  	return nil, nil
   189  }
   190  
   191  func (store *stateStore) Save(key string, v []byte) error {
   192  	return nil
   193  }
   194  
   195  //
   196  func BytesToTopic(b []byte) Topic {
   197  	topicHashMutex.Lock()
   198  	defer topicHashMutex.Unlock()
   199  	topicHashFunc.Reset()
   200  	topicHashFunc.Write(b)
   201  	return Topic(whisper.BytesToTopic(topicHashFunc.Sum(nil)))
   202  }
   203