Skip to content

Commit

Permalink
Merge pull request ethereum#14540 from bas-vk/whisper-api
Browse files Browse the repository at this point in the history
whisperv5: integrate whisper and implement API
  • Loading branch information
karalabe authored Jun 26, 2017
2 parents f321ed2 + ea1d182 commit feb2932
Show file tree
Hide file tree
Showing 31 changed files with 1,664 additions and 1,217 deletions.
33 changes: 27 additions & 6 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
"github.com/naoina/toml"
)

Expand All @@ -42,7 +43,7 @@ var (
Name: "dumpconfig",
Usage: "Show configuration values",
ArgsUsage: "",
Flags: append(nodeFlags, rpcFlags...),
Flags: append(append(nodeFlags, rpcFlags...), whisperFlags...),
Category: "MISCELLANEOUS COMMANDS",
Description: `The dumpconfig command shows configuration values.`,
}
Expand Down Expand Up @@ -76,6 +77,7 @@ type ethstatsConfig struct {

type gethConfig struct {
Eth eth.Config
Shh whisper.Config
Node node.Config
Ethstats ethstatsConfig
}
Expand All @@ -99,8 +101,8 @@ func defaultNodeConfig() node.Config {
cfg := node.DefaultConfig
cfg.Name = clientIdentifier
cfg.Version = params.VersionWithCommit(gitCommit)
cfg.HTTPModules = append(cfg.HTTPModules, "eth")
cfg.WSModules = append(cfg.WSModules, "eth")
cfg.HTTPModules = append(cfg.HTTPModules, "eth", "shh")
cfg.WSModules = append(cfg.WSModules, "eth", "shh")
cfg.IPCPath = "geth.ipc"
return cfg
}
Expand All @@ -109,6 +111,7 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
// Load defaults.
cfg := gethConfig{
Eth: eth.DefaultConfig,
Shh: whisper.DefaultConfig,
Node: defaultNodeConfig(),
}

Expand All @@ -130,19 +133,37 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
cfg.Ethstats.URL = ctx.GlobalString(utils.EthStatsURLFlag.Name)
}

utils.SetShhConfig(ctx, stack, &cfg.Shh)

return stack, cfg
}

// enableWhisper returns true in case one of the whisper flags is set.
func enableWhisper(ctx *cli.Context) bool {
for _, flag := range whisperFlags {
if ctx.GlobalIsSet(flag.GetName()) {
return true
}
}
return false
}

