-
Notifications
You must be signed in to change notification settings - Fork 7
Call Data and Deserialization Gas Cost
At the moment of writing, the deserialization gas cost test uses about 81,000 gas. Let's examine why the cost is so high.
The fixed cost for every transaction is 21,000. So the actual cost is about 60,000 gas.
From the yellow paper:
- 4 gas is paid for every zero byte of data or code for a transaction
- 68 gas is paid for every non-zero byte of data or code for a transaction
With this we can easily calculate how much the data costs to send. For the test case we calculate 43,172 gas.
Let's calculate the cost of some common things we need to send:
- A signature (v, r, s): (32+32+1)*68 = 4420 gas
- An address: 20*68 = 1360 gas
- Numbers in a uint256: in general, these will contain a lot of zeros. Let's say on average 6 non-zero bytes: 6 * 68 + 26 * 4 = 512 gas
For the 2 order test case we have to send 5 signatures:
- Every order has a signature AND a dual author signature
- The miner has a signature of all ring data
-> Total cost signatures: (2*2 + 1) * 4420 = 22,100 gas
Every order in the test needs 4 addresses
- Owner
- TokenS
- Dual author address
- Wallet
LRC is used to pay fees so we don't need to store the feeToken
We also send the miner and feeRecipient (these are not the same in the test, and do not equal tx.origin)
-> Total cost addresses: (2*4 + 2) * 1360 = 13,600 gas
Every order contains some uint256 numbers
- amountS
- amountB
- feeAmount
- validSince (can be reduced to a uint64)
-> Total cost: (4 * 2) * 512 = 4,000 gas
There's also the feePercentage, spendables indices, specs, ... which in total has a cost of ~2,000 gas
Using the above, the actual deserialization currently consumes 60,000 - 43,000 = 17,000 gas in the test case. This can definitely still be improved a bit.