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

Optimize the return value of chainid opcode #474

Closed
yanghang8612 opened this issue Nov 4, 2022 · 4 comments
Closed

Optimize the return value of chainid opcode #474

yanghang8612 opened this issue Nov 4, 2022 · 4 comments

Comments

@yanghang8612
Copy link
Contributor

yanghang8612 commented Nov 4, 2022

tip: 474
title: Optimize the return value of chainid opcode
author: yanghang8612 <yanghang8612@163.com>
status: Final
type: Standards Track
category: VM
created: 2022-11-04

Abstract

This TIP aims to optimize the return value of the chainid opcode.

Motivation

The chainId of the TRON mainnet can be queried through the jsonrpc interface, as follows

curl --location --request POST 'https://api.trongrid.io/jsonrpc' \
--header 'Content-Type: application/json' \
--data-raw '{"jsonrpc":"2.0", "method": "eth_chainId", "params": [], "id": 1}'

{"jsonrpc":"2.0","id":1,"result":"0x2b6653dc"}

After the Istanbul proposal takes effect, the chainid opcode becomes valid and it pushes the genesis block id of the current chain onto the stack (defined in tip-174).

In different networks, the return values of the chainid opcode are as follows:

  • mainnet: 0x00000000000000001ebf88508a03865c71d452e25f4d51194196a1d22b6653dc
  • nile: 0x0000000000000000d698d4192c56cb6be724a558448e2684802de4d6cd8690dc
  • shasta: 0x0000000000000000de1aa88295e1fcf982742f773e0419c5a9c134c994a9059e

The return value of the chainid opcode does not match the chainId value queried by the jsonrpc interface. When dApp developers use tools such as metamask to access the TRON network and send transactions, this difference will cause some transactions to fail, such as permit type transactions.

Meanwhile, the return value of the chainid opcode is very large. For the javascript language that commonly used in web3 application development, the maximum integer value is Number.MAX_SAFE_INTEGER(defined in link), and it is clear that the above return values exceed that Number.MAX_SAFE_INTEGER.

  • Number.MAX_SAFE_INTEGER: 2**53 - 1 = 9007199254740991

Therefore, it is necessary to optimize the return value of the chainid opcode in the TRON network.

Specification

After the getAllowOptimizedReturnValueOfChainId(71th) proposal takes effect, the chainid opcode pushes the same value as the jsonrpc interface onto the stack.

The new return value of the chainid opcode for all networks are as follows:

  • mainnet:
    • In Hex: 0x2b6653dc
    • In Decimal: 728126428
  • nile:
    • In Hex: 0xcd8690dc
    • In Decimal: 3448148188
  • shasta:
    • In Hex: 0x94a9059e
    • In Decimal: 2494104990

Rationale

The new values above do not conflict with any chainId of the existing EVM networks.

Backwards Compatibility

Firstly the new return values proposed in this tip are consistent with some of the TRON community's protocol standards, such as tip-1193 and tip-712.

Secondly, backwards compatibility is still under consideration for currently existing contracts, especially for some TRC20 with permit extentions.

Based on the mainnet data on April 24, we exhaustively analyzed the bytecode of deployed smart contracts, totaling 1,474,264, including 172 contracts containing the chainid instruction. We have the following analysis results, starting from the form of the chainid instruction, TVL and number of transactions.
For the form of using the chainid instruction, we have divided into the following 3 categories

  • Class I: every time contract gets the chainid by executing the chainid instruction, 134 in total, including 12 contracts that only query the chainid method
  • Class II: every time contract gets the chainid by constant or contract storage, 26 in total
  • Class III: contracts are not open source, and bytecode is difficult to analyze, 12 in total

According to the TVL of the contract and the number of transactions, we counted the top 10 contracts, as follows:

