prysm-pulse/validator/graffiti/parse_graffiti.go
Alexander Mollohan 644d5bbb1a
Graffiti hex support (#8894)
* more or less feature complete, needs unit tests

* Test support and concomitant refactors

* Resolving requested changes on PR

* minor fix before trying to figure out bazel

* bazel change

* minor changes requested in pr

* fixing build errors

* fixing build errors

* fixing PR feedback

* fixing PR feedback

* resolving nitpicks

* resolving nitpicks

* build failures REEEEE

* tests fail if its not this way, so this way it is

* getting the tests to pass

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
2021-06-10 16:54:24 +00:00

81 lines
2.1 KiB
Go

package graffiti
import (
"encoding/hex"
"io/ioutil"
"strings"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"gopkg.in/yaml.v2"
)
const (
hexGraffitiPrefix = "hex"
hex0xPrefix = "0x"
)
// Graffiti is a graffiti container.
type Graffiti struct {
Hash [32]byte
Default string `yaml:"default,omitempty"`
Ordered []string `yaml:"ordered,omitempty"`
Random []string `yaml:"random,omitempty"`
Specific map[types.ValidatorIndex]string `yaml:"specific,omitempty"`
}
// ParseGraffitiFile parses the graffiti file and returns the graffiti struct.
func ParseGraffitiFile(f string) (*Graffiti, error) {
yamlFile, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}
g := &Graffiti{}
if err := yaml.Unmarshal(yamlFile, g); err != nil {
return nil, err
}
for i, o := range g.Specific {
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.Default = ParseHexGraffiti(g.Default)
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]) == hexGraffitiPrefix {
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.WithField("graffiti", 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
}