Skip to content

Commit 9de944a

Browse files
janosdshulyak
authored andcommitted
cmd/swarm/global-store: global store cmd (ethereum#19014)
(cherry picked from commit 33d0a0e)
1 parent 571fc50 commit 9de944a

File tree

8 files changed

+476
-3
lines changed

8 files changed

+476
-3
lines changed

cmd/swarm/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const (
8282
SWARM_ENV_BOOTNODE_MODE = "SWARM_BOOTNODE_MODE"
8383
SWARM_ACCESS_PASSWORD = "SWARM_ACCESS_PASSWORD"
8484
SWARM_AUTO_DEFAULTPATH = "SWARM_AUTO_DEFAULTPATH"
85+
SWARM_GLOBALSTORE_API = "SWARM_GLOBALSTORE_API"
8586
GETH_ENV_DATADIR = "GETH_DATADIR"
8687
)
8788

@@ -262,6 +263,10 @@ func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Con
262263
currentConfig.BootnodeMode = ctx.GlobalBool(SwarmBootnodeModeFlag.Name)
263264
}
264265

266+
if ctx.GlobalIsSet(SwarmGlobalStoreAPIFlag.Name) {
267+
currentConfig.GlobalStoreAPI = ctx.GlobalString(SwarmGlobalStoreAPIFlag.Name)
268+
}
269+
265270
return currentConfig
266271

267272
}
@@ -375,6 +380,10 @@ func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) {
375380
currentConfig.BootnodeMode = bootnodeMode
376381
}
377382

383+
if api := os.Getenv(SWARM_GLOBALSTORE_API); api != "" {
384+
currentConfig.GlobalStoreAPI = api
385+
}
386+
378387
return currentConfig
379388
}
380389