Contract Class TVL Transactions
TUi9X79hK7vAqJ6UgivtnuQYNT4ozcBDAy I 220,071 TRX 3,418
TPrDk8WfuMheRFiC3Q5FsmCq74GuQXGcdL I 102,472 TRX 462,923
TZ2AQ2XnkVfYQ5Cozb9hZGWQn3KzibuKVS I 83,999 TRX 1,253
TGo8MJaSxvVVicZ1ixC7n3ibJMt9NJvxuU I 23,899 TRX 84,094
TZ1wkmeWCJbeWzTmoyo4Ye5p6an3YszALH I 954 TRX 130
TSP3nuX3QS7gdY1cRuAdT7Lv7TWUfYb9sn I 757 TRX 4
TDYFu54yh9ojbShRUzpquFWxUTKZeES46Q III 506 TRX 1116
TAncANF5aYbvgXDatmwTdvTa5N9PTrq95k III 461 TRX 87
TEaf3sA3UonFwhuDymv6eB4dHcgL52R47i I 250 TRX 290
TKdHFpukRZm2yiugKQXGNi1CviQXbfsYHk I 115 TRX 26

Most of the above contracts are Class I contracts, while the TVL of the top 10 is low. For the remaining types of contracts, they have no TVL and few transactions.

The use scenario of the chainid instruction is generally for participating in the calculation of domainSeparator of eip-712 or tip-712.

After analyzing the above 171 contracts, we found that the main methods used are the permit method or delegateBySig method. The above 2 methods are usually used for users to sign under the chain so as to perform authorization, transfer and some other operations. This method is an extension method and will not affect common contracts such as TRC20, TRC721, DeFi, etc.

Based on the above analysis, we believe that this tip's optimization for the return value of the chainid instruction will not cause serious problems. However, it needs the attention of dApp developers as follows:

  • For Class I contracts, the developer should modify the non-contract code to use the new chainid for off-chain signature calculation after this tip takes effect.
  • For Class II contracts, developers do not need to modify the code and still use the old chainid for off-chain signature calculation.
  • For Class III contracts, developers should evaluate the impact as soon as possible, and the contracts under this classification are as follows
Contract Class TVL Transactions
TAncANF5aYbvgXDatmwTdvTa5N9PTrq95k III 461 TRX 87
TCaLTA7LYW9FhyyMhtzjdBWUFeZbXUoxFQ III 0 TRX 1
TDELLa9bU2gWUKfbQp4Qj2z7SrtCPkMic5 III 0 TRX 1
TDYFu54yh9ojbShRUzpquFWxUTKZeES46Q III 506 TRX 1116
TEfFZWpBRdHnR2Sf1n9MU1XuMbA5TC1fqi III 0 TRX 1
TFSrKGtComjVLxfMyR467Bb7ALGwyJ5BzA III 0 TRX 1
TLANQMmWRS6vctnm8fhbSvUQbEwQbktmog III 0 TRX 6
TNJq3EeaKespFFv1ttJMJHpkmcMSXRE6Mf III 0 TRX 19
TQUhShsR5CwRFieKqUSHKJj8ofMdoQti3M III 0 TRX 3
TS8pBdHj9W9Rwti5BegRzwyF36F4YxWKnJ III 0 TRX 13
TSnuzoNgz9papmjn3dJP5XWwMoxQcLPAXZ III 0 TRX 3
TVLLW5XC9Y64WuAVp3ciU7pPn5m9d2YwFY III 0 TRX 30

Security Considerations

There is no known security consideration.

Welcome to discuss

@okwapa210
Copy link

Well done!

@L-Reid
Copy link

L-Reid commented Dec 9, 2022

permit type transaction has its own specific usage scenarios, and chainid is the required item. So i'm glad to see this optimization.

@yanghang8612
Copy link
Contributor Author

How to adapt to the changes of chainid ?

In the upcoming release of the new version of java-tron, the proposal corresponding to this TIP is named as getAllowOptimizedReturnValueOfChainId.Developers can query the status of the proposal to determine if the proposal has taken effect through the /wallet/getchainparameters interface provided by the fullnode or TronGrid.

For contracts that dynamically obtain the value of the chainid, the off-chain signing service should also dynamically obtain the value of the chainid by reading the contract or querying the eth_chainId interface provided by the fullnode or TronGird.

For contracts that solidify the value of the chainid as a constant, the off-chain signing service should also configure the value of the chainid as a constant.

@ethan1844
Copy link
Collaborator

Close this issue as it is implemented by GreatVoyage-v4.7.0.1.
Check TIP detail at TIP-474
Check implementation PR at tronprotocol/java-tron#4863

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants