Skip to content

Commit

Permalink
fix (*(*eth/common.Hash)(nil)).String()
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Jan 30, 2025
1 parent adb4950 commit 48435cb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ type (

d int
}

dumpWrapper struct {
Span

loc loc.PC
msg string
key string
}
)

var (
Expand Down Expand Up @@ -432,12 +440,40 @@ func (s Span) IOWriter(d int) io.Writer {
}
}

func (l *Logger) DumpWriter(d int, msg, key string) io.Writer {
return dumpWrapper{
Span: Span{
Logger: l,
},

loc: loc.Caller(1 + d),
msg: msg,
key: key,
}
}

func (s Span) DumpWriter(d int, msg, key string) io.Writer {
return dumpWrapper{
Span: s,

loc: loc.Caller(1 + d),
msg: msg,
key: key,
}
}

func (w writeWrapper) Write(p []byte) (int, error) {
message(w.Logger, w.ID, w.d, p, nil)

return len(p), nil
}

func (w dumpWrapper) Write(p []byte) (int, error) {
message(w.Logger, w.ID, -1, w.msg, []any{KeyCaller, w.loc, w.key, p})

return len(p), nil
}

func (l *Logger) Write(p []byte) (int, error) {
if l == nil || l.Writer == nil {
return len(p), nil
Expand Down
34 changes: 34 additions & 0 deletions tlwire/encoder_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tlwire

import (
"fmt"
"math/big"
"net/netip"
"net/url"
"reflect"
Expand Down Expand Up @@ -109,6 +110,32 @@ func init() {

return e.AppendString(b, u.String())
})

SetEncoder((*big.Int)(nil), func(e *Encoder, b []byte, x interface{}) []byte {
b = e.AppendSemantic(b, Big)

y := x.(*big.Int)
if y == nil {
return e.AppendNil(b)
}

if y.Sign() >= 0 && y.BitLen() <= 64 {
return e.AppendUint64(b, y.Uint64())
}

if y.BitLen() <= 63 {
return e.AppendInt64(b, y.Int64())
}

b = e.AppendTag(b, String, 0)
st := len(b)

b = y.Append(b, 10)

b = e.InsertLen(b, st, len(b)-st)

return b
})
}

func (e *Encoder) AppendKeyValue(b []byte, key string, v interface{}) []byte {
Expand Down Expand Up @@ -162,6 +189,13 @@ func (e *Encoder) appendRaw(b []byte, r reflect.Value, visited ptrSet) []byte {
switch v := v.(type) {
case TlogAppender:
return v.TlogAppend(b)
}

if r.Kind() == reflect.Pointer && r.IsNil() {
return e.AppendNil(b)
}

switch v := v.(type) {
case interface{ ProtoMessage() }:
// skip
case error:
Expand Down

0 comments on commit 48435cb

Please sign in to comment.