|
|
|
@ -21,17 +21,15 @@ package mru |
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
|
"context" |
|
|
|
|
"fmt" |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
|
"unsafe" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/chunk" |
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/log" |
|
|
|
|
"github.com/ethereum/go-ethereum/swarm/storage" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const chunkSize = 4096 // temporary until we implement FileStore in the resourcehandler
|
|
|
|
|
|
|
|
|
|
type Handler struct { |
|
|
|
|
chunkStore *storage.NetStore |
|
|
|
|
HashSize int |
|
|
|
@ -66,8 +64,7 @@ func init() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewHandler creates a new Mutable Resource API
|
|
|
|
|
func NewHandler(params *HandlerParams) (*Handler, error) { |
|
|
|
|
|
|
|
|
|
func NewHandler(params *HandlerParams) *Handler { |
|
|
|
|
rh := &Handler{ |
|
|
|
|
resources: make(map[uint64]*resource), |
|
|
|
|
storeTimeout: defaultStoreTimeout, |
|
|
|
@ -82,7 +79,7 @@ func NewHandler(params *HandlerParams) (*Handler, error) { |
|
|
|
|
hashPool.Put(hashfunc) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return rh, nil |
|
|
|
|
return rh |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SetStore sets the store backend for the Mutable Resource API
|
|
|
|
@ -94,9 +91,8 @@ func (h *Handler) SetStore(store *storage.NetStore) { |
|
|
|
|
// If it looks like a resource update, the chunk address is checked against the ownerAddr of the update's signature
|
|
|
|
|
// It implements the storage.ChunkValidator interface
|
|
|
|
|
func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool { |
|
|
|
|
|
|
|
|
|
dataLength := len(data) |
|
|
|
|
if dataLength < minimumChunkLength { |
|
|
|
|
if dataLength < minimumChunkLength || dataLength > chunk.DefaultSize+8 { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -106,7 +102,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool { |
|
|
|
|
rootAddr, _ := metadataHash(data) |
|
|
|
|
valid := bytes.Equal(chunkAddr, rootAddr) |
|
|
|
|
if !valid { |
|
|
|
|
log.Debug(fmt.Sprintf("Invalid root metadata chunk with address: %s", chunkAddr.Hex())) |
|
|
|
|
log.Debug("Invalid root metadata chunk with address", "addr", chunkAddr.Hex()) |
|
|
|
|
} |
|
|
|
|
return valid |
|
|
|
|
} |
|
|
|
@ -118,7 +114,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool { |
|
|
|
|
// First, deserialize the chunk
|
|
|
|
|
var r SignedResourceUpdate |
|
|
|
|
if err := r.fromChunk(chunkAddr, data); err != nil { |
|
|
|
|
log.Debug("Invalid resource chunk with address %s: %s ", chunkAddr.Hex(), err.Error()) |
|
|
|
|
log.Debug("Invalid resource chunk", "addr", chunkAddr.Hex(), "err", err.Error()) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -126,7 +122,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool { |
|
|
|
|
// that was used to retrieve this chunk
|
|
|
|
|
// if this validation fails, someone forged a chunk.
|
|
|
|
|
if !bytes.Equal(chunkAddr, r.updateHeader.UpdateAddr()) { |
|
|
|
|
log.Debug("period,version,rootAddr contained in update chunk do not match updateAddr %s", chunkAddr.Hex()) |
|
|
|
|
log.Debug("period,version,rootAddr contained in update chunk do not match updateAddr", "addr", chunkAddr.Hex()) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -134,7 +130,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool { |
|
|
|
|
// If it fails, it means either the signature is not valid, data is corrupted
|
|
|
|
|
// or someone is trying to update someone else's resource.
|
|
|
|
|
if err := r.Verify(); err != nil { |
|
|
|
|
log.Debug("Invalid signature: %v", err) |
|
|
|
|
log.Debug("Invalid signature", "err", err) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -172,11 +168,6 @@ func (h *Handler) GetVersion(rootAddr storage.Address) (uint32, error) { |
|
|
|
|
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.
|
|
|
|
|
func (h *Handler) New(ctx context.Context, request *Request) error { |
|
|
|
|
|
|
|
|
|