diff --git a/cmd/swarm/global-store/main.go b/cmd/swarm/global-store/main.go
new file mode 100644
index 0000000000..1490307cbe
--- /dev/null
+++ b/cmd/swarm/global-store/main.go
@@ -0,0 +1,83 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of go-ethereum.
+//
+// go-ethereum is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// go-ethereum 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with go-ethereum. If not, see .
+
+package main
+
+import (
+ "os"
+
+ "github.com/ethereum/go-ethereum/cmd/utils"
+ "github.com/ethereum/go-ethereum/log"
+ cli "gopkg.in/urfave/cli.v1"
+)
+
+var gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
+
+func main() {
+ err := newApp().Run(os.Args)
+ if err != nil {
+ log.Error(err.Error())
+ os.Exit(1)
+ }
+}
+
+// newApp construct a new instance of Swarm Global Store.
+// Method Run is called on it in the main function and in tests.
+func newApp() (app *cli.App) {
+ app = utils.NewApp(gitCommit, "Swarm Global Store")
+
+ app.Name = "global-store"
+
+ // app flags (for all commands)
+ app.Flags = []cli.Flag{
+ cli.IntFlag{
+ Name: "verbosity",
+ Value: 3,
+ Usage: "verbosity level",
+ },
+ }
+
+ app.Commands = []cli.Command{
+ {
+ Name: "websocket",
+ Aliases: []string{"ws"},
+ Usage: "start swarm global store with websocket endpoint",
+ Action: startWS,
+ // Flags only for "start" command.
+ // Allow app flags to be specified after the
+ // command argument.
+ Flags: append(app.Flags,
+ cli.StringFlag{
+ Name: "dir",
+ Value: "",
+ Usage: "data directory",
+ },
+ cli.StringFlag{
+ Name: "endpoint",
+ Value: "0.0.0.0:3033",
+ Usage: "websocket endpoint",
+ },
+ cli.StringSliceFlag{
+ Name: "origins",
+ Value: &cli.StringSlice{"*"},
+ Usage: "websocket origins",
+ },
+ ),
+ },
+ }
+
+ return app
+}
diff --git a/cmd/swarm/global-store/run_test.go b/cmd/swarm/global-store/run_test.go
new file mode 100644
index 0000000000..d7ef626e55
--- /dev/null
+++ b/cmd/swarm/global-store/run_test.go
@@ -0,0 +1,49 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of go-ethereum.
+//
+// go-ethereum is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// go-ethereum 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with go-ethereum. If not, see .
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "testing"
+
+ "github.com/docker/docker/pkg/reexec"
+ "github.com/ethereum/go-ethereum/internal/cmdtest"
+)
+
+func init() {
+ reexec.Register("swarm-global-store", func() {
+ if err := newApp().Run(os.Args); err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(1)
+ }
+ os.Exit(0)
+ })
+}
+
+func runGlobalStore(t *testing.T, args ...string) *cmdtest.TestCmd {
+ tt := cmdtest.NewTestCmd(t, nil)
+ tt.Run("swarm-global-store", args...)
+ return tt
+}
+
+func TestMain(m *testing.M) {
+ if reexec.Init() {
+ return
+ }
+ os.Exit(m.Run())
+}
diff --git a/cmd/swarm/global-store/websocket.go b/cmd/swarm/global-store/websocket.go
new file mode 100644
index 0000000000..afe9b7826c
--- /dev/null
+++ b/cmd/swarm/global-store/websocket.go
@@ -0,0 +1,66 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of go-ethereum.
+//
+// go-ethereum is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// go-ethereum 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with go-ethereum. If not, see .
+
+package main
+
+import (
+ "net"
+ "net/http"
+ "os"
+
+ "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/rpc"
+ "github.com/ethereum/go-ethereum/swarm/storage/mock"
+ "github.com/ethereum/go-ethereum/swarm/storage/mock/db"
+ "github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
+ cli "gopkg.in/urfave/cli.v1"
+)
+
+func startWS(ctx *cli.Context) (err error) {
+ log.PrintOrigins(true)
+ log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(true))))
+
+ var globalStore mock.GlobalStorer
+ dir := ctx.String("dir")
+ if dir != "" {
+ dbStore, err := db.NewGlobalStore(dir)
+ if err != nil {
+ return err
+ }
+ defer dbStore.Close()
+ globalStore = dbStore
+ log.Info("database global store", "dir", dir)
+ } else {
+ globalStore = mem.NewGlobalStore()
+ log.Info("in-memory global store")
+ }
+
+ server := rpc.NewServer()
+ if err := server.RegisterName("mockStore", globalStore); err != nil {
+ return err
+ }
+
+ endpoint := ctx.String("endpoint")
+ listener, err := net.Listen("tcp", endpoint)
+ if err != nil {
+ return err
+ }
+ wsAddress := listener.Addr().String()
+ origins := ctx.StringSlice("origins")
+ log.Info("websocket", "address", wsAddress, "origins", origins)
+
+ return http.Serve(listener, server.WebsocketHandler(origins))
+}
diff --git a/cmd/swarm/global-store/websocket_test.go b/cmd/swarm/global-store/websocket_test.go
new file mode 100644
index 0000000000..c0a6ee1daa
--- /dev/null
+++ b/cmd/swarm/global-store/websocket_test.go
@@ -0,0 +1,101 @@
+// Copyright 2019 The go-ethereum Authors
+// This file is part of go-ethereum.
+//
+// go-ethereum is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// go-ethereum 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 General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with go-ethereum. If not, see .
+
+package main
+
+import (
+ "context"
+ "io/ioutil"
+ "net"
+ "os"
+ "testing"
+ "time"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/rpc"
+ mockRPC "github.com/ethereum/go-ethereum/swarm/storage/mock/rpc"
+)
+
+func TestWebsocket_InMemory(t *testing.T) {
+ testWebsocket(t, true)
+}
+
+func TestWebsocket_Database(t *testing.T) {
+ dir, err := ioutil.TempDir("", "swarm-global-store-")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(dir)
+
+ testWebsocket(t, true, "--dir", dir)
+
+ testWebsocket(t, false, "--dir", dir)
+}
+
+func testWebsocket(t *testing.T, put bool, args ...string) {
+ addr := findFreeTCPAddress(t)
+ testCmd := runGlobalStore(t, append([]string{"ws", "--endpoint", addr}, args...)...)
+ defer testCmd.Interrupt()
+
+ var client *rpc.Client
+ var err error
+ for i := 0; i < 1000; i++ {
+ client, err = rpc.DialWebsocket(context.Background(), "ws://"+addr, "")
+ if err == nil {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ store := mockRPC.NewGlobalStore(client)
+ defer store.Close()
+
+ node := store.NewNodeStore(common.HexToAddress("123abc"))
+
+ wantKey := "key"
+ wantValue := "value"
+
+ if put {
+ err = node.Put([]byte(wantKey), []byte(wantValue))
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ gotValue, err := node.Get([]byte(wantKey))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if string(gotValue) != wantValue {
+ t.Errorf("got value %s for key %s, want %s", string(gotValue), wantKey, wantValue)
+ }
+}
+
+func findFreeTCPAddress(t *testing.T) (addr string) {
+ t.Helper()
+
+ listener, err := net.Listen("tcp", "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer listener.Close()
+
+ return listener.Addr().String()
+}