2014-12-08 11:43:33 +00:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2014-12-08 12:16:50 +00:00
|
|
|
"bytes"
|
2014-12-10 13:17:32 +00:00
|
|
|
"crypto/ecdsa"
|
2014-12-09 23:03:50 +00:00
|
|
|
"errors"
|
2014-12-08 11:43:33 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-12-10 13:17:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-12-12 21:23:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event/filter"
|
2014-12-15 11:01:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2014-12-08 11:43:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"gopkg.in/fatih/set.v0"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MOVE ME
|
|
|
|
type Hash struct {
|
|
|
|
hash string
|
|
|
|
}
|
|
|
|
|
|
|
|
var EmptyHash Hash
|
|
|
|
|
|
|
|
func H(hash []byte) Hash {
|
|
|
|
return Hash{string(hash)}
|
|
|
|
}
|
|
|
|
func HS(hash string) Hash {
|
|
|
|
return Hash{hash}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:16:50 +00:00
|
|
|
func (self Hash) Compare(other Hash) int {
|
|
|
|
return bytes.Compare([]byte(self.hash), []byte(other.hash))
|
|
|
|
}
|
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
// MOVE ME END
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusMsg = 0x0
|
|
|
|
envelopesMsg = 0x01
|
|
|
|
)
|
|
|
|
|
2014-12-12 21:23:42 +00:00
|
|
|
type MessageEvent struct {
|
|
|
|
To *ecdsa.PrivateKey
|
|
|
|
From *ecdsa.PublicKey
|
|
|
|
Message *Message
|
|
|
|
}
|
|
|
|
|
2014-12-09 23:03:50 +00:00
|
|
|
const DefaultTtl = 50 * time.Second
|
2014-12-08 13:25:52 +00:00
|
|
|
|
2014-12-15 11:01:55 +00:00
|
|
|
var wlogger = logger.NewLogger("SHH")
|
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
type Whisper struct {
|
|
|
|
protocol p2p.Protocol
|
2014-12-12 21:23:42 +00:00
|
|
|
filters *filter.Filters
|
2014-12-08 11:43:33 +00:00
|
|
|
|
|
|
|
mmu sync.RWMutex
|
|
|
|
messages map[Hash]*Envelope
|
2014-12-09 23:03:50 +00:00
|
|
|
expiry map[uint32]*set.SetNonTS
|
2014-12-08 11:43:33 +00:00
|
|
|
|
|
|
|
quit chan struct{}
|
2014-12-12 21:23:42 +00:00
|
|
|
|
|
|
|
keys []*ecdsa.PrivateKey
|
2014-12-08 11:43:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 21:23:42 +00:00
|
|
|
func New() *Whisper {
|
2014-12-08 11:43:33 +00:00
|
|
|
whisper := &Whisper{
|
|
|
|
messages: make(map[Hash]*Envelope),
|
2014-12-12 21:23:42 +00:00
|
|
|
filters: filter.New(),
|
2014-12-09 23:03:50 +00:00
|
|
|
expiry: make(map[uint32]*set.SetNonTS),
|
2014-12-08 11:43:33 +00:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
2014-12-12 21:23:42 +00:00
|
|
|
whisper.filters.Start()
|
2014-12-08 11:43:33 +00:00
|
|
|
|
2014-12-12 21:23:42 +00:00
|
|
|
// XXX TODO REMOVE TESTING CODE
|
2014-12-15 11:01:55 +00:00
|
|
|
//msg := NewMessage([]byte(fmt.Sprintf("Hello world. This is whisper-go. Incase you're wondering; the time is %v", time.Now())))
|
|
|
|
//envelope, _ := msg.Seal(DefaultPow, Opts{
|
|
|
|
// Ttl: DefaultTtl,
|
|
|
|
//})
|
|
|
|
//if err := whisper.Send(envelope); err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
//}
|
2014-12-12 21:23:42 +00:00
|
|
|
// XXX TODO REMOVE TESTING CODE
|
2014-12-08 13:25:52 +00:00
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
// p2p whisper sub protocol handler
|
|
|
|
whisper.protocol = p2p.Protocol{
|
|
|
|
Name: "shh",
|
|
|
|
Version: 2,
|
|
|
|
Length: 2,
|
|
|
|
Run: whisper.msgHandler,
|
|
|
|
}
|
|
|
|
|
|
|
|
return whisper
|
|
|
|
}
|
|
|
|
|
2014-12-15 11:01:55 +00:00
|
|
|
func (self *Whisper) Start() {
|
|
|
|
wlogger.Infoln("Whisper started")
|
|
|
|
go self.update()
|
|
|
|
}
|
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
func (self *Whisper) Stop() {
|
|
|
|
close(self.quit)
|
|
|
|
}
|
|
|
|
|
2014-12-09 23:03:50 +00:00
|
|
|
func (self *Whisper) Send(envelope *Envelope) error {
|
|
|
|
return self.add(envelope)
|
2014-12-08 11:43:33 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 21:23:42 +00:00
|
|
|
func (self *Whisper) NewIdentity() *ecdsa.PrivateKey {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
self.keys = append(self.keys, key)
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) HasIdentity(key *ecdsa.PrivateKey) bool {
|
|
|
|
for _, key := range self.keys {
|
|
|
|
if key.D.Cmp(key.D) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) Watch(opts Filter) int {
|
|
|
|
return self.filters.Install(filter.Generic{
|
|
|
|
Str1: string(crypto.FromECDSA(opts.To)),
|
|
|
|
Str2: string(crypto.FromECDSAPub(opts.From)),
|
|
|
|
Fn: func(data interface{}) {
|
|
|
|
opts.Fn(data.(*Message))
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:16:50 +00:00
|
|
|
// Main handler for passing whisper messages to whisper peer objects
|
2014-12-08 11:43:33 +00:00
|
|
|
func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
|
|
|
|
wpeer := NewPeer(self, peer, ws)
|
2014-12-09 23:03:50 +00:00
|
|
|
// initialise whisper peer (handshake/status)
|
2014-12-08 11:43:33 +00:00
|
|
|
if err := wpeer.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-08 12:16:50 +00:00
|
|
|
// kick of the main handler for broadcasting/managing envelopes
|
2014-12-08 11:43:33 +00:00
|
|
|
go wpeer.start()
|
2014-12-08 12:16:50 +00:00
|
|
|
defer wpeer.stop()
|
2014-12-08 11:43:33 +00:00
|
|
|
|
2014-12-08 12:16:50 +00:00
|
|
|
// Main *read* loop. Writing is done by the peer it self.
|
2014-12-08 11:43:33 +00:00
|
|
|
for {
|
|
|
|
msg, err := ws.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
envelope, err := NewEnvelopeFromReader(msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
peer.Infoln(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-09 23:03:50 +00:00
|
|
|
if err := self.add(envelope); err != nil {
|
|
|
|
// TODO Punish peer here. Invalid envelope.
|
|
|
|
peer.Infoln(err)
|
|
|
|
}
|
2014-12-08 13:25:52 +00:00
|
|
|
wpeer.addKnown(envelope)
|
2014-12-08 11:43:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:16:50 +00:00
|
|
|
// takes care of adding envelopes to the messages pool. At this moment no sanity checks are being performed.
|
2014-12-09 23:03:50 +00:00
|
|
|
func (self *Whisper) add(envelope *Envelope) error {
|
|
|
|
if !envelope.valid() {
|
2014-12-12 21:23:42 +00:00
|
|
|
return errors.New("invalid pow provided for envelope")
|
2014-12-09 23:03:50 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
self.mmu.Lock()
|
|
|
|
defer self.mmu.Unlock()
|
|
|
|
|
2014-12-09 23:03:50 +00:00
|
|
|
hash := envelope.Hash()
|
|
|
|
self.messages[hash] = envelope
|
2014-12-08 11:43:33 +00:00
|
|
|
if self.expiry[envelope.Expiry] == nil {
|
|
|
|
self.expiry[envelope.Expiry] = set.NewNonTS()
|
|
|
|
}
|
2014-12-09 23:03:50 +00:00
|
|
|
|
|
|
|
if !self.expiry[envelope.Expiry].Has(hash) {
|
|
|
|
self.expiry[envelope.Expiry].Add(hash)
|
2014-12-12 21:23:42 +00:00
|
|
|
self.postEvent(envelope)
|
2014-12-09 23:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-12-08 11:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) update() {
|
|
|
|
expire := time.NewTicker(800 * time.Millisecond)
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-expire.C:
|
|
|
|
self.expire()
|
|
|
|
case <-self.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-08 12:16:50 +00:00
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
func (self *Whisper) expire() {
|
|
|
|
self.mmu.Lock()
|
|
|
|
defer self.mmu.Unlock()
|
|
|
|
|
2014-12-09 23:03:50 +00:00
|
|
|
now := uint32(time.Now().Unix())
|
2014-12-08 11:43:33 +00:00
|
|
|
for then, hashSet := range self.expiry {
|
|
|
|
if then > now {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hashSet.Each(func(v interface{}) bool {
|
|
|
|
delete(self.messages, v.(Hash))
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
self.expiry[then].Clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Whisper) envelopes() (envelopes []*Envelope) {
|
|
|
|
self.mmu.RLock()
|
|
|
|
defer self.mmu.RUnlock()
|
|
|
|
|
|
|
|
envelopes = make([]*Envelope, len(self.messages))
|
|
|
|
i := 0
|
|
|
|
for _, envelope := range self.messages {
|
|
|
|
envelopes[i] = envelope
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:23:42 +00:00
|
|
|
func (self *Whisper) postEvent(envelope *Envelope) {
|
|
|
|
for _, key := range self.keys {
|
|
|
|
if message, err := envelope.Open(key); err == nil {
|
|
|
|
// Create a custom filter?
|
|
|
|
self.filters.Notify(filter.Generic{
|
|
|
|
Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())),
|
|
|
|
}, message)
|
|
|
|
} else {
|
2014-12-15 11:01:55 +00:00
|
|
|
wlogger.Infoln(err)
|
2014-12-12 21:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 11:43:33 +00:00
|
|
|
func (self *Whisper) Protocol() p2p.Protocol {
|
|
|
|
return self.protocol
|
|
|
|
}
|