Commit 3bd536b2 authored by Zahoor Mohamed's avatar Zahoor Mohamed Committed by GitHub

global integration test related fixes (#602)

* removed chunk type test in IsPotential, local existance check in repair

* removed chunk type test in IsPotential, local existance check in repair

* removed logging info

* fixed testcase for reparing chunk that is not present

* fixed review commenst by Elad
parent 1ce046ee
...@@ -127,8 +127,6 @@ func TestDeliver(t *testing.T) { ...@@ -127,8 +127,6 @@ func TestDeliver(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// trojan chunk has its type set through the validator called by the store, so this needs to be simulated
c.WithType(swarm.ContentAddressed)
// create and register handler // create and register handler
var tt trojan.Topic // test variable to check handler func was correctly called var tt trojan.Topic // test variable to check handler func was correctly called
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package recovery
var (
ErrChunkNotPresent = errChunkNotPresent
)
...@@ -6,6 +6,7 @@ package recovery ...@@ -6,6 +6,7 @@ package recovery
import ( import (
"context" "context"
"errors"
"github.com/ethersphere/bee/pkg/logging" "github.com/ethersphere/bee/pkg/logging"
"github.com/ethersphere/bee/pkg/pss" "github.com/ethersphere/bee/pkg/pss"
...@@ -25,6 +26,10 @@ var ( ...@@ -25,6 +26,10 @@ var (
RecoveryTopic = trojan.NewTopic(RecoveryTopicText) RecoveryTopic = trojan.NewTopic(RecoveryTopicText)
) )
var (
errChunkNotPresent = errors.New("chunk repair: chunk not present in local store for repairing")
)
// RecoveryHook defines code to be executed upon failing to retrieve chunks. // RecoveryHook defines code to be executed upon failing to retrieve chunks.
type RecoveryHook func(chunkAddress swarm.Address, targets trojan.Targets) error type RecoveryHook func(chunkAddress swarm.Address, targets trojan.Targets) error
...@@ -48,6 +53,17 @@ func NewRepairHandler(s storage.Storer, logger logging.Logger, pushSyncer pushsy ...@@ -48,6 +53,17 @@ func NewRepairHandler(s storage.Storer, logger logging.Logger, pushSyncer pushsy
return func(ctx context.Context, m *trojan.Message) error { return func(ctx context.Context, m *trojan.Message) error {
chAddr := m.Payload chAddr := m.Payload
// check if the chunk exists in the local store and proceed.
// otherwise the Get will trigger a unnecessary network retrieve
exists, err := s.Has(ctx, swarm.NewAddress(chAddr))
if err != nil {
return err
}
if !exists {
return errChunkNotPresent
}
// retrieve the chunk from the local store
ch, err := s.Get(ctx, storage.ModeGetRequest, swarm.NewAddress(chAddr)) ch, err := s.Get(ctx, storage.ModeGetRequest, swarm.NewAddress(chAddr))
if err != nil { if err != nil {
logger.Tracef("chunk repair: error while getting chunk for repairing: %v", err) logger.Tracef("chunk repair: error while getting chunk for repairing: %v", err)
......
...@@ -201,7 +201,7 @@ func TestNewRepairHandler(t *testing.T) { ...@@ -201,7 +201,7 @@ func TestNewRepairHandler(t *testing.T) {
// invoke the chunk repair handler // invoke the chunk repair handler
err = repairHandler(context.Background(), &msg) err = repairHandler(context.Background(), &msg)
if err != nil && err.Error() != "storage: not found" { if !errors.Is(err, recovery.ErrChunkNotPresent) {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -105,14 +105,6 @@ func (a Address) MarshalJSON() ([]byte, error) { ...@@ -105,14 +105,6 @@ func (a Address) MarshalJSON() ([]byte, error) {
// ZeroAddress is the address that has no value. // ZeroAddress is the address that has no value.
var ZeroAddress = NewAddress(nil) var ZeroAddress = NewAddress(nil)
// Type describes a kind of chunk, whether it is content-addressed or other
type Type int
const (
Unknown Type = iota
ContentAddressed
)
type Chunk interface { type Chunk interface {
Address() Address Address() Address
Data() []byte Data() []byte
...@@ -121,8 +113,6 @@ type Chunk interface { ...@@ -121,8 +113,6 @@ type Chunk interface {
TagID() uint32 TagID() uint32
WithTagID(t uint32) Chunk WithTagID(t uint32) Chunk
Equal(Chunk) bool Equal(Chunk) bool
Type() Type
WithType(t Type) Chunk
} }
type chunk struct { type chunk struct {
...@@ -130,7 +120,6 @@ type chunk struct { ...@@ -130,7 +120,6 @@ type chunk struct {
sdata []byte sdata []byte
pinCounter uint64 pinCounter uint64
tagID uint32 tagID uint32
typ Type
} }
func NewChunk(addr Address, data []byte) Chunk { func NewChunk(addr Address, data []byte) Chunk {
...@@ -174,15 +163,6 @@ func (c *chunk) Equal(cp Chunk) bool { ...@@ -174,15 +163,6 @@ func (c *chunk) Equal(cp Chunk) bool {
return c.Address().Equal(cp.Address()) && bytes.Equal(c.Data(), cp.Data()) return c.Address().Equal(cp.Address()) && bytes.Equal(c.Data(), cp.Data())
} }
func (c *chunk) Type() Type {
return c.typ
}
func (c *chunk) WithType(t Type) Chunk {
c.typ = t
return c
}
type Validator interface { type Validator interface {
Validate(ch Chunk) (valid bool) Validate(ch Chunk) (valid bool)
} }
......
...@@ -115,11 +115,6 @@ func Unwrap(c swarm.Chunk) (*Message, error) { ...@@ -115,11 +115,6 @@ func Unwrap(c swarm.Chunk) (*Message, error) {
// IsPotential returns true if the given chunk is a potential trojan // IsPotential returns true if the given chunk is a potential trojan
func IsPotential(c swarm.Chunk) bool { func IsPotential(c swarm.Chunk) bool {
// chunk must be content-addressed to be trojan
if c.Type() != swarm.ContentAddressed {
return false
}
data := c.Data() data := c.Data()
// check for minimum chunk data length // check for minimum chunk data length
trojanChunkMinDataLen := swarm.SpanSize + NonceSize + TopicSize + LengthSize trojanChunkMinDataLen := swarm.SpanSize + NonceSize + TopicSize + LengthSize
......
...@@ -194,14 +194,7 @@ func TestUnwrap(t *testing.T) { ...@@ -194,14 +194,7 @@ func TestUnwrap(t *testing.T) {
func TestIsPotential(t *testing.T) { func TestIsPotential(t *testing.T) {
c := chunktesting.GenerateTestRandomChunk() c := chunktesting.GenerateTestRandomChunk()
// invalid type
c.WithType(swarm.Unknown)
if trojan.IsPotential(c) {
t.Fatal("non content-addressed chunk marked as potential trojan")
}
// valid type, but invalid trojan message length // valid type, but invalid trojan message length
c.WithType(swarm.ContentAddressed)
length := len(c.Data()) - 73 // go 1 byte over the maximum allowed length := len(c.Data()) - 73 // go 1 byte over the maximum allowed
lengthBuf := make([]byte, 2) lengthBuf := make([]byte, 2)
binary.BigEndian.PutUint16(lengthBuf, uint16(length)) binary.BigEndian.PutUint16(lengthBuf, uint16(length))
...@@ -214,7 +207,6 @@ func TestIsPotential(t *testing.T) { ...@@ -214,7 +207,6 @@ func TestIsPotential(t *testing.T) {
// valid type, but invalid chunk data length // valid type, but invalid chunk data length
data := make([]byte, 10) data := make([]byte, 10)
c = swarm.NewChunk(swarm.ZeroAddress, data) c = swarm.NewChunk(swarm.ZeroAddress, data)
c.WithType(swarm.ContentAddressed)
if trojan.IsPotential(c) { if trojan.IsPotential(c) {
t.Fatal("chunk with invalid data length marked as potential trojan") t.Fatal("chunk with invalid data length marked as potential trojan")
} }
...@@ -225,7 +217,6 @@ func TestIsPotential(t *testing.T) { ...@@ -225,7 +217,6 @@ func TestIsPotential(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
c.WithType(swarm.ContentAddressed)
if !trojan.IsPotential(c) { if !trojan.IsPotential(c) {
t.Fatal("valid test trojan chunk not marked as potential trojan") t.Fatal("valid test trojan chunk not marked as potential trojan")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment