forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/swarm/global-store: global store cmd (ethereum#19014)
(cherry picked from commit 33d0a0e)
- Loading branch information
Showing
8 changed files
with
476 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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 <http://www.gnu.org/licenses/>. | ||
|
||
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" | ||
) | ||
|
||
// startHTTP starts a global store with HTTP RPC server. | ||
// It is used for "http" cli command. | ||
func startHTTP(ctx *cli.Context) (err error) { | ||
server, cleanup, err := newServer(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer cleanup() | ||
|
||
listener, err := net.Listen("tcp", ctx.String("addr")) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info("http", "address", listener.Addr().String()) | ||
|
||
return http.Serve(listener, server) | ||
} | ||
|
||
// startWS starts a global store with WebSocket RPC server. | ||
// It is used for "websocket" cli command. | ||
func startWS(ctx *cli.Context) (err error) { | ||
server, cleanup, err := newServer(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer cleanup() | ||
|
||
listener, err := net.Listen("tcp", ctx.String("addr")) | ||
if err != nil { | ||
return err | ||
} | ||
origins := ctx.StringSlice("origins") | ||
log.Info("websocket", "address", listener.Addr().String(), "origins", origins) | ||
|
||
return http.Serve(listener, server.WebsocketHandler(origins)) | ||
} | ||
|
||
// newServer creates a global store and returns its RPC server. | ||
// Returned cleanup function should be called only if err is nil. | ||
func newServer(ctx *cli.Context) (server *rpc.Server, cleanup func(), err error) { | ||
log.PrintOrigins(true) | ||
log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(false)))) | ||
|
||
cleanup = func() {} | ||
var globalStore mock.GlobalStorer | ||
dir := ctx.String("dir") | ||
if dir != "" { | ||
dbStore, err := db.NewGlobalStore(dir) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
cleanup = func() { | ||
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 { | ||
cleanup() | ||
return nil, nil, err | ||
} | ||
|
||
return server, cleanup, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// 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 <http://www.gnu.org/licenses/>. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"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" | ||
) | ||
|
||
// TestHTTP_InMemory tests in-memory global store that exposes | ||
// HTTP server. | ||
func TestHTTP_InMemory(t *testing.T) { | ||
testHTTP(t, true) | ||
} | ||
|
||
// TestHTTP_Database tests global store with persisted database | ||
// that exposes HTTP server. | ||
func TestHTTP_Database(t *testing.T) { | ||
dir, err := ioutil.TempDir("", "swarm-global-store-") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
// create a fresh global store | ||
testHTTP(t, true, "--dir", dir) | ||
|
||
// check if data saved by the previous global store instance | ||
testHTTP(t, false, "--dir", dir) | ||
} | ||
|
||
// testWebsocket starts global store binary with HTTP server | ||
// and validates that it can store and retrieve data. | ||
// If put is false, no data will be stored, only retrieved, | ||
// giving the possibility to check if data is present in the | ||
// storage directory. | ||
func testHTTP(t *testing.T, put bool, args ...string) { | ||
addr := findFreeTCPAddress(t) | ||
testCmd := runGlobalStore(t, append([]string{"http", "--addr", addr}, args...)...) | ||
defer testCmd.Interrupt() | ||
|
||
client, err := rpc.DialHTTP("http://" + addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// wait until global store process is started as | ||
// rpc.DialHTTP is actually not connecting | ||
for i := 0; i < 1000; i++ { | ||
_, err = http.DefaultClient.Get("http://" + 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) | ||
} | ||
} | ||
|
||
// TestWebsocket_InMemory tests in-memory global store that exposes | ||
// WebSocket server. | ||
func TestWebsocket_InMemory(t *testing.T) { | ||
testWebsocket(t, true) | ||
} | ||
|
||
// TestWebsocket_Database tests global store with persisted database | ||
// that exposes HTTP server. | ||
func TestWebsocket_Database(t *testing.T) { | ||
dir, err := ioutil.TempDir("", "swarm-global-store-") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.RemoveAll(dir) | ||
|
||
// create a fresh global store | ||
testWebsocket(t, true, "--dir", dir) | ||
|
||
// check if data saved by the previous global store instance | ||
testWebsocket(t, false, "--dir", dir) | ||
} | ||
|
||
// testWebsocket starts global store binary with WebSocket server | ||
// and validates that it can store and retrieve data. | ||
// If put is false, no data will be stored, only retrieved, | ||
// giving the possibility to check if data is present in the | ||
// storage directory. | ||
func testWebsocket(t *testing.T, put bool, args ...string) { | ||
addr := findFreeTCPAddress(t) | ||
testCmd := runGlobalStore(t, append([]string{"ws", "--addr", addr}, args...)...) | ||
defer testCmd.Interrupt() | ||
|
||
var client *rpc.Client | ||
var err error | ||
// wait until global store process is started | ||
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) | ||
} | ||
} | ||
|
||
// findFreeTCPAddress returns a local address (IP:Port) to which | ||
// global store can listen on. | ||
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() | ||
} |
Oops, something went wrong.