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

TIP-543: Implement EIP-3855 PUSH0 instruction #543

Closed
yanghang8612 opened this issue May 15, 2023 · 9 comments
Closed

TIP-543: Implement EIP-3855 PUSH0 instruction #543

yanghang8612 opened this issue May 15, 2023 · 9 comments

Comments

@yanghang8612
Copy link
Contributor

yanghang8612 commented May 15, 2023

tip: 543
title: Implement EIP-3855 PUSH0 instruction
author: yanghang8612@163.com
status: Last Call
type: Standards Track
category: VM
created: 2023-05-15

Summary

As part of the Ethereum Shanghai upgrade, EIP-3855: PUSH0 Instruction is required to be implemented to TRON.

Abstract

Introduce the PUSH0 (0x5f) instruction, which pushes the constant value 0 onto the stack.

Motivation

The Ethereum Shanghai upgrade inclues EIP-3855: PUSH0 Instruction, an EIP designed to introduce a new PUSH0 instruction to reduce gas consumption during contract deployment and invocation.

So to accommodate the changes in EVM due to the Shanghai upgrade, we introduced this TIP.

Original motivation from EIP-3855:

Many instructions expect offsets as inputs, which in a number of cases are zero. A good example is the return data parameters of CALLs, which are set to zeroes in case the contract prefers using RETURNDATA*. This is only one example, but there are many other reasons why a contract would need to push a zero value. They can achieve that today by PUSH1 0, which costs 3 gas at runtime, and is encoded as two bytes which means 2 * 200 gas deployment cost.

Because of the overall cost many try to use various other instructions to achieve the same effect. Common examples include PC, MSIZE, CALLDATASIZE, RETURNDATASIZE, CODESIZE, CALLVALUE, and SELFBALANCE. Some of these cost only 2 gas and are a single byte long, but their value can depend on the context.

We have conducted an analysis on Mainnet (block ranges 8,567,259…8,582,058 and 12,205,970…12,817,405), and ~11.5% of all the PUSH* instructions executed push a value of zero.

The main motivations for this change include:

  1. Reducing contract code size.
  2. Reducing the risk of contracts (mis)using various instructions as an optimisation measure. Repricing/changing those instructions can be more risky.
  3. Reduce the need to use DUP instructions for duplicating zeroes.

To put the "waste" into perspective, across existing accounts 340,557,331 bytes are wasted on PUSH1 00 instructions, which means 68,111,466,200 gas was spent to deploy them. In practice a lot of these accounts share identical bytecode with others, so their total stored size in clients is lower, however the deploy time cost must have been paid nevertheless.

An example for 2) is changing the behaviour of RETURNDATASIZE such that it may not be guaranteed to be zero at the beginning of the call frame.

Specification

The instruction PUSH0 is introduced at 0x5f. It has no immediate data, pops no items from the stack, and places a single item with the value 0 onto the stack. The cost of this instruction is 2 energy (aka base).

Rationale

Energy cost

The base energy cost is used for instructions which place constant values onto the stack, such as ADDRESS, ORIGIN, and so forth.

Opcode

0x5f means it is in a "contiguous" space with the rest of the PUSH implementations and potentially could share the implementation.

Backwards Compatibility

This TIP introduces a new opcode which did not exists previously. Already deployed contracts using this opcode could change their behaviour after this TIP.

Test Cases

  • 5F -- successful execution, stack consist of a single item, set to zero
  • 5F5F..5F (1024 times) -- successful execution, stack consists of 1024 items, all set to zero
  • 5F5F..5F (1025 times) -- execution aborts due to out of stack

Security Considerations

There is no security considerations.

@KrisdiaPaul
Copy link

what does this PUSH0 mean? this is totally confusing

@yanghang8612
Copy link
Contributor Author

@KrisdiaPaul If you want to know the details of PUSH0 instruction, please refer to EIP-3855.

@Ocea91
Copy link

Ocea91 commented May 25, 2023

Introducing PUSH0 saves both system resources and fees for either party.

@ikirudoughnuts
Copy link

It is good if it helps reduce gas/energy consumption.

@tinteth
Copy link

tinteth commented May 30, 2023

Will this make tron EVM?

@yanghang8612 yanghang8612 changed the title Adapt to Ethereum Shanghai Upgrade TIP-543: Adapt to Ethereum Shanghai Upgrade Jun 13, 2023
@yanghang8612 yanghang8612 changed the title TIP-543: Adapt to Ethereum Shanghai Upgrade TIP-543: Support Ethereum Shanghai upgrade Jun 13, 2023
@yanghang8612
Copy link
Contributor Author

Will this make tron EVM?

The TVM was designed to be the EVM equivalent at the outset, and the purpose of this TIP is to enable the TVM to support the changes associated with the Ethereum Shanghai upgrade of the EVM.

@yanghang8612 yanghang8612 changed the title TIP-543: Support Ethereum Shanghai upgrade TIP-543: PUSH0 instruction Jun 14, 2023
@yanghang8612 yanghang8612 changed the title TIP-543: PUSH0 instruction TIP-543: Implement EIP-3855 PUSH0 instruction Jun 14, 2023
@tinteth
Copy link

tinteth commented Jun 22, 2023

Thank you for getting back to me.

As for smart contract equivalence, will this allow users to bring their EVM smart contracts and integrate them into the TVM?

Are there any opcode concerns to be aware of?

@yanghang8612
Copy link
Contributor Author

@tinteth Developers can migrate their smart contracts to the TRON network with development kits provided by many TRON communities, such as Tron-IDE, TronWeb, TronBox, etc.

This TIP mainly brings the PUSH0 instruction introduced by EVM after the Shanghai upgrade. Developers will be able to use the compiler to compile smart contracts and specify the version of EVM as Shanghai after this TIP is introduced. This will reduce gas/energy consumption during contract deployment and invocation.

TVM supports the full EVM opcodes, and TVM also introduces some unique opcodes. For details, please refer to: https://developers.tron.network/docs/tvm#differences-from-evm

@yanghang8612
Copy link
Contributor Author

Close this issue as it is implemented by GreatVoyage-v4.7.2.
Check TIP detail at TIP-543
Check implementation PR at tronprotocol/java-tron#5175

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

6 participants
@yanghang8612 @Ocea91 @ikirudoughnuts @KrisdiaPaul @tinteth and others