Skip to content

Commit

Permalink
integrate shwap into shrex
Browse files Browse the repository at this point in the history
  • Loading branch information
walldiss committed Jul 25, 2024
1 parent c716c97 commit bfc59c9
Show file tree
Hide file tree
Showing 59 changed files with 694 additions and 994 deletions.
13 changes: 13 additions & 0 deletions libs/utils/close.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
"io"

logging "github.com/ipfs/go-log/v2"
)

func CloseAndLog(log logging.StandardLogger, name string, closer io.Closer) {
if err := closer.Close(); err != nil {
log.Warnf("closing %s: %s", name, err)
}
}
35 changes: 10 additions & 25 deletions share/getters/utils.go → libs/utils/ctx.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
package getters
package utils

import (
"context"
"errors"
"time"

logging "github.com/ipfs/go-log/v2"
"go.opentelemetry.io/otel"
)

var (
tracer = otel.Tracer("share/getters")
log = logging.Logger("share/getters")
// ResetContextOnError returns a fresh context if the given context has an error.
func ResetContextOnError(ctx context.Context) context.Context {
if ctx.Err() != nil {
ctx = context.Background()
}

errOperationNotSupported = errors.New("operation is not supported")
)
return ctx
}

// ctxWithSplitTimeout will split timeout stored in context by splitFactor and return the result if
// CtxWithSplitTimeout will split timeout stored in context by splitFactor and return the result if
// it is greater than minTimeout. minTimeout == 0 will be ignored, splitFactor <= 0 will be ignored
func ctxWithSplitTimeout(
func CtxWithSplitTimeout(
ctx context.Context,
splitFactor int,
minTimeout time.Duration,
Expand All @@ -42,16 +40,3 @@ func ctxWithSplitTimeout(
}
return context.WithTimeout(ctx, splitTimeout)
}

// ErrorContains reports whether any error in err's tree matches any error in targets tree.
func ErrorContains(err, target error) bool {
if errors.Is(err, target) || target == nil {
return true
}

target = errors.Unwrap(target)
if target == nil {
return false
}
return ErrorContains(err, target)
}
110 changes: 2 additions & 108 deletions share/getters/utils_test.go → libs/utils/ctx_test.go
Original file line number Diff line number Diff line change
@@ -1,119 +1,13 @@
package getters
package utils

import (
"context"
"errors"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_ErrorContains(t *testing.T) {
err1 := errors.New("1")
err2 := errors.New("2")

w1 := func(err error) error {
return fmt.Errorf("wrap1: %w", err)
}
w2 := func(err error) error {
return fmt.Errorf("wrap1: %w", err)
}

type args struct {
err error
target error
}
tests := []struct {
name string
args args
want bool
}{
{
"nil err",
args{
err: nil,
target: err1,
},
false,
},
{
"nil target",
args{
err: err1,
target: nil,
},
true,
},
{
"errors.Is true",
args{
err: w1(err1),
target: err1,
},
true,
},
{
"errors.Is false",
args{
err: w1(err1),
target: err2,
},
false,
},
{
"same wrap but different base error",
args{
err: w1(err1),
target: w1(err2),
},
false,
},
{
"both wrapped true",
args{
err: w1(err1),
target: w2(err1),
},
true,
},
{
"both wrapped false",
args{
err: w1(err1),
target: w2(err2),
},
false,
},
{
"multierr first in slice",
args{
err: errors.Join(w1(err1), w2(err2)),
target: w2(err1),
},
true,
},
{
"multierr second in slice",
args{
err: errors.Join(w1(err1), w2(err2)),
target: w1(err2),
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t,
tt.want,
ErrorContains(tt.args.err, tt.args.target),
"ErrorContains(%v, %v)", tt.args.err, tt.args.target)
})
}
}

func Test_ctxWithSplitTimeout(t *testing.T) {
type args struct {
ctxTimeout time.Duration
Expand Down Expand Up @@ -216,7 +110,7 @@ func Test_ctxWithSplitTimeout(t *testing.T) {
ctx, cancel = context.WithTimeout(ctx, tt.args.ctxTimeout)
}
t.Cleanup(cancel)
got, _ := ctxWithSplitTimeout(ctx, sf, tt.args.minTimeout)
got, _ := CtxWithSplitTimeout(ctx, sf, tt.args.minTimeout)
dl, ok := got.Deadline()
// in case no deadline is found in ctx or not expected to be found, check both cases apply at the
// same time
Expand Down
16 changes: 16 additions & 0 deletions libs/utils/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import "errors"

// ErrorContains reports whether any error in err's tree matches any error in targets tree.
func ErrorContains(err, target error) bool {
if errors.Is(err, target) || target == nil {
return true
}

target = errors.Unwrap(target)
if target == nil {
return false
}
return ErrorContains(err, target)
}
112 changes: 112 additions & 0 deletions libs/utils/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package utils

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ErrorContains(t *testing.T) {
err1 := errors.New("1")
err2 := errors.New("2")

w1 := func(err error) error {
return fmt.Errorf("wrap1: %w", err)
}
w2 := func(err error) error {
return fmt.Errorf("wrap1: %w", err)
}

type args struct {
err error
target error
}
tests := []struct {
name string
args args
want bool
}{
{
"nil err",
args{
err: nil,
target: err1,
},
false,
},
{
"nil target",
args{
err: err1,
target: nil,
},
true,
},
{
"errors.Is true",
args{
err: w1(err1),
target: err1,
},
true,
},
{
"errors.Is false",
args{
err: w1(err1),
target: err2,
},
false,
},
{
"same wrap but different base error",
args{
err: w1(err1),
target: w1(err2),
},
false,
},
{
"both wrapped true",
args{
err: w1(err1),
target: w2(err1),
},
true,
},
{
"both wrapped false",
args{
err: w1(err1),
target: w2(err2),
},
false,
},
{
"multierr first in slice",
args{
err: errors.Join(w1(err1), w2(err2)),
target: w2(err1),
},
true,
},
{
"multierr second in slice",
args{
err: errors.Join(w1(err1), w2(err2)),
target: w1(err2),
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t,
tt.want,
ErrorContains(tt.args.err, tt.args.target),
"ErrorContains(%v, %v)", tt.args.err, tt.args.target)
})
}
}
14 changes: 0 additions & 14 deletions libs/utils/resetctx.go

This file was deleted.

7 changes: 4 additions & 3 deletions nodebuilder/share/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/celestiaorg/celestia-node/share/eds"
"github.com/celestiaorg/celestia-node/share/getters"
"github.com/celestiaorg/celestia-node/share/ipld"
"github.com/celestiaorg/celestia-node/share/shwap/p2p/shrex/getter"
)

func newShareModule(getter share.Getter, avail share.Availability) Module {
Expand Down Expand Up @@ -42,7 +43,7 @@ func ensureEmptyEDSInBS(ctx context.Context, bServ blockservice.BlockService) er
}

func lightGetter(
shrexGetter *getters.ShrexGetter,
shrexGetter *getter.ShrexGetter,
ipldGetter *getters.IPLDGetter,
cfg Config,
) share.Getter {
Expand All @@ -60,7 +61,7 @@ func lightGetter(
// manual after corruption is detected).
func bridgeGetter(
storeGetter *getters.StoreGetter,
shrexGetter *getters.ShrexGetter,
shrexGetter *getter.ShrexGetter,
cfg Config,
) share.Getter {
var cascade []share.Getter
Expand All @@ -73,7 +74,7 @@ func bridgeGetter(

func fullGetter(
storeGetter *getters.StoreGetter,
shrexGetter *getters.ShrexGetter,
shrexGetter *getter.ShrexGetter,
ipldGetter *getters.IPLDGetter,
cfg Config,
) share.Getter {
Expand Down
Loading

0 comments on commit bfc59c9

Please sign in to comment.