Skip to content

Commit

Permalink
fix: all linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Sep 12, 2024
1 parent 3031548 commit 5979f36
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 11 deletions.
6 changes: 3 additions & 3 deletions libevm/ethtest/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewPseudoRand(seed uint64) *PseudoRand {

// Address returns a pseudorandom address.
func (r *PseudoRand) Address() (a common.Address) {
r.Read(a[:])
r.Read(a[:]) //nolint:gosec,errcheck // Guaranteed nil error
return a
}

Expand All @@ -29,13 +29,13 @@ func (r *PseudoRand) AddressPtr() *common.Address {

// Hash returns a pseudorandom hash.
func (r *PseudoRand) Hash() (h common.Hash) {
r.Read(h[:])
r.Read(h[:]) //nolint:gosec,errcheck // Guaranteed nil error
return h
}

// Bytes returns `n` pseudorandom bytes.
func (r *PseudoRand) Bytes(n uint) []byte {
b := make([]byte, n)
r.Read(b)
r.Read(b) //nolint:gosec,errcheck // Guaranteed nil error
return b
}
9 changes: 9 additions & 0 deletions libevm/hookstest/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// for the lifetime of the current test, clearing them via tb's
// [testing.TB.Cleanup].
func Register[C params.ChainConfigHooks, R params.RulesHooks](tb testing.TB, extras params.Extras[C, R]) {
tb.Helper()
params.TestOnlyClearRegisteredExtras()
tb.Cleanup(params.TestOnlyClearRegisteredExtras)
params.RegisterExtras(extras)
Expand All @@ -32,13 +33,17 @@ type Stub struct {
// Register is a convenience wrapper for registering s as both the
// [params.ChainConfigHooks] and [params.RulesHooks] via [Register].
func (s *Stub) Register(tb testing.TB) {
tb.Helper()
Register(tb, params.Extras[*Stub, *Stub]{
NewRules: func(_ *params.ChainConfig, _ *params.Rules, _ *Stub, blockNum *big.Int, isMerge bool, timestamp uint64) *Stub {
return s
},
})
}

// PrecompileOverride uses the s.PrecompileOverrides map, if non-empty, as the
// canonical source of all overrides. If the map is empty then no precompiles
// are overridden.
func (s Stub) PrecompileOverride(a common.Address) (libevm.PrecompiledContract, bool) {
if len(s.PrecompileOverrides) == 0 {
return nil, false
Expand All @@ -47,13 +52,17 @@ func (s Stub) PrecompileOverride(a common.Address) (libevm.PrecompiledContract,
return p, ok
}

// CanExecuteTransaction proxies arguments to the s.CanExecuteTransactionFn
// function if non-nil, otherwise it acts as a noop.
func (s Stub) CanExecuteTransaction(from common.Address, to *common.Address, sr libevm.StateReader) error {
if f := s.CanExecuteTransactionFn; f != nil {
return f(from, to, sr)
}
return nil
}

// CanCreateContract proxies arguments to the s.CanCreateContractFn function if
// non-nil, otherwise it acts as a noop.
func (s Stub) CanCreateContract(cc *libevm.AddressContext, sr libevm.StateReader) error {
if f := s.CanCreateContractFn; f != nil {
return f(cc, sr)
Expand Down
1 change: 1 addition & 0 deletions libevm/pseudo/constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestConstructor(t *testing.T) {
testConstructor[struct{ x string }](t)
}

//nolint:thelper // This is the test itself so we want local line numbers reported.
func testConstructor[T any](t *testing.T) {
var zero T
t.Run(fmt.Sprintf("%T", zero), func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions libevm/pseudo/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func MustNewValue[T any](t *Type) *Value[T] {
}

// Get returns the value.
func (a *Value[T]) Get() T { return a.t.val.get().(T) }
func (v *Value[T]) Get() T { return v.t.val.get().(T) } //nolint:forcetypeassert // invariant

// Set sets the value.
func (a *Value[T]) Set(v T) { a.t.val.mustSet(v) }
func (v *Value[T]) Set(val T) { v.t.val.mustSet(val) }

// MarshalJSON implements the [json.Marshaler] interface.
func (t *Type) MarshalJSON() ([]byte, error) { return t.val.MarshalJSON() }
Expand Down
3 changes: 2 additions & 1 deletion libevm/pseudo/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestType(t *testing.T) {
testType(t, "nil pointer", Zero[*float64], (*float64)(nil), new(float64), 0)
}

//nolint:thelper // This is the test itself so we want local line numbers reported.
func testType[T any](t *testing.T, name string, ctor func() *Pseudo[T], init T, setTo T, invalid any) {
t.Run(name, func(t *testing.T) {
typ, val := ctor().TypeAndValue()
Expand Down Expand Up @@ -67,7 +68,7 @@ func testType[T any](t *testing.T, name string, ctor func() *Pseudo[T], init T,
})
}

//nolint:ineffassign // Although `typ` is overwritten it's to demonstrate different approaches.
//nolint:ineffassign,testableexamples // Although `typ` is overwritten it's to demonstrate different approaches
func ExamplePseudo_TypeAndValue() {
typ, val := From("hello").TypeAndValue()

Expand Down
2 changes: 1 addition & 1 deletion params/config.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func notStructMessage[T any]() string {
return fmt.Sprintf("%T is not a struct nor a pointer to a struct", x)
}

// An ExtraPayloadGettter provides strongly typed access to the extra payloads
// An ExtraPayloadGetter provides strongly typed access to the extra payloads
// carried by [ChainConfig] and [Rules] structs. The only valid way to construct
// a getter is by a call to [RegisterExtras].
type ExtraPayloadGetter[C ChainConfigHooks, R RulesHooks] struct {
Expand Down
6 changes: 3 additions & 3 deletions params/config.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ func TestRegisterExtras(t *testing.T) {
tt.register()
defer TestOnlyClearRegisteredExtras()

in := &ChainConfig{
input := &ChainConfig{
ChainID: big.NewInt(142857),
extra: tt.ccExtra,
}

buf, err := json.Marshal(in)
buf, err := json.Marshal(input)
require.NoError(t, err)

got := new(ChainConfig)
require.NoError(t, json.Unmarshal(buf, got))
assert.Equal(t, tt.ccExtra.Interface(), got.extraPayload().Interface())
assert.Equal(t, in, got)
assert.Equal(t, input, got)

gotRules := got.Rules(nil, false, 0)
assert.Equal(t, tt.wantRulesExtra, gotRules.extraPayload().Interface())
Expand Down
2 changes: 1 addition & 1 deletion params/example.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (r RulesExtra) PrecompileOverride(addr common.Address) (_ libevm.Precompile
// implementation detail.
func (r RulesExtra) CanCreateContract(*libevm.AddressContext, libevm.StateReader) error {
if time.Unix(int64(r.timestamp), 0).UTC().Day() != int(time.Tuesday) {

Check failure on line 109 in params/example.libevm_test.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion uint64 -> int64 (gosec)
return errors.New("uh oh!")
return errors.New("uh oh")
}
return nil
}
Expand Down

0 comments on commit 5979f36

Please sign in to comment.