func makeFullNode(ctx *cli.Context) *node.Node {
stack, cfg := makeConfigNode(ctx)

utils.RegisterEthService(stack, &cfg.Eth)

// Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
shhEnabled := enableWhisper(ctx)
shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
if shhEnabled || shhAutoEnabled {
utils.RegisterShhService(stack)
if ctx.GlobalIsSet(utils.WhisperMaxMessageSizeFlag.Name) {
cfg.Shh.MaxMessageSize = uint32(ctx.Int(utils.WhisperMaxMessageSizeFlag.Name))
}
if ctx.GlobalIsSet(utils.WhisperMinPOWFlag.Name) {
cfg.Shh.MinimumAcceptedPOW = ctx.Float64(utils.WhisperMinPOWFlag.Name)
}
utils.RegisterShhService(stack, &cfg.Shh)
}

// Add the Ethereum Stats daemon if requested.
Expand Down
2 changes: 1 addition & 1 deletion cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
Action: utils.MigrateFlags(localConsole),
Name: "console",
Usage: "Start an interactive JavaScript environment",
Flags: append(append(nodeFlags, rpcFlags...), consoleFlags...),
Flags: append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...),
Category: "CONSOLE COMMANDS",
Description: `
The Geth console is an interactive shell for the JavaScript runtime environment
Expand Down
8 changes: 7 additions & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ var (
utils.NetrestrictFlag,
utils.NodeKeyFileFlag,
utils.NodeKeyHexFlag,
utils.WhisperEnabledFlag,
utils.DevModeFlag,
utils.TestnetFlag,
utils.RinkebyFlag,
Expand Down Expand Up @@ -125,6 +124,12 @@ var (
utils.IPCDisabledFlag,
utils.IPCPathFlag,
}

whisperFlags = []cli.Flag{
utils.WhisperEnabledFlag,
utils.WhisperMaxMessageSizeFlag,
utils.WhisperMinPOWFlag,
}
)

func init() {
Expand Down Expand Up @@ -161,6 +166,7 @@ func init() {
app.Flags = append(app.Flags, rpcFlags...)
app.Flags = append(app.Flags, consoleFlags...)
app.Flags = append(app.Flags, debug.Flags...)
app.Flags = append(app.Flags, whisperFlags...)

app.Before = func(ctx *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU())
Expand Down
9 changes: 5 additions & 4 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ var AppHelpFlagGroups = []flagGroup{
utils.NoCompactionFlag,
}, debug.Flags...),
},
{
Name: "WHISPER (EXPERIMENTAL)",
Flags: whisperFlags,
},
{
Name: "DEPRECATED",
Flags: []cli.Flag{
Expand All @@ -195,10 +199,7 @@ var AppHelpFlagGroups = []flagGroup{
},
},
{
Name: "EXPERIMENTAL",
Flags: []cli.Flag{
utils.WhisperEnabledFlag,
},
Name: "MISC",
},
}

Expand Down
35 changes: 28 additions & 7 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,6 @@ var (
Usage: "Restricts network communication to the given IP networks (CIDR masks)",
}

WhisperEnabledFlag = cli.BoolFlag{
Name: "shh",
Usage: "Enable Whisper",
}

// ATM the url is left to the user and deployment to
JSpathFlag = cli.StringFlag{
Name: "jspath",
Expand All @@ -463,6 +458,20 @@ var (
Usage: "Suggested gas price is the given percentile of a set of recent transaction gas prices",
Value: eth.DefaultConfig.GPO.Percentile,
}
WhisperEnabledFlag = cli.BoolFlag{
Name: "shh",
Usage: "Enable Whisper",
}
WhisperMaxMessageSizeFlag = cli.IntFlag{
Name: "shh.maxmessagesize",
Usage: "Max message size accepted",
Value: int(whisper.DefaultMaxMessageSize),
}
WhisperMinPOWFlag = cli.Float64Flag{
Name: "shh.pow",
Usage: "Minimum POW accepted",
Value: whisper.DefaultMinimumPoW,
}
)

// MakeDataDir retrieves the currently requested data directory, terminating
Expand Down Expand Up @@ -878,6 +887,16 @@ func checkExclusive(ctx *cli.Context, flags ...cli.Flag) {
}
}

// SetShhConfig applies shh-related command line flags to the config.
func SetShhConfig(ctx *cli.Context, stack *node.Node, cfg *whisper.Config) {
if ctx.GlobalIsSet(WhisperMaxMessageSizeFlag.Name) {
cfg.MaxMessageSize = uint32(ctx.GlobalUint(WhisperMaxMessageSizeFlag.Name))
}
if ctx.GlobalIsSet(WhisperMinPOWFlag.Name) {
cfg.MinimumAcceptedPOW = ctx.GlobalFloat64(WhisperMinPOWFlag.Name)
}
}

// SetEthConfig applies eth-related command line flags to the config.
func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
// Avoid conflicting network flags
Expand Down Expand Up @@ -983,8 +1002,10 @@ func RegisterEthService(stack *node.Node, cfg *eth.Config) {
}

// RegisterShhService configures Whisper and adds it to the given node.
func RegisterShhService(stack *node.Node) {
if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
func RegisterShhService(stack *node.Node, cfg *whisper.Config) {
if err := stack.Register(func(n *node.ServiceContext) (node.Service, error) {
return whisper.New(cfg), nil
}); err != nil {
Fatalf("Failed to register the Whisper service: %v", err)
}
}
Expand Down
16 changes: 11 additions & 5 deletions cmd/wnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var (
argVerbosity = flag.Int("verbosity", int(log.LvlError), "log verbosity level")
argTTL = flag.Uint("ttl", 30, "time-to-live for messages in seconds")
argWorkTime = flag.Uint("work", 5, "work time in seconds")
argMaxSize = flag.Int("maxsize", whisper.DefaultMaxMessageLength, "max size of message")
argMaxSize = flag.Uint("maxsize", uint(whisper.DefaultMaxMessageSize), "max size of message")
argPoW = flag.Float64("pow", whisper.DefaultMinimumPoW, "PoW for normal messages in float format (e.g. 2.7)")
argServerPoW = flag.Float64("mspow", whisper.DefaultMinimumPoW, "PoW requirement for Mail Server request")

Expand Down Expand Up @@ -198,18 +198,24 @@ func initialize() {
peers = append(peers, peer)
}

cfg := &whisper.Config{
MaxMessageSize: uint32(*argMaxSize),
MinimumAcceptedPOW: *argPoW,
}

if *mailServerMode {
if len(msPassword) == 0 {
msPassword, err = console.Stdin.PromptPassword("Please enter the Mail Server password: ")
if err != nil {
utils.Fatalf("Failed to read Mail Server password: %s", err)
}
}
shh = whisper.New()

shh = whisper.New(cfg)
shh.RegisterServer(&mailServer)
mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW)
} else {
shh = whisper.New()
shh = whisper.New(cfg)
}

if *argPoW != whisper.DefaultMinimumPoW {
Expand All @@ -219,8 +225,8 @@ func initialize() {
}
}

if *argMaxSize != whisper.DefaultMaxMessageLength {
err := shh.SetMaxMessageLength(*argMaxSize)
if uint32(*argMaxSize) != whisper.DefaultMaxMessageSize {
err := shh.SetMaxMessageSize(uint32(*argMaxSize))
if err != nil {
utils.Fatalf("Failed to set max message size: %s", err)
}
Expand Down
62 changes: 36 additions & 26 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,6 @@ const Shh_JS = `
web3._extend({
property: 'shh',
methods: [
new web3._extend.Method({
name: 'info',
call: 'shh_info'
}),
new web3._extend.Method({
name: 'setMaxMessageLength',
call: 'shh_setMaxMessageLength',
Expand All @@ -541,8 +537,8 @@ web3._extend({
params: 1
}),
new web3._extend.Method({
name: 'allowP2PMessagesFromPeer',
call: 'shh_allowP2PMessagesFromPeer',
name: 'markTrustedPeer',
call: 'shh_markTrustedPeer',
params: 1
}),
new web3._extend.Method({
Expand Down Expand Up @@ -570,57 +566,67 @@ web3._extend({
params: 1
}),
new web3._extend.Method({
name: 'generateSymmetricKey',
call: 'shh_generateSymmetricKey',
name: 'newSymKey',
call: 'shh_newSymKey',
}),
new web3._extend.Method({
name: 'addSymmetricKeyDirect',
call: 'shh_addSymmetricKeyDirect',
name: 'addSymKey',
call: 'shh_addSymKey',
params: 1
}),
new web3._extend.Method({
name: 'addSymmetricKeyFromPassword',
call: 'shh_addSymmetricKeyFromPassword',
name: 'generateSymKeyFromPassword',
call: 'shh_generateSymKeyFromPassword',
params: 1
}),
new web3._extend.Method({
name: 'hasSymmetricKey',
call: 'shh_hasSymmetricKey',
name: 'hasSymKey',
call: 'shh_hasSymKey',
params: 1
}),
new web3._extend.Method({
name: 'getSymmetricKey',
call: 'shh_getSymmetricKey',
name: 'getSymKey',
call: 'shh_getSymKey',
params: 1
}),
new web3._extend.Method({
name: 'deleteSymmetricKey',
call: 'shh_deleteSymmetricKey',
name: 'deleteSymKey',
call: 'shh_deleteSymKey',
params: 1
}),
new web3._extend.Method({
name: 'subscribe',
call: 'shh_subscribe',
params: 1
params: 2
}),
new web3._extend.Method({
name: 'unsubscribe',
call: 'shh_unsubscribe',
params: 1
}),
new web3._extend.Method({
name: 'getNewSubscriptionMessages',
call: 'shh_getNewSubscriptionMessages',
name: 'post',
call: 'shh_post',
params: 1
}),
new web3._extend.Method({
name: 'getFloatingMessages',
call: 'shh_getFloatingMessages',
name: 'publicKey',
call: 'shh_getPublicKey',
params: 1
}),
new web3._extend.Method({
name: 'post',
call: 'shh_post',
name: 'getFilterMessages',
call: 'shh_getFilterMessages',
params: 1
}),
new web3._extend.Method({
name: 'deleteMessageFilter',
call: 'shh_deleteMessageFilter',
params: 1
}),
new web3._extend.Method({
name: 'newMessageFilter',
call: 'shh_newMessageFilter',
params: 1
})
],
Expand All @@ -630,7 +636,11 @@ web3._extend({
name: 'version',
getter: 'shh_version',
outputFormatter: web3._extend.utils.toDecimal
})
}),
new web3._extend.Property({
name: 'info',
getter: 'shh_info'
}),
]
});
`
Expand Down
4 changes: 3 additions & 1 deletion mobile/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
}
// Register the Whisper protocol if requested
if config.WhisperEnabled {
if err := rawStack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
if err := rawStack.Register(func(*node.ServiceContext) (node.Service, error) {
return whisper.New(&whisper.DefaultConfig), nil
}); err != nil {
return nil, fmt.Errorf("whisper init: %v", err)
}
}
Expand Down
Loading

0 comments on commit feb2932

Please sign in to comment.