Skip to content

Commit

Permalink
fix(evm-precompiles): add assertNumArgs validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Oct 2, 2024
1 parent fa62d35 commit baac2e4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
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 @@ func assertContractQuery(contract *vm.Contract) error {

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 @@ func (p precompileFunToken) decomposeBankSendArgs(args []any) (
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 @@ func (p precompileWasm) queryRaw(
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 @@ func (p precompileWasm) parseInstantiateArgs(args []any, sender string) (
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 @@ func (p precompileWasm) parseExecuteArgs(args []any) (
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 @@ func (p precompileWasm) parseQueryArgs(args []any) (
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 @@ func (p precompileWasm) parseExecuteMultiArgs(args []any) (
},
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

0 comments on commit baac2e4

Please sign in to comment.