Skip to content

Commit 001a618

Browse files
committed
swarm/storage: simplify ChunkValidator interface
1 parent 1e190a3 commit 001a618

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

swarm/storage/feed/handler.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ func (h *Handler) SetStore(store *storage.NetStore) {
8282
// Validate is a chunk validation method
8383
// If it looks like a feed update, the chunk address is checked against the userAddr of the update's signature
8484
// It implements the storage.ChunkValidator interface
85-
func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
86-
dataLength := len(data)
87-
if dataLength < minimumSignedUpdateLength {
85+
func (h *Handler) Validate(chunk storage.Chunk) bool {
86+
if len(chunk.Data()) < minimumSignedUpdateLength {
8887
return false
8988
}
9089

@@ -94,8 +93,8 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
9493

9594
// First, deserialize the chunk
9695
var r Request
97-
if err := r.fromChunk(chunkAddr, data); err != nil {
98-
log.Debug("Invalid feed update chunk", "addr", chunkAddr.Hex(), "err", err.Error())
96+
if err := r.fromChunk(chunk); err != nil {
97+
log.Debug("Invalid feed update chunk", "addr", chunk.Address(), "err", err)
9998
return false
10099
}
101100

@@ -198,7 +197,7 @@ func (h *Handler) Lookup(ctx context.Context, query *Query) (*cacheEntry, error)
198197
}
199198

200199
var request Request
201-
if err := request.fromChunk(chunk.Address(), chunk.Data()); err != nil {
200+
if err := request.fromChunk(chunk); err != nil {
202201
return nil, nil
203202
}
204203
if request.Time <= timeLimit {

swarm/storage/feed/handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func TestValidator(t *testing.T) {
366366
if err != nil {
367367
t.Fatal(err)
368368
}
369-
if !rh.Validate(chunk.Address(), chunk.Data()) {
369+
if !rh.Validate(chunk) {
370370
t.Fatal("Chunk validator fail on update chunk")
371371
}
372372

@@ -375,7 +375,7 @@ func TestValidator(t *testing.T) {
375375
address[0] = 11
376376
address[15] = 99
377377

378-
if rh.Validate(address, chunk.Data()) {
378+
if rh.Validate(storage.NewChunk(address, chunk.Data())) {
379379
t.Fatal("Expected Validate to fail with false chunk address")
380380
}
381381
}

swarm/storage/feed/request.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ func (r *Request) toChunk() (storage.Chunk, error) {
171171
}
172172

173173
// fromChunk populates this structure from chunk data. It does not verify the signature is valid.
174-
func (r *Request) fromChunk(updateAddr storage.Address, chunkdata []byte) error {
174+
func (r *Request) fromChunk(chunk storage.Chunk) error {
175175
// for update chunk layout see Request definition
176176

177+
chunkdata := chunk.Data()
178+
177179
//deserialize the feed update portion
178180
if err := r.Update.binaryGet(chunkdata[:len(chunkdata)-signatureLength]); err != nil {
179181
return err
@@ -189,7 +191,7 @@ func (r *Request) fromChunk(updateAddr storage.Address, chunkdata []byte) error
189191
}
190192

191193
r.Signature = signature
192-
r.idAddr = updateAddr
194+
r.idAddr = chunk.Address()
193195
r.binaryData = chunkdata
194196

195197
return nil

swarm/storage/feed/request_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestUpdateChunkSerializationErrorChecking(t *testing.T) {
197197

198198
// Test that parseUpdate fails if the chunk is too small
199199
var r Request
200-
if err := r.fromChunk(storage.ZeroAddr, make([]byte, minimumUpdateDataLength-1+signatureLength)); err == nil {
200+
if err := r.fromChunk(storage.NewChunk(storage.ZeroAddr, make([]byte, minimumUpdateDataLength-1+signatureLength))); err == nil {
201201
t.Fatalf("Expected request.fromChunk to fail when chunkData contains less than %d bytes", minimumUpdateDataLength)
202202
}
203203

@@ -226,7 +226,7 @@ func TestUpdateChunkSerializationErrorChecking(t *testing.T) {
226226
compareByteSliceToExpectedHex(t, "chunk", chunk.Data(), "0x0000000000000000776f726c64206e657773207265706f72742c20657665727920686f7572000000876a8936a7cd0b79ef0735ad0896c1afe278781ce803000000000019416c206269656e206861636572206a616dc3a173206c652066616c7461207072656d696f5a0ffe0bc27f207cd5b00944c8b9cee93e08b89b5ada777f123ac535189333f174a6a4ca2f43a92c4a477a49d774813c36ce8288552c58e6205b0ac35d0507eb00")
227227

228228
var recovered Request
229-
recovered.fromChunk(chunk.Address(), chunk.Data())
229+
recovered.fromChunk(chunk)
230230
if !reflect.DeepEqual(recovered, r) {
231231
t.Fatal("Expected recovered feed update request to equal the original one")
232232
}
@@ -282,7 +282,7 @@ func TestReverse(t *testing.T) {
282282

283283
// check that we can recover the owner account from the update chunk's signature
284284
var checkUpdate Request
285-
if err := checkUpdate.fromChunk(chunk.Address(), chunk.Data()); err != nil {
285+
if err := checkUpdate.fromChunk(chunk); err != nil {
286286
t.Fatal(err)
287287
}
288288
checkdigest, err := checkUpdate.GetDigest()

swarm/storage/localstore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (ls *LocalStore) isValid(chunk Chunk) bool {
9292
// ls.Validators contains a list of one validator per chunk type.
9393
// if one validator succeeds, then the chunk is valid
9494
for _, v := range ls.Validators {
95-
if valid = v.Validate(chunk.Address(), chunk.Data()); valid {
95+
if valid = v.Validate(chunk); valid {
9696
break
9797
}
9898
}

swarm/storage/localstore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func TestValidator(t *testing.T) {
118118

119119
type boolTestValidator bool
120120

121-
func (self boolTestValidator) Validate(addr Address, data []byte) bool {
121+
func (self boolTestValidator) Validate(chunk Chunk) bool {
122122
return bool(self)
123123
}
124124

swarm/storage/types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (c ChunkData) Data() []byte {
327327
}
328328

329329
type ChunkValidator interface {
330-
Validate(addr Address, data []byte) bool
330+
Validate(chunk Chunk) bool
331331
}
332332

333333
// Provides method for validation of content address in chunks
@@ -344,7 +344,8 @@ func NewContentAddressValidator(hasher SwarmHasher) *ContentAddressValidator {
344344
}
345345

346346
// Validate that the given key is a valid content address for the given data
347-
func (v *ContentAddressValidator) Validate(addr Address, data []byte) bool {
347+
func (v *ContentAddressValidator) Validate(chunk Chunk) bool {
348+
data := chunk.Data()
348349
if l := len(data); l < 9 || l > ch.DefaultSize+8 {
349350
// log.Error("invalid chunk size", "chunk", addr.Hex(), "size", l)
350351
return false
@@ -355,7 +356,7 @@ func (v *ContentAddressValidator) Validate(addr Address, data []byte) bool {
355356
hasher.Write(data[8:])
356357
hash := hasher.Sum(nil)
357358

358-
return bytes.Equal(hash, addr[:])
359+
return bytes.Equal(hash, chunk.Address())
359360
}
360361

361362
type ChunkStore interface {

0 commit comments

Comments
 (0)