2020-09-17 19:32:40 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-02-09 10:05:22 +00:00
|
|
|
|
2021-02-16 07:45:34 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2020-09-17 19:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Given input string `block_root:epoch_number`, this verifies the input string is valid, and
|
|
|
|
// returns the block root as bytes and epoch number as unsigned integers.
|
2021-02-09 10:05:22 +00:00
|
|
|
func convertWspInput(wsp string) ([]byte, types.Epoch, error) {
|
2020-09-17 19:32:40 +00:00
|
|
|
if wsp == "" {
|
|
|
|
return nil, 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weak subjectivity input string must contain ":" to separate epoch and block root.
|
|
|
|
if !strings.Contains(wsp, ":") {
|
|
|
|
return nil, 0, fmt.Errorf("%s did not contain column", wsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip prefix "0x" if it's part of the input string.
|
2020-10-12 16:12:00 +00:00
|
|
|
wsp = strings.TrimPrefix(wsp, "0x")
|
2020-09-17 19:32:40 +00:00
|
|
|
|
|
|
|
// Get the hexadecimal block root from input string.
|
|
|
|
s := strings.Split(wsp, ":")
|
|
|
|
if len(s) != 2 {
|
|
|
|
return nil, 0, errors.New("weak subjectivity checkpoint input should be in `block_root:epoch_number` format")
|
|
|
|
}
|
|
|
|
|
|
|
|
bRoot, err := hex.DecodeString(s[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
if len(bRoot) != 32 {
|
|
|
|
return nil, 0, errors.New("block root is not length of 32")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the epoch number from input string.
|
|
|
|
epoch, err := strconv.ParseUint(s[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
2021-02-09 10:05:22 +00:00
|
|
|
return bRoot, types.Epoch(epoch), nil
|
2020-09-17 19:32:40 +00:00
|
|
|
}
|