diff options
author | ethersphere <thesw@rm.eth> | 2018-06-20 20:06:27 +0800 |
---|---|---|
committer | ethersphere <thesw@rm.eth> | 2018-06-22 03:10:31 +0800 |
commit | e187711c6545487d4cac3701f0f506bb536234e2 (patch) | |
tree | d2f6150f70b84b36e49a449082aeda267b4b9046 /swarm/storage/mock/rpc | |
parent | 574378edb50c907b532946a1d4654dbd6701b20a (diff) | |
download | go-tangerine-e187711c6545487d4cac3701f0f506bb536234e2.tar.gz go-tangerine-e187711c6545487d4cac3701f0f506bb536234e2.tar.zst go-tangerine-e187711c6545487d4cac3701f0f506bb536234e2.zip |
swarm: network rewrite merge
Diffstat (limited to 'swarm/storage/mock/rpc')
-rw-r--r-- | swarm/storage/mock/rpc/rpc.go | 84 | ||||
-rw-r--r-- | swarm/storage/mock/rpc/rpc_test.go | 41 |
2 files changed, 125 insertions, 0 deletions
diff --git a/swarm/storage/mock/rpc/rpc.go b/swarm/storage/mock/rpc/rpc.go new file mode 100644 index 000000000..6e735f698 --- /dev/null +++ b/swarm/storage/mock/rpc/rpc.go @@ -0,0 +1,84 @@ +// Copyright 2018 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/>. + +// Package rpc implements an RPC client that connect to a centralized mock store. +// Centralazied mock store can be any other mock store implementation that is +// registered to Ethereum RPC server under mockStore name. Methods that defines +// mock.GlobalStore are the same that are used by RPC. Example: +// +// server := rpc.NewServer() +// server.RegisterName("mockStore", mem.NewGlobalStore()) +package rpc + +import ( + "fmt" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/swarm/log" + "github.com/ethereum/go-ethereum/swarm/storage/mock" +) + +// GlobalStore is rpc.Client that connects to a centralized mock store. +// Closing GlobalStore instance is required to release RPC client resources. +type GlobalStore struct { + client *rpc.Client +} + +// NewGlobalStore creates a new instance of GlobalStore. +func NewGlobalStore(client *rpc.Client) *GlobalStore { + return &GlobalStore{ + client: client, + } +} + +// Close closes RPC client. +func (s *GlobalStore) Close() error { + s.client.Close() + return nil +} + +// NewNodeStore returns a new instance of NodeStore that retrieves and stores +// chunk data only for a node with address addr. +func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore { + return mock.NewNodeStore(addr, s) +} + +// Get calls a Get method to RPC server. +func (s *GlobalStore) Get(addr common.Address, key []byte) (data []byte, err error) { + err = s.client.Call(&data, "mockStore_get", addr, key) + if err != nil && err.Error() == "not found" { + // pass the mock package value of error instead an rpc error + return data, mock.ErrNotFound + } + return data, err +} + +// Put calls a Put method to RPC server. +func (s *GlobalStore) Put(addr common.Address, key []byte, data []byte) error { + err := s.client.Call(nil, "mockStore_put", addr, key, data) + return err +} + +// HasKey calls a HasKey method to RPC server. +func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool { + var has bool + if err := s.client.Call(&has, "mockStore_hasKey", addr, key); err != nil { + log.Error(fmt.Sprintf("mock store HasKey: addr %s, key %064x: %v", addr, key, err)) + return false + } + return has +} diff --git a/swarm/storage/mock/rpc/rpc_test.go b/swarm/storage/mock/rpc/rpc_test.go new file mode 100644 index 000000000..52b634a44 --- /dev/null +++ b/swarm/storage/mock/rpc/rpc_test.go @@ -0,0 +1,41 @@ +// Copyright 2018 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/>. + +package rpc + +import ( + "testing" + + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/swarm/storage/mock/mem" + "github.com/ethereum/go-ethereum/swarm/storage/mock/test" +) + +// TestDBStore is running test for a GlobalStore +// using test.MockStore function. +func TestRPCStore(t *testing.T) { + serverStore := mem.NewGlobalStore() + + server := rpc.NewServer() + if err := server.RegisterName("mockStore", serverStore); err != nil { + t.Fatal(err) + } + + store := NewGlobalStore(rpc.DialInProc(server)) + defer store.Close() + + test.MockStore(t, store, 100) +} |