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

feat(encoding): remove weird type overhead thing #1691

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
- "generate-check"
- "tidy-sync-check"
- "test-unit-cover"
- "test-unit-bench"
- "test-unit-fuzz"
- "test-forge-cover"
- "test-forge-fuzz"
Expand Down
1 change: 0 additions & 1 deletion beacond/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0 h1:vO9K86uuk2ZBuiOjAGM/yFrGEWdNPEv/wmPxFRzuFRo=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0/go.mod h1:64cB2wvltmK5plHlJFLYOZYGsaTKNW2EZgcHBisHP7o=
github.com/cometbft/cometbft-db v0.12.0 h1:v77/z0VyfSU7k682IzZeZPFZrQAKiQwkqGN0QzAjMi0=
github.com/cometbft/cometbft-db v0.12.0/go.mod h1:aX2NbCrjNVd2ZajYxt1BsiFf/Z+TQ2MN0VxdicheYuw=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0 h1:X6qp8ZhLKhk1Vw4JHqWVZKt21xomVWykr6ltY4yXIno=
Expand Down
8 changes: 8 additions & 0 deletions build/scripts/testing.mk
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ test-unit-cover: ## run golang unit tests with coverage
@go list -f '{{.Dir}}/...' -m | xargs \
go test -race -coverprofile=test-unit-cover.txt

test-unit-bench: ## run golang unit benchmarks
@echo "Running unit tests with benchmarks..."
@go list -f '{{.Dir}}/...' -m | xargs \
go test -bench=. -run=^$ -benchmem

# On MacOS, if there is a linking issue on the fuzz tests,
# use the old linker with flags -ldflags=-extldflags=-Wl,-ld_classic
Expand All @@ -207,6 +211,10 @@ test-unit-fuzz: ## run fuzz tests
go test ./mod/payload/pkg/cache/... -fuzz=FuzzPayloadIDCacheConcurrency -fuzztime=${SHORT_FUZZ_TIME}
go test -fuzz=FuzzHashTreeRoot ./mod/primitives/pkg/merkle -fuzztime=${MEDIUM_FUZZ_TIME}





test-e2e: ## run e2e tests
@$(MAKE) build-docker VERSION=kurtosis-local test-e2e-no-build

