| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | // Copyright 2016 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package storage | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/metrics" | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/swarm/log" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/swarm/storage/mock" | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | type LocalStoreParams struct { | 
					
						
							|  |  |  | 	*StoreParams | 
					
						
							|  |  |  | 	ChunkDbPath string | 
					
						
							|  |  |  | 	Validators  []ChunkValidator `toml:"-"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewDefaultLocalStoreParams() *LocalStoreParams { | 
					
						
							|  |  |  | 	return &LocalStoreParams{ | 
					
						
							|  |  |  | 		StoreParams: NewDefaultStoreParams(), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //this can only finally be set after all config options (file, cmd line, env vars) | 
					
						
							|  |  |  | //have been evaluated | 
					
						
							|  |  |  | func (p *LocalStoreParams) Init(path string) { | 
					
						
							|  |  |  | 	if p.ChunkDbPath == "" { | 
					
						
							|  |  |  | 		p.ChunkDbPath = filepath.Join(path, "chunks") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // LocalStore is a combination of inmemory db over a disk persisted db | 
					
						
							|  |  |  | // implements a Get/Put with fallback (caching) logic using any 2 ChunkStores | 
					
						
							|  |  |  | type LocalStore struct { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	Validators []ChunkValidator | 
					
						
							|  |  |  | 	memStore   *MemStore | 
					
						
							|  |  |  | 	DbStore    *LDBStore | 
					
						
							|  |  |  | 	mu         sync.Mutex | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This constructor uses MemStore and DbStore as components | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func NewLocalStore(params *LocalStoreParams, mockStore *mock.NodeStore) (*LocalStore, error) { | 
					
						
							|  |  |  | 	ldbparams := NewLDBStoreParams(params.StoreParams, params.ChunkDbPath) | 
					
						
							|  |  |  | 	dbStore, err := NewMockDbStore(ldbparams, mockStore) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &LocalStore{ | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		memStore:   NewMemStore(params.StoreParams, dbStore), | 
					
						
							|  |  |  | 		DbStore:    dbStore, | 
					
						
							|  |  |  | 		Validators: params.Validators, | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | func NewTestLocalStoreForAddr(params *LocalStoreParams) (*LocalStore, error) { | 
					
						
							|  |  |  | 	ldbparams := NewLDBStoreParams(params.StoreParams, params.ChunkDbPath) | 
					
						
							|  |  |  | 	dbStore, err := NewLDBStore(ldbparams) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	localStore := &LocalStore{ | 
					
						
							|  |  |  | 		memStore:   NewMemStore(params.StoreParams, dbStore), | 
					
						
							|  |  |  | 		DbStore:    dbStore, | 
					
						
							|  |  |  | 		Validators: params.Validators, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return localStore, nil | 
					
						
							| 
									
										
										
										
											2018-02-23 14:19:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 17:13:48 +02:00
										 |  |  | // isValid returns true if chunk passes any of the LocalStore Validators. | 
					
						
							|  |  |  | // isValid also returns true if LocalStore has no Validators. | 
					
						
							|  |  |  | func (ls *LocalStore) isValid(chunk Chunk) bool { | 
					
						
							|  |  |  | 	// by default chunks are valid. if we have 0 validators, then all chunks are valid. | 
					
						
							|  |  |  | 	valid := true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ls.Validators contains a list of one validator per chunk type. | 
					
						
							|  |  |  | 	// if one validator succeeds, then the chunk is valid | 
					
						
							|  |  |  | 	for _, v := range ls.Validators { | 
					
						
							|  |  |  | 		if valid = v.Validate(chunk.Address(), chunk.Data()); valid { | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return valid | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | // Put is responsible for doing validation and storage of the chunk | 
					
						
							|  |  |  | // by using configured ChunkValidators, MemStore and LDBStore. | 
					
						
							|  |  |  | // If the chunk is not valid, its GetErrored function will | 
					
						
							|  |  |  | // return ErrChunkInvalid. | 
					
						
							|  |  |  | // This method will check if the chunk is already in the MemStore | 
					
						
							|  |  |  | // and it will return it if it is. If there is an error from | 
					
						
							|  |  |  | // the MemStore.Get, it will be returned by calling GetErrored | 
					
						
							|  |  |  | // on the chunk. | 
					
						
							|  |  |  | // This method is responsible for closing Chunk.ReqC channel | 
					
						
							|  |  |  | // when the chunk is stored in memstore. | 
					
						
							|  |  |  | // After the LDBStore.Put, it is ensured that the MemStore | 
					
						
							|  |  |  | // contains the chunk with the same data, but nil ReqC channel. | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | func (ls *LocalStore) Put(ctx context.Context, chunk Chunk) error { | 
					
						
							| 
									
										
										
										
											2018-10-04 17:13:48 +02:00
										 |  |  | 	if !ls.isValid(chunk) { | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 		return ErrChunkInvalid | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	log.Trace("localstore.put", "key", chunk.Address()) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	ls.mu.Lock() | 
					
						
							|  |  |  | 	defer ls.mu.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	_, err := ls.memStore.Get(ctx, chunk.Address()) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	if err != nil && err != ErrChunkNotFound { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	ls.memStore.Put(ctx, chunk) | 
					
						
							|  |  |  | 	err = ls.DbStore.Put(ctx, chunk) | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get(chunk *Chunk) looks up a chunk in the local stores | 
					
						
							|  |  |  | // This method is blocking until the chunk is retrieved | 
					
						
							|  |  |  | // so additional timeout may be needed to wrap this call if | 
					
						
							|  |  |  | // ChunkStores are remote and can have long latency | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | func (ls *LocalStore) Get(ctx context.Context, addr Address) (chunk Chunk, err error) { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	ls.mu.Lock() | 
					
						
							|  |  |  | 	defer ls.mu.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | 	return ls.get(ctx, addr) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | func (ls *LocalStore) get(ctx context.Context, addr Address) (chunk Chunk, err error) { | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | 	chunk, err = ls.memStore.Get(ctx, addr) | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil && err != ErrChunkNotFound { | 
					
						
							|  |  |  | 		metrics.GetOrRegisterCounter("localstore.get.error", nil).Inc(1) | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err == nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		metrics.GetOrRegisterCounter("localstore.get.cachehit", nil).Inc(1) | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 		return chunk, nil | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	metrics.GetOrRegisterCounter("localstore.get.cachemiss", nil).Inc(1) | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | 	chunk, err = ls.DbStore.Get(ctx, addr) | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 		metrics.GetOrRegisterCounter("localstore.get.error", nil).Inc(1) | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 17:40:28 +02:00
										 |  |  | 	ls.memStore.Put(ctx, chunk) | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	return chunk, nil | 
					
						
							| 
									
										
										
										
											2016-08-29 21:18:00 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-09 00:01:12 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | func (ls *LocalStore) FetchFunc(ctx context.Context, addr Address) func(context.Context) error { | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	ls.mu.Lock() | 
					
						
							|  |  |  | 	defer ls.mu.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	_, err := ls.get(ctx, addr) | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		return nil | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | 	return func(context.Context) error { | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-13 11:42:19 +02:00
										 |  |  | func (ls *LocalStore) BinIndex(po uint8) uint64 { | 
					
						
							|  |  |  | 	return ls.DbStore.BinIndex(po) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (ls *LocalStore) Iterator(from uint64, to uint64, po uint8, f func(Address, uint64) bool) error { | 
					
						
							|  |  |  | 	return ls.DbStore.SyncIterator(from, to, po, f) | 
					
						
							| 
									
										
										
										
											2018-06-20 14:06:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Close the local store | 
					
						
							|  |  |  | func (ls *LocalStore) Close() { | 
					
						
							|  |  |  | 	ls.DbStore.Close() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-10-03 14:31:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Migrate checks the datastore schema vs the runtime schema, and runs migrations if they don't match | 
					
						
							|  |  |  | func (ls *LocalStore) Migrate() error { | 
					
						
							|  |  |  | 	schema, err := ls.DbStore.GetSchema() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Error(err.Error()) | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log.Debug("found schema", "schema", schema, "runtime-schema", CurrentDbSchema) | 
					
						
							|  |  |  | 	if schema != CurrentDbSchema { | 
					
						
							|  |  |  | 		// run migrations | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if schema == "" { | 
					
						
							|  |  |  | 			log.Debug("running migrations for", "schema", schema, "runtime-schema", CurrentDbSchema) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-04 17:13:48 +02:00
										 |  |  | 			// delete chunks that are not valid, i.e. chunks that do not pass any of the ls.Validators | 
					
						
							|  |  |  | 			ls.DbStore.Cleanup(func(c *chunk) bool { | 
					
						
							|  |  |  | 				return !ls.isValid(c) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2018-10-03 14:31:59 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			err := ls.DbStore.PutSchema(DbSchemaPurity) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				log.Error(err.Error()) | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |