github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/mock/rpc/rpc.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package rpc implements an RPC client that connect to a centralized mock store. 18 // Centralazied mock store can be any other mock store implementation that is 19 // registered to Ethereum RPC server under mockStore name. Methods that defines 20 // mock.GlobalStore are the same that are used by RPC. Example: 21 // 22 // server := rpc.NewServer() 23 // server.RegisterName("mockStore", mem.NewGlobalStore()) 24 package rpc 25 26 import ( 27 "fmt" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/rpc" 31 "github.com/ethereum/go-ethereum/swarm/log" 32 "github.com/ethereum/go-ethereum/swarm/storage/mock" 33 ) 34 35 // GlobalStore is rpc.Client that connects to a centralized mock store. 36 // Closing GlobalStore instance is required to release RPC client resources. 37 type GlobalStore struct { 38 client *rpc.Client 39 } 40 41 // NewGlobalStore creates a new instance of GlobalStore. 42 func NewGlobalStore(client *rpc.Client) *GlobalStore { 43 return &GlobalStore{ 44 client: client, 45 } 46 } 47 48 // Close closes RPC client. 49 func (s *GlobalStore) Close() error { 50 s.client.Close() 51 return nil 52 } 53 54 // NewNodeStore returns a new instance of NodeStore that retrieves and stores 55 // chunk data only for a node with address addr. 56 func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore { 57 return mock.NewNodeStore(addr, s) 58 } 59 60 // Get calls a Get method to RPC server. 61 func (s *GlobalStore) Get(addr common.Address, key []byte) (data []byte, err error) { 62 err = s.client.Call(&data, "mockStore_get", addr, key) 63 if err != nil && err.Error() == "not found" { 64 // pass the mock package value of error instead an rpc error 65 return data, mock.ErrNotFound 66 } 67 return data, err 68 } 69 70 // Put calls a Put method to RPC server. 71 func (s *GlobalStore) Put(addr common.Address, key []byte, data []byte) error { 72 err := s.client.Call(nil, "mockStore_put", addr, key, data) 73 return err 74 } 75 76 // Delete calls a Delete method to RPC server. 77 func (s *GlobalStore) Delete(addr common.Address, key []byte) error { 78 err := s.client.Call(nil, "mockStore_delete", addr, key) 79 return err 80 } 81 82 // HasKey calls a HasKey method to RPC server. 83 func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool { 84 var has bool 85 if err := s.client.Call(&has, "mockStore_hasKey", addr, key); err != nil { 86 log.Error(fmt.Sprintf("mock store HasKey: addr %s, key %064x: %v", addr, key, err)) 87 return false 88 } 89 return has 90 }