From 7342b16e62adc4d9889a7254fe60013c45d42696 Mon Sep 17 00:00:00 2001 From: JasonTulp Date: Fri, 7 Jun 2024 14:35:23 +1200 Subject: [PATCH 1/3] Add extra cost to is_precompile check for PrecompileSets --- evm-precompiles/erc1155/src/lib.rs | 5 +++-- evm-precompiles/erc20/src/lib.rs | 5 +++-- evm-precompiles/erc721/src/lib.rs | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/evm-precompiles/erc1155/src/lib.rs b/evm-precompiles/erc1155/src/lib.rs index 1dea4c847..5b49b934e 100644 --- a/evm-precompiles/erc1155/src/lib.rs +++ b/evm-precompiles/erc1155/src/lib.rs @@ -213,16 +213,17 @@ where } fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult { + let extra_cost = RuntimeHelper::::db_read_gas_cost(); if let Some(collection_id) = Runtime::evm_id_to_runtime_id(Address(address), ERC1155_PRECOMPILE_ADDRESS_PREFIX) { // Check whether the collection exists IsPrecompileResult::Answer { is_precompile: pallet_sft::Pallet::::collection_exists(collection_id), - extra_cost: 0, // TODO: account gas for above storage read? + extra_cost } } else { - IsPrecompileResult::Answer { is_precompile: false, extra_cost: 0 } + IsPrecompileResult::Answer { is_precompile: false, extra_cost } } } } diff --git a/evm-precompiles/erc20/src/lib.rs b/evm-precompiles/erc20/src/lib.rs index 14651160f..df1643aec 100644 --- a/evm-precompiles/erc20/src/lib.rs +++ b/evm-precompiles/erc20/src/lib.rs @@ -124,16 +124,17 @@ where } fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult { + let extra_cost = RuntimeHelper::::db_read_gas_cost(); if let Some(asset_id) = Runtime::evm_id_to_runtime_id(Address(address), ERC20_PRECOMPILE_ADDRESS_PREFIX) { // Check if the asset exists IsPrecompileResult::Answer { is_precompile: >::asset_exists(asset_id), - extra_cost: 0, // TODO: account gas for above storage read? + extra_cost } } else { - IsPrecompileResult::Answer { is_precompile: false, extra_cost: 0 } + IsPrecompileResult::Answer { is_precompile: false, extra_cost } } } } diff --git a/evm-precompiles/erc721/src/lib.rs b/evm-precompiles/erc721/src/lib.rs index 110af70b8..26b341812 100644 --- a/evm-precompiles/erc721/src/lib.rs +++ b/evm-precompiles/erc721/src/lib.rs @@ -227,16 +227,17 @@ where } fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult { + let extra_cost = RuntimeHelper::::db_read_gas_cost(); if let Some(collection_id) = Runtime::evm_id_to_runtime_id(Address(address), ERC721_PRECOMPILE_ADDRESS_PREFIX) { // Check whether the collection exists IsPrecompileResult::Answer { is_precompile: pallet_nft::Pallet::::collection_exists(collection_id), - extra_cost: 0, // TODO: account gas for above storage read? + extra_cost } } else { - IsPrecompileResult::Answer { is_precompile: false, extra_cost: 0 } + IsPrecompileResult::Answer { is_precompile: false, extra_cost } } } } From 6fb84d62451c2fc4a834391df144a51deeadc614 Mon Sep 17 00:00:00 2001 From: JasonTulp Date: Fri, 7 Jun 2024 15:36:30 +1200 Subject: [PATCH 2/3] Update txCosts for 3 affected precompiles --- e2e/test/ERC1155/TxCosts.md | 26 +++++++++++++------------- e2e/test/ERC20/TxCosts.md | 12 ++++++------ e2e/test/ERC721/TxCosts.md | 30 +++++++++++++++--------------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/e2e/test/ERC1155/TxCosts.md b/e2e/test/ERC1155/TxCosts.md index 143016d1a..a77342e2b 100644 --- a/e2e/test/ERC1155/TxCosts.md +++ b/e2e/test/ERC1155/TxCosts.md @@ -5,23 +5,23 @@ | uri | 27560 | 22400 | 0 | | balanceOf | 25957 | 22433 | 0 | | balanceOfBatch | 32585 | 24106 | 0 | -| setApprovalForAll | 47025 | 26466 | 0 | +| setApprovalForAll | 47025 | 27501 | 0 | | isApprovedForAll | 26076 | 23184 | 0 | -| safeTransferFrom | 59163 | 31608 | 23335 | -| safeBatchTransferFrom | 50205 | 35107 | 26669 | -| mint | 33152 | 31645 | 23502 | -| mintBatch | 42210 | 32033 | 26835 | -| burn | 32581 | 26332 | 20002 | -| burnBatch | 38043 | 28962 | 23335 | +| safeTransferFrom | 59163 | 32160 | 23335 | +| safeBatchTransferFrom | 50205 | 35812 | 26670 | +| mint | 33152 | 32245 | 23503 | +| mintBatch | 42210 | 32633 | 26836 | +| burn | 32581 | 27608 | 20002 | +| burnBatch | 38043 | 31828 | 23335 | ## Generated tx costs(fees) for ERC1155 Precompiles | Function Call | Contract cost (Drops) | Precompile cost (Drops) | Extrinsic cost (Drops) | |:----------------------|:---------------------:|:-----------------------:|:----------------------:| -| safeTransferFrom | 880468 | 444524 | 350033 | -| safeBatchTransferFrom | 728112 | 515451 | 400049 | -| mint | 493954 | 445709 | 352539 | -| mintBatch | 589858 | 458130 | 402539 | -| burn | 475652 | 389438 | 300033 | -| burnBatch | 569231 | 433828 | 350033 | +| safeTransferFrom | 880468 | 462181 | 350038 | +| safeBatchTransferFrom | 728112 | 535973 | 400055 | +| mint | 493954 | 464911 | 352545 | +| mintBatch | 589858 | 477332 | 402545 | +| burn | 475652 | 407185 | 300038 | +| burnBatch | 569231 | 451575 | 350038 | diff --git a/e2e/test/ERC20/TxCosts.md b/e2e/test/ERC20/TxCosts.md index 607fcff50..80c1bb628 100644 --- a/e2e/test/ERC20/TxCosts.md +++ b/e2e/test/ERC20/TxCosts.md @@ -5,9 +5,9 @@ | totalSupply | 23717 | 22388 | 0 | | balanceOf | 25860 | 23974 | 0 | | allowance | 26064 | 23273 | 0 | -| approval | 47152 | 26364 | 20170 | -| transfer | 52698 | 44134 | 20173 | -| transferFrom | 44716 | 51794 | 23174 | +| approval | 47152 | 26470 | 20170 | +| transfer | 52698 | 43641 | 20172 | +| transferFrom | 44716 | 51726 | 23340 | | name | 25926 | 22388 | 0 | | decimals | 22354 | 22388 | 0 | | symbol | 25945 | 22388 | 0 | @@ -17,6 +17,6 @@ | Function Call | Contract cost (Drops) | Precompile cost (Drops) | Extrinsic cost (Drops) | |:--------------|:---------------------:|:-----------------------:|:----------------------:| -| approval | 702835 | 391134 | 302559 | -| transfer | 781323 | 633958 | 302595 | -| transferFrom | 647164 | 761966 | 347616 | +| approval | 702835 | 396804 | 302557 | +| transfer | 781323 | 622692 | 302592 | +| transferFrom | 647164 | 760526 | 350114 | diff --git a/e2e/test/ERC721/TxCosts.md b/e2e/test/ERC721/TxCosts.md index 0515906ca..d92947323 100644 --- a/e2e/test/ERC721/TxCosts.md +++ b/e2e/test/ERC721/TxCosts.md @@ -6,28 +6,28 @@ | ownerOf | 25847 | 23242 | 0 | | getApproved | 27395 | 23242 | 0 | | isApprovedForAll | 26082 | 23973 | 0 | -| mint | 53193 | 28898 | 17335 | -| burn | 37870 | 35114 | 17169 | -| approve | 50740 | 27831 | 23835 | -| setApprovalForAll | 47011 | 26360 | 23335 | -| safetransferFrom | 67181 | 35303 | 0 | -| transferFrom | 66839 | 34917 | 20669 | +| mint | 53193 | 31731 | 17336 | +| burn | 37870 | 35731 | 17170 | +| approve | 50740 | 28884 | 23835 | +| setApprovalForAll | 47011 | 26467 | 23335 | +| safetransferFrom | 67181 | 36625 | 0 | +| transferFrom | 66839 | 35560 | 20670 | | name | 25932 | 22388 | 0 | | symbol | 25938 | 22388 | 0 | | tokenURI | 25964 | 23242 | 0 | | owner | 23728 | 22388 | 0 | -| transferOwnership | 29147 | 27675 | 19835 | -| renounceOwnership | 30272 | 27559 | 19668 | +| transferOwnership | 29147 | 28498 | 19835 | +| renounceOwnership | 30272 | 28372 | 19835 | ## Generated tx costs(fees) for ERC721 Precompiles | Function Call | Contract cost (Drops) | Precompile cost (Drops) | Extrinsic cost (Drops) | |:------------------|:---------------------:|:-----------------------:|:----------------------:| -| mint | 791884 | 431188 | 260036 | -| burn | 490684 | 515676 | 257548 | -| approve | 739543 | 417326 | 357535 | -| setApprovalForAll | 699609 | 390954 | 350028 | -| transferFrom | 884488 | 509930 | 310049 | -| transferOwnership | 436153 | 410231 | 297532 | -| renounceOwnership | 352310 | 404890 | 295032 | +| mint | 791884 | 448454 | 260041 | +| burn | 490684 | 533648 | 257553 | +| approve | 739543 | 430633 | 357539 | +| setApprovalForAll | 699609 | 396639 | 350030 | +| transferFrom | 884488 | 528637 | 310054 | +| transferOwnership | 436153 | 424962 | 297536 | +| renounceOwnership | 352310 | 419621 | 297536 | From d65bc4b4bf91326f954b51a21a9ef097caf9712c Mon Sep 17 00:00:00 2001 From: JasonTulp Date: Mon, 10 Jun 2024 13:03:28 +1200 Subject: [PATCH 3/3] Address PR comments --- evm-precompiles/erc1155/src/lib.rs | 6 +++--- evm-precompiles/erc20/src/lib.rs | 6 +++--- evm-precompiles/erc721/src/lib.rs | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/evm-precompiles/erc1155/src/lib.rs b/evm-precompiles/erc1155/src/lib.rs index 5b49b934e..7d130adec 100644 --- a/evm-precompiles/erc1155/src/lib.rs +++ b/evm-precompiles/erc1155/src/lib.rs @@ -213,17 +213,17 @@ where } fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult { - let extra_cost = RuntimeHelper::::db_read_gas_cost(); if let Some(collection_id) = Runtime::evm_id_to_runtime_id(Address(address), ERC1155_PRECOMPILE_ADDRESS_PREFIX) { + let extra_cost = RuntimeHelper::::db_read_gas_cost(); // Check whether the collection exists IsPrecompileResult::Answer { is_precompile: pallet_sft::Pallet::::collection_exists(collection_id), - extra_cost + extra_cost, } } else { - IsPrecompileResult::Answer { is_precompile: false, extra_cost } + IsPrecompileResult::Answer { is_precompile: false, extra_cost: 0 } } } } diff --git a/evm-precompiles/erc20/src/lib.rs b/evm-precompiles/erc20/src/lib.rs index df1643aec..785431eb5 100644 --- a/evm-precompiles/erc20/src/lib.rs +++ b/evm-precompiles/erc20/src/lib.rs @@ -124,17 +124,17 @@ where } fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult { - let extra_cost = RuntimeHelper::::db_read_gas_cost(); if let Some(asset_id) = Runtime::evm_id_to_runtime_id(Address(address), ERC20_PRECOMPILE_ADDRESS_PREFIX) { + let extra_cost = RuntimeHelper::::db_read_gas_cost(); // Check if the asset exists IsPrecompileResult::Answer { is_precompile: >::asset_exists(asset_id), - extra_cost + extra_cost, } } else { - IsPrecompileResult::Answer { is_precompile: false, extra_cost } + IsPrecompileResult::Answer { is_precompile: false, extra_cost: 0 } } } } diff --git a/evm-precompiles/erc721/src/lib.rs b/evm-precompiles/erc721/src/lib.rs index 26b341812..109df83e9 100644 --- a/evm-precompiles/erc721/src/lib.rs +++ b/evm-precompiles/erc721/src/lib.rs @@ -227,17 +227,17 @@ where } fn is_precompile(&self, address: H160, _remaining_gas: u64) -> IsPrecompileResult { - let extra_cost = RuntimeHelper::::db_read_gas_cost(); if let Some(collection_id) = Runtime::evm_id_to_runtime_id(Address(address), ERC721_PRECOMPILE_ADDRESS_PREFIX) { + let extra_cost = RuntimeHelper::::db_read_gas_cost(); // Check whether the collection exists IsPrecompileResult::Answer { is_precompile: pallet_nft::Pallet::::collection_exists(collection_id), - extra_cost + extra_cost, } } else { - IsPrecompileResult::Answer { is_precompile: false, extra_cost } + IsPrecompileResult::Answer { is_precompile: false, extra_cost: 0 } } } }