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

feat(evm): gas usage in precompiles: limits, local gas meters #2093

Merged
merged 33 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
87355c8
feat(evm): gas usage in precompiles: limits, local gas meters
onikonychev Oct 25, 2024
c27bdad
fix: local precompile gas meters
onikonychev Oct 26, 2024
abd2d91
test(precompile): tests for precompile local gas meters
onikonychev Oct 28, 2024
f3e3b87
fix: increased funtoken precompile gas limit in favor to heavy user c…
onikonychev Oct 28, 2024
67de775
chore: moved evm grpc calls back to msg_server.go
onikonychev Oct 29, 2024
6845613
chore: resolve conflicts
onikonychev Oct 31, 2024
84c63b6
cleanup
onikonychev Oct 31, 2024
362ed5a
test: added sad test for precompile local gas
onikonychev Oct 31, 2024
3c9a984
Merge branch 'main' into fix/local-gas
k-yang Oct 31, 2024
c37446a
Merge branch 'main' into fix/local-gas
k-yang Oct 31, 2024
f602811
fix: duplicate import
k-yang Oct 31, 2024
3dc8f2c
chore: cleanup
onikonychev Oct 31, 2024
50c8247
Merge branch 'main' into fix/local-gas
k-yang Oct 31, 2024
947ddce
refactor: clean up imports
k-yang Oct 31, 2024
10fac9e
fix: bad merge
k-yang Oct 31, 2024
3ee5f59
cleanup
onikonychev Oct 31, 2024
df34075
Merge branch 'fix/local-gas' of github.com:NibiruChain/nibiru into fi…
onikonychev Oct 31, 2024
8151d98
fix: force switched precompile ctx gas configs
onikonychev Oct 31, 2024
3b94b1b
cleanup
onikonychev Oct 31, 2024
6ff8739
fix: gas used discrepancy in estimate gas vs actual execution gas
onikonychev Nov 1, 2024
c43219e
chore: cleaned up solidity contracts calling bankSend precompile
onikonychev Nov 1, 2024
e53d6ad
refactor(gas): simplify estimate gas error handling
k-yang Nov 2, 2024
dfaf522
refactor(gas): simplify EstimateGasForEvmCallType
k-yang Nov 2, 2024
cdeb00d
Merge branch 'main' of github.com:NibiruChain/nibiru into fix/local-gas
onikonychev Nov 4, 2024
f688bae
fix: comment on HandleOutOfGasPanic
onikonychev Nov 4, 2024
fe15859
Merge branch 'fix/local-gas' of github.com:NibiruChain/nibiru into fi…
onikonychev Nov 4, 2024
e67045e
Merge branch 'main' into fix/local-gas
k-yang Nov 4, 2024
f5d37ac
fix: missing gas limit call
k-yang Nov 4, 2024
667aec8
test: add WasmGasLimitQuery
k-yang Nov 4, 2024
fcaa604
refactor: update CallContractWIthInput
k-yang Nov 4, 2024
fa8063c
refactor(evm): prefer CallContract over CallContractWithInput
k-yang Nov 4, 2024
6d0deb8
refactor: add clarifying context to error messages
k-yang Nov 5, 2024
8d0e2fb
fix: eth api test
onikonychev Nov 5, 2024
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 @@ -69,6 +69,7 @@ consistent setup and dynamic gas calculations, addressing the following tickets.
- [#2088](https://github.com/NibiruChain/nibiru/pull/2088) - refactor(evm): remove outdated comment and improper error message text
- [#2089](https://github.com/NibiruChain/nibiru/pull/2089) - better handling of gas consumption within erc20 contract execution
- [#2091](https://github.com/NibiruChain/nibiru/pull/2091) - feat(evm): add fun token creation fee validation
- [#2093](https://github.com/NibiruChain/nibiru/pull/2093) - feat(evm): gas usage in precompiles: limits, local gas meters
- [#2094](https://github.com/NibiruChain/nibiru/pull/2094) - fix(evm): Following
from the changs in #2086, this pull request implements a new `JournalChange`
struct that saves a deep copy of the state multi store before each
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "TestFunTokenPrecompileLocalGas",
"sourceName": "contracts/TestFunTokenPrecompileLocalGas.sol",
"abi": [
{
"inputs": [
{
"internalType": "address",
"name": "erc20_",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "bech32Recipient",
"type": "string"
}
],
"name": "callBankSend",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "bech32Recipient",
"type": "string"
},
{
"internalType": "uint256",
"name": "customGas",
"type": "uint256"
}
],
"name": "callBankSendLocalGas",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x608060405234801561001057600080fd5b50604051610951380380610951833981810160405281019061003291906100db565b806000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050610108565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006100a88261007d565b9050919050565b6100b88161009d565b81146100c357600080fd5b50565b6000815190506100d5816100af565b92915050565b6000602082840312156100f1576100f0610078565b5b60006100ff848285016100c6565b91505092915050565b61083a806101176000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806359b6ed891461003b57806390d2b5e714610057575b600080fd5b610055600480360381019061005091906104d0565b610073565b005b610071600480360381019061006c919061053f565b6101db565b005b600061080073ffffffffffffffffffffffffffffffffffffffff168260008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1686866040516024016100c49392919061066a565b6040516020818303038152906040527f03003bc5000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505060405161014e91906106ef565b60006040518083038160008787f1925050503d806000811461018c576040519150601f19603f3d011682016040523d82523d6000602084013e610191565b606091505b50509050806101d5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101cc90610778565b60405180910390fd5b50505050565b600061080073ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff16848460405160240161022b9392919061066a565b6040516020818303038152906040527f03003bc5000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516102b591906106ef565b6000604051808303816000865af19150503d80600081146102f2576040519150601f19603f3d011682016040523d82523d6000602084013e6102f7565b606091505b505090508061033b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610332906107e4565b60405180910390fd5b505050565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b61036781610354565b811461037257600080fd5b50565b6000813590506103848161035e565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6103dd82610394565b810181811067ffffffffffffffff821117156103fc576103fb6103a5565b5b80604052505050565b600061040f610340565b905061041b82826103d4565b919050565b600067ffffffffffffffff82111561043b5761043a6103a5565b5b61044482610394565b9050602081019050919050565b82818337600083830152505050565b600061047361046e84610420565b610405565b90508281526020810184848401111561048f5761048e61038f565b5b61049a848285610451565b509392505050565b600082601f8301126104b7576104b661038a565b5b81356104c7848260208601610460565b91505092915050565b6000806000606084860312156104e9576104e861034a565b5b60006104f786828701610375565b935050602084013567ffffffffffffffff8111156105185761051761034f565b5b610524868287016104a2565b925050604061053586828701610375565b9150509250925092565b600080604083850312156105565761055561034a565b5b600061056485828601610375565b925050602083013567ffffffffffffffff8111156105855761058461034f565b5b610591858286016104a2565b9150509250929050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006105c68261059b565b9050919050565b6105d6816105bb565b82525050565b6105e581610354565b82525050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561062557808201518184015260208101905061060a565b60008484015250505050565b600061063c826105eb565b61064681856105f6565b9350610656818560208601610607565b61065f81610394565b840191505092915050565b600060608201905061067f60008301866105cd565b61068c60208301856105dc565b818103604083015261069e8184610631565b9050949350505050565b600081519050919050565b600081905092915050565b60006106c9826106a8565b6106d381856106b3565b93506106e3818560208601610607565b80840191505092915050565b60006106fb82846106be565b915081905092915050565b7f4661696c656420746f2063616c6c2062616e6b53656e6420776974682063757360008201527f746f6d2067617300000000000000000000000000000000000000000000000000602082015250565b60006107626027836105f6565b915061076d82610706565b604082019050919050565b6000602082019050818103600083015261079181610755565b9050919050565b7f4661696c656420746f2063616c6c2062616e6b53656e64000000000000000000600082015250565b60006107ce6017836105f6565b91506107d982610798565b602082019050919050565b600060208201905081810360008301526107fd816107c1565b905091905056fea2646970667358221220cf16927fc50953575dc9f444e1aefe75fa60bf10c7688dd039cb7503669ab76964736f6c63430008180033",
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100365760003560e01c806359b6ed891461003b57806390d2b5e714610057575b600080fd5b610055600480360381019061005091906104d0565b610073565b005b610071600480360381019061006c919061053f565b6101db565b005b600061080073ffffffffffffffffffffffffffffffffffffffff168260008054906101000a900473ffffffffffffffffffffffffffffffffffffffff1686866040516024016100c49392919061066a565b6040516020818303038152906040527f03003bc5000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff838183161783525050505060405161014e91906106ef565b60006040518083038160008787f1925050503d806000811461018c576040519150601f19603f3d011682016040523d82523d6000602084013e610191565b606091505b50509050806101d5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101cc90610778565b60405180910390fd5b50505050565b600061080073ffffffffffffffffffffffffffffffffffffffff1660008054906101000a900473ffffffffffffffffffffffffffffffffffffffff16848460405160240161022b9392919061066a565b6040516020818303038152906040527f03003bc5000000000000000000000000000000000000000000000000000000007bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff83818316178352505050506040516102b591906106ef565b6000604051808303816000865af19150503d80600081146102f2576040519150601f19603f3d011682016040523d82523d6000602084013e6102f7565b606091505b505090508061033b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610332906107e4565b60405180910390fd5b505050565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b61036781610354565b811461037257600080fd5b50565b6000813590506103848161035e565b92915050565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6103dd82610394565b810181811067ffffffffffffffff821117156103fc576103fb6103a5565b5b80604052505050565b600061040f610340565b905061041b82826103d4565b919050565b600067ffffffffffffffff82111561043b5761043a6103a5565b5b61044482610394565b9050602081019050919050565b82818337600083830152505050565b600061047361046e84610420565b610405565b90508281526020810184848401111561048f5761048e61038f565b5b61049a848285610451565b509392505050565b600082601f8301126104b7576104b661038a565b5b81356104c7848260208601610460565b91505092915050565b6000806000606084860312156104e9576104e861034a565b5b60006104f786828701610375565b935050602084013567ffffffffffffffff8111156105185761051761034f565b5b610524868287016104a2565b925050604061053586828701610375565b9150509250925092565b600080604083850312156105565761055561034a565b5b600061056485828601610375565b925050602083013567ffffffffffffffff8111156105855761058461034f565b5b610591858286016104a2565b9150509250929050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006105c68261059b565b9050919050565b6105d6816105bb565b82525050565b6105e581610354565b82525050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561062557808201518184015260208101905061060a565b60008484015250505050565b600061063c826105eb565b61064681856105f6565b9350610656818560208601610607565b61065f81610394565b840191505092915050565b600060608201905061067f60008301866105cd565b61068c60208301856105dc565b818103604083015261069e8184610631565b9050949350505050565b600081519050919050565b600081905092915050565b60006106c9826106a8565b6106d381856106b3565b93506106e3818560208601610607565b80840191505092915050565b60006106fb82846106be565b915081905092915050565b7f4661696c656420746f2063616c6c2062616e6b53656e6420776974682063757360008201527f746f6d2067617300000000000000000000000000000000000000000000000000602082015250565b60006107626027836105f6565b915061076d82610706565b604082019050919050565b6000602082019050818103600083015261079181610755565b9050919050565b7f4661696c656420746f2063616c6c2062616e6b53656e64000000000000000000600082015250565b60006107ce6017836105f6565b91506107d982610798565b602082019050919050565b600060208201905081810360008301526107fd816107c1565b905091905056fea2646970667358221220cf16927fc50953575dc9f444e1aefe75fa60bf10c7688dd039cb7503669ab76964736f6c63430008180033",
"linkReferences": {},
"deployedLinkReferences": {}
}
47 changes: 47 additions & 0 deletions x/evm/embeds/contracts/TestFunTokenPrecompileLocalGas.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./FunToken.sol";

contract TestFunTokenPrecompileLocalGas {
address erc20;

constructor(address erc20_) {
erc20 = erc20_;
}
onikonychev marked this conversation as resolved.
Show resolved Hide resolved

// Calls bankSend of the FunToken Precompile with the default gas.
// Internal call could use all the gas for the parent call.
function callBankSend(
uint256 amount,
string memory bech32Recipient
) public {
(bool success,) = FUNTOKEN_PRECOMPILE_ADDRESS.call(
abi.encodeWithSignature(
"bankSend(address,uint256,string)",
erc20,
amount,
bech32Recipient
)
);
require(success, "Failed to call bankSend");
}
onikonychev marked this conversation as resolved.
Show resolved Hide resolved

// Calls bankSend of the FunToken Precompile with the gas amount set in parameter.
// Internal call should fail if the gas provided is insufficient.
function callBankSendLocalGas(
uint256 amount,
string memory bech32Recipient,
uint256 customGas
) public {
(bool success,) = FUNTOKEN_PRECOMPILE_ADDRESS.call{gas: customGas}(
abi.encodeWithSignature(
"bankSend(address,uint256,string)",
erc20,
amount,
bech32Recipient
)
);
require(success, "Failed to call bankSend with custom gas");
}
onikonychev marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 9 additions & 0 deletions x/evm/embeds/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ var (
testErc20MaliciousNameJson []byte
//go:embed artifacts/contracts/TestERC20MaliciousTransfer.sol/TestERC20MaliciousTransfer.json
testErc20MaliciousTransferJson []byte
//go:embed artifacts/contracts/TestFunTokenPrecompileLocalGas.sol/TestFunTokenPrecompileLocalGas.json
testFunTokenPrecompileLocalGasJson []byte
//go:embed artifacts/contracts/TestERC20TransferThenPrecompileSend.sol/TestERC20TransferThenPrecompileSend.json
testERC20TransferThenPrecompileSendJson []byte
//go:embed artifacts/contracts/TestNativeSendThenPrecompileSend.sol/TestNativeSendThenPrecompileSend.json
Expand Down Expand Up @@ -80,6 +82,12 @@ var (
Name: "TestERC20MaliciousTransfer.sol",
EmbedJSON: testErc20MaliciousTransferJson,
}
// SmartContract_TestFunTokenPrecompileLocalGas is a test contract
// which allows precompile execution with custom local gas set (calling precompile within contract)
SmartContract_TestFunTokenPrecompileLocalGas = CompiledEvmContract{
Name: "TestFunTokenPrecompileLocalGas.sol",
EmbedJSON: testFunTokenPrecompileLocalGasJson,
}
// SmartContract_TestNativeSendThenPrecompileSendJson is a test contract
// that performs two sends in a single call: a native nibi send and a precompile bankSend.
// It tests a race condition where the state DB commit
Expand All @@ -106,6 +114,7 @@ func init() {
SmartContract_TestERC20.MustLoad()
SmartContract_TestERC20MaliciousName.MustLoad()
SmartContract_TestERC20MaliciousTransfer.MustLoad()
SmartContract_TestFunTokenPrecompileLocalGas.MustLoad()
SmartContract_TestNativeSendThenPrecompileSendJson.MustLoad()
SmartContract_TestERC20TransferThenPrecompileSend.MustLoad()
}
Expand Down
1 change: 1 addition & 0 deletions x/evm/embeds/embeds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestLoadContracts(t *testing.T) {
embeds.SmartContract_TestERC20.MustLoad()
embeds.SmartContract_TestERC20MaliciousName.MustLoad()
embeds.SmartContract_TestERC20MaliciousTransfer.MustLoad()
embeds.SmartContract_TestFunTokenPrecompileLocalGas.MustLoad()
embeds.SmartContract_TestNativeSendThenPrecompileSendJson.MustLoad()
embeds.SmartContract_TestERC20TransferThenPrecompileSend.MustLoad()
})
Expand Down
5 changes: 5 additions & 0 deletions x/evm/evmtest/test_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@
func (deps TestDeps) GoCtx() context.Context {
return sdk.WrapSDKContext(deps.Ctx)
}

func (deps TestDeps) ResetGasMeter() {
deps.EvmKeeper.ResetTransientGasUsed(deps.Ctx)
deps.EvmKeeper.ResetGasMeterAndConsumeGas(deps.Ctx, 0)

Check warning on line 67 in x/evm/evmtest/test_deps.go

View check run for this annotation

Codecov / codecov/patch

x/evm/evmtest/test_deps.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}
Loading
Loading