Skip to content

Commit c52dfd5

Browse files
authored
p2p/simulations/adapters/exec: fix some issues (ethereum#21801)
- Remove the ws:// prefix from the status endpoint since the ws:// is already included in the stack.WSEndpoint(). - Don't register the services again in the node start. Registration is already done in the initialization stage. - Expose admin namespace via websocket. This namespace is necessary for connecting the peers via websocket. - Offer logging relevant options for exec adapter. It's really painful to mix all log output in the single console. So this PR offers two additional options for exec adapter in this case testers can config the log output(e.g. file output) and log level for each p2p node.
1 parent 0c34eae commit c52dfd5

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

p2p/simulations/adapters/exec.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,19 @@ func (n *ExecNode) Start(snapshots map[string][]byte) (err error) {
184184
if err != nil {
185185
return fmt.Errorf("error generating node config: %s", err)
186186
}
187-
187+
// expose the admin namespace via websocket if it's not enabled
188+
exposed := confCopy.Stack.WSExposeAll
189+
if !exposed {
190+
for _, api := range confCopy.Stack.WSModules {
191+
if api == "admin" {
192+
exposed = true
193+
break
194+
}
195+
}
196+
}
197+
if !exposed {
198+
confCopy.Stack.WSModules = append(confCopy.Stack.WSModules, "admin")
199+
}
188200
// start the one-shot server that waits for startup information
189201
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
190202
defer cancel()
@@ -362,13 +374,44 @@ type execNodeConfig struct {
362374
PeerAddrs map[string]string `json:"peer_addrs,omitempty"`
363375
}
364376

377+
func initLogging() {
378+
// Initialize the logging by default first.
379+
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
380+
glogger.Verbosity(log.LvlInfo)
381+
log.Root().SetHandler(glogger)
382+
383+
confEnv := os.Getenv(envNodeConfig)
384+
if confEnv == "" {
385+
return
386+
}
387+
var conf execNodeConfig
388+
if err := json.Unmarshal([]byte(confEnv), &conf); err != nil {
389+
return
390+
}
391+
var writer = os.Stderr
392+
if conf.Node.LogFile != "" {
393+
logWriter, err := os.Create(conf.Node.LogFile)
394+
if err != nil {
395+
return
396+
}
397+
writer = logWriter
398+
}
399+
var verbosity = log.LvlInfo
400+
if conf.Node.LogVerbosity <= log.LvlTrace && conf.Node.LogVerbosity >= log.LvlCrit {
401+
verbosity = conf.Node.LogVerbosity
402+
}
403+
// Reinitialize the logger
404+
glogger = log.NewGlogHandler(log.StreamHandler(writer, log.TerminalFormat(true)))
405+
glogger.Verbosity(verbosity)
406+
log.Root().SetHandler(glogger)
407+
}
408+
365409
// execP2PNode starts a simulation node when the current binary is executed with
366410
// argv[0] being "p2p-node", reading the service / ID from argv[1] / argv[2]
367411
// and the node config from an environment variable.
368412
func execP2PNode() {
369-
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
370-
glogger.Verbosity(log.LvlInfo)
371-
log.Root().SetHandler(glogger)
413+
initLogging()
414+
372415
statusURL := os.Getenv(envStatusURL)
373416
if statusURL == "" {
374417
log.Crit("missing " + envStatusURL)
@@ -380,7 +423,7 @@ func execP2PNode() {
380423
if stackErr != nil {
381424
status.Err = stackErr.Error()
382425
} else {
383-
status.WSEndpoint = "ws://" + stack.WSEndpoint()
426+
status.WSEndpoint = stack.WSEndpoint()
384427
status.NodeInfo = stack.Server().NodeInfo()
385428
}
386429

@@ -454,7 +497,6 @@ func startExecNodeStack() (*node.Node, error) {
454497
return nil, err
455498
}
456499
services[name] = service
457-
stack.RegisterLifecycle(service)
458500
}
459501

460502
// Add the snapshot API.

p2p/simulations/adapters/types.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ type NodeConfig struct {
120120
Reachable func(id enode.ID) bool
121121

122122
Port uint16
123+
124+
// LogFile is the log file name of the p2p node at runtime.
125+
//
126+
// The default value is empty so that the default log writer
127+
// is the system standard output.
128+
LogFile string
129+
130+
// LogVerbosity is the log verbosity of the p2p node at runtime.
131+
//
132+
// The default verbosity is INFO.
133+
LogVerbosity log.Lvl
123134
}
124135

125136
// nodeConfigJSON is used to encode and decode NodeConfig as JSON by encoding
@@ -128,10 +139,12 @@ type nodeConfigJSON struct {
128139
ID string `json:"id"`
129140
PrivateKey string `json:"private_key"`
130141
Name string `json:"name"`
131-
Services []string `json:"services"`
142+
Lifecycles []string `json:"lifecycles"`
132143
Properties []string `json:"properties"`
133144
EnableMsgEvents bool `json:"enable_msg_events"`
134145
Port uint16 `json:"port"`
146+
LogFile string `json:"logfile"`
147+
LogVerbosity int `json:"log_verbosity"`
135148
}
136149

137150
// MarshalJSON implements the json.Marshaler interface by encoding the config
@@ -140,10 +153,12 @@ func (n *NodeConfig) MarshalJSON() ([]byte, error) {
140153
confJSON := nodeConfigJSON{
141154
ID: n.ID.String(),
142155
Name: n.Name,
143-
Services: n.Lifecycles,
156+
Lifecycles: n.Lifecycles,
144157
Properties: n.Properties,
145158
Port: n.Port,
146159
EnableMsgEvents: n.EnableMsgEvents,
160+
LogFile: n.LogFile,
161+
LogVerbosity: int(n.LogVerbosity),
147162
}
148163
if n.PrivateKey != nil {
149164
confJSON.PrivateKey = hex.EncodeToString(crypto.FromECDSA(n.PrivateKey))
@@ -178,10 +193,12 @@ func (n *NodeConfig) UnmarshalJSON(data []byte) error {
178193
}
179194

180195
n.Name = confJSON.Name
181-
n.Lifecycles = confJSON.Services
196+
n.Lifecycles = confJSON.Lifecycles
182197
n.Properties = confJSON.Properties
183198
n.Port = confJSON.Port
184199
n.EnableMsgEvents = confJSON.EnableMsgEvents
200+
n.LogFile = confJSON.LogFile
201+
n.LogVerbosity = log.Lvl(confJSON.LogVerbosity)
185202

186203
return nil
187204
}
@@ -211,6 +228,7 @@ func RandomNodeConfig() *NodeConfig {
211228
Name: fmt.Sprintf("node_%s", enodId.String()),
212229
Port: port,
213230
EnableMsgEvents: true,
231+
LogVerbosity: log.LvlInfo,
214232
}
215233
}
216234

0 commit comments

Comments
 (0)