-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from maticnetwork/jhilliard/monitor-panic-fix
ENR Parser and Monitor Fixes
- Loading branch information
Showing
8 changed files
with
321 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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,94 @@ | ||
package enr | ||
|
||
import ( | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/p2p/enode" | ||
) | ||
|
||
var ( | ||
//go:embed usage.md | ||
usage string | ||
inputFileName *string | ||
) | ||
|
||
var ENRCmd = &cobra.Command{ | ||
Use: "enr [flags]", | ||
Short: "Convert between ENR and Enode format", | ||
Long: usage, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
rawData, err := getInputData(cmd, args) | ||
if err != nil { | ||
log.Error().Err(err).Msg("Unable to read input") | ||
return err | ||
} | ||
lines := strings.Split(string(rawData), "\n") | ||
|
||
for _, l := range lines { | ||
var node *enode.Node | ||
var err error | ||
l = strings.TrimSpace(l) | ||
if l == "" { | ||
continue | ||
} | ||
isENR := false | ||
if strings.HasPrefix(l, "enr:") { | ||
isENR = true | ||
node, err = enode.Parse(enode.V4ID{}, l) | ||
if err != nil { | ||
log.Error().Err(err).Str("line", l).Msg("Unable to parse enr record") | ||
continue | ||
} | ||
} else { | ||
node, err = enode.ParseV4(l) | ||
if err != nil { | ||
log.Error().Err(err).Str("line", l).Msg("Unable to parse node record") | ||
continue | ||
} | ||
} | ||
genericNode := make(map[string]string, 0) | ||
if isENR { | ||
genericNode["enr"] = node.String() | ||
} | ||
genericNode["enode"] = node.URLv4() | ||
genericNode["id"] = node.ID().String() | ||
genericNode["ip"] = node.IP().String() | ||
genericNode["tcp"] = fmt.Sprintf("%d", node.TCP()) | ||
genericNode["udp"] = fmt.Sprintf("%d", node.UDP()) | ||
jsonOut, err := json.Marshal(genericNode) | ||
if err != nil { | ||
log.Error().Err(err).Msg("unable to convert node to json") | ||
continue | ||
} | ||
fmt.Println(string(jsonOut)) | ||
} | ||
return nil | ||
}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
flagSet := ENRCmd.PersistentFlags() | ||
inputFileName = flagSet.String("file", "", "Provide a file that's holding ENRs") | ||
} | ||
func getInputData(cmd *cobra.Command, args []string) ([]byte, error) { | ||
if inputFileName != nil && *inputFileName != "" { | ||
return os.ReadFile(*inputFileName) | ||
} | ||
|
||
if len(args) >= 1 { | ||
concat := strings.Join(args, "\n") | ||
return []byte(concat), nil | ||
} | ||
|
||
return io.ReadAll(os.Stdin) | ||
} |
This file contains 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,37 @@ | ||
This function is meant to help handle ENR data. Given an input ENR it will output the parsed enode and other values that are part of the payload. | ||
|
||
The command below will take an ENR and process it: | ||
```bash | ||
echo 'enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8' | \ | ||
polycli enr | jq '.' | ||
``` | ||
|
||
This is the output: | ||
```json | ||
{ | ||
"enode": "enode://ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31387574077f301b421bc84df7266c44e9e6d569fc56be00812904767bf5ccd1fc7f@127.0.0.1:0?discport=30303", | ||
"enr": "enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8", | ||
"id": "a448f24c6d18e575453db13171562b71999873db5b286df957af199ec94617f7", | ||
"ip": "127.0.0.1", | ||
"tcp": "0", | ||
"udp": "30303" | ||
} | ||
``` | ||
|
||
This command can be used a few different ways | ||
```bash | ||
enr_data="enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8" | ||
|
||
# First form - reading from stdin | ||
echo "$enr_data" | polycli enr | ||
|
||
# Second form - reading from file | ||
tmp_file="$(mktemp)" | ||
echo "$enr_data" > "$tmp_file" | ||
polycli enr --file "$tmp_file" | ||
|
||
# Third form - command line args | ||
polycli enr "$enr_data" | ||
``` | ||
|
||
All three forms support multiple lines. Each line will be convert into a JSON object and printed. |
This file contains 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.