Skip to content

Commit

Permalink
fix(evm-precompiles): add assertNumArgs validation (#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine authored Oct 3, 2024
1 parent 629aea3 commit 1588744
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 @@ -125,6 +125,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2045](https://github.com/NibiruChain/nibiru/pull/2045) - test(evm): backend tests with test network and real txs
- [#2053](https://github.com/NibiruChain/nibiru/pull/2053) - refactor(evm): converted untyped event to typed and cleaned up
- [#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)
}
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
}

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
}

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
}

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
}

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
}

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
}

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

0 comments on commit 1588744

Please sign in to comment.