Expand Down
1 change: 0 additions & 1 deletion mod/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0 h1:vO9K86uuk2ZBuiOjAGM/yFrGEWdNPEv/wmPxFRzuFRo=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0/go.mod h1:64cB2wvltmK5plHlJFLYOZYGsaTKNW2EZgcHBisHP7o=
github.com/cometbft/cometbft-db v0.12.0 h1:v77/z0VyfSU7k682IzZeZPFZrQAKiQwkqGN0QzAjMi0=
github.com/cometbft/cometbft-db v0.12.0/go.mod h1:aX2NbCrjNVd2ZajYxt1BsiFf/Z+TQ2MN0VxdicheYuw=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0 h1:X6qp8ZhLKhk1Vw4JHqWVZKt21xomVWykr6ltY4yXIno=
Expand Down
1 change: 0 additions & 1 deletion mod/config/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0 h1:vO9K86uuk2ZBuiOjAGM/yFrGEWdNPEv/wmPxFRzuFRo=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0/go.mod h1:64cB2wvltmK5plHlJFLYOZYGsaTKNW2EZgcHBisHP7o=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0 h1:X6qp8ZhLKhk1Vw4JHqWVZKt21xomVWykr6ltY4yXIno=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0/go.mod h1:NDFKiBBD8HJC6QQLAoUI99YhsiRZtg2+FJWfk6A6m6o=
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
Expand Down
1 change: 0 additions & 1 deletion mod/consensus-types/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0 h1:vO9K86uuk2ZBuiOjAGM/yFrGEWdNPEv/wmPxFRzuFRo=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0/go.mod h1:64cB2wvltmK5plHlJFLYOZYGsaTKNW2EZgcHBisHP7o=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0 h1:X6qp8ZhLKhk1Vw4JHqWVZKt21xomVWykr6ltY4yXIno=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0/go.mod h1:NDFKiBBD8HJC6QQLAoUI99YhsiRZtg2+FJWfk6A6m6o=
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
Expand Down
1 change: 0 additions & 1 deletion mod/consensus/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0 h1:vO9K86uuk2ZBuiOjAGM/yFrGEWdNPEv/wmPxFRzuFRo=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0/go.mod h1:64cB2wvltmK5plHlJFLYOZYGsaTKNW2EZgcHBisHP7o=
github.com/cometbft/cometbft-db v0.12.0 h1:v77/z0VyfSU7k682IzZeZPFZrQAKiQwkqGN0QzAjMi0=
github.com/cometbft/cometbft-db v0.12.0/go.mod h1:aX2NbCrjNVd2ZajYxt1BsiFf/Z+TQ2MN0VxdicheYuw=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0 h1:X6qp8ZhLKhk1Vw4JHqWVZKt21xomVWykr6ltY4yXIno=
Expand Down
1 change: 0 additions & 1 deletion mod/node-core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0 h1:vO9K86uuk2ZBuiOjAGM/yFrGEWdNPEv/wmPxFRzuFRo=
github.com/cometbft/cometbft v1.0.0-rc1.0.20240702102754-c4fa9a6225b0/go.mod h1:64cB2wvltmK5plHlJFLYOZYGsaTKNW2EZgcHBisHP7o=
github.com/cometbft/cometbft-db v0.12.0 h1:v77/z0VyfSU7k682IzZeZPFZrQAKiQwkqGN0QzAjMi0=
github.com/cometbft/cometbft-db v0.12.0/go.mod h1:aX2NbCrjNVd2ZajYxt1BsiFf/Z+TQ2MN0VxdicheYuw=
github.com/cometbft/cometbft/api v1.0.0-rc.1.0.20240702102754-c4fa9a6225b0 h1:X6qp8ZhLKhk1Vw4JHqWVZKt21xomVWykr6ltY4yXIno=
Expand Down
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
7 changes: 6 additions & 1 deletion mod/primitives/pkg/bytes/b96_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ func TestB96HashTreeRoot(t *testing.T) {
merkleizer := merkleizer.New[[32]byte, bytes.B32]()
root2, err := merkleizer.MerkleizeByteSlice(tt.input[:])
require.NoError(t, err)
require.Equal(t, root1, root2, "HashTreeRoot and HashTreeRoot2 should produce the same result")
require.Equal(
t,
root1,
root2,
"HashTreeRoot and HashTreeRoot2 should produce the same result",
)
})
}
}
4 changes: 2 additions & 2 deletions mod/primitives/pkg/bytes/b_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ func TestUnmarshalFixedJSON(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := bytes.UnmarshalFixedJSON(tt.typ, tt.input, tt.out)
err := bytes.UnmarshalFixedJSON(tt.input, tt.out)
if tt.wantErr {
require.Error(t, err, "Test case: %s", tt.name)
} else {
Expand Down Expand Up @@ -1385,7 +1385,7 @@ func TestUnmarshalFixedText(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := bytes.UnmarshalFixedText(tt.typename, tt.input, tt.out)
err := bytes.UnmarshalFixedText(tt.input, tt.out)
if tt.wantErr {
require.Error(t, err, "Test case: %s", tt.name)
} else {
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)
Comment on lines +51 to +55
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Update required: Test function TestDecodeFixedJSON uses outdated signature

The test function TestDecodeFixedJSON in mod/primitives/pkg/encoding/hex/bytes_test.go still uses the old signature of DecodeFixedJSON with the typename parameter. Please update the test to match the new function signature.

  • File: mod/primitives/pkg/encoding/hex/bytes_test.go
  • Lines: 11-20
func TestDecodeFixedJSON(t *testing.T) {
	tests := []struct {
		name      string
		input     []byte
		out       []byte
	}{
		// Test cases
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := hex.DecodeFixedJSON(tt.input, tt.out)
			// Additional test logic
		})
	}
}

Please remove the typename field and update the test cases accordingly.

Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to DecodeFixedJSON match the new signature.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `DecodeFixedJSON` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go $'DecodeFixedJSON'

Length of output: 474


Script:

#!/bin/bash
# Description: Verify the context of each usage of `DecodeFixedJSON` to ensure they match the new signature.

# Extract and show lines around the function definition and its usage in bytes.go
rg -A 5 -B 5 'DecodeFixedJSON' mod/primitives/pkg/encoding/hex/bytes.go

# Extract and show lines around the function usage in bytes_test.go
rg -A 5 -B 5 'DecodeFixedJSON' mod/primitives/pkg/encoding/hex/bytes_test.go

# Extract and show lines around the function usage in utils.go
rg -A 5 -B 5 'DecodeFixedJSON' mod/primitives/pkg/bytes/utils.go

Length of output: 1533

}

