github.com/xxRanger/go-ethereum@v1.8.23/swarm/storage/mock/mock.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 mock defines types that are used by different implementations 18 // of mock storages. 19 // 20 // Implementations of mock storages are located in directories 21 // under this package: 22 // 23 // - db - LevelDB backend 24 // - mem - in memory map backend 25 // - rpc - RPC client that can connect to other backends 26 // 27 // Mock storages can implement Importer and Exporter interfaces 28 // for importing and exporting all chunk data that they contain. 29 // The exported file is a tar archive with all files named by 30 // hexadecimal representations of chunk keys and with content 31 // with JSON-encoded ExportedChunk structure. Exported format 32 // should be preserved across all mock store implementations. 33 package mock 34 35 import ( 36 "errors" 37 "io" 38 39 "github.com/ethereum/go-ethereum/common" 40 ) 41 42 // ErrNotFound indicates that the chunk is not found. 43 var ErrNotFound = errors.New("not found") 44 45 // NodeStore holds the node address and a reference to the GlobalStore 46 // in order to access and store chunk data only for one node. 47 type NodeStore struct { 48 store GlobalStorer 49 addr common.Address 50 } 51 52 // NewNodeStore creates a new instance of NodeStore that keeps 53 // chunk data using GlobalStorer with a provided address. 54 func NewNodeStore(addr common.Address, store GlobalStorer) *NodeStore { 55 return &NodeStore{ 56 store: store, 57 addr: addr, 58 } 59 } 60 61 // Get returns chunk data for a key for a node that has the address 62 // provided on NodeStore initialization. 63 func (n *NodeStore) Get(key []byte) (data []byte, err error) { 64 return n.store.Get(n.addr, key) 65 } 66 67 // Put saves chunk data for a key for a node that has the address 68 // provided on NodeStore initialization. 69 func (n *NodeStore) Put(key []byte, data []byte) error { 70 return n.store.Put(n.addr, key, data) 71 } 72 73 // Delete removes chunk data for a key for a node that has the address 74 // provided on NodeStore initialization. 75 func (n *NodeStore) Delete(key []byte) error { 76 return n.store.Delete(n.addr, key) 77 } 78 79 // GlobalStorer defines methods for mock db store 80 // that stores chunk data for all swarm nodes. 81 // It is used in tests to construct mock NodeStores 82 // for swarm nodes and to track and validate chunks. 83 type GlobalStorer interface { 84 Get(addr common.Address, key []byte) (data []byte, err error) 85 Put(addr common.Address, key []byte, data []byte) error 86 Delete(addr common.Address, key []byte) error 87 HasKey(addr common.Address, key []byte) bool 88 // NewNodeStore creates an instance of NodeStore 89 // to be used by a single swarm node with 90 // address addr. 91 NewNodeStore(addr common.Address) *NodeStore 92 } 93 94 // Importer defines method for importing mock store data 95 // from an exported tar archive. 96 type Importer interface { 97 Import(r io.Reader) (n int, err error) 98 } 99 100 // Exporter defines method for exporting mock store data 101 // to a tar archive. 102 type Exporter interface { 103 Export(w io.Writer) (n int, err error) 104 } 105 106 // ExportedChunk is the structure that is saved in tar archive for 107 // each chunk as JSON-encoded bytes. 108 type ExportedChunk struct { 109 Data []byte `json:"d"` 110 Addrs []common.Address `json:"a"` 111 }