Skip to content

Commit

Permalink
Merge branch 'main' into kocu/config-rf
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski authored Oct 25, 2024
2 parents bb95533 + 7262cf3 commit e4047cc
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 6 deletions.
1 change: 1 addition & 0 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Each entry must include the Github issue reference in the following format:
### Improvements

* [#22233](https://github.com/cosmos/cosmos-sdk/pull/22233) Use sonic json library for faster json handling
* [#22347](https://github.com/cosmos/cosmos-sdk/pull/22347) Add cosmossdk.io/log/slog to allow using a standard library log/slog-backed logger.

## [v1.4.1](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.4.1) - 2024-08-16

Expand Down
2 changes: 2 additions & 0 deletions log/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Log

The `cosmossdk.io/log` provides a zerolog logging implementation for the Cosmos SDK and Cosmos SDK modules.

To use a logger wrapping an instance of the standard library's `log/slog` package, use `cosmossdk.io/log/slog`.
50 changes: 50 additions & 0 deletions log/slog/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Package slog contains a Logger type that satisfies [cosmossdk.io/log.Logger],
// backed by a standard library [*log/slog.Logger].
package slog

import (
"log/slog"

"cosmossdk.io/log"
)

var _ log.Logger = Logger{}

// Logger satisfies [log.Logger] with logging backed by
// an instance of [*slog.Logger].
type Logger struct {
log *slog.Logger
}

// NewCustomLogger returns a Logger backed by an existing slog.Logger instance.
// All logging methods are called directly on the *slog.Logger;
// therefore it is the caller's responsibility to configure message filtering,
// level filtering, output format, and so on.
func NewCustomLogger(log *slog.Logger) Logger {
return Logger{log: log}
}

func (l Logger) Info(msg string, keyVals ...any) {
l.log.Info(msg, keyVals...)
}

func (l Logger) Warn(msg string, keyVals ...any) {
l.log.Warn(msg, keyVals...)
}

func (l Logger) Error(msg string, keyVals ...any) {
l.log.Error(msg, keyVals...)
}

func (l Logger) Debug(msg string, keyVals ...any) {
l.log.Debug(msg, keyVals...)
}

func (l Logger) With(keyVals ...any) log.Logger {
return Logger{log: l.log.With(keyVals...)}
}

// Impl returns l's underlying [*slog.Logger].
func (l Logger) Impl() any {
return l.log
}
92 changes: 92 additions & 0 deletions log/slog/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package slog_test

import (
"bytes"
"encoding/json"
stdslog "log/slog"
"testing"

"cosmossdk.io/log/slog"
)

func TestSlog(t *testing.T) {
var buf bytes.Buffer
h := stdslog.NewJSONHandler(&buf, &stdslog.HandlerOptions{
Level: stdslog.LevelDebug,
})
logger := slog.NewCustomLogger(stdslog.New(h))

type logLine struct {
Level string `json:"level"`
Msg string `json:"msg"`
Num int `json:"num"`
}

var line logLine

logger.Debug("Message one", "num", 1)
if err := json.Unmarshal(buf.Bytes(), &line); err != nil {
t.Fatal(err)
}
if want := (logLine{
Level: stdslog.LevelDebug.String(),
Msg: "Message one",
Num: 1,
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}

buf.Reset()
logger.Info("Message two", "num", 2)
if err := json.Unmarshal(buf.Bytes(), &line); err != nil {
t.Fatal(err)
}
if want := (logLine{
Level: stdslog.LevelInfo.String(),
Msg: "Message two",
Num: 2,
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}

buf.Reset()
logger.Warn("Message three", "num", 3)
if err := json.Unmarshal(buf.Bytes(), &line); err != nil {
t.Fatal(err)
}
if want := (logLine{
Level: stdslog.LevelWarn.String(),
Msg: "Message three",
Num: 3,
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}

buf.Reset()
logger.Error("Message four", "num", 4)
if err := json.Unmarshal(buf.Bytes(), &line); err != nil {
t.Fatal(err)
}
if want := (logLine{
Level: stdslog.LevelError.String(),
Msg: "Message four",
Num: 4,
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}

wLogger := logger.With("num", 5)
buf.Reset()
wLogger.Info("Using .With")

if err := json.Unmarshal(buf.Bytes(), &line); err != nil {
t.Fatal(err)
}
if want := (logLine{
Level: stdslog.LevelInfo.String(),
Msg: "Using .With",
Num: 5,
}); want != line {
t.Fatalf("unexpected log record: want %v, got %v", want, line)
}
}
1 change: 1 addition & 0 deletions x/tx/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Since v0.13.0, x/tx follows Cosmos SDK semver: https://github.com/cosmos/cosmos-
* [#21782](https://github.com/cosmos/cosmos-sdk/pull/21782) Fix JSON attribute sort order on messages with oneof fields.
* [#21825](https://github.com/cosmos/cosmos-sdk/pull/21825) Fix decimal encoding and field ordering in Amino JSON encoder.
* [#21850](https://github.com/cosmos/cosmos-sdk/pull/21850) Support bytes field as signer.
* [#22311](https://github.com/cosmos/cosmos-sdk/pull/22311) Fix add feePayer as signer.

## [v0.13.5](https://github.com/cosmos/cosmos-sdk/releases/tag/x/tx/v0.13.5) - 2024-09-18

Expand Down
12 changes: 12 additions & 0 deletions x/tx/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ func (d *Decoder) Decode(txBytes []byte) (*DecodedTx, error) {
}
}

// If a fee payer is specified in the AuthInfo, it must be added to the list of signers
if authInfo.Fee != nil && authInfo.Fee.Payer != "" {
feeAddr, err := d.signingCtx.AddressCodec().StringToBytes(authInfo.Fee.Payer)
if err != nil {
return nil, errorsmod.Wrap(ErrTxDecode, err.Error())
}

if _, seen := seenSigners[string(feeAddr)]; !seen {
signers = append(signers, feeAddr)
}
}

return &DecodedTx{
Messages: msgs,
DynamicMessages: dynamicMsgs,
Expand Down
36 changes: 30 additions & 6 deletions x/tx/decode/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,42 @@ func TestDecode(t *testing.T) {
gogoproto.RegisterType(&testpb.A{}, string((&testpb.A{}).ProtoReflect().Descriptor().FullName()))

testCases := []struct {
name string
msg proto.Message
error string
name string
msg proto.Message
feePayer string
error string
expectedSigners int
}{
{
name: "happy path",
msg: &bankv1beta1.MsgSend{},
name: "happy path",
msg: &bankv1beta1.MsgSend{},
expectedSigners: 1,
},
{
name: "empty signer option",
msg: &testpb.A{},
error: "no cosmos.msg.v1.signer option found for message A; use DefineCustomGetSigners to specify a custom getter: tx parse error",
},
{
name: "invalid feePayer",
msg: &bankv1beta1.MsgSend{},
feePayer: "payer",
error: `encoding/hex: invalid byte: U+0070 'p': tx parse error`,
},
{
name: "valid feePayer",
msg: &bankv1beta1.MsgSend{},
feePayer: "636f736d6f733168363935356b3836397a72306770383975717034337a373263393033666d35647a366b75306c", // hexadecimal to work with dummyAddressCodec
expectedSigners: 2,
},
{
name: "same msg signer and feePayer",
msg: &bankv1beta1.MsgSend{
FromAddress: "636f736d6f733168363935356b3836397a72306770383975717034337a373263393033666d35647a366b75306c",
},
feePayer: "636f736d6f733168363935356b3836397a72306770383975717034337a373263393033666d35647a366b75306c",
expectedSigners: 1,
},
}

for _, tc := range testCases {
Expand All @@ -94,7 +117,7 @@ func TestDecode(t *testing.T) {
Fee: &txv1beta1.Fee{
Amount: []*basev1beta1.Coin{{Amount: "100", Denom: "denom"}},
GasLimit: 100,
Payer: "payer",
Payer: tc.feePayer,
Granter: "",
},
},
Expand All @@ -109,6 +132,7 @@ func TestDecode(t *testing.T) {
return
}
require.NoError(t, err)
require.Equal(t, len(decodeTx.Signers), tc.expectedSigners)

require.Equal(t,
fmt.Sprintf("/%s", tc.msg.ProtoReflect().Descriptor().FullName()),
Expand Down

0 comments on commit e4047cc

Please sign in to comment.