Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lib/common) implement MustHexToBigInt #1547

Merged
merged 4 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions dot/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"math/big"
"path/filepath"

"github.com/ChainSafe/gossamer/dot/state"
Expand Down Expand Up @@ -97,8 +96,7 @@ func newHeaderFromFile(filename string) (*types.Header, error) {
return nil, errors.New("invalid number field in header JSON")
}

numBytes := common.MustHexToBytes(hexNum)
num := big.NewInt(0).SetBytes(numBytes)
num := common.MustHexToBigInt(hexNum)

parentHashStr, ok := jsonHeader["parentHash"].(string)
if !ok {
Expand Down
26 changes: 26 additions & 0 deletions lib/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/hex"
"errors"
"io"
"math/big"
"strconv"
"strings"
)
Expand Down Expand Up @@ -103,6 +104,31 @@ func MustHexToBytes(in string) []byte {
return out
}

// MustHexToBigInt turns a 0x prefixed hex string into a big.Int
// it panic if it cannot decode the string
func MustHexToBigInt(in string) *big.Int {
if len(in) < 2 {
panic("invalid string")
}

if strings.Compare(in[:2], "0x") != 0 {
panic(ErrNoPrefix)
}

// Ensure we have an even length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	in = in[2:]

	// Ensure we have an even length
	if len(in)%2 != 0 {
		in = "0" + in
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks better, updated.

if len(in)%2 != 0 {
in = in[:2] + "0" + in[2:]
}

in = in[2:]
out, err := hex.DecodeString(in)
if err != nil {
panic(err)
}

return big.NewInt(0).SetBytes(out)
}

// BytesToHex turns a byte slice into a 0x prefixed hex string
func BytesToHex(in []byte) string {
s := hex.EncodeToString(in)
Expand Down
33 changes: 33 additions & 0 deletions lib/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ package common

import (
"bytes"
"math/big"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestStringToInts(t *testing.T) {
Expand Down Expand Up @@ -182,3 +186,32 @@ func TestSwapNibbles(t *testing.T) {
}
}
}

func TestMustHexToBigInt(t *testing.T) {
tests := []struct {
in string
out *big.Int
}{
{"0x0", big.NewInt(0).SetBytes([]byte{0})},
{"0x00", big.NewInt(0).SetBytes([]byte{0})},
{"0x1", big.NewInt(1)},
{"0x01", big.NewInt(1)},
{"0xf", big.NewInt(15)},
{"0x0f", big.NewInt(15)},
{"0x10", big.NewInt(16)},
{"0xff", big.NewInt(255)},
{"0x50429", big.NewInt(328745)},
{"0x050429", big.NewInt(328745)},
}

for _, test := range tests {
res := MustHexToBigInt(test.in)
require.Equal(t, test.out, res)
}
}

func TestMustHexToBigIntPanic(t *testing.T) {
assert.Panics(t, func() { MustHexToBigInt("1") }, "should panic for string len < 2")
assert.Panics(t, func() { MustHexToBigInt("12") }, "should panic for string not starting with 0x")
assert.Panics(t, func() { MustHexToBigInt("0xzz") }, "should panic for string not containing hex characters")
}