Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graffiti hex support #8894

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fa62192
more or less feature complete, needs unit tests
d00medman May 13, 2021
63b504b
Test support and concomitant refactors
d00medman May 14, 2021
a3d2e1c
Resolving requested changes on PR
d00medman May 17, 2021
d6619e5
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
d00medman May 17, 2021
7153f24
Merge branch 'develop' into graffiti-hex-support
d00medman May 24, 2021
c23fb07
minor fix before trying to figure out bazel
d00medman May 24, 2021
37debdf
bazel change
d00medman May 24, 2021
3c551b1
Merge branch 'develop' into graffiti-hex-support
rauljordan May 24, 2021
3358209
minor changes requested in pr
d00medman May 24, 2021
c89e3f3
Merge branch 'graffiti-hex-support' of https://github.com/d00medman/p…
d00medman May 24, 2021
afb8bc9
fixing build errors
d00medman May 24, 2021
011026c
fixing build errors
d00medman May 24, 2021
c49d4eb
Merge branch 'develop' into graffiti-hex-support
rauljordan May 25, 2021
42447d6
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
d00medman May 26, 2021
303df39
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
d00medman May 28, 2021
a1de46e
fixing PR feedback
d00medman May 28, 2021
66007c4
Merge branch 'graffiti-hex-support' of https://github.com/d00medman/p…
d00medman May 28, 2021
ea2de75
fixing PR feedback
d00medman May 28, 2021
0c99f5d
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
d00medman Jun 8, 2021
f57f976
resolving nitpicks
d00medman Jun 8, 2021
68e2e00
resolving nitpicks
d00medman Jun 8, 2021
e4e54dd
build failures REEEEE
d00medman Jun 9, 2021
e2454b5
tests fail if its not this way, so this way it is
d00medman Jun 9, 2021
72a8110
getting the tests to pass
d00medman Jun 9, 2021
d10fe42
Merge branch 'develop' into graffiti-hex-support
prestonvanloon Jun 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion validator/graffiti/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ 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",
"@com_github_sirupsen_logrus//:go_default_library",
"@in_gopkg_yaml_v2//:go_default_library",
],
)
Expand All @@ -19,6 +23,7 @@ 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",
],
Expand Down
5 changes: 5 additions & 0 deletions validator/graffiti/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package graffiti

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "graffiti")
60 changes: 60 additions & 0 deletions validator/graffiti/parse_graffiti.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package graffiti

import (
"encoding/hex"
"io/ioutil"
"strings"

types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

const (
hexGraffitiPrefixKey = "hex"
d00medman marked this conversation as resolved.
Show resolved Hide resolved
hex0xPrefix = "0x"
)

// Graffiti is a graffiti container.
type Graffiti struct {
Hash [32]byte
Expand All @@ -19,6 +27,15 @@ 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
Expand All @@ -27,6 +44,49 @@ 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
}

// 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 {
d00medman marked this conversation as resolved.
Show resolved Hide resolved
target := splitGraffiti[1]
if target == "" {
log.WithFields(logrus.Fields{
d00medman marked this conversation as resolved.
Show resolved Hide resolved
"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 rawGraffiti
}
62 changes: 62 additions & 0 deletions validator/graffiti/parse_graffiti_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
)

Expand Down Expand Up @@ -157,3 +158,64 @@ 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: "standard with hex tag",
want: "hola mundo!",
input: "hex:686f6c61206d756e646f21",
},
{
name: "irregularly cased hex tag",
want: "hola mundo!",
input: "HEX:686f6c61206d756e646f21",
},
{
name: "hex tag without accompanying data",
want: "hex:",
input: "hex:",
},
{
name: "Passing non-hex data with hex tag",
want: "hex:hola mundo!",
input: "hex:hola mundo!",
},
{
name: "unmarked hex input",
want: "0x686f6c61206d756e646f21",
input: "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)
assert.Equal(t, out, tt.want)
})
}
}
4 changes: 2 additions & 2 deletions validator/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down