// 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 @@ -13,7 +13,7 @@
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN AS IS BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
Expand All @@ -22,11 +22,10 @@
package hex_test

import (
"reflect"
"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 Expand Up @@ -149,7 +148,7 @@ func TestDecodeFixedText(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := make([]byte, len(tt.expected))
err := hex.DecodeFixedText(tt.typename, tt.input, out)
err := hex.DecodeFixedText(tt.input, out)
if tt.expectErr {
require.Error(t, err, "Test case : %s", tt.name)
} else {
Expand Down Expand Up @@ -201,8 +200,6 @@ func TestDecodeFixedJSON(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := hex.DecodeFixedJSON(
reflect.TypeOf(tt.typename),
reflect.TypeOf(tt.input),
tt.input,
tt.out,
)
Expand All @@ -217,7 +214,6 @@ func TestDecodeFixedJSON(t *testing.T) {
}

func BenchmarkDecodeFixedText(b *testing.B) {
typename := "exampleType"
sizes := []int{100, 1000, 10000} // Different input sizes

for _, size := range sizes {
Expand All @@ -240,7 +236,7 @@ func BenchmarkDecodeFixedText(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
err := hex.DecodeFixedText(typename, input, out)
err := hex.DecodeFixedText(input, out)
if err != nil {
b.Fatalf("DecodeFixedText failed: %v", err)
}
Expand Down
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")
Comment on lines +28 to +36
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Action Required: Remove or Replace WrapUnmarshalError Reference

The function WrapUnmarshalError has been removed from the codebase, but there is still a reference to it in the test file mod/primitives/pkg/math/u64_test.go. This reference should be removed or replaced to ensure the codebase is consistent and error-free.

  • File: mod/primitives/pkg/math/u64_test.go
  • Line: Reference to WrapUnmarshalError
Analysis chain

LGTM! Verify the removal of WrapUnmarshalError.

The code changes are approved.

However, ensure that the removal of WrapUnmarshalError does not affect error handling in other parts of the codebase.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the removal of `WrapUnmarshalError` and its usage in the codebase.

# Test: Search for the function usage. Expect: No occurrences of `WrapUnmarshalError`.
rg --type go --pattern $'WrapUnmarshalError'

Length of output: 455


Script:

#!/bin/bash
# Description: Verify the removal of `WrapUnmarshalError` and its usage in the codebase.

# Test: Search for the function usage. Expect: No occurrences of `WrapUnmarshalError`.
rg 'WrapUnmarshalError' --type go

Length of output: 96


Script:

#!/bin/bash
# Description: Verify the definition of `WrapUnmarshalError` in the codebase.

# Test: Search for the function definition. Expect: No occurrences of `WrapUnmarshalError`.
rg 'func WrapUnmarshalError' --type go

Length of output: 40

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
}
Loading
Loading