2016-11-25 05:50:30 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/*
|
2017-02-23 00:22:43 +00:00
|
|
|
Package whisper implements the Whisper protocol (version 5).
|
2016-11-25 05:50:30 +00:00
|
|
|
|
|
|
|
Whisper combines aspects of both DHTs and datagram messaging systems (e.g. UDP).
|
|
|
|
As such it may be likened and compared to both, not dissimilar to the
|
|
|
|
matter/energy duality (apologies to physicists for the blatant abuse of a
|
|
|
|
fundamental and beautiful natural principle).
|
|
|
|
|
|
|
|
Whisper is a pure identity-based messaging system. Whisper provides a low-level
|
|
|
|
(non-application-specific) but easily-accessible API without being based upon
|
|
|
|
or prejudiced by the low-level hardware attributes and characteristics,
|
|
|
|
particularly the notion of singular endpoints.
|
|
|
|
*/
|
|
|
|
package whisperv5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2017-04-12 18:26:54 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2016-11-25 05:50:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
EnvelopeVersion = uint64(0)
|
|
|
|
ProtocolVersion = uint64(5)
|
|
|
|
ProtocolVersionStr = "5.0"
|
|
|
|
ProtocolName = "shh"
|
|
|
|
|
2017-02-23 00:22:43 +00:00
|
|
|
statusCode = 0 // used by whisper protocol
|
|
|
|
messagesCode = 1 // normal whisper message
|
|
|
|
p2pCode = 2 // peer-to-peer message (to be consumed by the peer, but not forwarded any further)
|
|
|
|
p2pRequestCode = 3 // peer-to-peer message, used by Dapp protocol
|
|
|
|
NumberOfMessageCodes = 64
|
2016-11-25 05:50:30 +00:00
|
|
|
|
|
|
|
paddingMask = byte(3)
|
|
|
|
signatureFlag = byte(4)
|
|
|
|
|
2017-05-16 07:42:30 +00:00
|
|
|
TopicLength = 4
|
|
|
|
signatureLength = 65
|
|
|
|
aesKeyLength = 32
|
|
|
|
AESNonceLength = 12
|
|
|
|
keyIdSize = 32
|
2016-11-25 05:50:30 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
MaxMessageSize = uint32(10 * 1024 * 1024) // maximum accepted size of a message.
|
|
|
|
DefaultMaxMessageSize = uint32(1024 * 1024)
|
|
|
|
DefaultMinimumPoW = 0.001
|
2016-11-25 05:50:30 +00:00
|
|
|
|
2017-05-16 07:42:30 +00:00
|
|
|
padSizeLimit = 256 // just an arbitrary number, could be changed without breaking the protocol (must not exceed 2^24)
|
2017-02-23 00:22:43 +00:00
|
|
|
messageQueueLimit = 1024
|
2016-11-25 05:50:30 +00:00
|
|
|
|
|
|
|
expirationCycle = time.Second
|
|
|
|
transmissionCycle = 300 * time.Millisecond
|
|
|
|
|
|
|
|
DefaultTTL = 50 // seconds
|
|
|
|
SynchAllowance = 10 // seconds
|
|
|
|
)
|
|
|
|
|
|
|
|
type unknownVersionError uint64
|
|
|
|
|
|
|
|
func (e unknownVersionError) Error() string {
|
|
|
|
return fmt.Sprintf("invalid envelope version %d", uint64(e))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MailServer represents a mail server, capable of
|
|
|
|
// archiving the old messages for subsequent delivery
|
|
|
|
// to the peers. Any implementation must ensure that both
|
|
|
|
// functions are thread-safe. Also, they must return ASAP.
|
|
|
|
// DeliverMail should use directMessagesCode for delivery,
|
|
|
|
// in order to bypass the expiry checks.
|
|
|
|
type MailServer interface {
|
|
|
|
Archive(env *Envelope)
|
2017-02-23 17:24:40 +00:00
|
|
|
DeliverMail(whisperPeer *Peer, request *Envelope)
|
2016-11-25 05:50:30 +00:00
|
|
|
}
|
2017-04-12 18:26:54 +00:00
|
|
|
|
|
|
|
// NotificationServer represents a notification server,
|
|
|
|
// capable of screening incoming envelopes for special
|
|
|
|
// topics, and once located, subscribe client nodes as
|
|
|
|
// recipients to notifications (push notifications atm)
|
|
|
|
type NotificationServer interface {
|
|
|
|
// Start initializes notification sending loop
|
|
|
|
Start(server *p2p.Server) error
|
|
|
|
|
|
|
|
// Stop stops notification sending loop, releasing related resources
|
|
|
|
Stop() error
|
|
|
|
}
|