Skip to content

refactor: clean up utils pkg#3566

Open
thiagodeev wants to merge 13 commits intomainfrom
thiagodeev/refactor-utils
Open

refactor: clean up utils pkg#3566
thiagodeev wants to merge 13 commits intomainfrom
thiagodeev/refactor-utils

Conversation

@thiagodeev
Copy link
Copy Markdown
Contributor

@thiagodeev thiagodeev commented Apr 17, 2026

Related to #1198
A refactor in the 'log' and 'network' files will be made in follow-up PRs.

Cleanup of the utils/ package: move domain types closer to their semantic homes, replace small wrappers with stdlib equivalents, and remove dead code.

Relocated

  • utils.DataSize + byte-size constants (Kilobyte, Megabyte, …) → db/
  • utils.Base64core/transaction.go. Dropped redundant MarshalJSON/UnmarshalJSON methods — they were functionally identical to the default encoding/json behavior for named string types. Also dropped the RegisterCustomTypeFunc(Base64) blocks in the v8/v9/v10 validators: the validate:"base64" tag applies natively to named string types (in v8/v9 the type wasn't even used — doubly dead code).
  • BlockSignFunccore/block.go; Signsequencer/ as unexported newBlockSigner. The sequencer is the only creator; other consumers (builder, blockchain, statebackend) only need the type.
  • NonNilSlice / DerefSlice consolidated into utils/slices.go.

Replaced with stdlib / language builtins

  • utils.RunAndWrapOnError(fn, err)errors.Join(err, fn()) at 28 call sites. errors.Join is strictly better: it preserves both errors for errors.Is/errors.As (the old impl used %v on the run error, making it unreachable via Unwrap). Here are some tests comparing both: ref.
  • utils.HeapPtr(x) → Go 1.26's new(x) (now accepts expressions).
  • The mapping-style use utils.Map(s, utils.HeapPtr[T]) → new utils.ToPtrSlice(s) helper (7 call sites in RPC trace adapters).
  • utils.ToHex(*big.Int)fmt.Sprintf("0x%x", …).
  • Dropped github.com/pkg/errors dependency in favor of stdlib errors.Join.

Deleted

  • utils/bigint.go, utils/const.go, utils/zero_value.go,utils/maps.go (unused ToMap/ToSlice), and the now-empty utils/wrap.go, utils/sign.go, utils/base64.go.

Incidental bug fix (sequencer block signer) - Me & my friend Claude Code

While relocating Sign, it was noticed that the signature-chunking loop sliced signatureBytes[start:step] instead of [start:start+step]. For a 64-byte stark-curve ECDSA signature (felt.Bytes = 32), the second iteration read signatureBytes[32:32] — an empty slice — so the S component was silently dropped and replaced with felt(0). Every block signature produced by the sequencer was malformed and would fail cryptographic verification.
The loop was replaced with a direct two-felt construction (signature size is fixed at the algorithm level) and the test was strengthened to reconstruct the signature and round-trip it through PublicKey.Verify.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 17, 2026

Codecov Report

❌ Patch coverage is 88.75740% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.84%. Comparing base (026d019) to head (38e1f3a).

Files with missing lines Patch % Lines
migration/deprecated/bucket_migrator.go 20.00% 4 Missing ⚠️
sequencer/sequencer_utils.go 75.00% 1 Missing and 2 partials ⚠️
migration/deprecated/migration.go 33.33% 2 Missing ⚠️
sync/data_source.go 33.33% 2 Missing ⚠️
utils/slices.go 86.66% 1 Missing and 1 partial ⚠️
core/deprecatedstate/state.go 50.00% 0 Missing and 1 partial ⚠️
db/pebble/db.go 83.33% 1 Missing ⚠️
db/pebblev2/db.go 83.33% 1 Missing ⚠️
db/remote/db.go 66.66% 1 Missing ⚠️
grpc/handlers.go 50.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3566      +/-   ##
==========================================
+ Coverage   75.76%   75.84%   +0.08%     
==========================================
  Files         386      380       -6     
  Lines       33748    33713      -35     
==========================================
+ Hits        25569    25571       +2     
+ Misses       6394     6361      -33     
+ Partials     1785     1781       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@thiagodeev thiagodeev force-pushed the thiagodeev/refactor-utils branch from dc3b80b to b7e166b Compare April 20, 2026 13:06
@thiagodeev thiagodeev added the disable-deploy-test We don't want to run deploy tests with this PR because it might affect our development environment. label Apr 20, 2026
  Move `Base64` from `utils/base64.go` to `core/transaction.go`, closer to its semantic home as a Starknet transaction wire-format type. Drop the custom `MarshalJSON`/`UnmarshalJSON` methods — they were functionally identical to the default encoding/json behavior for named string types.

  Also drop the `RegisterCustomTypeFunc(core.Base64)` blocks in the v8, v9, and v10 validators. The go-playground/validator `base64` tag applies natively to named string types (`reflect.Kind() == String`), so the registration was redundant. In v8 and v9 the registration was doubly dead since no field of that type exists in those packages.

  Updates the 8 call sites in mempool, p2p2mempool adapter, and the v8/v9/v10 RPC validators and transaction types accordingly.
  Move `BlockSignFunc` from `utils/sign.go` to `core/block.go`, and move the signer factory to `sequencer/sequencer_utils.go` as the unexported `newBlockSigner` — the sequencer is the only creator; every other package just consumes the type. Update the consumers in `builder`, `blockchain`, and `blockchain/statebackend` accordingly, and delete `utils/sign.go`.

  While relocating, fix a pre-existing bug in the signer: the chunking loop sliced `signatureBytes[start:step]` instead of `[start:start+step]`, so the second iteration read an empty slice ([32:32]) and produced a zero felt in place of the S component of the signature. For a 64-byte stark-curve ECDSA signature the result was always `[R, 0]` — the signature was malformed and would fail verification. Replace the loop with a direct two-felt construction since the signature size is fixed at the algorithm level. Strengthen the unit test to reconstruct the signature and round-trip it through `PublicKey.Verify`, which catches this exact class of bug.
@thiagodeev thiagodeev removed the disable-deploy-test We don't want to run deploy tests with this PR because it might affect our development environment. label Apr 20, 2026
@thiagodeev thiagodeev deployed to Development April 20, 2026 18:13 — with GitHub Actions Active
@thiagodeev thiagodeev changed the title refactor utils refactor: clean up utils pkg Apr 20, 2026
@thiagodeev thiagodeev marked this pull request as ready for review April 20, 2026 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant