Skip to content

Commit

Permalink
bet
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Jul 4, 2024
1 parent 2b03b61 commit 2f091e2
Show file tree
Hide file tree
Showing 27 changed files with 41 additions and 78 deletions.
9 changes: 2 additions & 7 deletions mod/primitives/pkg/bytes/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
package bytes

import (
"reflect"

"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
)

//nolint:gochecknoglobals // reflect.Type of Bytes set at runtime
var bytesT = reflect.TypeOf(Bytes(nil))

// Bytes marshals/unmarshals as a JSON string with 0x prefix.
// The empty slice marshals as "0x".
type Bytes []byte
Expand All @@ -41,7 +36,7 @@ func (b Bytes) MarshalText() ([]byte, error) {

// UnmarshalJSON implements json.Unmarshaler.
func (b *Bytes) UnmarshalJSON(input []byte) error {
return hex.UnmarshalJSONText(input, b, bytesT)
return hex.UnmarshalJSONText(input, b)
}

// UnmarshalText implements encoding.TextUnmarshaler.
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b20.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b32.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b4.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b48.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
"github.com/prysmaticlabs/gohashtree"
)
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b8.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/bytes/b96.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
"github.com/prysmaticlabs/gohashtree"
)
Expand Down
12 changes: 5 additions & 7 deletions mod/primitives/pkg/bytes/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
package bytes

import (
"reflect"

"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
)

// ------------------------------ Helpers ------------------------------
Expand Down Expand Up @@ -108,13 +106,13 @@ func ExtendToSize(slice []byte, length int) []byte {
// UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length
// of out determines the required input length. This function is commonly used
// to implement the UnmarshalJSON method for fixed-size types.
func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error {
return hex.DecodeFixedJSON(typ, bytesT, input, out)
func UnmarshalFixedJSON(input, out []byte) error {
return hex.DecodeFixedJSON(input, out)
}

// UnmarshalFixedText decodes the input as a string with 0x prefix. The length
// of out determines the required input length. This function is commonly used
// to implement the UnmarshalText method for fixed-size types.
func UnmarshalFixedText(typename string, input, out []byte) error {
return hex.DecodeFixedText(typename, input, out)
func UnmarshalFixedText(input, out []byte) error {
return hex.DecodeFixedText(input, out)
}
4 changes: 1 addition & 3 deletions mod/primitives/pkg/eip4844/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package eip4844

import (
"reflect"

"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
)

Expand All @@ -31,7 +29,7 @@ type Blob [131072]byte

// UnmarshalJSON parses a blob in hex syntax.
func (b *Blob) UnmarshalJSON(input []byte) error {
return bytes.UnmarshalFixedJSON(reflect.TypeOf(Blob{}), input, b[:])
return bytes.UnmarshalFixedJSON(input, b[:])
}

// MarshalText returns the hex representation of b.
Expand Down
3 changes: 0 additions & 3 deletions mod/primitives/pkg/eip4844/kzg_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package eip4844

import (
"reflect"

"github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/constants"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto/sha256"
Expand Down Expand Up @@ -63,7 +61,6 @@ func (c KZGCommitment) HashTreeRoot() ([32]byte, error) {
// UnmarshalJSON parses a commitment in hex syntax.
func (c *KZGCommitment) UnmarshalJSON(input []byte) error {
return bytes.UnmarshalFixedJSON(
reflect.TypeOf(KZGCommitment{}),
input,
c[:],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package hex

import (
"encoding/hex"
"reflect"

"github.com/berachain/beacon-kit/mod/errors"
)
Expand All @@ -49,29 +48,24 @@ func UnmarshalByteText(input []byte) ([]byte, error) {
// DecodeFixedJSON decodes the input as a string with 0x prefix. The length
// of out determines the required input length. This function is commonly used
// to implement the UnmarshalJSON method for fixed-size types.
func DecodeFixedJSON(typ reflect.Type,
bytesT reflect.Type,
input,
out []byte) error {
func DecodeFixedJSON(input, out []byte) error {
if !isQuotedString(input) {
return WrapUnmarshalError(ErrNonQuotedString, bytesT)
return ErrNonQuotedString
}
return WrapUnmarshalError(
DecodeFixedText(typ.String(), input[1:len(input)-1], out), typ,
)
return DecodeFixedText(input[1:len(input)-1], out)
}

// DecodeFixedText decodes the input as a string with 0x prefix. The length
// of out determines the required input length.
func DecodeFixedText(typename string, input, out []byte) error {
func DecodeFixedText(input, out []byte) error {
raw, err := formatAndValidateText(input)
if err != nil {
return err
}
if len(raw)/encDecRatio != len(out) {
return errors.Newf(
"hex string has length %d, want %d for %s",
len(raw), len(out)*encDecRatio, typename,
"hex string has length %d, want %d",
len(raw), len(out)*encDecRatio,
)
}
// Pre-verify syntax and decode in a single pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strconv"
"testing"

"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/stretchr/testify/require"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,18 @@
package hex

import (
"encoding/json"
"errors"
"reflect"
)

var (
ErrEmptyString = errors.New("empty hex string")
ErrMissingPrefix = errors.New("hex string without 0x prefix")
ErrOddLength = errors.New("hex string of odd length")
ErrNonQuotedString = errors.New("non-quoted hex string")
ErrInvalidString = errors.New("invalid hex string")

ErrLeadingZero = errors.New("hex number with leading zero digits")
ErrEmptyNumber = errors.New("hex string \"0x\"")
ErrUint64Range = errors.New("hex number > 64 bits")
ErrBig256Range = errors.New("hex number > 256 bits")

ErrEmptyString = errors.New("empty hex string")
ErrMissingPrefix = errors.New("hex string without 0x prefix")
ErrOddLength = errors.New("hex string of odd length")
ErrNonQuotedString = errors.New("non-quoted hex string")
ErrInvalidString = errors.New("invalid hex string")
ErrLeadingZero = errors.New("hex number with leading zero digits")
ErrEmptyNumber = errors.New("hex string \"0x\"")
ErrUint64Range = errors.New("hex number > 64 bits")
ErrBig256Range = errors.New("hex number > 256 bits")
ErrInvalidBigWordSize = errors.New("weird big.Word size")
)

// WrapUnmarshalError wraps an error occurring during JSON unmarshaling.
func WrapUnmarshalError(err error, t reflect.Type) error {
if err != nil {
err = &json.UnmarshalTypeError{Value: err.Error(), Type: t}
}

return err
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"strconv"
"testing"

"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/stretchr/testify/require"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ package hex

import (
"encoding"
"reflect"
)

// UnmarshalJSONText unmarshals a JSON string with a 0x prefix into a given
// TextUnmarshaler. It validates the input and then removes the surrounding
// quotes before passing the inner content to the UnmarshalText method.
func UnmarshalJSONText(input []byte,
u encoding.TextUnmarshaler,
t reflect.Type,
) error {
if err := ValidateUnmarshalInput(input); err != nil {
return WrapUnmarshalError(err, t)
return err
}
return WrapUnmarshalError(u.UnmarshalText(input[1:len(input)-1]), t)
return u.UnmarshalText(input[1 : len(input)-1])
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package hex_test
import (
"testing"

"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/math/u256.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"math/big"

byteslib "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes"
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/holiman/uint256"
)

Expand Down
8 changes: 2 additions & 6 deletions mod/primitives/pkg/math/u64.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ import (
"encoding/binary"
"math/big"
"math/bits"
"reflect"
"strconv"

"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/constants"
)

//nolint:gochecknoglobals // stores the reflect type of U64.
var uint64T = reflect.TypeOf(U64(0))

//nolint:lll
type (
// U64 represents a 64-bit unsigned integer that is both SSZ and JSON
Expand Down Expand Up @@ -116,7 +112,7 @@ func (u U64) MarshalText() ([]byte, error) {

// UnmarshalJSON implements json.Unmarshaler.
func (u *U64) UnmarshalJSON(input []byte) error {
return hex.UnmarshalJSONText(input, u, uint64T)
return hex.UnmarshalJSONText(input, u)
}

// ---------------------------------- Hex ----------------------------------
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/math/u64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"reflect"
"testing"

"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/net/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strings"

gethprimitives "github.com/berachain/beacon-kit/mod/geth-primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
)

// HexRegexp is a regular expression to match hexadecimal characters.
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives/pkg/net/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"testing"

gethprimitives "github.com/berachain/beacon-kit/mod/geth-primitives"
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/net/jwt"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion mod/storage/pkg/filedb/range_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"strconv"

"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex"
db "github.com/berachain/beacon-kit/mod/storage/pkg/interfaces"
"github.com/berachain/beacon-kit/mod/storage/pkg/pruner"
)
Expand Down

0 comments on commit 2f091e2

Please sign in to comment.