prysm-pulse/validator/slashing-protection/local/standard-protection-format/helpers.go

39 lines
888 B
Go
Raw Normal View History

Implement Standard Slashing Protection JSON With Importing Logic (#7675) * Use new attestation protection * tests fixes * fix tests * fix comment * fix TestSetTargetData * fix tests * empty history handling * fix another test * mock domain request * fix empty handling * use far future epoch * use far future epoch * migrate data * copy byte array to resolve sigbus error * init validator protection on pre validation * Import interchange json * Import interchange json * reduce visibility * use return value * raul feedback * rename fixes * import test * checkout att v2 changes * define import method for interchange format in its own package * rename and made operations atomic * eip comment * begin amending test file * finish happy path for import tests * attempt the interchange import tests * fixed tests * happy and sad paths tested * good error messages * fix up comment with proper eip link * tests for helpers * helpers * all tests pass * proper test comment * terence feedback * validate metadata func * versioning check * begin handling duplicatesz * handle duplicate public keys with potentially different data, first pass * better handling of duplicate data * ensure duplicates are taken care of * comprehensive tests for deduplication of signed blocks * tests for deduplication * Update validator/slashing-protection/local/standard-protection-format/helpers_test.go Co-authored-by: Shay Zluf <thezluf@gmail.com> * Update validator/slashing-protection/local/standard-protection-format/helpers_test.go Co-authored-by: Shay Zluf <thezluf@gmail.com> * tests for maxuint64 and package level comment * tests passing * edge cases pass Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
2020-11-17 22:37:43 +00:00
package interchangeformat
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
)
func uint64FromString(str string) (uint64, error) {
return strconv.ParseUint(str, 10, 64)
}
func pubKeyFromHex(str string) ([48]byte, error) {
pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
if err != nil {
return [48]byte{}, err
}
if len(pubKeyBytes) != 48 {
return [48]byte{}, fmt.Errorf("public key does not correct, 48-byte length: %s", str)
}
var pk [48]byte
copy(pk[:], pubKeyBytes[:48])
return pk, nil
}
func rootFromHex(str string) ([32]byte, error) {
rootHexBytes, err := hex.DecodeString(strings.TrimPrefix(str, "0x"))
if err != nil {
return [32]byte{}, err
}
if len(rootHexBytes) != 32 {
return [32]byte{}, fmt.Errorf("public key does not correct, 32-byte length: %s", str)
}
var root [32]byte
copy(root[:], rootHexBytes[:32])
return root, nil
}