From 44e942ece58fd0e7c2c689295fdd75af57ffe6f5 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Tue, 16 Jan 2018 12:02:07 +0100 Subject: [PATCH] swarm: simplify mock NodeStore by removing the NodeStorer interface --- swarm/storage/dbstore.go | 8 +++--- swarm/storage/dbstore_test.go | 2 +- swarm/storage/localstore.go | 2 +- swarm/storage/mock/db/db.go | 26 ++----------------- swarm/storage/mock/mem/mem.go | 26 ++----------------- swarm/storage/mock/mock.go | 44 +++++++++++++++++++++++---------- swarm/storage/mock/rpc/rpc.go | 26 ++----------------- swarm/storage/mock/test/test.go | 8 +++--- swarm/swarm.go | 2 +- 9 files changed, 48 insertions(+), 96 deletions(-) diff --git a/swarm/storage/dbstore.go b/swarm/storage/dbstore.go index 1e9adc5298..79273b75c8 100644 --- a/swarm/storage/dbstore.go +++ b/swarm/storage/dbstore.go @@ -81,7 +81,7 @@ type DbStore struct { // saving and retreiving chunk data from the store. // They must be set on DbStore initialization and must not be nil. // They are used to bypass the default functionality of DbStore with - // mock.NodeStorer for testing purposes. + // mock.NodeStore for testing purposes. putDataFunc func(batch *leveldb.Batch, _ Key, data []byte) getFunc func(key Key) (chunk *Chunk, err error) } @@ -122,7 +122,7 @@ func NewDbStore(path string, hash SwarmHasher, capacity uint64, radius int) (s * // NewMockDbStore creates a new instance of DbStore with // mockStore set to a provided value. If mockStore argument is nil, // this function behaves exactly as NewDbStore. -func NewMockDbStore(path string, hash SwarmHasher, capacity uint64, radius int, mockStore mock.NodeStorer) (s *DbStore, err error) { +func NewMockDbStore(path string, hash SwarmHasher, capacity uint64, radius int, mockStore *mock.NodeStore) (s *DbStore, err error) { s, err = NewDbStore(path, hash, capacity, radius) if err != nil { return nil, err @@ -475,7 +475,7 @@ func (s *DbStore) dbPutDataFunc(batch *leveldb.Batch, _ Key, data []byte) { // newMockPutDataFunc returns a function that stores the chunk data // to a mock store to bypass the default functionality of DbStore. -func newMockPutDataFunc(mockStore mock.NodeStorer) func(_ *leveldb.Batch, key Key, data []byte) { +func newMockPutDataFunc(mockStore *mock.NodeStore) func(_ *leveldb.Batch, key Key, data []byte) { return func(_ *leveldb.Batch, key Key, data []byte) { if err := mockStore.Put(key, data); err != nil { log.Error(fmt.Sprintf("%T: Chunk %v put: %v", mockStore, key.Log(), err)) @@ -550,7 +550,7 @@ func (s *DbStore) dbGetFunc(key Key) (chunk *Chunk, err error) { // newMockGetFunc returns a function that reads chunk data from // the mock database, which is used as the value for DbStore.getFunc // to bypass the default functionality of DbStore with a mock store. -func newMockGetFunc(mockStore mock.NodeStorer) func(key Key) (chunk *Chunk, err error) { +func newMockGetFunc(mockStore *mock.NodeStore) func(key Key) (chunk *Chunk, err error) { return func(key Key) (chunk *Chunk, err error) { data, err := mockStore.Get(key) if err != nil { diff --git a/swarm/storage/dbstore_test.go b/swarm/storage/dbstore_test.go index 625f7e6098..99b09c7de8 100644 --- a/swarm/storage/dbstore_test.go +++ b/swarm/storage/dbstore_test.go @@ -193,7 +193,7 @@ func TestDbStoreSyncIterator(t *testing.T) { } } -func initMockDbStore(t *testing.T, mockStore mock.NodeStorer) *DbStore { +func initMockDbStore(t *testing.T, mockStore *mock.NodeStore) *DbStore { dir, err := ioutil.TempDir("", "bzz-storage-test-mock") if err != nil { t.Fatal(err) diff --git a/swarm/storage/localstore.go b/swarm/storage/localstore.go index 6a523f45aa..7abfc9b086 100644 --- a/swarm/storage/localstore.go +++ b/swarm/storage/localstore.go @@ -31,7 +31,7 @@ type LocalStore struct { // This constructor uses MemStore and DbStore as components. // If mockStore is not nil, it will be used by DbStore to store chunk data. -func NewLocalStore(hash SwarmHasher, params *StoreParams, mockStore mock.NodeStorer) (*LocalStore, error) { +func NewLocalStore(hash SwarmHasher, params *StoreParams, mockStore *mock.NodeStore) (*LocalStore, error) { dbStore, err := NewMockDbStore(params.ChunkDbPath, hash, params.DbCapacity, params.Radius, mockStore) if err != nil { return nil, err diff --git a/swarm/storage/mock/db/db.go b/swarm/storage/mock/db/db.go index 379fa02c99..335f1e9a81 100644 --- a/swarm/storage/mock/db/db.go +++ b/swarm/storage/mock/db/db.go @@ -57,11 +57,8 @@ func (s *GlobalStore) Close() error { // 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.NodeStorer { - return &NodeStore{ - store: s, - addr: addr, - } +func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore { + return mock.NewNodeStore(addr, s) } // Get returns chunk data if the chunk with key exists for node @@ -217,25 +214,6 @@ func (s *GlobalStore) Export(w io.Writer) (n int, err error) { return n, err } -// NodeStore holds the node address and a reference to the GlobalStore -// in order to access and store chunk data only for one node. -type NodeStore struct { - store *GlobalStore - addr common.Address -} - -// Get returns chunk data for a key for a node that has the address -// provided on NodeStore initialization. -func (n *NodeStore) Get(key []byte) (data []byte, err error) { - return n.store.Get(n.addr, key) -} - -// Put saves chunk data for a key for a node that has the address -// provided on NodeStore initialization. -func (n *NodeStore) Put(key []byte, data []byte) error { - return n.store.Put(n.addr, key, data) -} - var ( nodeKeyPrefix = []byte("node-") dataKeyPrefix = []byte("data-") diff --git a/swarm/storage/mock/mem/mem.go b/swarm/storage/mock/mem/mem.go index d2d0fb79ed..c6cbeb7c1a 100644 --- a/swarm/storage/mock/mem/mem.go +++ b/swarm/storage/mock/mem/mem.go @@ -49,11 +49,8 @@ func NewGlobalStore() *GlobalStore { // 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.NodeStorer { - return &NodeStore{ - store: s, - addr: addr, - } +func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore { + return mock.NewNodeStore(addr, s) } // Get returns chunk data if the chunk with key exists for node @@ -176,22 +173,3 @@ func (s *GlobalStore) Export(w io.Writer) (n int, err error) { } return n, err } - -// NodeStore holds the node address and a reference to the GlobalStore -// in order to access and store chunk data only for one node. -type NodeStore struct { - store *GlobalStore - addr common.Address -} - -// Get returns chunk data for a key for a node that has the address -// provided on NodeStore initialization. -func (n *NodeStore) Get(key []byte) (data []byte, err error) { - return n.store.Get(n.addr, key) -} - -// Put saves chunk data for a key for a node that has the address -// provided on NodeStore initialization. -func (n *NodeStore) Put(key []byte, data []byte) error { - return n.store.Put(n.addr, key, data) -} diff --git a/swarm/storage/mock/mock.go b/swarm/storage/mock/mock.go index 728fa8aa00..81340f9274 100644 --- a/swarm/storage/mock/mock.go +++ b/swarm/storage/mock/mock.go @@ -39,10 +39,37 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// ErrNotFound is in all NodeStorer implementations -// to indicate that the chunk is not found. +// ErrNotFound indicates that the chunk is not found. var ErrNotFound = errors.New("not found") +// NodeStore holds the node address and a reference to the GlobalStore +// in order to access and store chunk data only for one node. +type NodeStore struct { + store GlobalStorer + addr common.Address +} + +// NewNodeStore creates a new instance of NodeStore that keeps +// chunk data using GlobalStorer with a provided address. +func NewNodeStore(addr common.Address, store GlobalStorer) *NodeStore { + return &NodeStore{ + store: store, + addr: addr, + } +} + +// Get returns chunk data for a key for a node that has the address +// provided on NodeStore initialization. +func (n *NodeStore) Get(key []byte) (data []byte, err error) { + return n.store.Get(n.addr, key) +} + +// Put saves chunk data for a key for a node that has the address +// provided on NodeStore initialization. +func (n *NodeStore) Put(key []byte, data []byte) error { + return n.store.Put(n.addr, key, data) +} + // GlobalStorer defines methods for mock db store // that stores chunk data for all swarm nodes. // It is used in tests to construct mock NodeStores @@ -51,19 +78,10 @@ type GlobalStorer interface { Get(addr common.Address, key []byte) (data []byte, err error) Put(addr common.Address, key []byte, data []byte) error HasKey(addr common.Address, key []byte) bool - // NewNodeStore creates an instance of NodeStorer + // NewNodeStore creates an instance of NodeStore // to be used by a single swarm node with // address addr. - NewNodeStore(addr common.Address) NodeStorer -} - -// NodeStorer defines methods that are required -// for accessing and storing chunk data. -// It is used for baypassing chunk data storing in -// storage.DbStore. -type NodeStorer interface { - Get(key []byte) (data []byte, err error) - Put(key []byte, data []byte) error + NewNodeStore(addr common.Address) *NodeStore } // Importer defines method for importing mock store data diff --git a/swarm/storage/mock/rpc/rpc.go b/swarm/storage/mock/rpc/rpc.go index 658b0a5bd6..6f0dac91ba 100644 --- a/swarm/storage/mock/rpc/rpc.go +++ b/swarm/storage/mock/rpc/rpc.go @@ -53,11 +53,8 @@ func (s *GlobalStore) Close() error { // 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.NodeStorer { - return &NodeStore{ - store: s, - addr: addr, - } +func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore { + return mock.NewNodeStore(addr, s) } // Get calls a Get method to RPC server. @@ -85,22 +82,3 @@ func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool { } return has } - -// NodeStore holds the node address and a reference to the GlobalStore -// in order to access and store chunk data only for one node. -type NodeStore struct { - store *GlobalStore - addr common.Address -} - -// Get returns chunk data for a key for a node that has the address -// provided on NodeStore initialization. -func (n *NodeStore) Get(key []byte) (data []byte, err error) { - return n.store.Get(n.addr, key) -} - -// Put saves chunk data for a key for a node that has the address -// provided on NodeStore initialization. -func (n *NodeStore) Put(key []byte, data []byte) error { - return n.store.Put(n.addr, key, data) -} diff --git a/swarm/storage/mock/test/test.go b/swarm/storage/mock/test/test.go index 7813a37a10..402634aa54 100644 --- a/swarm/storage/mock/test/test.go +++ b/swarm/storage/mock/test/test.go @@ -15,7 +15,7 @@ // along with the go-ethereum library. If not, see . // Package test provides functions that are used for testing -// GlobalStorer and NodeStorer implementations. +// GlobalStorer implementations. package test import ( @@ -30,10 +30,10 @@ import ( "github.com/ethereum/go-ethereum/swarm/storage/mock" ) -// MockStore creates NodeStorer instances from provided GlobalStorer, +// MockStore creates NodeStore instances from provided GlobalStorer, // each one with a unique address, stores different chunks on them // and checks if they are retrievable or not on all nodes. -// Attribute n defines the number of NodeStorers that will be created. +// Attribute n defines the number of NodeStores that will be created. func MockStore(t *testing.T, globalStore mock.GlobalStorer, n int) { t.Run("GlobalStore", func(t *testing.T) { addrs := make([]common.Address, n) @@ -75,7 +75,7 @@ func MockStore(t *testing.T, globalStore mock.GlobalStorer, n int) { }) t.Run("NodeStore", func(t *testing.T) { - nodes := make(map[common.Address]mock.NodeStorer) + nodes := make(map[common.Address]*mock.NodeStore) for i := 0; i < n; i++ { addr := common.HexToAddress(strconv.FormatInt(int64(i)+1, 16)) nodes[addr] = globalStore.NewNodeStore(addr) diff --git a/swarm/swarm.go b/swarm/swarm.go index d477db5fac..48e736ce72 100644 --- a/swarm/swarm.go +++ b/swarm/swarm.go @@ -82,7 +82,7 @@ func (self *Swarm) API() *SwarmAPI { // implements node.Service // If mockStore is not nil, it will be used as the storage for chunk data. // MockStore should be used only for testing. -func NewSwarm(ctx *node.ServiceContext, backend chequebook.Backend, ensClient *ethclient.Client, config *api.Config, swapEnabled, syncEnabled bool, cors string, pssEnabled bool, mockStore mock.NodeStorer) (self *Swarm, err error) { +func NewSwarm(ctx *node.ServiceContext, backend chequebook.Backend, ensClient *ethclient.Client, config *api.Config, swapEnabled, syncEnabled bool, cors string, pssEnabled bool, mockStore *mock.NodeStore) (self *Swarm, err error) { if bytes.Equal(common.FromHex(config.PublicKey), storage.ZeroKey) { return nil, fmt.Errorf("empty public key") }