cmd/swarm/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,9 @@ var (
176176
Name: "user",
177177
Usage: "Indicates the user who updates the feed",
178178
}
179+
SwarmGlobalStoreAPIFlag = cli.StringFlag{
180+
Name: "globalstore-api",
181+
Usage: "URL of the Global Store API provider (only for testing)",
182+
EnvVar: SWARM_GLOBALSTORE_API,
183+
}
179184
)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"net"
21+
"net/http"
22+
"os"
23+
24+
"github.com/ethereum/go-ethereum/log"
25+
"github.com/ethereum/go-ethereum/rpc"
26+
"github.com/ethereum/go-ethereum/swarm/storage/mock"
27+
"github.com/ethereum/go-ethereum/swarm/storage/mock/db"
28+
"github.com/ethereum/go-ethereum/swarm/storage/mock/mem"
29+
cli "gopkg.in/urfave/cli.v1"
30+
)
31+
32+
// startHTTP starts a global store with HTTP RPC server.
33+
// It is used for "http" cli command.
34+
func startHTTP(ctx *cli.Context) (err error) {
35+
server, cleanup, err := newServer(ctx)
36+
if err != nil {
37+
return err
38+
}
39+
defer cleanup()
40+
41+
listener, err := net.Listen("tcp", ctx.String("addr"))
42+
if err != nil {
43+
return err
44+
}
45+
log.Info("http", "address", listener.Addr().String())
46+
47+
return http.Serve(listener, server)
48+
}
49+
50+
// startWS starts a global store with WebSocket RPC server.
51+
// It is used for "websocket" cli command.
52+
func startWS(ctx *cli.Context) (err error) {
53+
server, cleanup, err := newServer(ctx)
54+
if err != nil {
55+
return err
56+
}
57+
defer cleanup()
58+
59+
listener, err := net.Listen("tcp", ctx.String("addr"))
60+
if err != nil {
61+
return err
62+
}
63+
origins := ctx.StringSlice("origins")
64+
log.Info("websocket", "address", listener.Addr().String(), "origins", origins)
65+
66+
return http.Serve(listener, server.WebsocketHandler(origins))
67+
}
68+
69+
// newServer creates a global store and returns its RPC server.
70+
// Returned cleanup function should be called only if err is nil.
71+
func newServer(ctx *cli.Context) (server *rpc.Server, cleanup func(), err error) {
72+
log.PrintOrigins(true)
73+
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(false))))
74+
75+
cleanup = func() {}
76+
var globalStore mock.GlobalStorer
77+
dir := ctx.String("dir")
78+
if dir != "" {
79+
dbStore, err := db.NewGlobalStore(dir)
80+
if err != nil {
81+
return nil, nil, err
82+
}
83+
cleanup = func() {
84+
dbStore.Close()
85+
}
86+
globalStore = dbStore
87+
log.Info("database global store", "dir", dir)
88+
} else {
89+
globalStore = mem.NewGlobalStore()
90+
log.Info("in-memory global store")
91+
}
92+
93+
server = rpc.NewServer()
94+
if err := server.RegisterName("mockStore", globalStore); err != nil {
95+
cleanup()
96+
return nil, nil, err
97+
}
98+
99+
return server, cleanup, nil
100+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Copyright 2019 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// go-ethereum is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"io/ioutil"
22+
"net"
23+
"net/http"
24+
"os"
25+
"testing"
26+
"time"
27+
28+
"github.com/ethereum/go-ethereum/common"
29+
"github.com/ethereum/go-ethereum/rpc"
30+
mockRPC "github.com/ethereum/go-ethereum/swarm/storage/mock/rpc"
31+
)
32+
33+
// TestHTTP_InMemory tests in-memory global store that exposes
34+
// HTTP server.
35+
func TestHTTP_InMemory(t *testing.T) {
36+
testHTTP(t, true)
37+
}
38+
39+
// TestHTTP_Database tests global store with persisted database
40+
// that exposes HTTP server.
41+
func TestHTTP_Database(t *testing.T) {
42+
dir, err := ioutil.TempDir("", "swarm-global-store-")
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
defer os.RemoveAll(dir)
47+
48+
// create a fresh global store
49+
testHTTP(t, true, "--dir", dir)
50+
51+
// check if data saved by the previous global store instance
52+
testHTTP(t, false, "--dir", dir)
53+
}
54+
55+
// testWebsocket starts global store binary with HTTP server
56+
// and validates that it can store and retrieve data.
57+
// If put is false, no data will be stored, only retrieved,
58+
// giving the possibility to check if data is present in the
59+
// storage directory.
60+
func testHTTP(t *testing.T, put bool, args ...string) {
61+
addr := findFreeTCPAddress(t)
62+
testCmd := runGlobalStore(t, append([]string{"http", "--addr", addr}, args...)...)
63+
defer testCmd.Interrupt()
64+
65+
client, err := rpc.DialHTTP("http://" + addr)
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
70+
// wait until global store process is started as
71+
// rpc.DialHTTP is actually not connecting
72+
for i := 0; i < 1000; i++ {
73+
_, err = http.DefaultClient.Get("http://" + addr)
74+
if err == nil {
75+
break
76+
}
77+
time.Sleep(10 * time.Millisecond)
78+
}
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
83+
store := mockRPC.NewGlobalStore(client)
84+
defer store.Close()
85+
86+
node := store.NewNodeStore(common.HexToAddress("123abc"))
87+
88+
wantKey := "key"
89+
wantValue := "value"
90+
91+
if put {
92+
err = node.Put([]byte(wantKey), []byte(wantValue))
93+
if err != nil {
94+
t.Fatal(err)
95+
}
96+
}
97+
98+
gotValue, err := node.Get([]byte(wantKey))
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
103+
if string(gotValue) != wantValue {
104+
t.Errorf("got value %s for key %s, want %s", string(gotValue), wantKey, wantValue)
105+
}
106+
}
107+
108+
// TestWebsocket_InMemory tests in-memory global store that exposes
109+
// WebSocket server.
110+
func TestWebsocket_InMemory(t *testing.T) {
111+
testWebsocket(t, true)
112+
}
113+
114+
// TestWebsocket_Database tests global store with persisted database
115+
// that exposes HTTP server.
116+
func TestWebsocket_Database(t *testing.T) {
117+
dir, err := ioutil.TempDir("", "swarm-global-store-")
118+
if err != nil {
119+
t.Fatal(err)
120+
}
121+
defer os.RemoveAll(dir)
122+
123+
// create a fresh global store
124+
testWebsocket(t, true, "--dir", dir)
125+
126+
// check if data saved by the previous global store instance
127+
testWebsocket(t, false, "--dir", dir)
128+
}
129+
130+
// testWebsocket starts global store binary with WebSocket server
131+
// and validates that it can store and retrieve data.
132+
// If put is false, no data will be stored, only retrieved,
133+
// giving the possibility to check if data is present in the
134+
// storage directory.
135+
func testWebsocket(t *testing.T, put bool, args ...string) {
136+
addr := findFreeTCPAddress(t)
137+
testCmd := runGlobalStore(t, append([]string{"ws", "--addr", addr}, args...)...)
138+
defer testCmd.Interrupt()
139+
140+
var client *rpc.Client
141+
var err error
142+
// wait until global store process is started
143+
for i := 0; i < 1000; i++ {
144+
client, err = rpc.DialWebsocket(context.Background(), "ws://"+addr, "")
145+
if err == nil {
146+
break
147+
}
148+
time.Sleep(10 * time.Millisecond)
149+
}
150+
if err != nil {
151+
t.Fatal(err)
152+
}
153+
154+
store := mockRPC.NewGlobalStore(client)
155+
defer store.Close()
156+
157+
node := store.NewNodeStore(common.HexToAddress("123abc"))
158+
159+
wantKey := "key"
160+
wantValue := "value"
161+
162+
if put {
163+
err = node.Put([]byte(wantKey), []byte(wantValue))
164+
if err != nil {
165+
t.Fatal(err)
166+
}
167+
}
168+
169+
gotValue, err := node.Get([]byte(wantKey))
170+
if err != nil {
171+
t.Fatal(err)
172+
}
173+
174+
if string(gotValue) != wantValue {
175+
t.Errorf("got value %s for key %s, want %s", string(gotValue), wantKey, wantValue)
176+
}
177+
}
178+
179+
// findFreeTCPAddress returns a local address (IP:Port) to which
180+
// global store can listen on.
181+
func findFreeTCPAddress(t *testing.T) (addr string) {
182+
t.Helper()
183+
184+
listener, err := net.Listen("tcp", "")
185+
if err != nil {
186+
t.Fatal(err)
187+
}
188+
defer listener.Close()
189+
190+
return listener.Addr().String()
191+
}

0 commit comments

Comments
 (0)