-
Notifications
You must be signed in to change notification settings - Fork 1
fix(ethrpc): log client rejections below ERROR and surface whitelist detail #322
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
Open
dhyaniarun1993
wants to merge
1
commit into
main
Choose a base branch
from
fix/ethrpc-log-levels-whitelist-detail
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package service_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/ethereum/go-ethereum/common" | ||
| "github.com/ethereum/go-ethereum/common/hexutil" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| "go.uber.org/zap/zaptest/observer" | ||
|
|
||
| apperr "github.com/chainsafe/canton-middleware/pkg/app/errors" | ||
| "github.com/chainsafe/canton-middleware/pkg/ethrpc/service" | ||
| "github.com/chainsafe/canton-middleware/pkg/ethrpc/service/mocks" | ||
| ) | ||
|
|
||
| // observedLog wraps a mock Service with the logging decorator, capturing every | ||
| // emitted entry so a test can assert the severity chosen for a given error. | ||
| func observedLog(t *testing.T) (*mocks.Service, service.Service, *observer.ObservedLogs) { | ||
| t.Helper() | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| svc := mocks.NewService(t) | ||
| return svc, service.NewLog(svc, zap.New(core)), logs | ||
| } | ||
|
|
||
| // TestLogService_Severity locks in that client-side rejections are logged below | ||
| // ERROR — read-path probes at Debug, transfer attempts at Warn — while genuine | ||
| // server faults stay at ERROR. This keeps routine wallet probing from drowning | ||
| // real failures in the logs. | ||
| func TestLogService_Severity(t *testing.T) { | ||
| t.Run("Call client error is Debug, not Error", func(t *testing.T) { | ||
| svc, logSvc, logs := observedLog(t) | ||
| svc.EXPECT().Call(mock.Anything, mock.Anything). | ||
| Return(nil, apperr.BadRequestError(nil, "unknown method")).Once() | ||
|
|
||
| _, err := logSvc.Call(context.Background(), nil) | ||
| require.Error(t, err) | ||
|
|
||
| entries := logs.FilterMessage("Call failed").All() | ||
| require.Len(t, entries, 1) | ||
| assert.Equal(t, zapcore.DebugLevel, entries[0].Level) | ||
| }) | ||
|
|
||
| t.Run("SendRawTransaction client error is Warn and carries the detail", func(t *testing.T) { | ||
| svc, logSvc, logs := observedLog(t) | ||
| svc.EXPECT().SendRawTransaction(mock.Anything, mock.Anything). | ||
| Return(common.Hash{}, apperr.ForbiddenError( | ||
| errors.New("sender not whitelisted: 0xabc"), "sender not whitelisted: 0xabc")).Once() | ||
|
|
||
| _, err := logSvc.SendRawTransaction(context.Background(), hexutil.Bytes{0x01}) | ||
| require.Error(t, err) | ||
|
|
||
| entries := logs.FilterMessage("SendRawTransaction failed").All() | ||
| require.Len(t, entries, 1) | ||
| assert.Equal(t, zapcore.WarnLevel, entries[0].Level) | ||
| // The rejected address must reach the log, not just the generic message. | ||
| assert.Contains(t, entries[0].ContextMap()["error"], "sender not whitelisted: 0xabc") | ||
| }) | ||
|
|
||
| t.Run("server fault stays at Error", func(t *testing.T) { | ||
| svc, logSvc, logs := observedLog(t) | ||
| svc.EXPECT().SendRawTransaction(mock.Anything, mock.Anything). | ||
| Return(common.Hash{}, apperr.DependencyError(errors.New("db down"), "insert mempool entry")).Once() | ||
|
|
||
| _, err := logSvc.SendRawTransaction(context.Background(), hexutil.Bytes{0x01}) | ||
| require.Error(t, err) | ||
|
|
||
| entries := logs.FilterMessage("SendRawTransaction failed").All() | ||
| require.Len(t, entries, 1) | ||
| assert.Equal(t, zapcore.ErrorLevel, entries[0].Level) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mutating a slice parameter in-place via
appendis a Go anti-pattern that can lead to subtle bugs if the caller's slice has extra capacity or is reused in the future. Additionally, adding a defensivenilcheck forerrprevents potential issues iflogErris ever called with anilerror.Using 3-index slicing (
fields[:len(fields):len(fields)]) forces a copy of the slice when appending, ensuring the caller's underlying array is never mutated.