prysm-pulse/beacon-chain/node/helper.go
Victor Farazdagi a8e501b3cf
ETH2 Types: Epoch (#8373)
* update deps

* update deps

* update protos/*

* update deps

* reset protos

* update protos

* update shared/params/config

* update protos

* update /shared

* update shared/slotutil and shared/testutil

* update beacon-chain/core/helpers

* updates beacon-chain/state

* update beacon-chain/forkchoice

* update beacon-chain/blockchain

* update beacon-chain/cache

* update beacon-chain/core

* update beacon-chain/db

* update beacon-chain/node

* update beacon-chain/p2p

* update beacon-chain/rpc

* update beacon-chain/sync

* go mod tidy

* make sure that beacon-chain build suceeds

* go fmt

* update e2e tests

* update slasher

* remove redundant alias

* update validator

* gazelle

* fix build errors in unit tests

* go fmt

* update deps

* update fuzz/BUILD.bazel

* fix unit tests

* more unit test fixes

* fix blockchain UTs

* more unit test fixes
2021-02-09 10:05:22 +00:00

50 lines
1.2 KiB
Go

package node
import (
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
"github.com/prysmaticlabs/eth2-types"
)
// 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.
func convertWspInput(wsp string) ([]byte, types.Epoch, error) {
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.
wsp = strings.TrimPrefix(wsp, "0x")
// 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
}
return bRoot, types.Epoch(epoch), nil
}