Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app/z: fields refactoring #2946

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions app/eth2wrap/synthproposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/attestantio/go-eth2-client/spec/deneb"
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
shuffle "github.com/protolambda/eth2-shuffle"
"go.uber.org/zap"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/log"
Expand Down Expand Up @@ -152,7 +151,7 @@ func (h *synthWrapper) syntheticProposal(ctx context.Context, slot eth2p0.Slot,
}
signed, err := h.Client.SignedBeaconBlock(ctx, opts)
if err != nil {
if fieldExists(err, zap.Int("status_code", http.StatusNotFound)) {
if z.ContainsField(err, z.Int("status_code", http.StatusNotFound)) {
continue
}

Expand Down Expand Up @@ -213,34 +212,6 @@ func (h *synthWrapper) syntheticProposal(ctx context.Context, slot eth2p0.Slot,
return proposal, nil
}

// fieldExists checks if the given field exists as part of the given error.
func fieldExists(err error, field zap.Field) bool {
type structErr interface {
Fields() []z.Field
}

sterr, ok := err.(structErr) //nolint:errorlint
if !ok {
return false
}

zfs := sterr.Fields()
var zapFs []zap.Field
for _, field := range zfs {
field(func(zp zap.Field) {
zapFs = append(zapFs, zp)
})
}

for _, zaps := range zapFs {
if zaps.Equals(field) {
return true
}
}

return false
}

// fraction returns a fraction of the transactions in the block.
// This is used to reduce the size of synthetic blocks to manageable levels.
func fraction(transactions []bellatrix.Transaction) []bellatrix.Transaction {
Expand Down
33 changes: 33 additions & 0 deletions app/z/zapfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,46 @@

import (
"fmt"
"slices"

"go.uber.org/zap"
)

// Field wraps one or more zap fields.
type Field func(add func(zap.Field))

// Fields returns the fields of an internal structured error.
func Fields(err error) []Field {
type structErr interface {
Fields() []Field
}

serr, ok := err.(structErr) //nolint:errorlint
if !ok {
return []Field{}
}

Check warning on line 26 in app/z/zapfield.go

View check run for this annotation

Codecov / codecov/patch

app/z/zapfield.go#L25-L26

Added lines #L25 - L26 were not covered by tests

return serr.Fields()
}

// ContainsField returns true if the error contains the given field.
func ContainsField(err error, field Field) bool {
fields := Fields(err)
var targetField zap.Field
field(func(zapField zap.Field) {
targetField = zapField
})

return slices.ContainsFunc(fields, func(f Field) bool {
var sourceField zap.Field
f(func(zapField zap.Field) {
sourceField = zapField
})

return targetField.Equals(sourceField)
})
}

// Err returns a wrapped zap error field. It will include an additional stack trace and fields
// if the error is an internal structured error.
// NOTE: This is only used when logging errors on other levels than Error since it has built-in support for errors.
Expand Down
138 changes: 138 additions & 0 deletions app/z/zapfield_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright © 2022-2024 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1

package z_test

import (
"slices"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/z"
)

func TestFields(t *testing.T) {
err := errors.New("test", z.Str("foo", "bar"), z.I64("zet", 123))

fields := z.Fields(err)

require.Len(t, fields, 2)
require.NotNil(t, fields[0])
require.NotNil(t, fields[1])
}

func TestContainsField(t *testing.T) {
f1 := z.Str("foo", "bar")
f2 := z.I64("zet", 123)
err := errors.New("test", f1, f2)

require.True(t, z.ContainsField(err, f1))
require.True(t, z.ContainsField(err, f2))
require.False(t, z.ContainsField(err, z.Bool("bool", true)))
}

func TestErr(t *testing.T) {
err := errors.New("test", z.Str("foo", "bar"), z.I64("zet", 123))

field := z.Err(err)

ufs := unwrap(field)
require.Len(t, ufs, 4) // zap.Error, zap.Stack, foo, zet
require.True(t, slices.ContainsFunc(ufs, func(f zap.Field) bool {
return f.Equals(zap.String("foo", "bar"))
}))
require.True(t, slices.ContainsFunc(ufs, func(f zap.Field) bool {
return f.Equals(zap.Int64("zet", 123))
}))
require.True(t, slices.ContainsFunc(ufs, func(f zap.Field) bool {
return f.Key == "stacktrace"
}))
require.True(t, slices.ContainsFunc(ufs, func(f zap.Field) bool {
return f.Key == "error"
}))
}

func TestStr(t *testing.T) {
field := z.Str("foo", "bar")

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.String("foo", "bar")))
}

func TestBool(t *testing.T) {
field := z.Bool("foo", true)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.Bool("foo", true)))
}

func TestI64(t *testing.T) {
field := z.I64("foo", 123)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.Int64("foo", 123)))
}

func TestU64(t *testing.T) {
field := z.U64("foo", 123)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.Uint64("foo", 123)))
}

func TestInt(t *testing.T) {
field := z.Int("foo", 123)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.Int("foo", 123)))
}

func TestUint(t *testing.T) {
field := z.Uint("foo", 123)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.Uint("foo", 123)))
}

func TestF64(t *testing.T) {
field := z.F64("foo", 123.45)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.Float64("foo", 123.45)))
}

func TestAny(t *testing.T) {
field := z.Any("foo", 123.45)

ufs := unwrap(field)
require.Len(t, ufs, 1)
require.True(t, ufs[0].Equals(zap.String("foo", "123.45")))
}

func TestSkip(t *testing.T) {
ufs := unwrap(z.Skip)
require.Empty(t, ufs)
}

func unwrap(fields ...z.Field) []zap.Field {
var resp []zap.Field

adder := func(f zap.Field) {
resp = append(resp, f)
}

for _, field := range fields {
field(adder)
}

return resp
}
Loading