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

78 lines
1.9 KiB
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"
"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"
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
)
func initializeProgressBar(numItems int, msg string) *progressbar.ProgressBar {
return progressbar.NewOptions(
numItems,
progressbar.OptionFullWidth(),
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
progressbar.OptionOnCompletion(func() { fmt.Println() }),
progressbar.OptionSetDescription(msg),
)
}
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
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 is not correct, 48-byte length: %s", str)
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
}
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("wrong root length, 32-byte length: %s", str)
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
}
var root [32]byte
copy(root[:], rootHexBytes[:32])
return root, nil
}
func rootToHexString(root []byte) (string, error) {
// Nil signing roots are allowed in EIP-3076.
if len(root) == 0 {
return "", nil
}
if len(root) != 32 {
return "", fmt.Errorf("wanted length 32, received %d", len(root))
}
return fmt.Sprintf("%#x", root), nil
}
func pubKeyToHexString(pubKey []byte) (string, error) {
if len(pubKey) != 48 {
return "", fmt.Errorf("wanted length 48, received %d", len(pubKey))
}
return fmt.Sprintf("%#x", pubKey), nil
}