github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/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 const ( 43 // DefaultLimit should be used as default limit for 44 // Keys, Nodes, NodeKeys and KeyNodes GlobarStorer 45 // methids implementations. 46 DefaultLimit = 100 47 // MaxLimit should be used as the maximal returned number 48 // of items for Keys, Nodes, NodeKeys and KeyNodes GlobarStorer 49 // methids implementations, regardless of provided limit. 50 MaxLimit = 1000 51 ) 52 53 // ErrNotFound indicates that the chunk is not found. 54 var ErrNotFound = errors.New("not found") 55 56 // NodeStore holds the node address and a reference to the GlobalStore 57 // in order to access and store chunk data only for one node. 58 type NodeStore struct { 59 store GlobalStorer 60 addr common.Address 61 } 62 63 // NewNodeStore creates a new instance of NodeStore that keeps 64 // chunk data using GlobalStorer with a provided address. 65 func NewNodeStore(addr common.Address, store GlobalStorer) *NodeStore { 66 return &NodeStore{ 67 store: store, 68 addr: addr, 69 } 70 } 71 72 // Get returns chunk data for a key for a node that has the address 73 // provided on NodeStore initialization. 74 func (n *NodeStore) Get(key []byte) (data []byte, err error) { 75 return n.store.Get(n.addr, key) 76 } 77 78 // Put saves chunk data for a key for a node that has the address 79 // provided on NodeStore initialization. 80 func (n *NodeStore) Put(key []byte, data []byte) error { 81 return n.store.Put(n.addr, key, data) 82 } 83 84 // Delete removes chunk data for a key for a node that has the address 85 // provided on NodeStore initialization. 86 func (n *NodeStore) Delete(key []byte) error { 87 return n.store.Delete(n.addr, key) 88 } 89 90 func (n *NodeStore) Keys(startKey []byte, limit int) (keys Keys, err error) { 91 return n.store.NodeKeys(n.addr, startKey, limit) 92 } 93 94 // GlobalStorer defines methods for mock db store 95 // that stores chunk data for all swarm nodes. 96 // It is used in tests to construct mock NodeStores 97 // for swarm nodes and to track and validate chunks. 98 type GlobalStorer interface { 99 Get(addr common.Address, key []byte) (data []byte, err error) 100 Put(addr common.Address, key []byte, data []byte) error 101 Delete(addr common.Address, key []byte) error 102 HasKey(addr common.Address, key []byte) bool 103 Keys(startKey []byte, limit int) (keys Keys, err error) 104 Nodes(startAddr *common.Address, limit int) (nodes Nodes, err error) 105 NodeKeys(addr common.Address, startKey []byte, limit int) (keys Keys, err error) 106 KeyNodes(key []byte, startAddr *common.Address, limit int) (nodes Nodes, err error) 107 // NewNodeStore creates an instance of NodeStore 108 // to be used by a single swarm node with 109 // address addr. 110 NewNodeStore(addr common.Address) *NodeStore 111 } 112 113 // Keys are returned results by Keys and NodeKeys GlobalStorer methods. 114 type Keys struct { 115 Keys [][]byte 116 Next []byte 117 } 118 119 // Nodes are returned results by Nodes and KeyNodes GlobalStorer methods. 120 type Nodes struct { 121 Addrs []common.Address 122 Next *common.Address 123 } 124 125 // Importer defines method for importing mock store data 126 // from an exported tar archive. 127 type Importer interface { 128 Import(r io.Reader) (n int, err error) 129 } 130 131 // Exporter defines method for exporting mock store data 132 // to a tar archive. 133 type Exporter interface { 134 Export(w io.Writer) (n int, err error) 135 } 136 137 // ExportedChunk is the structure that is saved in tar archive for 138 // each chunk as JSON-encoded bytes. 139 type ExportedChunk struct { 140 Data []byte `json:"d"` 141 Addrs []common.Address `json:"a"` 142 }