@@ -3,6 +3,7 @@ package rpc
33import (
44 "context"
55 "fmt"
6+ "net/url"
67 "sync"
78 "sync/atomic"
89 "time"
@@ -32,6 +33,9 @@ type NodeOption func(*Node)
3233type Node struct {
3334 Client * http.HTTP
3435
36+ // Save endpoint url for redacted logging
37+ endpoint * url.URL
38+
3539 onStart []OnNodeStart
3640 onStatus []OnNodeStatus
3741 onEvent map [string ][]OnNodeEvent
@@ -45,8 +49,11 @@ type Node struct {
4549}
4650
4751func NewNode (client * http.HTTP , options ... NodeOption ) * Node {
52+ endpoint , _ := url .Parse (client .Remote ())
53+
4854 node := & Node {
4955 Client : client ,
56+ endpoint : endpoint ,
5057 started : make (chan struct {}),
5158 startedOnce : sync.Once {},
5259 subscriptions : make (map [string ]<- chan ctypes.ResultEvent ),
@@ -60,6 +67,24 @@ func NewNode(client *http.HTTP, options ...NodeOption) *Node {
6067 return node
6168}
6269
70+ func (n * Node ) Endpoint () string {
71+ if n .endpoint == nil {
72+ return n .Client .Remote ()
73+ }
74+ ep := * n .endpoint
75+ if _ , has := ep .User .Password (); has {
76+ ep .User = nil
77+ }
78+ return ep .String ()
79+ }
80+
81+ func (n * Node ) Redacted () string {
82+ if n .endpoint == nil {
83+ return n .Client .Remote ()
84+ }
85+ return n .endpoint .Redacted ()
86+ }
87+
6388func (n * Node ) OnStart (callback OnNodeStart ) {
6489 n .onStart = append (n .onStart , callback )
6590}
@@ -142,25 +167,25 @@ func (n *Node) Start(ctx context.Context) error {
142167 for {
143168 select {
144169 case <- ctx .Done ():
145- log .Debug ().Err (ctx .Err ()).Str ("node" , n .Client . Remote ()).Msgf ("stopping node status loop" )
170+ log .Debug ().Err (ctx .Err ()).Str ("node" , n .Redacted ()).Msgf ("stopping node status loop" )
146171 return nil
147172
148173 case evt := <- blocksEvents :
149- log .Debug ().Str ("node" , n .Client . Remote ()).Msg ("got new block event" )
174+ log .Debug ().Str ("node" , n .Redacted ()).Msg ("got new block event" )
150175 n .saveLatestBlock (evt .Data .(types.EventDataNewBlock ).Block )
151176 n .handleEvent (ctx , EventNewBlock , & evt )
152177 blocksTicker .Reset (10 * time .Second )
153178
154179 case evt := <- validatorEvents :
155- log .Debug ().Str ("node" , n .Client . Remote ()).Msg ("got validator set update event" )
180+ log .Debug ().Str ("node" , n .Redacted ()).Msg ("got validator set update event" )
156181 n .handleEvent (ctx , EventValidatorSetUpdates , & evt )
157182
158183 case <- blocksTicker .C :
159- log .Debug ().Str ("node" , n .Client . Remote ()).Msg ("syncing latest blocks" )
184+ log .Debug ().Str ("node" , n .Redacted ()).Msg ("syncing latest blocks" )
160185 n .syncBlocks (ctx )
161186
162187 case <- statusTicker .C :
163- log .Debug ().Str ("node" , n .Client . Remote ()).Msg ("syncing status" )
188+ log .Debug ().Str ("node" , n .Redacted ()).Msg ("syncing status" )
164189 n .syncStatus (ctx )
165190 }
166191 }
@@ -190,7 +215,7 @@ func (n *Node) syncStatus(ctx context.Context) (*ctypes.ResultStatus, error) {
190215 retry .Delay (1 * time .Second ),
191216 retry .Attempts (2 ),
192217 // retry.OnRetry(func(_ uint, err error) {
193- // log.Warn().Err(err).Msgf("retrying status on %s", n.Client.Remote ())
218+ // log.Warn().Err(err).Msgf("retrying status on %s", n.Redacted ())
194219 // }),
195220 }
196221
@@ -201,12 +226,12 @@ func (n *Node) syncStatus(ctx context.Context) (*ctypes.ResultStatus, error) {
201226 n .status .Store (status )
202227
203228 if err != nil {
204- return status , fmt .Errorf ("failed to get status of %s: %w" , n .Client . Remote (), err )
229+ return status , fmt .Errorf ("failed to get status of %s: %w" , n .Redacted (), err )
205230 }
206231
207232 if status .SyncInfo .CatchingUp {
208233 // We're catching up, not synced
209- log .Warn ().Msgf ("node %s is catching up at block %d" , n .Client . Remote (), status .SyncInfo .LatestBlockHeight )
234+ log .Warn ().Msgf ("node %s is catching up at block %d" , n .Redacted (), status .SyncInfo .LatestBlockHeight )
210235 return status , nil
211236 }
212237
0 commit comments