From fa6219212e6b0fee3a6eb062ca11e45191ab94ea Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Thu, 13 May 2021 17:21:22 -0400 Subject: [PATCH 01/15] more or less feature complete, needs unit tests --- validator/graffiti/BUILD.bazel | 7 ++++- validator/graffiti/log.go | 5 +++ validator/graffiti/parse_graffiti.go | 37 +++++++++++++++++++++++ validator/graffiti/parse_graffiti_test.go | 2 ++ validator/node/node.go | 4 +-- 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 validator/graffiti/log.go diff --git a/validator/graffiti/BUILD.bazel b/validator/graffiti/BUILD.bazel index 672fb4fa05b9..e9b6534f5c83 100644 --- a/validator/graffiti/BUILD.bazel +++ b/validator/graffiti/BUILD.bazel @@ -3,13 +3,17 @@ load("@prysm//tools/go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = ["parse_graffiti.go"], + srcs = [ + "log.go", + "parse_graffiti.go" + ], importpath = "github.com/prysmaticlabs/prysm/validator/graffiti", visibility = ["//validator:__subpackages__"], deps = [ "//shared/hashutil:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", "@in_gopkg_yaml_v2//:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", ], ) @@ -21,5 +25,6 @@ go_test( "//shared/hashutil:go_default_library", "//shared/testutil/require:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", ], ) diff --git a/validator/graffiti/log.go b/validator/graffiti/log.go new file mode 100644 index 000000000000..b689212ba49a --- /dev/null +++ b/validator/graffiti/log.go @@ -0,0 +1,5 @@ +package graffiti + +import "github.com/sirupsen/logrus" + +var log = logrus.WithField("prefix", "graffiti") diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 9e23dfebc2a0..9c45e4a24256 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -1,7 +1,9 @@ package graffiti import ( + "encoding/hex" "io/ioutil" + "strings" types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/shared/hashutil" @@ -27,6 +29,41 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { if err := yaml.Unmarshal(yamlFile, g); err != nil { return nil, err } + + specific := make(map[types.ValidatorIndex]string, len(g.Specific)) + for i, o := range g.Specific { + specific[types.ValidatorIndex(i)] = ParseHexGraffiti(o) + } + g.Specific = specific + g.Default = ParseHexGraffiti(g.Default) + g.Ordered = ParseGraffitiStrings(g.Ordered) + g.Random = ParseGraffitiStrings(g.Random) g.Hash = hashutil.Hash(yamlFile) return g, nil } + +func ParseGraffitiStrings(input []string) []string { + output := make([]string, len(input)) + for _, i := range input { + output = append(output, ParseHexGraffiti(i)) + } + return output +} + +// ParseHexGraffiti checks if a graffiti input is being represented in hex and converts it to ASCII if so +func ParseHexGraffiti(rawGraffiti string) string { + splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) + if "hex" == splitGraffiti[0] { + if splitGraffiti[1] == "" { + log.Debug("Blank hex tag to be interpreted as itself") + return rawGraffiti + } + + graffiti, err := hex.DecodeString(splitGraffiti[1]) + if err != nil { + log.WithError(err).Debug("Error while decoding hex string") + } + return string(graffiti) + } + return rawGraffiti +} diff --git a/validator/graffiti/parse_graffiti_test.go b/validator/graffiti/parse_graffiti_test.go index 46c8cea1c0e9..6fff5984acd5 100644 --- a/validator/graffiti/parse_graffiti_test.go +++ b/validator/graffiti/parse_graffiti_test.go @@ -11,6 +11,8 @@ import ( "github.com/prysmaticlabs/prysm/shared/testutil/require" ) +// TODO: don't forget tests fucko + func TestParseGraffitiFile_Default(t *testing.T) { input := []byte(`default: "Mr T was here"`) diff --git a/validator/node/node.go b/validator/node/node.go index 27adbbd524df..20cc74020474 100644 --- a/validator/node/node.go +++ b/validator/node/node.go @@ -409,7 +409,7 @@ func (c *ValidatorClient) registerValidatorService( LogValidatorBalances: logValidatorBalances, EmitAccountMetrics: emitAccountMetrics, CertFlag: cert, - GraffitiFlag: graffiti, + GraffitiFlag: g.ParseHexGraffiti(graffiti), GrpcMaxCallRecvMsgSizeFlag: maxCallRecvMsgSize, GrpcRetriesFlag: grpcRetries, GrpcRetryDelay: grpcRetryDelay, @@ -421,10 +421,10 @@ func (c *ValidatorClient) registerValidatorService( GraffitiStruct: gStruct, LogDutyCountDown: c.cliCtx.Bool(flags.EnableDutyCountDown.Name), }) - if err != nil { return errors.Wrap(err, "could not initialize validator service") } + return c.services.RegisterService(v) } func (c *ValidatorClient) registerSlasherService() error { From 63b504bcfdb026328ba55898be46534aaf6c642e Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Fri, 14 May 2021 15:00:21 -0400 Subject: [PATCH 02/15] Test support and concomitant refactors --- validator/graffiti/parse_graffiti.go | 23 +++++----- validator/graffiti/parse_graffiti_test.go | 53 ++++++++++++++++++++++- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 9c45e4a24256..69dbbf52ae6e 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -21,6 +21,14 @@ type Graffiti struct { // ParseGraffitiFile parses the graffiti file and returns the graffiti struct. func ParseGraffitiFile(f string) (*Graffiti, error) { + parseGraffitiStrings := func(input []string) []string { + output := make([]string, len(input)) + for _, i := range input { + output = append(output, ParseHexGraffiti(i)) + } + return output + } + yamlFile, err := ioutil.ReadFile(f) if err != nil { return nil, err @@ -36,24 +44,16 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { } g.Specific = specific g.Default = ParseHexGraffiti(g.Default) - g.Ordered = ParseGraffitiStrings(g.Ordered) - g.Random = ParseGraffitiStrings(g.Random) + g.Ordered = parseGraffitiStrings(g.Ordered) + g.Random = parseGraffitiStrings(g.Random) g.Hash = hashutil.Hash(yamlFile) return g, nil } -func ParseGraffitiStrings(input []string) []string { - output := make([]string, len(input)) - for _, i := range input { - output = append(output, ParseHexGraffiti(i)) - } - return output -} - // ParseHexGraffiti checks if a graffiti input is being represented in hex and converts it to ASCII if so func ParseHexGraffiti(rawGraffiti string) string { splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) - if "hex" == splitGraffiti[0] { + if strings.ToLower(splitGraffiti[0]) == "hex" { if splitGraffiti[1] == "" { log.Debug("Blank hex tag to be interpreted as itself") return rawGraffiti @@ -62,6 +62,7 @@ func ParseHexGraffiti(rawGraffiti string) string { graffiti, err := hex.DecodeString(splitGraffiti[1]) if err != nil { log.WithError(err).Debug("Error while decoding hex string") + return rawGraffiti } return string(graffiti) } diff --git a/validator/graffiti/parse_graffiti_test.go b/validator/graffiti/parse_graffiti_test.go index 6fff5984acd5..6e3dd9700312 100644 --- a/validator/graffiti/parse_graffiti_test.go +++ b/validator/graffiti/parse_graffiti_test.go @@ -125,7 +125,7 @@ ordered: - "Mr F was here" specific: - 1234: Yolo + 1234: "hex:796f6c6f" 555: "What's up" 703727: Meow`) @@ -159,3 +159,54 @@ specific: } require.DeepEqual(t, wanted, got) } + +func TestParseHexGraffiti(t *testing.T) { + tests := []struct { + name string + want string + input string + }{ + { + name: "standard", + want: "hola mundo!", + input: "hola mundo!", + }, + { + name: "hex", + want: "hola mundo!", + input: "hex:686f6c61206d756e646f21", + }, + { + name: "HEX", + want: "hola mundo!", + input: "HEX:686f6c61206d756e646f21", + }, + { + name: "irregular hex 1", + want: "hex:", + input: "hex:", + }, + { + name: "irregular hex 1", + want: "hex:hola mundo!", + input: "hex:hola mundo!", + }, + { + name: "unsupported hex 1", + want: "0x686f6c61206d756e646f21", + input: "0x686f6c61206d756e646f21", + }, + { + name: "unsupported hex 2", + want: "hex:0x686f6c61206d756e646f21", + input: "hex:0x686f6c61206d756e646f21", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + out := ParseHexGraffiti(tt.input) + require.Equal(t, out, tt.want) + }) + } +} From a3d2e1cf5a15742dcae11e9d79dfee9097e77783 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Mon, 17 May 2021 12:34:54 -0400 Subject: [PATCH 03/15] Resolving requested changes on PR --- validator/graffiti/parse_graffiti.go | 18 ++++++++++++-- validator/graffiti/parse_graffiti_test.go | 29 +++++++++++++++-------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 69dbbf52ae6e..b04be4e97260 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -21,6 +21,7 @@ type Graffiti struct { // ParseGraffitiFile parses the graffiti file and returns the graffiti struct. func ParseGraffitiFile(f string) (*Graffiti, error) { + // helper function to avoid coding this twice while parsing the Ordered and Random fields of the graffiti struct parseGraffitiStrings := func(input []string) []string { output := make([]string, len(input)) for _, i := range input { @@ -42,6 +43,7 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { for i, o := range g.Specific { specific[types.ValidatorIndex(i)] = ParseHexGraffiti(o) } + g.Specific = specific g.Default = ParseHexGraffiti(g.Default) g.Ordered = parseGraffitiStrings(g.Ordered) @@ -54,12 +56,24 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { func ParseHexGraffiti(rawGraffiti string) string { splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) if strings.ToLower(splitGraffiti[0]) == "hex" { - if splitGraffiti[1] == "" { + target := splitGraffiti[1] + if target == "" { log.Debug("Blank hex tag to be interpreted as itself") return rawGraffiti } - graffiti, err := hex.DecodeString(splitGraffiti[1]) + if len(target) > 3 { + if target[:2] == "0x" { + target = target[2:] + } + } + + if target == "" { + log.Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") + return rawGraffiti + } + + graffiti, err := hex.DecodeString(target) if err != nil { log.WithError(err).Debug("Error while decoding hex string") return rawGraffiti diff --git a/validator/graffiti/parse_graffiti_test.go b/validator/graffiti/parse_graffiti_test.go index 6e3dd9700312..3dc9782e62f7 100644 --- a/validator/graffiti/parse_graffiti_test.go +++ b/validator/graffiti/parse_graffiti_test.go @@ -8,11 +8,10 @@ import ( types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/prysmaticlabs/prysm/shared/testutil/assert" "github.com/prysmaticlabs/prysm/shared/testutil/require" ) -// TODO: don't forget tests fucko - func TestParseGraffitiFile_Default(t *testing.T) { input := []byte(`default: "Mr T was here"`) @@ -172,41 +171,51 @@ func TestParseHexGraffiti(t *testing.T) { input: "hola mundo!", }, { - name: "hex", + name: "standard with hex tag", want: "hola mundo!", input: "hex:686f6c61206d756e646f21", }, { - name: "HEX", + name: "irregularly cased hex tag", want: "hola mundo!", input: "HEX:686f6c61206d756e646f21", }, { - name: "irregular hex 1", + name: "hex tag without accompanying data", want: "hex:", input: "hex:", }, { - name: "irregular hex 1", + name: "Passing non-hex data with hex tag", want: "hex:hola mundo!", input: "hex:hola mundo!", }, { - name: "unsupported hex 1", + name: "unmarked hex input", want: "0x686f6c61206d756e646f21", input: "0x686f6c61206d756e646f21", }, { - name: "unsupported hex 2", - want: "hex:0x686f6c61206d756e646f21", + name: "Properly tagged hex data with 0x prefix", + want: "hola mundo!", input: "hex:0x686f6c61206d756e646f21", }, + { + name: "hex tag with 0x prefix and no other data", + want: "hex:0x", + input: "hex:0x", + }, + { + name: "hex tag with 0x prefix and invalid hex data", + want: "hex:0xhola mundo", + input: "hex:0xhola mundo", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { out := ParseHexGraffiti(tt.input) - require.Equal(t, out, tt.want) + assert.Equal(t, out, tt.want) }) } } From c23fb0776f679b811b57bb2a68354a8bf885454c Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Mon, 24 May 2021 12:22:01 -0400 Subject: [PATCH 04/15] minor fix before trying to figure out bazel --- validator/graffiti/parse_graffiti.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index b04be4e97260..eda5b9d2a480 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -62,10 +62,8 @@ func ParseHexGraffiti(rawGraffiti string) string { return rawGraffiti } - if len(target) > 3 { - if target[:2] == "0x" { - target = target[2:] - } + if len(target) > 3 && target[:2] == "0x" { + target = target[2:] } if target == "" { From 37debdfa44fc4aabd1b280f04f186bb2a7046369 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Mon, 24 May 2021 12:28:53 -0400 Subject: [PATCH 05/15] bazel change --- validator/graffiti/BUILD.bazel | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/validator/graffiti/BUILD.bazel b/validator/graffiti/BUILD.bazel index e9b6534f5c83..eff9a265b9ae 100644 --- a/validator/graffiti/BUILD.bazel +++ b/validator/graffiti/BUILD.bazel @@ -5,15 +5,15 @@ go_library( name = "go_default_library", srcs = [ "log.go", - "parse_graffiti.go" + "parse_graffiti.go", ], importpath = "github.com/prysmaticlabs/prysm/validator/graffiti", visibility = ["//validator:__subpackages__"], deps = [ "//shared/hashutil:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", - "@in_gopkg_yaml_v2//:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", + "@in_gopkg_yaml_v2//:go_default_library", ], ) @@ -23,8 +23,8 @@ go_test( embed = [":go_default_library"], deps = [ "//shared/hashutil:go_default_library", + "//shared/testutil/assert:go_default_library", "//shared/testutil/require:go_default_library", "@com_github_prysmaticlabs_eth2_types//:go_default_library", - "@com_github_sirupsen_logrus//:go_default_library", ], ) From 33582095e258696b0c544aa6ec3ce86ea955f57e Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Mon, 24 May 2021 13:42:25 -0400 Subject: [PATCH 06/15] minor changes requested in pr --- validator/graffiti/parse_graffiti.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index eda5b9d2a480..1950dae289cb 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -10,6 +10,11 @@ import ( "gopkg.in/yaml.v2" ) +const ( + hexGraffitiPrefixKey = "hex" + hex0xPrefix = "0x" +) + // Graffiti is a graffiti container. type Graffiti struct { Hash [32]byte @@ -55,19 +60,23 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { // ParseHexGraffiti checks if a graffiti input is being represented in hex and converts it to ASCII if so func ParseHexGraffiti(rawGraffiti string) string { splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) - if strings.ToLower(splitGraffiti[0]) == "hex" { + if strings.ToLower(splitGraffiti[0]) == hexGraffitiPrefixKey { target := splitGraffiti[1] if target == "" { - log.Debug("Blank hex tag to be interpreted as itself") + log.WithFields(logrus.Fields{ + "input": rawGraffiti + }).Debug("Blank hex tag to be interpreted as itself") return rawGraffiti } - if len(target) > 3 && target[:2] == "0x" { + if len(target) > 3 && target[:2] == hex0xPrefix { target = target[2:] } if target == "" { - log.Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") + log.WithFields(logrus.Fields{ + "input": rawGraffiti + }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") return rawGraffiti } From afb8bc9dd7f305774c5543b936afbed929cb3338 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Mon, 24 May 2021 17:33:29 -0400 Subject: [PATCH 07/15] fixing build errors --- validator/graffiti/parse_graffiti.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 1950dae289cb..002345948334 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -64,7 +64,7 @@ func ParseHexGraffiti(rawGraffiti string) string { target := splitGraffiti[1] if target == "" { log.WithFields(logrus.Fields{ - "input": rawGraffiti + "input": rawGraffiti, }).Debug("Blank hex tag to be interpreted as itself") return rawGraffiti } @@ -75,7 +75,7 @@ func ParseHexGraffiti(rawGraffiti string) string { if target == "" { log.WithFields(logrus.Fields{ - "input": rawGraffiti + "input": rawGraffiti, }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") return rawGraffiti } From 011026cd2825c09da0d926a10f79bfa98f34803a Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Mon, 24 May 2021 18:39:33 -0400 Subject: [PATCH 08/15] fixing build errors --- validator/graffiti/parse_graffiti.go | 1 + 1 file changed, 1 insertion(+) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 002345948334..43297cc243bf 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -7,6 +7,7 @@ import ( types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/shared/hashutil" + "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) From a1de46ed0b6fa093f4c7da0a1f8553ebd7dc9ae8 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Fri, 28 May 2021 12:10:58 -0400 Subject: [PATCH 09/15] fixing PR feedback --- validator/graffiti/parse_graffiti_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/graffiti/parse_graffiti_test.go b/validator/graffiti/parse_graffiti_test.go index 3dc9782e62f7..c3098718b42c 100644 --- a/validator/graffiti/parse_graffiti_test.go +++ b/validator/graffiti/parse_graffiti_test.go @@ -124,7 +124,7 @@ ordered: - "Mr F was here" specific: - 1234: "hex:796f6c6f" + 1234: "Yolo" 555: "What's up" 703727: Meow`) From ea2de75ca95430f8f90f7753c27544a0e4940281 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Fri, 28 May 2021 12:12:39 -0400 Subject: [PATCH 10/15] fixing PR feedback --- validator/graffiti/parse_graffiti_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/graffiti/parse_graffiti_test.go b/validator/graffiti/parse_graffiti_test.go index c3098718b42c..35e3ce4e0f21 100644 --- a/validator/graffiti/parse_graffiti_test.go +++ b/validator/graffiti/parse_graffiti_test.go @@ -124,7 +124,7 @@ ordered: - "Mr F was here" specific: - 1234: "Yolo" + 1234: Yolo 555: "What's up" 703727: Meow`) From f57f9764fb2f2621cf91ada3b8da181ec155125f Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Tue, 8 Jun 2021 17:37:03 -0400 Subject: [PATCH 11/15] resolving nitpicks --- validator/graffiti/parse_graffiti.go | 51 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 43297cc243bf..eb90350ec6f7 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -12,8 +12,8 @@ import ( ) const ( - hexGraffitiPrefixKey = "hex" - hex0xPrefix = "0x" + hexGraffitiPrefix = "hex" + hex0xPrefix = "0x" ) // Graffiti is a graffiti container. @@ -61,32 +61,31 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { // ParseHexGraffiti checks if a graffiti input is being represented in hex and converts it to ASCII if so func ParseHexGraffiti(rawGraffiti string) string { splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) - if strings.ToLower(splitGraffiti[0]) == hexGraffitiPrefixKey { - target := splitGraffiti[1] - if target == "" { - log.WithFields(logrus.Fields{ - "input": rawGraffiti, - }).Debug("Blank hex tag to be interpreted as itself") - return rawGraffiti - } + if strings.ToLower(splitGraffiti[0]) != hexGraffitiPrefixKey { + return rawGraffiti + } - if len(target) > 3 && target[:2] == hex0xPrefix { - target = target[2:] - } + target := splitGraffiti[1] + if target == "" { + log.WithFields("graffiti", rawGraffiti).Debug("Blank hex tag to be interpreted as itself") + return rawGraffiti + } - if target == "" { - log.WithFields(logrus.Fields{ - "input": rawGraffiti, - }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") - return rawGraffiti - } + if len(target) > 3 && target[:2] == hex0xPrefix { + target = target[2:] + } - graffiti, err := hex.DecodeString(target) - if err != nil { - log.WithError(err).Debug("Error while decoding hex string") - return rawGraffiti - } - return string(graffiti) + if target == "" { + log.WithFields(logrus.Fields{ + "input": rawGraffiti, + }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") + return rawGraffiti + } + + graffiti, err := hex.DecodeString(target) + if err != nil { + log.WithError(err).Debug("Error while decoding hex string") + return rawGraffiti } - return rawGraffiti + return string(graffiti) } From 68e2e00ae145839afdd30468e66f2ed907e4dad0 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Tue, 8 Jun 2021 18:15:47 -0400 Subject: [PATCH 12/15] resolving nitpicks --- validator/graffiti/parse_graffiti.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index eb90350ec6f7..7a41a0a8996b 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -67,7 +67,7 @@ func ParseHexGraffiti(rawGraffiti string) string { target := splitGraffiti[1] if target == "" { - log.WithFields("graffiti", rawGraffiti).Debug("Blank hex tag to be interpreted as itself") + log.WithField("graffiti", rawGraffiti).Debug("Blank hex tag to be interpreted as itself") return rawGraffiti } From e4e54ddbede395dcef8e403d92a0caffc59650c4 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Wed, 9 Jun 2021 12:09:03 -0400 Subject: [PATCH 13/15] build failures REEEEE --- validator/graffiti/parse_graffiti.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 7a41a0a8996b..7a837fd2098b 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -61,7 +61,7 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { // ParseHexGraffiti checks if a graffiti input is being represented in hex and converts it to ASCII if so func ParseHexGraffiti(rawGraffiti string) string { splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) - if strings.ToLower(splitGraffiti[0]) != hexGraffitiPrefixKey { + if strings.ToLower(splitGraffiti[0]) != hexGraffitiPrefix { return rawGraffiti } From e2454b5273ede021f6a290630d9686a220e9430a Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Wed, 9 Jun 2021 12:48:09 -0400 Subject: [PATCH 14/15] tests fail if its not this way, so this way it is --- validator/graffiti/parse_graffiti.go | 50 +++++++++++++--------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index 7a837fd2098b..d7b41e1ec0dd 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -61,31 +61,29 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { // ParseHexGraffiti checks if a graffiti input is being represented in hex and converts it to ASCII if so func ParseHexGraffiti(rawGraffiti string) string { splitGraffiti := strings.SplitN(rawGraffiti, ":", 2) - if strings.ToLower(splitGraffiti[0]) != hexGraffitiPrefix { - return rawGraffiti - } - - target := splitGraffiti[1] - if target == "" { - log.WithField("graffiti", rawGraffiti).Debug("Blank hex tag to be interpreted as itself") - return rawGraffiti - } - - if len(target) > 3 && target[:2] == hex0xPrefix { - target = target[2:] - } - - if target == "" { - log.WithFields(logrus.Fields{ - "input": rawGraffiti, - }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") - return rawGraffiti - } - - graffiti, err := hex.DecodeString(target) - if err != nil { - log.WithError(err).Debug("Error while decoding hex string") - return rawGraffiti + if strings.ToLower(splitGraffiti[0]) == hexGraffitiPrefix { + target := splitGraffiti[1] + if target == "" { + log.WithFields(logrus.Fields{ + "input": rawGraffiti, + }).Debug("Blank hex tag to be interpreted as itself") + return rawGraffiti + } + if len(target) > 3 && target[:2] == hex0xPrefix { + target = target[2:] + } + if target == "" { + log.WithFields(logrus.Fields{ + "input": rawGraffiti, + }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") + return rawGraffiti + } + graffiti, err := hex.DecodeString(target) + if err != nil { + log.WithError(err).Debug("Error while decoding hex string") + return rawGraffiti + } + return string(graffiti) } - return string(graffiti) + return rawGraffiti } From 72a81107fbe0463d051bd5a0fb806fa500fb1cd3 Mon Sep 17 00:00:00 2001 From: Alexander Mollohan Date: Wed, 9 Jun 2021 16:51:52 -0400 Subject: [PATCH 15/15] getting the tests to pass --- validator/graffiti/parse_graffiti.go | 33 ++++++++++------------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/validator/graffiti/parse_graffiti.go b/validator/graffiti/parse_graffiti.go index d7b41e1ec0dd..118c9218d11b 100644 --- a/validator/graffiti/parse_graffiti.go +++ b/validator/graffiti/parse_graffiti.go @@ -7,7 +7,6 @@ import ( types "github.com/prysmaticlabs/eth2-types" "github.com/prysmaticlabs/prysm/shared/hashutil" - "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) @@ -27,15 +26,6 @@ type Graffiti struct { // ParseGraffitiFile parses the graffiti file and returns the graffiti struct. func ParseGraffitiFile(f string) (*Graffiti, error) { - // helper function to avoid coding this twice while parsing the Ordered and Random fields of the graffiti struct - parseGraffitiStrings := func(input []string) []string { - output := make([]string, len(input)) - for _, i := range input { - output = append(output, ParseHexGraffiti(i)) - } - return output - } - yamlFile, err := ioutil.ReadFile(f) if err != nil { return nil, err @@ -45,16 +35,21 @@ func ParseGraffitiFile(f string) (*Graffiti, error) { return nil, err } - specific := make(map[types.ValidatorIndex]string, len(g.Specific)) for i, o := range g.Specific { - specific[types.ValidatorIndex(i)] = ParseHexGraffiti(o) + g.Specific[types.ValidatorIndex(i)] = ParseHexGraffiti(o) + } + + for i, v := range g.Ordered { + g.Ordered[i] = ParseHexGraffiti(v) + } + + for i, v := range g.Random { + g.Random[i] = ParseHexGraffiti(v) } - g.Specific = specific g.Default = ParseHexGraffiti(g.Default) - g.Ordered = parseGraffitiStrings(g.Ordered) - g.Random = parseGraffitiStrings(g.Random) g.Hash = hashutil.Hash(yamlFile) + return g, nil } @@ -64,18 +59,14 @@ func ParseHexGraffiti(rawGraffiti string) string { if strings.ToLower(splitGraffiti[0]) == hexGraffitiPrefix { target := splitGraffiti[1] if target == "" { - log.WithFields(logrus.Fields{ - "input": rawGraffiti, - }).Debug("Blank hex tag to be interpreted as itself") + log.WithField("graffiti", rawGraffiti).Debug("Blank hex tag to be interpreted as itself") return rawGraffiti } if len(target) > 3 && target[:2] == hex0xPrefix { target = target[2:] } if target == "" { - log.WithFields(logrus.Fields{ - "input": rawGraffiti, - }).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") + log.WithField("graffiti", rawGraffiti).Debug("Nothing after 0x prefix, hex tag to be interpreted as itself") return rawGraffiti } graffiti, err := hex.DecodeString(target)