Skip to content

Commit aa0d270

Browse files
adecaroHayim.Shaul@ibm.com
authored andcommitted
cleanup
Signed-off-by: Angelo De Caro <[email protected]>
1 parent 803cd12 commit aa0d270

File tree

9 files changed

+58
-46
lines changed

9 files changed

+58
-46
lines changed

pkg/utils/errors/errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ package errors
88

99
import "github.com/cockroachdb/errors"
1010

11+
// Join returns an error that wraps the given errors.
12+
func Join(errs ...error) error {
13+
return errors.Join(errs...)
14+
}
15+
1116
// HasType recursively checks errors wrapped using Wrapf until it detects the target error type
1217
func HasType(source, target error) bool {
1318
return source != nil && target != nil && errors.As(source, &target)

platform/common/utils/collections/slices.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ func Intersection[V comparable](a, b []V) []V { return slices.Intersection(a, b)
2020
func Repeat[T any](item T, times int) []T { return slices.Repeat(item, times) }
2121

2222
func GetUnique[T any](vs iterators.Iterator[T]) (T, error) { return iterators.GetUnique(vs) }
23+
24+
func FilterSlice[T any](input []T, keep func(T) bool) []T {
25+
out := make([]T, 0, len(input))
26+
for _, v := range input {
27+
if keep(v) {
28+
out = append(out, v)
29+
}
30+
}
31+
return out
32+
}

platform/view/services/endpoint/service.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1717
cdriver "github.com/hyperledger-labs/fabric-smart-client/platform/common/driver"
1818
"github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging"
19+
"github.com/hyperledger-labs/fabric-smart-client/platform/common/utils/collections"
1920
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services"
2021
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
2122
)
@@ -147,25 +148,13 @@ func (r *Service) GetResolver(ctx context.Context, id view.Identity) (*Resolver,
147148
}
148149

149150
func (r *Service) Bind(ctx context.Context, longTerm view.Identity, ephemeralIDs ...view.Identity) error {
150-
// check the IDs are different
151-
for _, ephemeral := range ephemeralIDs {
152-
if longTerm.Equal(ephemeral) {
153-
logger.DebugfContext(ctx, "cannot bind [%s] to [%s], they are the same", longTerm, ephemeral)
154-
return nil
155-
}
156-
}
157-
158-
// if all IDs are different, print to the log
159-
for _, ephemeral := range ephemeralIDs {
160-
logger.DebugfContext(ctx, "bind [%s] to [%s]", ephemeral, longTerm)
151+
// filter out the ids that are equal to longTerm
152+
toBind := collections.FilterSlice(ephemeralIDs, func(id view.Identity) bool {
153+
return !longTerm.Equal(id)
154+
})
155+
if err := r.bindingKVS.PutBindings(ctx, longTerm, toBind...); err != nil {
156+
return errors.WithMessagef(err, "failed storing bindings")
161157
}
162-
163-
for _, ephemeral := range ephemeralIDs {
164-
if err := r.bindingKVS.PutBindings(ctx, longTerm, ephemeral); err != nil {
165-
return errors.WithMessagef(err, "failed storing binding of [%s] to [%s]", ephemeral.UniqueID(), longTerm.UniqueID())
166-
}
167-
}
168-
169158
return nil
170159
}
171160

platform/view/services/storage/driver/sql/postgres/binding.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,6 @@ func newBindingStore(readDB, writeDB *sql.DB, table string) *BindingStore {
4040
}
4141
}
4242

43-
// func (db *BindingStore) PutBinding(ctx context.Context, ephemeral, longTerm view.Identity) error {
44-
// logger.DebugfContext(ctx, "Put binding for pair [%s:%s]", ephemeral.UniqueID(), longTerm.UniqueID())
45-
// if lt, err := db.GetLongTerm(ctx, longTerm); err != nil {
46-
// return err
47-
// } else if lt != nil && !lt.IsNone() {
48-
// logger.DebugfContext(ctx, "Replacing [%s] with long term [%s]", longTerm.UniqueID(), lt.UniqueID())
49-
// longTerm = lt
50-
// } else {
51-
// logger.DebugfContext(ctx, "Id [%s] is an unregistered long term ID", longTerm.UniqueID())
52-
// }
53-
// query := fmt.Sprintf(`
54-
// INSERT INTO %s (ephemeral_hash, long_term_id)
55-
// VALUES ($1, $2), ($3, $4)
56-
// ON CONFLICT DO NOTHING
57-
// `, db.table)
58-
// logger.Debug(query, ephemeral.UniqueID(), longTerm.UniqueID())
59-
// _, err := db.writeDB.ExecContext(ctx, query, ephemeral.UniqueID(), longTerm, longTerm.UniqueID(), longTerm)
60-
// if err == nil {
61-
// logger.DebugfContext(ctx, "Long-term and ephemeral ids registered [%s,%s]", longTerm, ephemeral)
62-
// return nil
63-
// }
64-
// if errors.Is(db.errorWrapper.WrapError(err), driver.UniqueKeyViolation) {
65-
// logger.Infof("tuple [%s,%s] already in db. Skipping...", ephemeral, longTerm)
66-
// return nil
67-
// }
68-
// return errors.Wrapf(err, "failed executing query [%s]", query)
69-
// }
70-
7143
func (db *BindingStore) PutBindings(ctx context.Context, longTerm view.Identity, ephemeral ...view.Identity) error {
7244
if len(ephemeral) == 0 {
7345
return errors.New("no ephemeral identities provided")

platform/view/services/view/mock/comm_layer.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/view/services/view/mock/identity_provider.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/view/services/view/mock/resolver.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/view/services/view/mock/session.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/view/services/view/mock/session_factory.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)