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

fix(evm-precompiles): add assertNumArgs validation #2060

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2044](https://github.com/NibiruChain/nibiru/pull/2044) - feat(evm): evm tx indexer service implemented
- [#2045](https://github.com/NibiruChain/nibiru/pull/2045) - test(evm): backend tests with test network and real txs
- [#2054](https://github.com/NibiruChain/nibiru/pull/2054) - feat(evm-precompile): Precompile for one-way EVM calls to invoke/execute Wasm contracts.
- [#2060](https://github.com/NibiruChain/nibiru/pull/2060) - fix(evm-precompiles): add assertNumArgs validation

#### Dapp modules: perp, spot, oracle, etc

Expand Down
10 changes: 10 additions & 0 deletions x/evm/precompile/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@

return nil
}

// assertNumArgs checks if the number of provided arguments matches the expected
// count. If lenArgs does not equal wantArgsLen, it returns an error describing
// the mismatch between expected and actual argument counts.
func assertNumArgs(lenArgs, wantArgsLen int) error {
if lenArgs != wantArgsLen {
return fmt.Errorf("expected %d arguments but got %d", wantArgsLen, lenArgs)

Check warning on line 52 in x/evm/precompile/errors.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/errors.go#L52

Added line #L52 was not covered by tests
}
return nil
}
7 changes: 4 additions & 3 deletions x/evm/precompile/funtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@
to string,
err error,
) {
// Note: The number of arguments is valiated before this function is called
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
// which validates against the the structure of the precompile's ABI.
if e := assertNumArgs(len(args), 3); e != nil {
err = e
return

Check warning on line 210 in x/evm/precompile/funtoken.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/funtoken.go#L209-L210

Added lines #L209 - L210 were not covered by tests
}

erc20, ok := args[0].(gethcommon.Address)
if !ok {
Expand Down
7 changes: 4 additions & 3 deletions x/evm/precompile/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,10 @@
return bz, err
}

// Note: The number of arguments is valiated before this function is called
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
// which validates against the the structure of the precompile's ABI.
if e := assertNumArgs(len(args), 2); e != nil {
err = e
return

Check warning on line 360 in x/evm/precompile/wasm.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/wasm.go#L359-L360

Added lines #L359 - L360 were not covered by tests
}

argIdx := 0
wasmContract, e := parseContractAddrArg(args[argIdx])
Expand Down
28 changes: 16 additions & 12 deletions x/evm/precompile/wasm_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@
txMsg wasm.MsgInstantiateContract,
err error,
) {
// Note: The number of arguments is valiated before this function is called
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
// which validates against the the structure of the precompile's ABI.
if e := assertNumArgs(len(args), 5); e != nil {
err = e
return

Check warning on line 89 in x/evm/precompile/wasm_parse.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/wasm_parse.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

argIdx := 0
admin, ok := args[argIdx].(string)
Expand Down Expand Up @@ -142,9 +143,10 @@
funds sdk.Coins,
err error,
) {
// Note: The number of arguments is valiated before this function is called
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
// which validates against the the structure of the precompile's ABI.
if e := assertNumArgs(len(args), 3); e != nil {
err = e
return

Check warning on line 148 in x/evm/precompile/wasm_parse.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/wasm_parse.go#L147-L148

Added lines #L147 - L148 were not covered by tests
}

argIdx := 0
contractAddrStr, ok := args[argIdx].(string)
Expand Down Expand Up @@ -187,9 +189,10 @@
req wasm.RawContractMessage,
err error,
) {
// Note: The number of arguments is valiated before this function is called
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
// which validates against the the structure of the precompile's ABI.
if e := assertNumArgs(len(args), 2); e != nil {
err = e
return

Check warning on line 194 in x/evm/precompile/wasm_parse.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/wasm_parse.go#L193-L194

Added lines #L193 - L194 were not covered by tests
}

argsIdx := 0
wasmContract, e := parseContractAddrArg(args[argsIdx])
Expand Down Expand Up @@ -220,9 +223,10 @@
},
err error,
) {
// Note: The number of arguments is valiated before this function is called
// during "DecomposeInput". DecomposeInput calls "method.Inputs.Unpack",
// which validates against the the structure of the precompile's ABI.
if e := assertNumArgs(len(args), 1); e != nil {
err = e
return

Check warning on line 228 in x/evm/precompile/wasm_parse.go

View check run for this annotation

Codecov / codecov/patch

x/evm/precompile/wasm_parse.go#L227-L228

Added lines #L227 - L228 were not covered by tests
}

arg := args[0]
execMsgs, ok := arg.([]struct {
Expand Down
Loading