mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
swarm/storage: move validation from localstore to validator
This commit is contained in:
parent
92ef6db7b7
commit
898a548ad1
3 changed files with 9 additions and 14 deletions
|
|
@ -24,7 +24,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/metrics"
|
"github.com/ethereum/go-ethereum/metrics"
|
||||||
cp "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/mock"
|
"github.com/ethereum/go-ethereum/swarm/storage/mock"
|
||||||
)
|
)
|
||||||
|
|
@ -99,12 +98,6 @@ func NewTestLocalStoreForAddr(params *LocalStoreParams) (*LocalStore, error) {
|
||||||
// After the LDBStore.Put, it is ensured that the MemStore
|
// After the LDBStore.Put, it is ensured that the MemStore
|
||||||
// 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) {
|
||||||
if l := len(chunk.SData); l < 9 || l > cp.DefaultSize+8 {
|
|
||||||
log.Debug("invalid chunk data", "addr", chunk.Addr, "len", l)
|
|
||||||
chunk.SetErrored(ErrChunkInvalid)
|
|
||||||
chunk.markAsStored()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
valid := true
|
valid := true
|
||||||
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 {
|
||||||
|
|
@ -112,7 +105,7 @@ func (ls *LocalStore) Put(ctx context.Context, chunk *Chunk) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !valid {
|
if !valid {
|
||||||
log.Trace("invalid content address", "addr", chunk.Addr)
|
log.Trace("invalid chunk", "addr", chunk.Addr, "len", len(chunk.SData))
|
||||||
chunk.SetErrored(ErrChunkInvalid)
|
chunk.SetErrored(ErrChunkInvalid)
|
||||||
chunk.markAsStored()
|
chunk.markAsStored()
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ package mru
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
@ -94,7 +93,6 @@ 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
|
// 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
|
// 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 {
|
||||||
return false
|
return false
|
||||||
|
|
@ -106,7 +104,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
|
||||||
rootAddr, _ := metadataHash(data)
|
rootAddr, _ := metadataHash(data)
|
||||||
valid := bytes.Equal(chunkAddr, rootAddr)
|
valid := bytes.Equal(chunkAddr, rootAddr)
|
||||||
if !valid {
|
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
|
return valid
|
||||||
}
|
}
|
||||||
|
|
@ -118,7 +116,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
|
||||||
// First, deserialize the chunk
|
// First, deserialize the chunk
|
||||||
var r SignedResourceUpdate
|
var r SignedResourceUpdate
|
||||||
if err := r.fromChunk(chunkAddr, data); err != nil {
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,7 +124,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
|
||||||
// that was used to retrieve this chunk
|
// that was used to retrieve this chunk
|
||||||
// if this validation fails, someone forged a chunk.
|
// if this validation fails, someone forged a chunk.
|
||||||
if !bytes.Equal(chunkAddr, r.updateHeader.UpdateAddr()) {
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,7 +132,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
|
// If it fails, it means either the signature is not valid, data is corrupted
|
||||||
// or someone is trying to update someone else's resource.
|
// or someone is trying to update someone else's resource.
|
||||||
if err := r.Verify(); err != nil {
|
if err := r.Verify(); err != nil {
|
||||||
log.Debug("Invalid signature: %v", err)
|
log.Debug("Invalid signature", "err", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -348,6 +348,10 @@ func NewContentAddressValidator(hasher SwarmHasher) *ContentAddressValidator {
|
||||||
|
|
||||||
// Validate that the given key is a valid content address for the given data
|
// Validate that the given key is a valid content address for the given data
|
||||||
func (v *ContentAddressValidator) Validate(addr Address, data []byte) bool {
|
func (v *ContentAddressValidator) Validate(addr Address, data []byte) bool {
|
||||||
|
if l := len(data); l < 9 || l > chunk.DefaultSize+8 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
hasher := v.Hasher()
|
hasher := v.Hasher()
|
||||||
hasher.ResetWithLength(data[:8])
|
hasher.ResetWithLength(data[:8])
|
||||||
hasher.Write(data[8:])
|
hasher.Write(data[8:])
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue