projectchicago / gastoken

⛽ Tokenize gas on Ethereum with GasToken ⛽
https://gastoken.io
718 stars 89 forks source link

More efficient GST2 implementation #6

Open fulldecent opened 5 years ago

fulldecent commented 5 years ago

Just wanted to say this is a cute project. I have been studying and wanted to share my notes.


Current contract call costs 22 gas.

https://github.com/projectchicago/gastoken/blob/master/contract/GST2_ETH.sol#L106-L114

Gas Set Inst
3 VERYLOW PUSH15 0xb3f879cb30fe243b4dfee438691c04
2 BASE CALLER
3 VERYLOW XOR
2 BASE PC
10 HIGH JUMPI
2 BASE CALLER
SELFDESTRUCT

Proposed update requires 19 gas.

Gas Set Inst
3 VERYLOW PUSH15 0xb3f879cb30fe243b4dfee438691cXX
2 BASE CALLER
3 VERYLOW XOR
8 MID JUMP
1 JUMPDEST JUMPDEST
2 BASE CALLER
SELFDESTRUCT

With this new technique, the comment on the homepage can be updated:

The address of our GST2 GasToken, 0x0000000000b3F879cb30FE243b4Dfee438691c04, is hardcoded. Note that this address is very short (only 15 non-zero bytes instead of 20). We generated our GasToken contract at such an address on purpose, so as to save some gas when creating contracts. The above child contract uses 22 bytes of code, and thus costs roughly 32000 + 22 * 200 = 36400 gas to create (the additional overhead of calling mint is only about 150 gas). If we could make use of address 0x0 for GasToken, we could save an extra 3000 gas or so for every created contract!

Don't worry about the 0x0 address. You want the 0xPC address. Then you can additionally save the PUSH15 and XOR operations.


This proposed update also affects the efficiency ratio and other parts of the web page.

lorenzb commented 5 years ago

@fulldecent Thanks for letting us know! ❤️ If I understand you correctly, this technique would shave off 3 gas per minted contract?

fulldecent commented 5 years ago

Yes, that is correct.

k06a commented 5 years ago

@fulldecent can you clarify what XX means?

fulldecent commented 5 years ago

XX = 0x04 ^ 0x13 = 0x17.

This is chosen so that

  1. 0xb3f879cb30fe243b4dfee438691c04 is the beneficiary (msg.caller)
  2. 0xb3f879cb30fe243b4dfee438691c04 ^ 0xb3f879cb30fe243b4dfee438691c17 = 0x13
  3. JUMP 0x13 lands on the JUMPDEST