mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
whisper: fixes warnings from the code linter
This commit is contained in:
parent
952482d5e4
commit
ed72c7ad7e
12 changed files with 318 additions and 292 deletions
|
|
@ -601,7 +601,7 @@ func requestExpiredMessagesLoop() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Failed to save symmetric key for mail request: %s", err)
|
utils.Fatalf("Failed to save symmetric key for mail request: %s", err)
|
||||||
}
|
}
|
||||||
peerID = extractIdFromEnode(*argEnode)
|
peerID = extractIDFromEnode(*argEnode)
|
||||||
shh.AllowP2PMessagesFromPeer(peerID)
|
shh.AllowP2PMessagesFromPeer(peerID)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
@ -652,7 +652,7 @@ func requestExpiredMessagesLoop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractIdFromEnode(s string) []byte {
|
func extractIDFromEnode(s string) []byte {
|
||||||
n, err := discover.ParseNode(s)
|
n, err := discover.ParseNode(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Failed to parse enode: %s", err)
|
utils.Fatalf("Failed to parse enode: %s", err)
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ func (api *PublicWhisperAPI) SetMaxMessageSize(ctx context.Context, size uint32)
|
||||||
return true, api.w.SetMaxMessageSize(size)
|
return true, api.w.SetMaxMessageSize(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMinPow sets the minimum PoW, and notifies the peers.
|
// SetMinPoW sets the minimum PoW, and notifies the peers.
|
||||||
func (api *PublicWhisperAPI) SetMinPoW(ctx context.Context, pow float64) (bool, error) {
|
func (api *PublicWhisperAPI) SetMinPoW(ctx context.Context, pow float64) (bool, error) {
|
||||||
return true, api.w.SetMinimumPoW(pow)
|
return true, api.w.SetMinimumPoW(pow)
|
||||||
}
|
}
|
||||||
|
|
@ -174,7 +174,7 @@ func (api *PublicWhisperAPI) GetPublicKey(ctx context.Context, id string) (hexut
|
||||||
return crypto.FromECDSAPub(&key.PublicKey), nil
|
return crypto.FromECDSAPub(&key.PublicKey), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPublicKey returns the private key associated with the given key. The key is the hex
|
// GetPrivateKey returns the private key associated with the given key. The key is the hex
|
||||||
// encoded representation of a key in the form specified in section 4.3.6 of ANSI X9.62.
|
// encoded representation of a key in the form specified in section 4.3.6 of ANSI X9.62.
|
||||||
func (api *PublicWhisperAPI) GetPrivateKey(ctx context.Context, id string) (hexutil.Bytes, error) {
|
func (api *PublicWhisperAPI) GetPrivateKey(ctx context.Context, id string) (hexutil.Bytes, error) {
|
||||||
key, err := api.w.GetPrivateKey(id)
|
key, err := api.w.GetPrivateKey(id)
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,13 @@
|
||||||
|
|
||||||
package whisperv6
|
package whisperv6
|
||||||
|
|
||||||
|
// Config represents the configuration state of a whisper node.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
MaxMessageSize uint32 `toml:",omitempty"`
|
MaxMessageSize uint32 `toml:",omitempty"`
|
||||||
MinimumAcceptedPOW float64 `toml:",omitempty"`
|
MinimumAcceptedPOW float64 `toml:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DefaultConfig represents (shocker!) the default configuration.
|
||||||
var DefaultConfig = Config{
|
var DefaultConfig = Config{
|
||||||
MaxMessageSize: DefaultMaxMessageSize,
|
MaxMessageSize: DefaultMaxMessageSize,
|
||||||
MinimumAcceptedPOW: DefaultMinimumPoW,
|
MinimumAcceptedPOW: DefaultMinimumPoW,
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,9 @@ Whisper is a pure identity-based messaging system. Whisper provides a low-level
|
||||||
or prejudiced by the low-level hardware attributes and characteristics,
|
or prejudiced by the low-level hardware attributes and characteristics,
|
||||||
particularly the notion of singular endpoints.
|
particularly the notion of singular endpoints.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Contains the Whisper protocol constant definitions
|
||||||
|
|
||||||
package whisperv6
|
package whisperv6
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -35,9 +38,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ProtocolVersion = uint64(6)
|
ProtocolVersion = uint64(6) // Protocol version number
|
||||||
ProtocolVersionStr = "6.0"
|
ProtocolVersionStr = "6.0" // The same, as a string
|
||||||
ProtocolName = "shh"
|
ProtocolName = "shh" // Nickname of the protocol in geth
|
||||||
|
|
||||||
// whisper protocol message codes, according to EIP-627
|
// whisper protocol message codes, according to EIP-627
|
||||||
statusCode = 0 // used by whisper protocol
|
statusCode = 0 // used by whisper protocol
|
||||||
|
|
@ -55,7 +58,7 @@ const (
|
||||||
signatureLength = 65 // in bytes
|
signatureLength = 65 // in bytes
|
||||||
aesKeyLength = 32 // in bytes
|
aesKeyLength = 32 // in bytes
|
||||||
AESNonceLength = 12 // in bytes
|
AESNonceLength = 12 // in bytes
|
||||||
keyIdSize = 32 // in bytes
|
keyIDSize = 32 // in bytes
|
||||||
bloomFilterSize = 64 // in bytes
|
bloomFilterSize = 64 // in bytes
|
||||||
|
|
||||||
EnvelopeHeaderLength = 20
|
EnvelopeHeaderLength = 20
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,8 @@ func (e *Envelope) Seal(options *MessageParams) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PoW computes (if necessary) and returns the proof of work target
|
||||||
|
// of the envelope.
|
||||||
func (e *Envelope) PoW() float64 {
|
func (e *Envelope) PoW() float64 {
|
||||||
if e.pow == 0 {
|
if e.pow == 0 {
|
||||||
e.calculatePoW(0)
|
e.calculatePoW(0)
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Filter represents a Whisper message filter
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
Src *ecdsa.PublicKey // Sender of the message
|
Src *ecdsa.PublicKey // Sender of the message
|
||||||
KeyAsym *ecdsa.PrivateKey // Private Key of recipient
|
KeyAsym *ecdsa.PrivateKey // Private Key of recipient
|
||||||
|
|
@ -39,12 +40,14 @@ type Filter struct {
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Filters represents a collection of filters
|
||||||
type Filters struct {
|
type Filters struct {
|
||||||
watchers map[string]*Filter
|
watchers map[string]*Filter
|
||||||
whisper *Whisper
|
whisper *Whisper
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFilters returns a newly created filter collection
|
||||||
func NewFilters(w *Whisper) *Filters {
|
func NewFilters(w *Whisper) *Filters {
|
||||||
return &Filters{
|
return &Filters{
|
||||||
watchers: make(map[string]*Filter),
|
watchers: make(map[string]*Filter),
|
||||||
|
|
@ -52,6 +55,7 @@ func NewFilters(w *Whisper) *Filters {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Install will add a new filter to the filter collection
|
||||||
func (fs *Filters) Install(watcher *Filter) (string, error) {
|
func (fs *Filters) Install(watcher *Filter) (string, error) {
|
||||||
if watcher.KeySym != nil && watcher.KeyAsym != nil {
|
if watcher.KeySym != nil && watcher.KeyAsym != nil {
|
||||||
return "", fmt.Errorf("filters must choose between symmetric and asymmetric keys")
|
return "", fmt.Errorf("filters must choose between symmetric and asymmetric keys")
|
||||||
|
|
@ -81,6 +85,8 @@ func (fs *Filters) Install(watcher *Filter) (string, error) {
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uninstall will remove a filter whose id has been specified from
|
||||||
|
// the filter collection
|
||||||
func (fs *Filters) Uninstall(id string) bool {
|
func (fs *Filters) Uninstall(id string) bool {
|
||||||
fs.mutex.Lock()
|
fs.mutex.Lock()
|
||||||
defer fs.mutex.Unlock()
|
defer fs.mutex.Unlock()
|
||||||
|
|
@ -91,12 +97,15 @@ func (fs *Filters) Uninstall(id string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get returns a filter from the collection with a specific ID
|
||||||
func (fs *Filters) Get(id string) *Filter {
|
func (fs *Filters) Get(id string) *Filter {
|
||||||
fs.mutex.RLock()
|
fs.mutex.RLock()
|
||||||
defer fs.mutex.RUnlock()
|
defer fs.mutex.RUnlock()
|
||||||
return fs.watchers[id]
|
return fs.watchers[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NotifyWatchers notifies any filter that has declared interest
|
||||||
|
// for the envelope's topic.
|
||||||
func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
|
func (fs *Filters) NotifyWatchers(env *Envelope, p2pMessage bool) {
|
||||||
var msg *ReceivedMessage
|
var msg *ReceivedMessage
|
||||||
|
|
||||||
|
|
@ -140,9 +149,9 @@ func (f *Filter) processEnvelope(env *Envelope) *ReceivedMessage {
|
||||||
msg := env.Open(f)
|
msg := env.Open(f)
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
return msg
|
return msg
|
||||||
} else {
|
|
||||||
log.Trace("processing envelope: failed to open", "hash", env.Hash().Hex())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Trace("processing envelope: failed to open", "hash", env.Hash().Hex())
|
||||||
} else {
|
} else {
|
||||||
log.Trace("processing envelope: does not match", "hash", env.Hash().Hex())
|
log.Trace("processing envelope: does not match", "hash", env.Hash().Hex())
|
||||||
}
|
}
|
||||||
|
|
@ -157,6 +166,7 @@ func (f *Filter) expectsSymmetricEncryption() bool {
|
||||||
return f.KeySym != nil
|
return f.KeySym != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trigger
|
||||||
func (f *Filter) Trigger(msg *ReceivedMessage) {
|
func (f *Filter) Trigger(msg *ReceivedMessage) {
|
||||||
f.mutex.Lock()
|
f.mutex.Lock()
|
||||||
defer f.mutex.Unlock()
|
defer f.mutex.Unlock()
|
||||||
|
|
@ -166,6 +176,8 @@ func (f *Filter) Trigger(msg *ReceivedMessage) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieve will return the list of all received messages associated
|
||||||
|
// to a filter.
|
||||||
func (f *Filter) Retrieve() (all []*ReceivedMessage) {
|
func (f *Filter) Retrieve() (all []*ReceivedMessage) {
|
||||||
f.mutex.Lock()
|
f.mutex.Lock()
|
||||||
defer f.mutex.Unlock()
|
defer f.mutex.Unlock()
|
||||||
|
|
@ -195,7 +207,7 @@ func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchEvelope checks if it's worth decrypting the message. If
|
// MatchEnvelope checks if it's worth decrypting the message. If
|
||||||
// it returns `true`, client code is expected to attempt decrypting
|
// it returns `true`, client code is expected to attempt decrypting
|
||||||
// the message and subsequently call MatchMessage.
|
// the message and subsequently call MatchMessage.
|
||||||
func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
|
func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
|
||||||
|
|
@ -206,6 +218,7 @@ func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
|
||||||
return f.MatchTopic(envelope.Topic)
|
return f.MatchTopic(envelope.Topic)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MatchTopic checks that the filter captures a given topic.
|
||||||
func (f *Filter) MatchTopic(topic TopicType) bool {
|
func (f *Filter) MatchTopic(topic TopicType) bool {
|
||||||
if len(f.Topics) == 0 {
|
if len(f.Topics) == 0 {
|
||||||
// any topic matches
|
// any topic matches
|
||||||
|
|
@ -233,6 +246,7 @@ func matchSingleTopic(topic TopicType, bt []byte) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsPubKeyEqual checks that two public keys are equal
|
||||||
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
||||||
if !ValidatePublicKey(a) {
|
if !ValidatePublicKey(a) {
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -109,7 +109,7 @@ func TestInstallFilters(t *testing.T) {
|
||||||
t.Fatalf("seed %d: failed to install filter: %s", seed, err)
|
t.Fatalf("seed %d: failed to install filter: %s", seed, err)
|
||||||
}
|
}
|
||||||
tst[i].id = j
|
tst[i].id = j
|
||||||
if len(j) != keyIdSize*2 {
|
if len(j) != keyIDSize*2 {
|
||||||
t.Fatalf("seed %d: wrong filter id size [%d]", seed, len(j))
|
t.Fatalf("seed %d: wrong filter id size [%d]", seed, len(j))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Options specifies the exact way a message should be wrapped into an Envelope.
|
// MessageParams specifies the exact way a message should be wrapped
|
||||||
|
// into an Envelope.
|
||||||
type MessageParams struct {
|
type MessageParams struct {
|
||||||
TTL uint32
|
TTL uint32
|
||||||
Src *ecdsa.PrivateKey
|
Src *ecdsa.PrivateKey
|
||||||
|
|
@ -86,7 +87,7 @@ func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
||||||
return msg.Dst != nil
|
return msg.Dst != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
// NewSentMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
||||||
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
msg.Raw = make([]byte, 1, len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
msg.Raw = make([]byte, 1, len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
||||||
|
|
@ -341,7 +342,8 @@ func (msg *ReceivedMessage) extractPadding(end int) (int, bool) {
|
||||||
return paddingSize, true
|
return paddingSize, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recover retrieves the public key of the message signer.
|
// SigToPubKey returns the public key associated to the message's
|
||||||
|
// signature.
|
||||||
func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
||||||
defer func() { recover() }() // in case of invalid signature
|
defer func() { recover() }() // in case of invalid signature
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ import (
|
||||||
set "gopkg.in/fatih/set.v0"
|
set "gopkg.in/fatih/set.v0"
|
||||||
)
|
)
|
||||||
|
|
||||||
// peer represents a whisper protocol peer connection.
|
// Peer represents a whisper protocol peer connection.
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
host *Whisper
|
host *Whisper
|
||||||
peer *p2p.Peer
|
peer *p2p.Peer
|
||||||
|
|
@ -58,48 +58,48 @@ func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *Peer {
|
||||||
|
|
||||||
// start initiates the peer updater, periodically broadcasting the whisper packets
|
// start initiates the peer updater, periodically broadcasting the whisper packets
|
||||||
// into the network.
|
// into the network.
|
||||||
func (p *Peer) start() {
|
func (peer *Peer) start() {
|
||||||
go p.update()
|
go peer.update()
|
||||||
log.Trace("start", "peer", p.ID())
|
log.Trace("start", "peer", peer.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
// stop terminates the peer updater, stopping message forwarding to it.
|
// stop terminates the peer updater, stopping message forwarding to it.
|
||||||
func (p *Peer) stop() {
|
func (peer *Peer) stop() {
|
||||||
close(p.quit)
|
close(peer.quit)
|
||||||
log.Trace("stop", "peer", p.ID())
|
log.Trace("stop", "peer", peer.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
// handshake sends the protocol initiation status message to the remote peer and
|
// handshake sends the protocol initiation status message to the remote peer and
|
||||||
// verifies the remote status too.
|
// verifies the remote status too.
|
||||||
func (p *Peer) handshake() error {
|
func (peer *Peer) handshake() error {
|
||||||
// Send the handshake status message asynchronously
|
// Send the handshake status message asynchronously
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
pow := p.host.MinPow()
|
pow := peer.host.MinPow()
|
||||||
powConverted := math.Float64bits(pow)
|
powConverted := math.Float64bits(pow)
|
||||||
bloom := p.host.BloomFilter()
|
bloom := peer.host.BloomFilter()
|
||||||
errc <- p2p.SendItems(p.ws, statusCode, ProtocolVersion, powConverted, bloom)
|
errc <- p2p.SendItems(peer.ws, statusCode, ProtocolVersion, powConverted, bloom)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Fetch the remote status packet and verify protocol match
|
// Fetch the remote status packet and verify protocol match
|
||||||
packet, err := p.ws.ReadMsg()
|
packet, err := peer.ws.ReadMsg()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if packet.Code != statusCode {
|
if packet.Code != statusCode {
|
||||||
return fmt.Errorf("peer [%x] sent packet %x before status packet", p.ID(), packet.Code)
|
return fmt.Errorf("peer [%x] sent packet %x before status packet", peer.ID(), packet.Code)
|
||||||
}
|
}
|
||||||
s := rlp.NewStream(packet.Payload, uint64(packet.Size))
|
s := rlp.NewStream(packet.Payload, uint64(packet.Size))
|
||||||
_, err = s.List()
|
_, err = s.List()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("peer [%x] sent bad status message: %v", p.ID(), err)
|
return fmt.Errorf("peer [%x] sent bad status message: %v", peer.ID(), err)
|
||||||
}
|
}
|
||||||
peerVersion, err := s.Uint()
|
peerVersion, err := s.Uint()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("peer [%x] sent bad status message (unable to decode version): %v", p.ID(), err)
|
return fmt.Errorf("peer [%x] sent bad status message (unable to decode version): %v", peer.ID(), err)
|
||||||
}
|
}
|
||||||
if peerVersion != ProtocolVersion {
|
if peerVersion != ProtocolVersion {
|
||||||
return fmt.Errorf("peer [%x]: protocol version mismatch %d != %d", p.ID(), peerVersion, ProtocolVersion)
|
return fmt.Errorf("peer [%x]: protocol version mismatch %d != %d", peer.ID(), peerVersion, ProtocolVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// only version is mandatory, subsequent parameters are optional
|
// only version is mandatory, subsequent parameters are optional
|
||||||
|
|
@ -107,34 +107,34 @@ func (p *Peer) handshake() error {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pow := math.Float64frombits(powRaw)
|
pow := math.Float64frombits(powRaw)
|
||||||
if math.IsInf(pow, 0) || math.IsNaN(pow) || pow < 0.0 {
|
if math.IsInf(pow, 0) || math.IsNaN(pow) || pow < 0.0 {
|
||||||
return fmt.Errorf("peer [%x] sent bad status message: invalid pow", p.ID())
|
return fmt.Errorf("peer [%x] sent bad status message: invalid pow", peer.ID())
|
||||||
}
|
}
|
||||||
p.powRequirement = pow
|
peer.powRequirement = pow
|
||||||
|
|
||||||
var bloom []byte
|
var bloom []byte
|
||||||
err = s.Decode(&bloom)
|
err = s.Decode(&bloom)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
sz := len(bloom)
|
sz := len(bloom)
|
||||||
if sz != bloomFilterSize && sz != 0 {
|
if sz != bloomFilterSize && sz != 0 {
|
||||||
return fmt.Errorf("peer [%x] sent bad status message: wrong bloom filter size %d", p.ID(), sz)
|
return fmt.Errorf("peer [%x] sent bad status message: wrong bloom filter size %d", peer.ID(), sz)
|
||||||
}
|
}
|
||||||
if isFullNode(bloom) {
|
if isFullNode(bloom) {
|
||||||
p.bloomFilter = nil
|
peer.bloomFilter = nil
|
||||||
} else {
|
} else {
|
||||||
p.bloomFilter = bloom
|
peer.bloomFilter = bloom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := <-errc; err != nil {
|
if err := <-errc; err != nil {
|
||||||
return fmt.Errorf("peer [%x] failed to send status packet: %v", p.ID(), err)
|
return fmt.Errorf("peer [%x] failed to send status packet: %v", peer.ID(), err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// update executes periodic operations on the peer, including message transmission
|
// update executes periodic operations on the peer, including message transmission
|
||||||
// and expiration.
|
// and expiration.
|
||||||
func (p *Peer) update() {
|
func (peer *Peer) update() {
|
||||||
// Start the tickers for the updates
|
// Start the tickers for the updates
|
||||||
expire := time.NewTicker(expirationCycle)
|
expire := time.NewTicker(expirationCycle)
|
||||||
transmit := time.NewTicker(transmissionCycle)
|
transmit := time.NewTicker(transmissionCycle)
|
||||||
|
|
@ -143,15 +143,15 @@ func (p *Peer) update() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-expire.C:
|
case <-expire.C:
|
||||||
p.expire()
|
peer.expire()
|
||||||
|
|
||||||
case <-transmit.C:
|
case <-transmit.C:
|
||||||
if err := p.broadcast(); err != nil {
|
if err := peer.broadcast(); err != nil {
|
||||||
log.Trace("broadcast failed", "reason", err, "peer", p.ID())
|
log.Trace("broadcast failed", "reason", err, "peer", peer.ID())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-p.quit:
|
case <-peer.quit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -185,24 +185,24 @@ func (peer *Peer) expire() {
|
||||||
|
|
||||||
// broadcast iterates over the collection of envelopes and transmits yet unknown
|
// broadcast iterates over the collection of envelopes and transmits yet unknown
|
||||||
// ones over the network.
|
// ones over the network.
|
||||||
func (p *Peer) broadcast() error {
|
func (peer *Peer) broadcast() error {
|
||||||
envelopes := p.host.Envelopes()
|
envelopes := peer.host.Envelopes()
|
||||||
bundle := make([]*Envelope, 0, len(envelopes))
|
bundle := make([]*Envelope, 0, len(envelopes))
|
||||||
for _, envelope := range envelopes {
|
for _, envelope := range envelopes {
|
||||||
if !p.marked(envelope) && envelope.PoW() >= p.powRequirement && p.bloomMatch(envelope) {
|
if !peer.marked(envelope) && envelope.PoW() >= peer.powRequirement && peer.bloomMatch(envelope) {
|
||||||
bundle = append(bundle, envelope)
|
bundle = append(bundle, envelope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(bundle) > 0 {
|
if len(bundle) > 0 {
|
||||||
// transmit the batch of envelopes
|
// transmit the batch of envelopes
|
||||||
if err := p2p.Send(p.ws, messagesCode, bundle); err != nil {
|
if err := p2p.Send(peer.ws, messagesCode, bundle); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// mark envelopes only if they were successfully sent
|
// mark envelopes only if they were successfully sent
|
||||||
for _, e := range bundle {
|
for _, e := range bundle {
|
||||||
p.mark(e)
|
peer.mark(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Trace("broadcast", "num. messages", len(bundle))
|
log.Trace("broadcast", "num. messages", len(bundle))
|
||||||
|
|
@ -210,25 +210,26 @@ func (p *Peer) broadcast() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) ID() []byte {
|
// ID returns a peer's id
|
||||||
id := p.peer.ID()
|
func (peer *Peer) ID() []byte {
|
||||||
|
id := peer.peer.ID()
|
||||||
return id[:]
|
return id[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) notifyAboutPowRequirementChange(pow float64) error {
|
func (peer *Peer) notifyAboutPowRequirementChange(pow float64) error {
|
||||||
i := math.Float64bits(pow)
|
i := math.Float64bits(pow)
|
||||||
return p2p.Send(p.ws, powRequirementCode, i)
|
return p2p.Send(peer.ws, powRequirementCode, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) notifyAboutBloomFilterChange(bloom []byte) error {
|
func (peer *Peer) notifyAboutBloomFilterChange(bloom []byte) error {
|
||||||
return p2p.Send(p.ws, bloomFilterExCode, bloom)
|
return p2p.Send(peer.ws, bloomFilterExCode, bloom)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) bloomMatch(env *Envelope) bool {
|
func (peer *Peer) bloomMatch(env *Envelope) bool {
|
||||||
if p.bloomFilter == nil {
|
if peer.bloomFilter == nil {
|
||||||
// no filter - full node, accepts all envelops
|
// no filter - full node, accepts all envelops
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return bloomFilterMatch(p.bloomFilter, env.Bloom())
|
return bloomFilterMatch(peer.bloomFilter, env.Bloom())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/p2p/nat"
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
||||||
)
|
)
|
||||||
|
|
||||||
var keys []string = []string{
|
var keys = []string{
|
||||||
"d49dcf37238dc8a7aac57dc61b9fee68f0a97f062968978b9fafa7d1033d03a9",
|
"d49dcf37238dc8a7aac57dc61b9fee68f0a97f062968978b9fafa7d1033d03a9",
|
||||||
"73fd6143c48e80ed3c56ea159fe7494a0b6b393a392227b422f4c3e8f1b54f98",
|
"73fd6143c48e80ed3c56ea159fe7494a0b6b393a392227b422f4c3e8f1b54f98",
|
||||||
"119dd32adb1daa7a4c7bf77f847fb28730785aa92947edf42fdd997b54de40dc",
|
"119dd32adb1daa7a4c7bf77f847fb28730785aa92947edf42fdd997b54de40dc",
|
||||||
|
|
@ -80,17 +80,17 @@ type TestNode struct {
|
||||||
shh *Whisper
|
shh *Whisper
|
||||||
id *ecdsa.PrivateKey
|
id *ecdsa.PrivateKey
|
||||||
server *p2p.Server
|
server *p2p.Server
|
||||||
filerId string
|
filerID string
|
||||||
}
|
}
|
||||||
|
|
||||||
var result TestData
|
var result TestData
|
||||||
var nodes [NumNodes]*TestNode
|
var nodes [NumNodes]*TestNode
|
||||||
var sharedKey []byte = []byte("some arbitrary data here")
|
var sharedKey = []byte("some arbitrary data here")
|
||||||
var sharedTopic TopicType = TopicType{0xF, 0x1, 0x2, 0}
|
var sharedTopic = TopicType{0xF, 0x1, 0x2, 0}
|
||||||
var expectedMessage []byte = []byte("per rectum ad astra")
|
var expectedMessage = []byte("per rectum ad astra")
|
||||||
var masterBloomFilter []byte
|
var masterBloomFilter []byte
|
||||||
var masterPow = 0.00000001
|
var masterPow = 0.00000001
|
||||||
var round int = 1
|
var round = 1
|
||||||
|
|
||||||
func TestSimulation(t *testing.T) {
|
func TestSimulation(t *testing.T) {
|
||||||
// create a chain of whisper nodes,
|
// create a chain of whisper nodes,
|
||||||
|
|
@ -186,7 +186,7 @@ func initialize(t *testing.T) {
|
||||||
topics = append(topics, sharedTopic)
|
topics = append(topics, sharedTopic)
|
||||||
f := Filter{KeySym: sharedKey}
|
f := Filter{KeySym: sharedKey}
|
||||||
f.Topics = [][]byte{topics[0][:]}
|
f.Topics = [][]byte{topics[0][:]}
|
||||||
node.filerId, err = node.shh.Subscribe(&f)
|
node.filerID, err = node.shh.Subscribe(&f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to install the filter: %s.", err)
|
t.Fatalf("failed to install the filter: %s.", err)
|
||||||
}
|
}
|
||||||
|
|
@ -199,9 +199,9 @@ func initialize(t *testing.T) {
|
||||||
name := common.MakeName("whisper-go", "2.0")
|
name := common.MakeName("whisper-go", "2.0")
|
||||||
var peers []*discover.Node
|
var peers []*discover.Node
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
peerNodeId := nodes[i-1].id
|
peerNodeID := nodes[i-1].id
|
||||||
peerPort := uint16(port - 1)
|
peerPort := uint16(port - 1)
|
||||||
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
|
peerNode := discover.PubkeyID(&peerNodeID.PublicKey)
|
||||||
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
|
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
|
||||||
peers = append(peers, peer)
|
peers = append(peers, peer)
|
||||||
}
|
}
|
||||||
|
|
@ -238,7 +238,7 @@ func stopServers() {
|
||||||
for i := 0; i < NumNodes; i++ {
|
for i := 0; i < NumNodes; i++ {
|
||||||
n := nodes[i]
|
n := nodes[i]
|
||||||
if n != nil {
|
if n != nil {
|
||||||
n.shh.Unsubscribe(n.filerId)
|
n.shh.Unsubscribe(n.filerID)
|
||||||
n.shh.Stop()
|
n.shh.Stop()
|
||||||
n.server.Stop()
|
n.server.Stop()
|
||||||
}
|
}
|
||||||
|
|
@ -260,9 +260,9 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
|
||||||
|
|
||||||
for j := 0; j < iterations; j++ {
|
for j := 0; j < iterations; j++ {
|
||||||
for i := first; i < NumNodes; i++ {
|
for i := first; i < NumNodes; i++ {
|
||||||
f := nodes[i].shh.GetFilter(nodes[i].filerId)
|
f := nodes[i].shh.GetFilter(nodes[i].filerID)
|
||||||
if f == nil {
|
if f == nil {
|
||||||
t.Fatalf("failed to get filterId %s from node %d, round %d.", nodes[i].filerId, i, round)
|
t.Fatalf("failed to get filterId %s from node %d, round %d.", nodes[i].filerID, i, round)
|
||||||
}
|
}
|
||||||
|
|
||||||
mail := f.Retrieve()
|
mail := f.Retrieve()
|
||||||
|
|
@ -281,7 +281,7 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
|
||||||
t.Fatalf("Test was not complete: timeout %d seconds.", iterations*cycle/1000)
|
t.Fatalf("Test was not complete: timeout %d seconds.", iterations*cycle/1000)
|
||||||
|
|
||||||
if !includingNodeZero {
|
if !includingNodeZero {
|
||||||
f := nodes[0].shh.GetFilter(nodes[0].filerId)
|
f := nodes[0].shh.GetFilter(nodes[0].filerID)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
t.Fatalf("node zero received a message with low PoW.")
|
t.Fatalf("node zero received a message with low PoW.")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,13 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Topic represents a cryptographically secure, probabilistic partial
|
// TopicType represents a cryptographically secure, probabilistic partial
|
||||||
// classifications of a message, determined as the first (left) 4 bytes of the
|
// classifications of a message, determined as the first (left) 4 bytes of the
|
||||||
// SHA3 hash of some arbitrary data given by the original author of the message.
|
// SHA3 hash of some arbitrary data given by the original author of the message.
|
||||||
type TopicType [TopicLength]byte
|
type TopicType [TopicLength]byte
|
||||||
|
|
||||||
|
// BytesToTopic converts from the byte array representation of a topic
|
||||||
|
// into the TopicType type.
|
||||||
func BytesToTopic(b []byte) (t TopicType) {
|
func BytesToTopic(b []byte) (t TopicType) {
|
||||||
sz := TopicLength
|
sz := TopicLength
|
||||||
if x := len(b); x < TopicLength {
|
if x := len(b); x < TopicLength {
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ import (
|
||||||
set "gopkg.in/fatih/set.v0"
|
set "gopkg.in/fatih/set.v0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Statistics holds several message-related counter for analytics
|
||||||
|
// purposes.
|
||||||
type Statistics struct {
|
type Statistics struct {
|
||||||
messagesCleared int
|
messagesCleared int
|
||||||
memoryCleared int
|
memoryCleared int
|
||||||
|
|
@ -130,8 +132,8 @@ func New(cfg *Config) *Whisper {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MinPow returns the PoW value required by this node.
|
// MinPow returns the PoW value required by this node.
|
||||||
func (w *Whisper) MinPow() float64 {
|
func (whisper *Whisper) MinPow() float64 {
|
||||||
val, exist := w.settings.Load(minPowIdx)
|
val, exist := whisper.settings.Load(minPowIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
return DefaultMinimumPoW
|
return DefaultMinimumPoW
|
||||||
}
|
}
|
||||||
|
|
@ -146,8 +148,8 @@ func (w *Whisper) MinPow() float64 {
|
||||||
// MinPowTolerance returns the value of minimum PoW which is tolerated for a limited
|
// MinPowTolerance returns the value of minimum PoW which is tolerated for a limited
|
||||||
// time after PoW was changed. If sufficient time have elapsed or no change of PoW
|
// time after PoW was changed. If sufficient time have elapsed or no change of PoW
|
||||||
// have ever occurred, the return value will be the same as return value of MinPow().
|
// have ever occurred, the return value will be the same as return value of MinPow().
|
||||||
func (w *Whisper) MinPowTolerance() float64 {
|
func (whisper *Whisper) MinPowTolerance() float64 {
|
||||||
val, exist := w.settings.Load(minPowToleranceIdx)
|
val, exist := whisper.settings.Load(minPowToleranceIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
return DefaultMinimumPoW
|
return DefaultMinimumPoW
|
||||||
}
|
}
|
||||||
|
|
@ -158,8 +160,8 @@ func (w *Whisper) MinPowTolerance() float64 {
|
||||||
// The nodes are required to send only messages that match the advertised bloom filter.
|
// The nodes are required to send only messages that match the advertised bloom filter.
|
||||||
// If a message does not match the bloom, it will tantamount to spam, and the peer will
|
// If a message does not match the bloom, it will tantamount to spam, and the peer will
|
||||||
// be disconnected.
|
// be disconnected.
|
||||||
func (w *Whisper) BloomFilter() []byte {
|
func (whisper *Whisper) BloomFilter() []byte {
|
||||||
val, exist := w.settings.Load(bloomFilterIdx)
|
val, exist := whisper.settings.Load(bloomFilterIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -170,8 +172,8 @@ func (w *Whisper) BloomFilter() []byte {
|
||||||
// time after new bloom was advertised to the peers. If sufficient time have elapsed
|
// time after new bloom was advertised to the peers. If sufficient time have elapsed
|
||||||
// or no change of bloom filter have ever occurred, the return value will be the same
|
// or no change of bloom filter have ever occurred, the return value will be the same
|
||||||
// as return value of BloomFilter().
|
// as return value of BloomFilter().
|
||||||
func (w *Whisper) BloomFilterTolerance() []byte {
|
func (whisper *Whisper) BloomFilterTolerance() []byte {
|
||||||
val, exist := w.settings.Load(bloomFilterToleranceIdx)
|
val, exist := whisper.settings.Load(bloomFilterToleranceIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -179,24 +181,24 @@ func (w *Whisper) BloomFilterTolerance() []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxMessageSize returns the maximum accepted message size.
|
// MaxMessageSize returns the maximum accepted message size.
|
||||||
func (w *Whisper) MaxMessageSize() uint32 {
|
func (whisper *Whisper) MaxMessageSize() uint32 {
|
||||||
val, _ := w.settings.Load(maxMsgSizeIdx)
|
val, _ := whisper.settings.Load(maxMsgSizeIdx)
|
||||||
return val.(uint32)
|
return val.(uint32)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overflow returns an indication if the message queue is full.
|
// Overflow returns an indication if the message queue is full.
|
||||||
func (w *Whisper) Overflow() bool {
|
func (whisper *Whisper) Overflow() bool {
|
||||||
val, _ := w.settings.Load(overflowIdx)
|
val, _ := whisper.settings.Load(overflowIdx)
|
||||||
return val.(bool)
|
return val.(bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIs returns the RPC descriptors the Whisper implementation offers
|
// APIs returns the RPC descriptors the Whisper implementation offers
|
||||||
func (w *Whisper) APIs() []rpc.API {
|
func (whisper *Whisper) APIs() []rpc.API {
|
||||||
return []rpc.API{
|
return []rpc.API{
|
||||||
{
|
{
|
||||||
Namespace: ProtocolName,
|
Namespace: ProtocolName,
|
||||||
Version: ProtocolVersionStr,
|
Version: ProtocolVersionStr,
|
||||||
Service: NewPublicWhisperAPI(w),
|
Service: NewPublicWhisperAPI(whisper),
|
||||||
Public: true,
|
Public: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -204,31 +206,31 @@ func (w *Whisper) APIs() []rpc.API {
|
||||||
|
|
||||||
// RegisterServer registers MailServer interface.
|
// RegisterServer registers MailServer interface.
|
||||||
// MailServer will process all the incoming messages with p2pRequestCode.
|
// MailServer will process all the incoming messages with p2pRequestCode.
|
||||||
func (w *Whisper) RegisterServer(server MailServer) {
|
func (whisper *Whisper) RegisterServer(server MailServer) {
|
||||||
w.mailServer = server
|
whisper.mailServer = server
|
||||||
}
|
}
|
||||||
|
|
||||||
// Protocols returns the whisper sub-protocols ran by this particular client.
|
// Protocols returns the whisper sub-protocols ran by this particular client.
|
||||||
func (w *Whisper) Protocols() []p2p.Protocol {
|
func (whisper *Whisper) Protocols() []p2p.Protocol {
|
||||||
return []p2p.Protocol{w.protocol}
|
return []p2p.Protocol{whisper.protocol}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version returns the whisper sub-protocols version number.
|
// Version returns the whisper sub-protocols version number.
|
||||||
func (w *Whisper) Version() uint {
|
func (whisper *Whisper) Version() uint {
|
||||||
return w.protocol.Version
|
return whisper.protocol.Version
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMaxMessageSize sets the maximal message size allowed by this node
|
// SetMaxMessageSize sets the maximal message size allowed by this node
|
||||||
func (w *Whisper) SetMaxMessageSize(size uint32) error {
|
func (whisper *Whisper) SetMaxMessageSize(size uint32) error {
|
||||||
if size > MaxMessageSize {
|
if size > MaxMessageSize {
|
||||||
return fmt.Errorf("message size too large [%d>%d]", size, MaxMessageSize)
|
return fmt.Errorf("message size too large [%d>%d]", size, MaxMessageSize)
|
||||||
}
|
}
|
||||||
w.settings.Store(maxMsgSizeIdx, size)
|
whisper.settings.Store(maxMsgSizeIdx, size)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBloomFilter sets the new bloom filter
|
// SetBloomFilter sets the new bloom filter
|
||||||
func (w *Whisper) SetBloomFilter(bloom []byte) error {
|
func (whisper *Whisper) SetBloomFilter(bloom []byte) error {
|
||||||
if len(bloom) != bloomFilterSize {
|
if len(bloom) != bloomFilterSize {
|
||||||
return fmt.Errorf("invalid bloom filter size: %d", len(bloom))
|
return fmt.Errorf("invalid bloom filter size: %d", len(bloom))
|
||||||
}
|
}
|
||||||
|
|
@ -236,45 +238,45 @@ func (w *Whisper) SetBloomFilter(bloom []byte) error {
|
||||||
b := make([]byte, bloomFilterSize)
|
b := make([]byte, bloomFilterSize)
|
||||||
copy(b, bloom)
|
copy(b, bloom)
|
||||||
|
|
||||||
w.settings.Store(bloomFilterIdx, b)
|
whisper.settings.Store(bloomFilterIdx, b)
|
||||||
w.notifyPeersAboutBloomFilterChange(b)
|
whisper.notifyPeersAboutBloomFilterChange(b)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// allow some time before all the peers have processed the notification
|
// allow some time before all the peers have processed the notification
|
||||||
time.Sleep(time.Duration(w.syncAllowance) * time.Second)
|
time.Sleep(time.Duration(whisper.syncAllowance) * time.Second)
|
||||||
w.settings.Store(bloomFilterToleranceIdx, b)
|
whisper.settings.Store(bloomFilterToleranceIdx, b)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMinimumPoW sets the minimal PoW required by this node
|
// SetMinimumPoW sets the minimal PoW required by this node
|
||||||
func (w *Whisper) SetMinimumPoW(val float64) error {
|
func (whisper *Whisper) SetMinimumPoW(val float64) error {
|
||||||
if val < 0.0 {
|
if val < 0.0 {
|
||||||
return fmt.Errorf("invalid PoW: %f", val)
|
return fmt.Errorf("invalid PoW: %f", val)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.settings.Store(minPowIdx, val)
|
whisper.settings.Store(minPowIdx, val)
|
||||||
w.notifyPeersAboutPowRequirementChange(val)
|
whisper.notifyPeersAboutPowRequirementChange(val)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
// allow some time before all the peers have processed the notification
|
// allow some time before all the peers have processed the notification
|
||||||
time.Sleep(time.Duration(w.syncAllowance) * time.Second)
|
time.Sleep(time.Duration(whisper.syncAllowance) * time.Second)
|
||||||
w.settings.Store(minPowToleranceIdx, val)
|
whisper.settings.Store(minPowToleranceIdx, val)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMinimumPoW sets the minimal PoW in test environment
|
// SetMinimumPowTest sets the minimal PoW in test environment
|
||||||
func (w *Whisper) SetMinimumPowTest(val float64) {
|
func (whisper *Whisper) SetMinimumPowTest(val float64) {
|
||||||
w.settings.Store(minPowIdx, val)
|
whisper.settings.Store(minPowIdx, val)
|
||||||
w.notifyPeersAboutPowRequirementChange(val)
|
whisper.notifyPeersAboutPowRequirementChange(val)
|
||||||
w.settings.Store(minPowToleranceIdx, val)
|
whisper.settings.Store(minPowToleranceIdx, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) notifyPeersAboutPowRequirementChange(pow float64) {
|
func (whisper *Whisper) notifyPeersAboutPowRequirementChange(pow float64) {
|
||||||
arr := w.getPeers()
|
arr := whisper.getPeers()
|
||||||
for _, p := range arr {
|
for _, p := range arr {
|
||||||
err := p.notifyAboutPowRequirementChange(pow)
|
err := p.notifyAboutPowRequirementChange(pow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -287,8 +289,8 @@ func (w *Whisper) notifyPeersAboutPowRequirementChange(pow float64) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) notifyPeersAboutBloomFilterChange(bloom []byte) {
|
func (whisper *Whisper) notifyPeersAboutBloomFilterChange(bloom []byte) {
|
||||||
arr := w.getPeers()
|
arr := whisper.getPeers()
|
||||||
for _, p := range arr {
|
for _, p := range arr {
|
||||||
err := p.notifyAboutBloomFilterChange(bloom)
|
err := p.notifyAboutBloomFilterChange(bloom)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -301,23 +303,23 @@ func (w *Whisper) notifyPeersAboutBloomFilterChange(bloom []byte) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) getPeers() []*Peer {
|
func (whisper *Whisper) getPeers() []*Peer {
|
||||||
arr := make([]*Peer, len(w.peers))
|
arr := make([]*Peer, len(whisper.peers))
|
||||||
i := 0
|
i := 0
|
||||||
w.peerMu.Lock()
|
whisper.peerMu.Lock()
|
||||||
for p := range w.peers {
|
for p := range whisper.peers {
|
||||||
arr[i] = p
|
arr[i] = p
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
w.peerMu.Unlock()
|
whisper.peerMu.Unlock()
|
||||||
return arr
|
return arr
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPeer retrieves peer by ID
|
// getPeer retrieves peer by ID
|
||||||
func (w *Whisper) getPeer(peerID []byte) (*Peer, error) {
|
func (whisper *Whisper) getPeer(peerID []byte) (*Peer, error) {
|
||||||
w.peerMu.Lock()
|
whisper.peerMu.Lock()
|
||||||
defer w.peerMu.Unlock()
|
defer whisper.peerMu.Unlock()
|
||||||
for p := range w.peers {
|
for p := range whisper.peers {
|
||||||
id := p.peer.ID()
|
id := p.peer.ID()
|
||||||
if bytes.Equal(peerID, id[:]) {
|
if bytes.Equal(peerID, id[:]) {
|
||||||
return p, nil
|
return p, nil
|
||||||
|
|
@ -328,8 +330,8 @@ func (w *Whisper) getPeer(peerID []byte) (*Peer, error) {
|
||||||
|
|
||||||
// AllowP2PMessagesFromPeer marks specific peer trusted,
|
// AllowP2PMessagesFromPeer marks specific peer trusted,
|
||||||
// which will allow it to send historic (expired) messages.
|
// which will allow it to send historic (expired) messages.
|
||||||
func (w *Whisper) AllowP2PMessagesFromPeer(peerID []byte) error {
|
func (whisper *Whisper) AllowP2PMessagesFromPeer(peerID []byte) error {
|
||||||
p, err := w.getPeer(peerID)
|
p, err := whisper.getPeer(peerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -342,8 +344,8 @@ func (w *Whisper) AllowP2PMessagesFromPeer(peerID []byte) error {
|
||||||
// request and respond with a number of peer-to-peer messages (possibly expired),
|
// request and respond with a number of peer-to-peer messages (possibly expired),
|
||||||
// which are not supposed to be forwarded any further.
|
// which are not supposed to be forwarded any further.
|
||||||
// The whisper protocol is agnostic of the format and contents of envelope.
|
// The whisper protocol is agnostic of the format and contents of envelope.
|
||||||
func (w *Whisper) RequestHistoricMessages(peerID []byte, envelope *Envelope) error {
|
func (whisper *Whisper) RequestHistoricMessages(peerID []byte, envelope *Envelope) error {
|
||||||
p, err := w.getPeer(peerID)
|
p, err := whisper.getPeer(peerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -352,22 +354,22 @@ func (w *Whisper) RequestHistoricMessages(peerID []byte, envelope *Envelope) err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendP2PMessage sends a peer-to-peer message to a specific peer.
|
// SendP2PMessage sends a peer-to-peer message to a specific peer.
|
||||||
func (w *Whisper) SendP2PMessage(peerID []byte, envelope *Envelope) error {
|
func (whisper *Whisper) SendP2PMessage(peerID []byte, envelope *Envelope) error {
|
||||||
p, err := w.getPeer(peerID)
|
p, err := whisper.getPeer(peerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return w.SendP2PDirect(p, envelope)
|
return whisper.SendP2PDirect(p, envelope)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendP2PDirect sends a peer-to-peer message to a specific peer.
|
// SendP2PDirect sends a peer-to-peer message to a specific peer.
|
||||||
func (w *Whisper) SendP2PDirect(peer *Peer, envelope *Envelope) error {
|
func (whisper *Whisper) SendP2PDirect(peer *Peer, envelope *Envelope) error {
|
||||||
return p2p.Send(peer.ws, p2pMessageCode, envelope)
|
return p2p.Send(peer.ws, p2pMessageCode, envelope)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyPair generates a new cryptographic identity for the client, and injects
|
// NewKeyPair generates a new cryptographic identity for the client, and injects
|
||||||
// it into the known identities for message decryption. Returns ID of the new key pair.
|
// it into the known identities for message decryption. Returns ID of the new key pair.
|
||||||
func (w *Whisper) NewKeyPair() (string, error) {
|
func (whisper *Whisper) NewKeyPair() (string, error) {
|
||||||
key, err := crypto.GenerateKey()
|
key, err := crypto.GenerateKey()
|
||||||
if err != nil || !validatePrivateKey(key) {
|
if err != nil || !validatePrivateKey(key) {
|
||||||
key, err = crypto.GenerateKey() // retry once
|
key, err = crypto.GenerateKey() // retry once
|
||||||
|
|
@ -384,55 +386,55 @@ func (w *Whisper) NewKeyPair() (string, error) {
|
||||||
return "", fmt.Errorf("failed to generate ID: %s", err)
|
return "", fmt.Errorf("failed to generate ID: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer whisper.keyMu.Unlock()
|
||||||
|
|
||||||
if w.privateKeys[id] != nil {
|
if whisper.privateKeys[id] != nil {
|
||||||
return "", fmt.Errorf("failed to generate unique ID")
|
return "", fmt.Errorf("failed to generate unique ID")
|
||||||
}
|
}
|
||||||
w.privateKeys[id] = key
|
whisper.privateKeys[id] = key
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteKeyPair deletes the specified key if it exists.
|
// DeleteKeyPair deletes the specified key if it exists.
|
||||||
func (w *Whisper) DeleteKeyPair(key string) bool {
|
func (whisper *Whisper) DeleteKeyPair(key string) bool {
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer whisper.keyMu.Unlock()
|
||||||
|
|
||||||
if w.privateKeys[key] != nil {
|
if whisper.privateKeys[key] != nil {
|
||||||
delete(w.privateKeys, key)
|
delete(whisper.privateKeys, key)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddKeyPair imports a asymmetric private key and returns it identifier.
|
// AddKeyPair imports a asymmetric private key and returns it identifier.
|
||||||
func (w *Whisper) AddKeyPair(key *ecdsa.PrivateKey) (string, error) {
|
func (whisper *Whisper) AddKeyPair(key *ecdsa.PrivateKey) (string, error) {
|
||||||
id, err := GenerateRandomID()
|
id, err := GenerateRandomID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to generate ID: %s", err)
|
return "", fmt.Errorf("failed to generate ID: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
w.privateKeys[id] = key
|
whisper.privateKeys[id] = key
|
||||||
w.keyMu.Unlock()
|
whisper.keyMu.Unlock()
|
||||||
|
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasKeyPair checks if the the whisper node is configured with the private key
|
// HasKeyPair checks if the the whisper node is configured with the private key
|
||||||
// of the specified public pair.
|
// of the specified public pair.
|
||||||
func (w *Whisper) HasKeyPair(id string) bool {
|
func (whisper *Whisper) HasKeyPair(id string) bool {
|
||||||
w.keyMu.RLock()
|
whisper.keyMu.RLock()
|
||||||
defer w.keyMu.RUnlock()
|
defer whisper.keyMu.RUnlock()
|
||||||
return w.privateKeys[id] != nil
|
return whisper.privateKeys[id] != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPrivateKey retrieves the private key of the specified identity.
|
// GetPrivateKey retrieves the private key of the specified identity.
|
||||||
func (w *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
|
func (whisper *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
|
||||||
w.keyMu.RLock()
|
whisper.keyMu.RLock()
|
||||||
defer w.keyMu.RUnlock()
|
defer whisper.keyMu.RUnlock()
|
||||||
key := w.privateKeys[id]
|
key := whisper.privateKeys[id]
|
||||||
if key == nil {
|
if key == nil {
|
||||||
return nil, fmt.Errorf("invalid id")
|
return nil, fmt.Errorf("invalid id")
|
||||||
}
|
}
|
||||||
|
|
@ -441,7 +443,7 @@ func (w *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
|
||||||
|
|
||||||
// GenerateSymKey generates a random symmetric key and stores it under id,
|
// GenerateSymKey generates a random symmetric key and stores it under id,
|
||||||
// which is then returned. Will be used in the future for session key exchange.
|
// which is then returned. Will be used in the future for session key exchange.
|
||||||
func (w *Whisper) GenerateSymKey() (string, error) {
|
func (whisper *Whisper) GenerateSymKey() (string, error) {
|
||||||
key := make([]byte, aesKeyLength)
|
key := make([]byte, aesKeyLength)
|
||||||
_, err := crand.Read(key)
|
_, err := crand.Read(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -455,18 +457,18 @@ func (w *Whisper) GenerateSymKey() (string, error) {
|
||||||
return "", fmt.Errorf("failed to generate ID: %s", err)
|
return "", fmt.Errorf("failed to generate ID: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer whisper.keyMu.Unlock()
|
||||||
|
|
||||||
if w.symKeys[id] != nil {
|
if whisper.symKeys[id] != nil {
|
||||||
return "", fmt.Errorf("failed to generate unique ID")
|
return "", fmt.Errorf("failed to generate unique ID")
|
||||||
}
|
}
|
||||||
w.symKeys[id] = key
|
whisper.symKeys[id] = key
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddSymKeyDirect stores the key, and returns its id.
|
// AddSymKeyDirect stores the key, and returns its id.
|
||||||
func (w *Whisper) AddSymKeyDirect(key []byte) (string, error) {
|
func (whisper *Whisper) AddSymKeyDirect(key []byte) (string, error) {
|
||||||
if len(key) != aesKeyLength {
|
if len(key) != aesKeyLength {
|
||||||
return "", fmt.Errorf("wrong key size: %d", len(key))
|
return "", fmt.Errorf("wrong key size: %d", len(key))
|
||||||
}
|
}
|
||||||
|
|
@ -476,23 +478,23 @@ func (w *Whisper) AddSymKeyDirect(key []byte) (string, error) {
|
||||||
return "", fmt.Errorf("failed to generate ID: %s", err)
|
return "", fmt.Errorf("failed to generate ID: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer whisper.keyMu.Unlock()
|
||||||
|
|
||||||
if w.symKeys[id] != nil {
|
if whisper.symKeys[id] != nil {
|
||||||
return "", fmt.Errorf("failed to generate unique ID")
|
return "", fmt.Errorf("failed to generate unique ID")
|
||||||
}
|
}
|
||||||
w.symKeys[id] = key
|
whisper.symKeys[id] = key
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddSymKeyFromPassword generates the key from password, stores it, and returns its id.
|
// AddSymKeyFromPassword generates the key from password, stores it, and returns its id.
|
||||||
func (w *Whisper) AddSymKeyFromPassword(password string) (string, error) {
|
func (whisper *Whisper) AddSymKeyFromPassword(password string) (string, error) {
|
||||||
id, err := GenerateRandomID()
|
id, err := GenerateRandomID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to generate ID: %s", err)
|
return "", fmt.Errorf("failed to generate ID: %s", err)
|
||||||
}
|
}
|
||||||
if w.HasSymKey(id) {
|
if whisper.HasSymKey(id) {
|
||||||
return "", fmt.Errorf("failed to generate unique ID")
|
return "", fmt.Errorf("failed to generate unique ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -503,59 +505,59 @@ func (w *Whisper) AddSymKeyFromPassword(password string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer whisper.keyMu.Unlock()
|
||||||
|
|
||||||
// double check is necessary, because deriveKeyMaterial() is very slow
|
// double check is necessary, because deriveKeyMaterial() is very slow
|
||||||
if w.symKeys[id] != nil {
|
if whisper.symKeys[id] != nil {
|
||||||
return "", fmt.Errorf("critical error: failed to generate unique ID")
|
return "", fmt.Errorf("critical error: failed to generate unique ID")
|
||||||
}
|
}
|
||||||
w.symKeys[id] = derived
|
whisper.symKeys[id] = derived
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasSymKey returns true if there is a key associated with the given id.
|
// HasSymKey returns true if there is a key associated with the given id.
|
||||||
// Otherwise returns false.
|
// Otherwise returns false.
|
||||||
func (w *Whisper) HasSymKey(id string) bool {
|
func (whisper *Whisper) HasSymKey(id string) bool {
|
||||||
w.keyMu.RLock()
|
whisper.keyMu.RLock()
|
||||||
defer w.keyMu.RUnlock()
|
defer whisper.keyMu.RUnlock()
|
||||||
return w.symKeys[id] != nil
|
return whisper.symKeys[id] != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteSymKey deletes the key associated with the name string if it exists.
|
// DeleteSymKey deletes the key associated with the name string if it exists.
|
||||||
func (w *Whisper) DeleteSymKey(id string) bool {
|
func (whisper *Whisper) DeleteSymKey(id string) bool {
|
||||||
w.keyMu.Lock()
|
whisper.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer whisper.keyMu.Unlock()
|
||||||
if w.symKeys[id] != nil {
|
if whisper.symKeys[id] != nil {
|
||||||
delete(w.symKeys, id)
|
delete(whisper.symKeys, id)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSymKey returns the symmetric key associated with the given id.
|
// GetSymKey returns the symmetric key associated with the given id.
|
||||||
func (w *Whisper) GetSymKey(id string) ([]byte, error) {
|
func (whisper *Whisper) GetSymKey(id string) ([]byte, error) {
|
||||||
w.keyMu.RLock()
|
whisper.keyMu.RLock()
|
||||||
defer w.keyMu.RUnlock()
|
defer whisper.keyMu.RUnlock()
|
||||||
if w.symKeys[id] != nil {
|
if whisper.symKeys[id] != nil {
|
||||||
return w.symKeys[id], nil
|
return whisper.symKeys[id], nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("non-existent key ID")
|
return nil, fmt.Errorf("non-existent key ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe installs a new message handler used for filtering, decrypting
|
// Subscribe installs a new message handler used for filtering, decrypting
|
||||||
// and subsequent storing of incoming messages.
|
// and subsequent storing of incoming messages.
|
||||||
func (w *Whisper) Subscribe(f *Filter) (string, error) {
|
func (whisper *Whisper) Subscribe(f *Filter) (string, error) {
|
||||||
s, err := w.filters.Install(f)
|
s, err := whisper.filters.Install(f)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
w.updateBloomFilter(f)
|
whisper.updateBloomFilter(f)
|
||||||
}
|
}
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// updateBloomFilter recalculates the new value of bloom filter,
|
// updateBloomFilter recalculates the new value of bloom filter,
|
||||||
// and informs the peers if necessary.
|
// and informs the peers if necessary.
|
||||||
func (w *Whisper) updateBloomFilter(f *Filter) {
|
func (whisper *Whisper) updateBloomFilter(f *Filter) {
|
||||||
aggregate := make([]byte, bloomFilterSize)
|
aggregate := make([]byte, bloomFilterSize)
|
||||||
for _, t := range f.Topics {
|
for _, t := range f.Topics {
|
||||||
top := BytesToTopic(t)
|
top := BytesToTopic(t)
|
||||||
|
|
@ -563,21 +565,21 @@ func (w *Whisper) updateBloomFilter(f *Filter) {
|
||||||
aggregate = addBloom(aggregate, b)
|
aggregate = addBloom(aggregate, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bloomFilterMatch(w.BloomFilter(), aggregate) {
|
if !bloomFilterMatch(whisper.BloomFilter(), aggregate) {
|
||||||
// existing bloom filter must be updated
|
// existing bloom filter must be updated
|
||||||
aggregate = addBloom(w.BloomFilter(), aggregate)
|
aggregate = addBloom(whisper.BloomFilter(), aggregate)
|
||||||
w.SetBloomFilter(aggregate)
|
whisper.SetBloomFilter(aggregate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFilter returns the filter by id.
|
// GetFilter returns the filter by id.
|
||||||
func (w *Whisper) GetFilter(id string) *Filter {
|
func (whisper *Whisper) GetFilter(id string) *Filter {
|
||||||
return w.filters.Get(id)
|
return whisper.filters.Get(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unsubscribe removes an installed message handler.
|
// Unsubscribe removes an installed message handler.
|
||||||
func (w *Whisper) Unsubscribe(id string) error {
|
func (whisper *Whisper) Unsubscribe(id string) error {
|
||||||
ok := w.filters.Uninstall(id)
|
ok := whisper.filters.Uninstall(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Unsubscribe: Invalid ID")
|
return fmt.Errorf("Unsubscribe: Invalid ID")
|
||||||
}
|
}
|
||||||
|
|
@ -586,8 +588,8 @@ func (w *Whisper) Unsubscribe(id string) error {
|
||||||
|
|
||||||
// Send injects a message into the whisper send queue, to be distributed in the
|
// Send injects a message into the whisper send queue, to be distributed in the
|
||||||
// network in the coming cycles.
|
// network in the coming cycles.
|
||||||
func (w *Whisper) Send(envelope *Envelope) error {
|
func (whisper *Whisper) Send(envelope *Envelope) error {
|
||||||
ok, err := w.add(envelope)
|
ok, err := whisper.add(envelope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -599,13 +601,13 @@ func (w *Whisper) Send(envelope *Envelope) error {
|
||||||
|
|
||||||
// Start implements node.Service, starting the background data propagation thread
|
// Start implements node.Service, starting the background data propagation thread
|
||||||
// of the Whisper protocol.
|
// of the Whisper protocol.
|
||||||
func (w *Whisper) Start(*p2p.Server) error {
|
func (whisper *Whisper) Start(*p2p.Server) error {
|
||||||
log.Info("started whisper v." + ProtocolVersionStr)
|
log.Info("started whisper v." + ProtocolVersionStr)
|
||||||
go w.update()
|
go whisper.update()
|
||||||
|
|
||||||
numCPU := runtime.NumCPU()
|
numCPU := runtime.NumCPU()
|
||||||
for i := 0; i < numCPU; i++ {
|
for i := 0; i < numCPU; i++ {
|
||||||
go w.processQueue()
|
go whisper.processQueue()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -613,26 +615,26 @@ func (w *Whisper) Start(*p2p.Server) error {
|
||||||
|
|
||||||
// Stop implements node.Service, stopping the background data propagation thread
|
// Stop implements node.Service, stopping the background data propagation thread
|
||||||
// of the Whisper protocol.
|
// of the Whisper protocol.
|
||||||
func (w *Whisper) Stop() error {
|
func (whisper *Whisper) Stop() error {
|
||||||
close(w.quit)
|
close(whisper.quit)
|
||||||
log.Info("whisper stopped")
|
log.Info("whisper stopped")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandlePeer is called by the underlying P2P layer when the whisper sub-protocol
|
// HandlePeer is called by the underlying P2P layer when the whisper sub-protocol
|
||||||
// connection is negotiated.
|
// connection is negotiated.
|
||||||
func (wh *Whisper) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
func (whisper *Whisper) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||||
// Create the new peer and start tracking it
|
// Create the new peer and start tracking it
|
||||||
whisperPeer := newPeer(wh, peer, rw)
|
whisperPeer := newPeer(whisper, peer, rw)
|
||||||
|
|
||||||
wh.peerMu.Lock()
|
whisper.peerMu.Lock()
|
||||||
wh.peers[whisperPeer] = struct{}{}
|
whisper.peers[whisperPeer] = struct{}{}
|
||||||
wh.peerMu.Unlock()
|
whisper.peerMu.Unlock()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
wh.peerMu.Lock()
|
whisper.peerMu.Lock()
|
||||||
delete(wh.peers, whisperPeer)
|
delete(whisper.peers, whisperPeer)
|
||||||
wh.peerMu.Unlock()
|
whisper.peerMu.Unlock()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Run the peer handshake and state updates
|
// Run the peer handshake and state updates
|
||||||
|
|
@ -642,11 +644,11 @@ func (wh *Whisper) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
|
||||||
whisperPeer.start()
|
whisperPeer.start()
|
||||||
defer whisperPeer.stop()
|
defer whisperPeer.stop()
|
||||||
|
|
||||||
return wh.runMessageLoop(whisperPeer, rw)
|
return whisper.runMessageLoop(whisperPeer, rw)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runMessageLoop reads and processes inbound messages directly to merge into client-global state.
|
// runMessageLoop reads and processes inbound messages directly to merge into client-global state.
|
||||||
func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
||||||
for {
|
for {
|
||||||
// fetch the next packet
|
// fetch the next packet
|
||||||
packet, err := rw.ReadMsg()
|
packet, err := rw.ReadMsg()
|
||||||
|
|
@ -654,7 +656,7 @@ func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
||||||
log.Warn("message loop", "peer", p.peer.ID(), "err", err)
|
log.Warn("message loop", "peer", p.peer.ID(), "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if packet.Size > wh.MaxMessageSize() {
|
if packet.Size > whisper.MaxMessageSize() {
|
||||||
log.Warn("oversized message received", "peer", p.peer.ID())
|
log.Warn("oversized message received", "peer", p.peer.ID())
|
||||||
return errors.New("oversized message received")
|
return errors.New("oversized message received")
|
||||||
}
|
}
|
||||||
|
|
@ -673,7 +675,7 @@ func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
||||||
|
|
||||||
trouble := false
|
trouble := false
|
||||||
for _, env := range envelopes {
|
for _, env := range envelopes {
|
||||||
cached, err := wh.add(env)
|
cached, err := whisper.add(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
trouble = true
|
trouble = true
|
||||||
log.Error("bad envelope received, peer will be disconnected", "peer", p.peer.ID(), "err", err)
|
log.Error("bad envelope received, peer will be disconnected", "peer", p.peer.ID(), "err", err)
|
||||||
|
|
@ -726,17 +728,17 @@ func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
||||||
log.Warn("failed to decode direct message, peer will be disconnected", "peer", p.peer.ID(), "err", err)
|
log.Warn("failed to decode direct message, peer will be disconnected", "peer", p.peer.ID(), "err", err)
|
||||||
return errors.New("invalid direct message")
|
return errors.New("invalid direct message")
|
||||||
}
|
}
|
||||||
wh.postEvent(&envelope, true)
|
whisper.postEvent(&envelope, true)
|
||||||
}
|
}
|
||||||
case p2pRequestCode:
|
case p2pRequestCode:
|
||||||
// Must be processed if mail server is implemented. Otherwise ignore.
|
// Must be processed if mail server is implemented. Otherwise ignore.
|
||||||
if wh.mailServer != nil {
|
if whisper.mailServer != nil {
|
||||||
var request Envelope
|
var request Envelope
|
||||||
if err := packet.Decode(&request); err != nil {
|
if err := packet.Decode(&request); err != nil {
|
||||||
log.Warn("failed to decode p2p request message, peer will be disconnected", "peer", p.peer.ID(), "err", err)
|
log.Warn("failed to decode p2p request message, peer will be disconnected", "peer", p.peer.ID(), "err", err)
|
||||||
return errors.New("invalid p2p request")
|
return errors.New("invalid p2p request")
|
||||||
}
|
}
|
||||||
wh.mailServer.DeliverMail(p, &request)
|
whisper.mailServer.DeliverMail(p, &request)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// New message types might be implemented in the future versions of Whisper.
|
// New message types might be implemented in the future versions of Whisper.
|
||||||
|
|
@ -750,128 +752,126 @@ func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error {
|
||||||
// add inserts a new envelope into the message pool to be distributed within the
|
// add inserts a new envelope into the message pool to be distributed within the
|
||||||
// whisper network. It also inserts the envelope into the expiration pool at the
|
// whisper network. It also inserts the envelope into the expiration pool at the
|
||||||
// appropriate time-stamp. In case of error, connection should be dropped.
|
// appropriate time-stamp. In case of error, connection should be dropped.
|
||||||
func (wh *Whisper) add(envelope *Envelope) (bool, error) {
|
func (whisper *Whisper) add(envelope *Envelope) (bool, error) {
|
||||||
now := uint32(time.Now().Unix())
|
now := uint32(time.Now().Unix())
|
||||||
sent := envelope.Expiry - envelope.TTL
|
sent := envelope.Expiry - envelope.TTL
|
||||||
|
|
||||||
if sent > now {
|
if sent > now {
|
||||||
if sent-DefaultSyncAllowance > now {
|
if sent-DefaultSyncAllowance > now {
|
||||||
return false, fmt.Errorf("envelope created in the future [%x]", envelope.Hash())
|
return false, fmt.Errorf("envelope created in the future [%x]", envelope.Hash())
|
||||||
} else {
|
}
|
||||||
// recalculate PoW, adjusted for the time difference, plus one second for latency
|
// recalculate PoW, adjusted for the time difference, plus one second for latency
|
||||||
envelope.calculatePoW(sent - now + 1)
|
envelope.calculatePoW(sent - now + 1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if envelope.Expiry < now {
|
if envelope.Expiry < now {
|
||||||
if envelope.Expiry+DefaultSyncAllowance*2 < now {
|
if envelope.Expiry+DefaultSyncAllowance*2 < now {
|
||||||
return false, fmt.Errorf("very old message")
|
return false, fmt.Errorf("very old message")
|
||||||
} else {
|
}
|
||||||
log.Debug("expired envelope dropped", "hash", envelope.Hash().Hex())
|
log.Debug("expired envelope dropped", "hash", envelope.Hash().Hex())
|
||||||
return false, nil // drop envelope without error
|
return false, nil // drop envelope without error
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if uint32(envelope.size()) > wh.MaxMessageSize() {
|
if uint32(envelope.size()) > whisper.MaxMessageSize() {
|
||||||
return false, fmt.Errorf("huge messages are not allowed [%x]", envelope.Hash())
|
return false, fmt.Errorf("huge messages are not allowed [%x]", envelope.Hash())
|
||||||
}
|
}
|
||||||
|
|
||||||
if envelope.PoW() < wh.MinPow() {
|
if envelope.PoW() < whisper.MinPow() {
|
||||||
// maybe the value was recently changed, and the peers did not adjust yet.
|
// maybe the value was recently changed, and the peers did not adjust yet.
|
||||||
// in this case the previous value is retrieved by MinPowTolerance()
|
// in this case the previous value is retrieved by MinPowTolerance()
|
||||||
// for a short period of peer synchronization.
|
// for a short period of peer synchronization.
|
||||||
if envelope.PoW() < wh.MinPowTolerance() {
|
if envelope.PoW() < whisper.MinPowTolerance() {
|
||||||
return false, fmt.Errorf("envelope with low PoW received: PoW=%f, hash=[%v]", envelope.PoW(), envelope.Hash().Hex())
|
return false, fmt.Errorf("envelope with low PoW received: PoW=%f, hash=[%v]", envelope.PoW(), envelope.Hash().Hex())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bloomFilterMatch(wh.BloomFilter(), envelope.Bloom()) {
|
if !bloomFilterMatch(whisper.BloomFilter(), envelope.Bloom()) {
|
||||||
// maybe the value was recently changed, and the peers did not adjust yet.
|
// maybe the value was recently changed, and the peers did not adjust yet.
|
||||||
// in this case the previous value is retrieved by BloomFilterTolerance()
|
// in this case the previous value is retrieved by BloomFilterTolerance()
|
||||||
// for a short period of peer synchronization.
|
// for a short period of peer synchronization.
|
||||||
if !bloomFilterMatch(wh.BloomFilterTolerance(), envelope.Bloom()) {
|
if !bloomFilterMatch(whisper.BloomFilterTolerance(), envelope.Bloom()) {
|
||||||
return false, fmt.Errorf("envelope does not match bloom filter, hash=[%v], bloom: \n%x \n%x \n%x",
|
return false, fmt.Errorf("envelope does not match bloom filter, hash=[%v], bloom: \n%x \n%x \n%x",
|
||||||
envelope.Hash().Hex(), wh.BloomFilter(), envelope.Bloom(), envelope.Topic)
|
envelope.Hash().Hex(), whisper.BloomFilter(), envelope.Bloom(), envelope.Topic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hash := envelope.Hash()
|
hash := envelope.Hash()
|
||||||
|
|
||||||
wh.poolMu.Lock()
|
whisper.poolMu.Lock()
|
||||||
_, alreadyCached := wh.envelopes[hash]
|
_, alreadyCached := whisper.envelopes[hash]
|
||||||
if !alreadyCached {
|
if !alreadyCached {
|
||||||
wh.envelopes[hash] = envelope
|
whisper.envelopes[hash] = envelope
|
||||||
if wh.expirations[envelope.Expiry] == nil {
|
if whisper.expirations[envelope.Expiry] == nil {
|
||||||
wh.expirations[envelope.Expiry] = set.NewNonTS()
|
whisper.expirations[envelope.Expiry] = set.NewNonTS()
|
||||||
}
|
}
|
||||||
if !wh.expirations[envelope.Expiry].Has(hash) {
|
if !whisper.expirations[envelope.Expiry].Has(hash) {
|
||||||
wh.expirations[envelope.Expiry].Add(hash)
|
whisper.expirations[envelope.Expiry].Add(hash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wh.poolMu.Unlock()
|
whisper.poolMu.Unlock()
|
||||||
|
|
||||||
if alreadyCached {
|
if alreadyCached {
|
||||||
log.Trace("whisper envelope already cached", "hash", envelope.Hash().Hex())
|
log.Trace("whisper envelope already cached", "hash", envelope.Hash().Hex())
|
||||||
} else {
|
} else {
|
||||||
log.Trace("cached whisper envelope", "hash", envelope.Hash().Hex())
|
log.Trace("cached whisper envelope", "hash", envelope.Hash().Hex())
|
||||||
wh.statsMu.Lock()
|
whisper.statsMu.Lock()
|
||||||
wh.stats.memoryUsed += envelope.size()
|
whisper.stats.memoryUsed += envelope.size()
|
||||||
wh.statsMu.Unlock()
|
whisper.statsMu.Unlock()
|
||||||
wh.postEvent(envelope, false) // notify the local node about the new message
|
whisper.postEvent(envelope, false) // notify the local node about the new message
|
||||||
if wh.mailServer != nil {
|
if whisper.mailServer != nil {
|
||||||
wh.mailServer.Archive(envelope)
|
whisper.mailServer.Archive(envelope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// postEvent queues the message for further processing.
|
// postEvent queues the message for further processing.
|
||||||
func (w *Whisper) postEvent(envelope *Envelope, isP2P bool) {
|
func (whisper *Whisper) postEvent(envelope *Envelope, isP2P bool) {
|
||||||
if isP2P {
|
if isP2P {
|
||||||
w.p2pMsgQueue <- envelope
|
whisper.p2pMsgQueue <- envelope
|
||||||
} else {
|
} else {
|
||||||
w.checkOverflow()
|
whisper.checkOverflow()
|
||||||
w.messageQueue <- envelope
|
whisper.messageQueue <- envelope
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkOverflow checks if message queue overflow occurs and reports it if necessary.
|
// checkOverflow checks if message queue overflow occurs and reports it if necessary.
|
||||||
func (w *Whisper) checkOverflow() {
|
func (whisper *Whisper) checkOverflow() {
|
||||||
queueSize := len(w.messageQueue)
|
queueSize := len(whisper.messageQueue)
|
||||||
|
|
||||||
if queueSize == messageQueueLimit {
|
if queueSize == messageQueueLimit {
|
||||||
if !w.Overflow() {
|
if !whisper.Overflow() {
|
||||||
w.settings.Store(overflowIdx, true)
|
whisper.settings.Store(overflowIdx, true)
|
||||||
log.Warn("message queue overflow")
|
log.Warn("message queue overflow")
|
||||||
}
|
}
|
||||||
} else if queueSize <= messageQueueLimit/2 {
|
} else if queueSize <= messageQueueLimit/2 {
|
||||||
if w.Overflow() {
|
if whisper.Overflow() {
|
||||||
w.settings.Store(overflowIdx, false)
|
whisper.settings.Store(overflowIdx, false)
|
||||||
log.Warn("message queue overflow fixed (back to normal)")
|
log.Warn("message queue overflow fixed (back to normal)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// processQueue delivers the messages to the watchers during the lifetime of the whisper node.
|
// processQueue delivers the messages to the watchers during the lifetime of the whisper node.
|
||||||
func (w *Whisper) processQueue() {
|
func (whisper *Whisper) processQueue() {
|
||||||
var e *Envelope
|
var e *Envelope
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-w.quit:
|
case <-whisper.quit:
|
||||||
return
|
return
|
||||||
|
|
||||||
case e = <-w.messageQueue:
|
case e = <-whisper.messageQueue:
|
||||||
w.filters.NotifyWatchers(e, false)
|
whisper.filters.NotifyWatchers(e, false)
|
||||||
|
|
||||||
case e = <-w.p2pMsgQueue:
|
case e = <-whisper.p2pMsgQueue:
|
||||||
w.filters.NotifyWatchers(e, true)
|
whisper.filters.NotifyWatchers(e, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update loops until the lifetime of the whisper node, updating its internal
|
// update loops until the lifetime of the whisper node, updating its internal
|
||||||
// state by expiring stale messages from the pool.
|
// state by expiring stale messages from the pool.
|
||||||
func (w *Whisper) update() {
|
func (whisper *Whisper) update() {
|
||||||
// Start a ticker to check for expirations
|
// Start a ticker to check for expirations
|
||||||
expire := time.NewTicker(expirationCycle)
|
expire := time.NewTicker(expirationCycle)
|
||||||
|
|
||||||
|
|
@ -879,9 +879,9 @@ func (w *Whisper) update() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-expire.C:
|
case <-expire.C:
|
||||||
w.expire()
|
whisper.expire()
|
||||||
|
|
||||||
case <-w.quit:
|
case <-whisper.quit:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -889,46 +889,46 @@ func (w *Whisper) update() {
|
||||||
|
|
||||||
// expire iterates over all the expiration timestamps, removing all stale
|
// expire iterates over all the expiration timestamps, removing all stale
|
||||||
// messages from the pools.
|
// messages from the pools.
|
||||||
func (w *Whisper) expire() {
|
func (whisper *Whisper) expire() {
|
||||||
w.poolMu.Lock()
|
whisper.poolMu.Lock()
|
||||||
defer w.poolMu.Unlock()
|
defer whisper.poolMu.Unlock()
|
||||||
|
|
||||||
w.statsMu.Lock()
|
whisper.statsMu.Lock()
|
||||||
defer w.statsMu.Unlock()
|
defer whisper.statsMu.Unlock()
|
||||||
w.stats.reset()
|
whisper.stats.reset()
|
||||||
now := uint32(time.Now().Unix())
|
now := uint32(time.Now().Unix())
|
||||||
for expiry, hashSet := range w.expirations {
|
for expiry, hashSet := range whisper.expirations {
|
||||||
if expiry < now {
|
if expiry < now {
|
||||||
// Dump all expired messages and remove timestamp
|
// Dump all expired messages and remove timestamp
|
||||||
hashSet.Each(func(v interface{}) bool {
|
hashSet.Each(func(v interface{}) bool {
|
||||||
sz := w.envelopes[v.(common.Hash)].size()
|
sz := whisper.envelopes[v.(common.Hash)].size()
|
||||||
delete(w.envelopes, v.(common.Hash))
|
delete(whisper.envelopes, v.(common.Hash))
|
||||||
w.stats.messagesCleared++
|
whisper.stats.messagesCleared++
|
||||||
w.stats.memoryCleared += sz
|
whisper.stats.memoryCleared += sz
|
||||||
w.stats.memoryUsed -= sz
|
whisper.stats.memoryUsed -= sz
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
w.expirations[expiry].Clear()
|
whisper.expirations[expiry].Clear()
|
||||||
delete(w.expirations, expiry)
|
delete(whisper.expirations, expiry)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stats returns the whisper node statistics.
|
// Stats returns the whisper node statistics.
|
||||||
func (w *Whisper) Stats() Statistics {
|
func (whisper *Whisper) Stats() Statistics {
|
||||||
w.statsMu.Lock()
|
whisper.statsMu.Lock()
|
||||||
defer w.statsMu.Unlock()
|
defer whisper.statsMu.Unlock()
|
||||||
|
|
||||||
return w.stats
|
return whisper.stats
|
||||||
}
|
}
|
||||||
|
|
||||||
// Envelopes retrieves all the messages currently pooled by the node.
|
// Envelopes retrieves all the messages currently pooled by the node.
|
||||||
func (w *Whisper) Envelopes() []*Envelope {
|
func (whisper *Whisper) Envelopes() []*Envelope {
|
||||||
w.poolMu.RLock()
|
whisper.poolMu.RLock()
|
||||||
defer w.poolMu.RUnlock()
|
defer whisper.poolMu.RUnlock()
|
||||||
|
|
||||||
all := make([]*Envelope, 0, len(w.envelopes))
|
all := make([]*Envelope, 0, len(whisper.envelopes))
|
||||||
for _, envelope := range w.envelopes {
|
for _, envelope := range whisper.envelopes {
|
||||||
all = append(all, envelope)
|
all = append(all, envelope)
|
||||||
}
|
}
|
||||||
return all
|
return all
|
||||||
|
|
@ -936,13 +936,13 @@ func (w *Whisper) Envelopes() []*Envelope {
|
||||||
|
|
||||||
// Messages iterates through all currently floating envelopes
|
// Messages iterates through all currently floating envelopes
|
||||||
// and retrieves all the messages, that this filter could decrypt.
|
// and retrieves all the messages, that this filter could decrypt.
|
||||||
func (w *Whisper) Messages(id string) []*ReceivedMessage {
|
func (whisper *Whisper) Messages(id string) []*ReceivedMessage {
|
||||||
result := make([]*ReceivedMessage, 0)
|
result := make([]*ReceivedMessage, 0)
|
||||||
w.poolMu.RLock()
|
whisper.poolMu.RLock()
|
||||||
defer w.poolMu.RUnlock()
|
defer whisper.poolMu.RUnlock()
|
||||||
|
|
||||||
if filter := w.filters.Get(id); filter != nil {
|
if filter := whisper.filters.Get(id); filter != nil {
|
||||||
for _, env := range w.envelopes {
|
for _, env := range whisper.envelopes {
|
||||||
msg := filter.processEnvelope(env)
|
msg := filter.processEnvelope(env)
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
result = append(result, msg)
|
result = append(result, msg)
|
||||||
|
|
@ -953,11 +953,11 @@ func (w *Whisper) Messages(id string) []*ReceivedMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
// isEnvelopeCached checks if envelope with specific hash has already been received and cached.
|
// isEnvelopeCached checks if envelope with specific hash has already been received and cached.
|
||||||
func (w *Whisper) isEnvelopeCached(hash common.Hash) bool {
|
func (whisper *Whisper) isEnvelopeCached(hash common.Hash) bool {
|
||||||
w.poolMu.Lock()
|
whisper.poolMu.Lock()
|
||||||
defer w.poolMu.Unlock()
|
defer whisper.poolMu.Unlock()
|
||||||
|
|
||||||
_, exist := w.envelopes[hash]
|
_, exist := whisper.envelopes[hash]
|
||||||
return exist
|
return exist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1019,7 +1019,7 @@ func BytesToUintBigEndian(b []byte) (res uint64) {
|
||||||
|
|
||||||
// GenerateRandomID generates a random string, which is then returned to be used as a key id
|
// GenerateRandomID generates a random string, which is then returned to be used as a key id
|
||||||
func GenerateRandomID() (id string, err error) {
|
func GenerateRandomID() (id string, err error) {
|
||||||
buf := make([]byte, keyIdSize)
|
buf := make([]byte, keyIDSize)
|
||||||
_, err = crand.Read(buf)
|
_, err = crand.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue