mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
swarm/storage: global chunk rules in MRU
This commit is contained in:
parent
898a548ad1
commit
7c0013c364
6 changed files with 13 additions and 29 deletions
|
|
@ -99,6 +99,8 @@ func NewTestLocalStoreForAddr(params *LocalStoreParams) (*LocalStore, error) {
|
||||||
// contains the chunk with the same data, but nil ReqC channel.
|
// contains the chunk with the same data, but nil ReqC channel.
|
||||||
func (ls *LocalStore) Put(ctx context.Context, chunk *Chunk) {
|
func (ls *LocalStore) Put(ctx context.Context, chunk *Chunk) {
|
||||||
valid := true
|
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 {
|
for _, v := range ls.Validators {
|
||||||
if valid = v.Validate(chunk.Addr, chunk.SData); valid {
|
if valid = v.Validate(chunk.Addr, chunk.SData); valid {
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/swarm/chunk"
|
||||||
"github.com/ethereum/go-ethereum/swarm/log"
|
"github.com/ethereum/go-ethereum/swarm/log"
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
const chunkSize = 4096 // temporary until we implement FileStore in the resourcehandler
|
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
chunkStore *storage.NetStore
|
chunkStore *storage.NetStore
|
||||||
HashSize int
|
HashSize int
|
||||||
|
|
@ -65,8 +64,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler creates a new Mutable Resource API
|
// NewHandler creates a new Mutable Resource API
|
||||||
func NewHandler(params *HandlerParams) (*Handler, error) {
|
func NewHandler(params *HandlerParams) *Handler {
|
||||||
|
|
||||||
rh := &Handler{
|
rh := &Handler{
|
||||||
resources: make(map[uint64]*resource),
|
resources: make(map[uint64]*resource),
|
||||||
storeTimeout: defaultStoreTimeout,
|
storeTimeout: defaultStoreTimeout,
|
||||||
|
|
@ -81,7 +79,7 @@ func NewHandler(params *HandlerParams) (*Handler, error) {
|
||||||
hashPool.Put(hashfunc)
|
hashPool.Put(hashfunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rh, nil
|
return rh
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetStore sets the store backend for the Mutable Resource API
|
// SetStore sets the store backend for the Mutable Resource API
|
||||||
|
|
@ -94,7 +92,7 @@ func (h *Handler) SetStore(store *storage.NetStore) {
|
||||||
// It implements the storage.ChunkValidator interface
|
// It implements the storage.ChunkValidator interface
|
||||||
func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
|
func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
|
||||||
dataLength := len(data)
|
dataLength := len(data)
|
||||||
if dataLength < minimumChunkLength {
|
if dataLength < minimumChunkLength || dataLength > chunk.DefaultSize+8 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -170,11 +168,6 @@ func (h *Handler) GetVersion(rootAddr storage.Address) (uint32, error) {
|
||||||
return rsrc.version, nil
|
return rsrc.version, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// \TODO should be hashsize * branches from the chosen chunker, implement with FileStore
|
|
||||||
func (h *Handler) chunkSize() int64 {
|
|
||||||
return chunkSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new metadata chunk out of the request passed in.
|
// New creates a new metadata chunk out of the request passed in.
|
||||||
func (h *Handler) New(ctx context.Context, request *Request) error {
|
func (h *Handler) New(ctx context.Context, request *Request) error {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -777,10 +777,7 @@ func TestValidatorInStore(t *testing.T) {
|
||||||
|
|
||||||
// set up resource handler and add is as a validator to the localstore
|
// set up resource handler and add is as a validator to the localstore
|
||||||
rhParams := &HandlerParams{}
|
rhParams := &HandlerParams{}
|
||||||
rh, err := NewHandler(rhParams)
|
rh := NewHandler(rhParams)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
store.Validators = append(store.Validators, rh)
|
store.Validators = append(store.Validators, rh)
|
||||||
|
|
||||||
// create content addressed chunks, one good, one faulty
|
// create content addressed chunks, one good, one faulty
|
||||||
|
|
|
||||||
|
|
@ -38,10 +38,7 @@ func (t *TestHandler) Close() {
|
||||||
// NewTestHandler creates Handler object to be used for testing purposes.
|
// NewTestHandler creates Handler object to be used for testing purposes.
|
||||||
func NewTestHandler(datadir string, params *HandlerParams) (*TestHandler, error) {
|
func NewTestHandler(datadir string, params *HandlerParams) (*TestHandler, error) {
|
||||||
path := filepath.Join(datadir, testDbDirName)
|
path := filepath.Join(datadir, testDbDirName)
|
||||||
rh, err := NewHandler(params)
|
rh := NewHandler(params)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("resource handler create fail: %v", err)
|
|
||||||
}
|
|
||||||
localstoreparams := storage.NewDefaultLocalStoreParams()
|
localstoreparams := storage.NewDefaultLocalStoreParams()
|
||||||
localstoreparams.Init(path)
|
localstoreparams.Init(path)
|
||||||
localStore, err := storage.NewLocalStore(localstoreparams, nil)
|
localStore, err := storage.NewLocalStore(localstoreparams, nil)
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/swarm/chunk"
|
||||||
"github.com/ethereum/go-ethereum/swarm/log"
|
"github.com/ethereum/go-ethereum/swarm/log"
|
||||||
"github.com/ethereum/go-ethereum/swarm/multihash"
|
"github.com/ethereum/go-ethereum/swarm/multihash"
|
||||||
)
|
)
|
||||||
|
|
@ -42,7 +43,7 @@ const chunkPrefixLength = 2 + 2
|
||||||
//
|
//
|
||||||
// Minimum size is Header + 1 (minimum data length, enforced)
|
// Minimum size is Header + 1 (minimum data length, enforced)
|
||||||
const minimumUpdateDataLength = updateHeaderLength + 1
|
const minimumUpdateDataLength = updateHeaderLength + 1
|
||||||
const maxUpdateDataLength = chunkSize - signatureLength - updateHeaderLength - chunkPrefixLength
|
const maxUpdateDataLength = chunk.DefaultSize - signatureLength - updateHeaderLength - chunkPrefixLength
|
||||||
|
|
||||||
// binaryPut serializes the resource update information into the given slice
|
// binaryPut serializes the resource update information into the given slice
|
||||||
func (r *resourceUpdate) binaryPut(serializedData []byte) error {
|
func (r *resourceUpdate) binaryPut(serializedData []byte) error {
|
||||||
|
|
|
||||||
|
|
@ -195,19 +195,13 @@ func NewSwarm(config *api.Config, mockStore *mock.NodeStore) (self *Swarm, err e
|
||||||
var resourceHandler *mru.Handler
|
var resourceHandler *mru.Handler
|
||||||
rhparams := &mru.HandlerParams{}
|
rhparams := &mru.HandlerParams{}
|
||||||
|
|
||||||
resourceHandler, err = mru.NewHandler(rhparams)
|
resourceHandler = mru.NewHandler(rhparams)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
resourceHandler.SetStore(netStore)
|
resourceHandler.SetStore(netStore)
|
||||||
|
|
||||||
validators := []storage.ChunkValidator{
|
self.lstore.Validators = []storage.ChunkValidator{
|
||||||
storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)),
|
storage.NewContentAddressValidator(storage.MakeHashFunc(storage.DefaultHash)),
|
||||||
|
resourceHandler,
|
||||||
}
|
}
|
||||||
if resourceHandler != nil {
|
|
||||||
validators = append(validators, resourceHandler)
|
|
||||||
}
|
|
||||||
self.lstore.Validators = validators
|
|
||||||
|
|
||||||
// setup local store
|
// setup local store
|
||||||
log.Debug(fmt.Sprintf("Set up local storage"))
|
log.Debug(fmt.Sprintf("Set up local storage"))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue