Skip to content

Instantly share code, notes, and snippets.

@karlfloersch
Last active September 8, 2019 04:58
Show Gist options
  • Save karlfloersch/1bf6ab7871f41e3a5a921c0a007ad5c6 to your computer and use it in GitHub Desktop.
Save karlfloersch/1bf6ab7871f41e3a5a921c0a007ad5c6 to your computer and use it in GitHub Desktop.
Call data gas cost calculator
# This function helps us calc how much many transitions & txs we can fit in a block with some calldata size
def get_cost_with_transition_size(transition_bytes, txs_in_a_transition=1, fixed_gas_cost=0, IS_EIP_2028_INCLUDED=True):
if not IS_EIP_2028_INCLUDED:
cd_gas_per_byte = 68
else:
cd_gas_per_byte = 16
gas_per_sstore = 20000
gas_cost_of_merkle_root = gas_per_sstore
cd_gas_per_tr = cd_gas_per_byte * transition_bytes
total_gas_in_block = 8000000
def sha3_gas(num_bytes):
num_words = num_bytes // 32
return 30 + 6 * num_words
sha3_gas_of_one_hash = sha3_gas(32)
gas_for_one_set_of_trs = sha3_gas(transition_bytes) + cd_gas_per_tr + sha3_gas_of_one_hash
# Note: Added `sha3_gas_of_one_hash` gas cost per tr (transition) because merkle tree hash cost is 2x number of leaves
total_trs_in_block = ((total_gas_in_block - gas_cost_of_merkle_root - fixed_gas_cost) / gas_for_one_set_of_trs)
print('total transitions in block:', total_trs_in_block)
print('gas per transition:', total_gas_in_block / total_trs_in_block)
eth_block_time = 14
print('avg txs per second', total_trs_in_block//eth_block_time*txs_in_a_transition)
state_root = 32 # bytes
# ERC20 Transfer with ECDSA signature
ecdsa_sig = 65 # bytes. Note from the signature we can determine the sender
recipient_address = 3 # bytes
amount = 7 # bytes
num_txs_in_transition = 15
transition_size = ((ecdsa_sig + recipient_address + amount) * num_txs_in_transition) + state_root
print('size,', transition_size)
# Calculate
print('\nECDSA ERC20 Transfers')
get_cost_with_transition_size(transition_size, num_txs_in_transition, 0, True)
# ERC20 Transfer with SNARK signature
sender_address = 3 # bytes
recipient_address = 3 # bytes
amount = 7 # bytes
num_txs_in_transition = 15
transition_size = ((sender_address + recipient_address + amount) * num_txs_in_transition) + state_root
gas_for_snark = 200000
# Calculate
print('\nSNARKED ERC20 Transfers')
get_cost_with_transition_size(transition_size, num_txs_in_transition, gas_for_snark, True)
# ERC20 Transfer with BLS signature
sender_address = 3 # bytes
recipient_address = 3 # bytes
amount = 7 # bytes
num_txs_in_transition = 15
transition_size = ((sender_address + recipient_address + amount) * num_txs_in_transition) + state_root
gas_for_bls_sig = 113000
# Calculate
print('\nSNARKED ERC20 Transfers')
get_cost_with_transition_size(transition_size, num_txs_in_transition, gas_for_bls_sig, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment