swarm: simplify mock NodeStore by removing the NodeStorer interface

This commit is contained in:
Janos Guljas 2018-01-16 12:02:07 +01:00
parent 86625af2aa
commit 44e942ece5
9 changed files with 48 additions and 96 deletions

View file

@ -81,7 +81,7 @@ type DbStore struct {
// saving and retreiving chunk data from the store. // saving and retreiving chunk data from the store.
// They must be set on DbStore initialization and must not be nil. // They must be set on DbStore initialization and must not be nil.
// They are used to bypass the default functionality of DbStore with // 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) putDataFunc func(batch *leveldb.Batch, _ Key, data []byte)
getFunc func(key Key) (chunk *Chunk, err error) 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 // NewMockDbStore creates a new instance of DbStore with
// mockStore set to a provided value. If mockStore argument is nil, // mockStore set to a provided value. If mockStore argument is nil,
// this function behaves exactly as NewDbStore. // 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) s, err = NewDbStore(path, hash, capacity, radius)
if err != nil { if err != nil {
return nil, err 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 // newMockPutDataFunc returns a function that stores the chunk data
// to a mock store to bypass the default functionality of DbStore. // 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) { return func(_ *leveldb.Batch, key Key, data []byte) {
if err := mockStore.Put(key, data); err != nil { if err := mockStore.Put(key, data); err != nil {
log.Error(fmt.Sprintf("%T: Chunk %v put: %v", mockStore, key.Log(), err)) 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 // newMockGetFunc returns a function that reads chunk data from
// the mock database, which is used as the value for DbStore.getFunc // the mock database, which is used as the value for DbStore.getFunc
// to bypass the default functionality of DbStore with a mock store. // 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) { return func(key Key) (chunk *Chunk, err error) {
data, err := mockStore.Get(key) data, err := mockStore.Get(key)
if err != nil { if err != nil {

View file

@ -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") dir, err := ioutil.TempDir("", "bzz-storage-test-mock")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View file

@ -31,7 +31,7 @@ type LocalStore struct {
// This constructor uses MemStore and DbStore as components. // This constructor uses MemStore and DbStore as components.
// If mockStore is not nil, it will be used by DbStore to store chunk data. // 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) dbStore, err := NewMockDbStore(params.ChunkDbPath, hash, params.DbCapacity, params.Radius, mockStore)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -57,11 +57,8 @@ func (s *GlobalStore) Close() error {
// NewNodeStore returns a new instance of NodeStore that retrieves and stores // NewNodeStore returns a new instance of NodeStore that retrieves and stores
// chunk data only for a node with address addr. // chunk data only for a node with address addr.
func (s *GlobalStore) NewNodeStore(addr common.Address) mock.NodeStorer { func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore {
return &NodeStore{ return mock.NewNodeStore(addr, s)
store: s,
addr: addr,
}
} }
// Get returns chunk data if the chunk with key exists for node // 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 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 ( var (
nodeKeyPrefix = []byte("node-") nodeKeyPrefix = []byte("node-")
dataKeyPrefix = []byte("data-") dataKeyPrefix = []byte("data-")

View file

@ -49,11 +49,8 @@ func NewGlobalStore() *GlobalStore {
// NewNodeStore returns a new instance of NodeStore that retrieves and stores // NewNodeStore returns a new instance of NodeStore that retrieves and stores
// chunk data only for a node with address addr. // chunk data only for a node with address addr.
func (s *GlobalStore) NewNodeStore(addr common.Address) mock.NodeStorer { func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore {
return &NodeStore{ return mock.NewNodeStore(addr, s)
store: s,
addr: addr,
}
} }
// Get returns chunk data if the chunk with key exists for node // 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 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)
}

View file

@ -39,10 +39,37 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
) )
// ErrNotFound is in all NodeStorer implementations // ErrNotFound indicates that the chunk is not found.
// to indicate that the chunk is not found.
var ErrNotFound = errors.New("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 // GlobalStorer defines methods for mock db store
// that stores chunk data for all swarm nodes. // that stores chunk data for all swarm nodes.
// It is used in tests to construct mock NodeStores // 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) Get(addr common.Address, key []byte) (data []byte, err error)
Put(addr common.Address, key []byte, data []byte) error Put(addr common.Address, key []byte, data []byte) error
HasKey(addr common.Address, key []byte) bool 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 // to be used by a single swarm node with
// address addr. // address addr.
NewNodeStore(addr common.Address) NodeStorer NewNodeStore(addr common.Address) *NodeStore
}
// 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
} }
// Importer defines method for importing mock store data // Importer defines method for importing mock store data

View file

@ -53,11 +53,8 @@ func (s *GlobalStore) Close() error {
// NewNodeStore returns a new instance of NodeStore that retrieves and stores // NewNodeStore returns a new instance of NodeStore that retrieves and stores
// chunk data only for a node with address addr. // chunk data only for a node with address addr.
func (s *GlobalStore) NewNodeStore(addr common.Address) mock.NodeStorer { func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore {
return &NodeStore{ return mock.NewNodeStore(addr, s)
store: s,
addr: addr,
}
} }
// Get calls a Get method to RPC server. // Get calls a Get method to RPC server.
@ -85,22 +82,3 @@ func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool {
} }
return has 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)
}

View file

@ -15,7 +15,7 @@
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// Package test provides functions that are used for testing // Package test provides functions that are used for testing
// GlobalStorer and NodeStorer implementations. // GlobalStorer implementations.
package test package test
import ( import (
@ -30,10 +30,10 @@ import (
"github.com/ethereum/go-ethereum/swarm/storage/mock" "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 // each one with a unique address, stores different chunks on them
// and checks if they are retrievable or not on all nodes. // 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) { func MockStore(t *testing.T, globalStore mock.GlobalStorer, n int) {
t.Run("GlobalStore", func(t *testing.T) { t.Run("GlobalStore", func(t *testing.T) {
addrs := make([]common.Address, n) 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) { 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++ { for i := 0; i < n; i++ {
addr := common.HexToAddress(strconv.FormatInt(int64(i)+1, 16)) addr := common.HexToAddress(strconv.FormatInt(int64(i)+1, 16))
nodes[addr] = globalStore.NewNodeStore(addr) nodes[addr] = globalStore.NewNodeStore(addr)

View file

@ -82,7 +82,7 @@ func (self *Swarm) API() *SwarmAPI {
// implements node.Service // implements node.Service
// If mockStore is not nil, it will be used as the storage for chunk data. // If mockStore is not nil, it will be used as the storage for chunk data.
// MockStore should be used only for testing. // 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) { if bytes.Equal(common.FromHex(config.PublicKey), storage.ZeroKey) {
return nil, fmt.Errorf("empty public key") return nil, fmt.Errorf("empty public key")
} }