github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/swarm/storage/mock/mem/mem.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 mem implements a mock store that keeps all chunk data in memory.
    18  // While it can be used for testing on smaller scales, the main purpose of this
    19  // package is to provide the simplest reference implementation of a mock store.
    20  package mem
    21  
    22  import (
    23  	"archive/tar"
    24  	"bytes"
    25  	"encoding/json"
    26  	"io"
    27  	"io/ioutil"
    28  	"sync"
    29  
    30  	"github.com/ethereum/go-ethereum/common"
    31  	"github.com/ethereum/go-ethereum/swarm/storage/mock"
    32  )
    33  
    34  // GlobalStore stores all chunk data and also keys and node addresses relations.
    35  // It implements mock.GlobalStore interface.
    36  type GlobalStore struct {
    37  	nodes map[string]map[common.Address]struct{}
    38  	data  map[string][]byte
    39  	mu    sync.Mutex
    40  }
    41  
    42  // NewGlobalStore creates a new instance of GlobalStore.
    43  func NewGlobalStore() *GlobalStore {
    44  	return &GlobalStore{
    45  		nodes: make(map[string]map[common.Address]struct{}),
    46  		data:  make(map[string][]byte),
    47  	}
    48  }
    49  
    50  // NewNodeStore returns a new instance of NodeStore that retrieves and stores
    51  // chunk data only for a node with address addr.
    52  func (s *GlobalStore) NewNodeStore(addr common.Address) *mock.NodeStore {
    53  	return mock.NewNodeStore(addr, s)
    54  }
    55  
    56  // Get returns chunk data if the chunk with key exists for node
    57  // on address addr.
    58  func (s *GlobalStore) Get(addr common.Address, key []byte) (data []byte, err error) {
    59  	s.mu.Lock()
    60  	defer s.mu.Unlock()
    61  
    62  	if _, ok := s.nodes[string(key)][addr]; !ok {
    63  		return nil, mock.ErrNotFound
    64  	}
    65  
    66  	data, ok := s.data[string(key)]
    67  	if !ok {
    68  		return nil, mock.ErrNotFound
    69  	}
    70  	return data, nil
    71  }
    72  
    73  // Put saves the chunk data for node with address addr.
    74  func (s *GlobalStore) Put(addr common.Address, key []byte, data []byte) error {
    75  	s.mu.Lock()
    76  	defer s.mu.Unlock()
    77  
    78  	if _, ok := s.nodes[string(key)]; !ok {
    79  		s.nodes[string(key)] = make(map[common.Address]struct{})
    80  	}
    81  	s.nodes[string(key)][addr] = struct{}{}
    82  	s.data[string(key)] = data
    83  	return nil
    84  }
    85  
    86  // HasKey returns whether a node with addr contains the key.
    87  func (s *GlobalStore) HasKey(addr common.Address, key []byte) bool {
    88  	s.mu.Lock()
    89  	defer s.mu.Unlock()
    90  
    91  	_, ok := s.nodes[string(key)][addr]
    92  	return ok
    93  }
    94  
    95  // Import reads tar archive from a reader that contains exported chunk data.
    96  // It returns the number of chunks imported and an error.
    97  func (s *GlobalStore) Import(r io.Reader) (n int, err error) {
    98  	s.mu.Lock()
    99  	defer s.mu.Unlock()
   100  
   101  	tr := tar.NewReader(r)
   102  
   103  	for {
   104  		hdr, err := tr.Next()
   105  		if err != nil {
   106  			if err == io.EOF {
   107  				break
   108  			}
   109  			return n, err
   110  		}
   111  
   112  		data, err := ioutil.ReadAll(tr)
   113  		if err != nil {
   114  			return n, err
   115  		}
   116  
   117  		var c mock.ExportedChunk
   118  		if err = json.Unmarshal(data, &c); err != nil {
   119  			return n, err
   120  		}
   121  
   122  		addrs := make(map[common.Address]struct{})
   123  		for _, a := range c.Addrs {
   124  			addrs[a] = struct{}{}
   125  		}
   126  
   127  		key := string(common.Hex2Bytes(hdr.Name))
   128  		s.nodes[key] = addrs
   129  		s.data[key] = c.Data
   130  		n++
   131  	}
   132  	return n, err
   133  }
   134  
   135  // Export writes to a writer a tar archive with all chunk data from
   136  // the store. It returns the number of chunks exported and an error.
   137  func (s *GlobalStore) Export(w io.Writer) (n int, err error) {
   138  	s.mu.Lock()
   139  	defer s.mu.Unlock()
   140  
   141  	tw := tar.NewWriter(w)
   142  	defer tw.Close()
   143  
   144  	buf := bytes.NewBuffer(make([]byte, 0, 1024))
   145  	encoder := json.NewEncoder(buf)
   146  	for key, addrs := range s.nodes {
   147  		al := make([]common.Address, 0, len(addrs))
   148  		for a := range addrs {
   149  			al = append(al, a)
   150  		}
   151  
   152  		buf.Reset()
   153  		if err = encoder.Encode(mock.ExportedChunk{
   154  			Addrs: al,
   155  			Data:  s.data[key],
   156  		}); err != nil {
   157  			return n, err
   158  		}
   159  
   160  		data := buf.Bytes()
   161  		hdr := &tar.Header{
   162  			Name: common.Bytes2Hex([]byte(key)),
   163  			Mode: 0644,
   164  			Size: int64(len(data)),
   165  		}
   166  		if err := tw.WriteHeader(hdr); err != nil {
   167  			return n, err
   168  		}
   169  		if _, err := tw.Write(data); err != nil {
   170  			return n, err
   171  		}
   172  		n++
   173  	}
   174  	return n, err
   175  }