Zilliqa / zq2

Zilliqa 2.0 code base
Apache License 2.0
9 stars 0 forks source link

Block time increases with the number of txns regardless of how much gas consumed #1322

Open DrZoltanFazekas opened 1 month ago

DrZoltanFazekas commented 1 month ago

In case of larger transactions with 1.3m gas each, we can fit only 65 of them in a block. The average block time increases to 4sec. Examples in blocks 23,487 to 23,501.

Somewhat smaller transactions consuming 516k gas each allow us to squeeze up to 150 txns in a block, but the block time increases to 5-6 sec. See blocks 12,107 to 12,111.

When I further decreased the gas used per txn to 165k, we squeezed up to 509 txns into a block, but ended up with a block time of 12 sec, e.g. block 12,905 and 12,906, or a timeout:

  view height timestmp                                   proposer      gas txn highqc
-------------------------------------------------------------------------------------
 12903  12903 10:52:25 0x58a7b212d07838bb466e6fd88b17cc97505397e1 34588470 210  12902 5s since last block
 12904         timeout 0xc6f65e983f921dfea53e2a11659a3c2347aa530f    after   5s
 12905  12903 10:52:35 0x58a7b212d07838bb466e6fd88b17cc97505397e1        0   0  12902 10s since last block

Investigate

  1. how a 12sec block time was possible given that the leader should stop adding further transactions to the block to be proposed when it gets close to the timeout: https://github.com/Zilliqa/zq2/blob/main/zilliqa/src/consensus.rs#L1105-L1106

  2. why the block time increases so dramatically with the number of txns even though the total gas consumed is the same, compare e.g. block 23,487 (next block after 3 sec) with block 12,904 (next block after 12 sec), both consuming 84m gas, but the first one containing only 65 txns while the second one 509 txns.

Both small and large transactions (in terms of gas consumption) are doing the same - computing the Fibonacci sequence and storing it in a mapping - the only difference is the number of iterations (the num argument):

contract Fibonacci {
    uint256 public constant MAX = 2 ** 255;
    uint256 public index;
    mapping(uint256 => uint256) public fib;
    function compute(uint256 num) public {
        fib[0] = 0;
        fib[1] = 1;
        for(uint i = 2; i <= num; i++) {
            fib[i] = fib[i-1] + fib[i-2];
            index = i > index ? i : index;
            // we continue computing Fibonacci numbers up to
            // num, unless we know the next number will overflow,
            // in which case of which we start over  
            if (fib[i] >= MAX) {
                compute(num - i);
                break;
            }
        }   
    }
}

Processing a block with 509 txns that compute the first 40 Fibonacci numbers (i.e. 509 * 40 = 20,360 iterations in total) takes 12 sec whereas processing another block with 65 txns that compute 360 Fibonacci numbers (23,400 iterations in total) takes "only" 3 sec.

DrZoltanFazekas commented 1 month ago

It turns out the increase in the block time has nothing to do with EVM execution. Here is a test with simple ZIL transfer transactions (21k gas each, no smart contract called):

  view height timestmp                                   proposer      gas txn highqc
-------------------------------------------------------------------------------------
 43085  43085 05:33:58 0xf58ab9a70c7690b821ba06ded018505c6f3a2d85        0   0  43084 
 43086  43086 05:33:59 0xf58ab9a70c7690b821ba06ded018505c6f3a2d85        0   0  43085 
 43087  43087 05:34:00 0xf23f210a9943113b652637330cba12443d3e5afb        0   0  43086 
 43088  43088 05:34:02 0x6099f3985e19dd8e42dc441660f21f9c9e3e52ba        0   0  43087 2s since last block
 43089  43089 05:34:03 0xee407ffbae60ecde3a3a8fface15fcc2a96482e8        0   0  43088 
 43090  43090 05:34:04 0xb54f939a67a3a616bf6861780c06d207041ba142        0   0  43089 
 43091  43091 05:34:05 0xe915c099f647b527617cf163b680d6a2d93e7f0b        0   0  43090 
 43092  43092 05:34:06 0x7dbdbaa802823570a03f670c1486a70e72a551a8        0   0  43091 
 43093  43093 05:34:07 0xf58ab9a70c7690b821ba06ded018505c6f3a2d85        0   0  43092 
 43094  43094 05:34:08 0x91d5051b136ffbba695aa928246a271051ab3a73        0   0  43093 
 43095  43095 05:34:10 0xf58ab9a70c7690b821ba06ded018505c6f3a2d85        0   0  43094 2s since last block
 43096  43096 05:34:11 0xee407ffbae60ecde3a3a8fface15fcc2a96482e8        0   0  43095 
 43097  43097 05:34:12 0xee407ffbae60ecde3a3a8fface15fcc2a96482e8        0   0  43096 
 43098  43098 05:34:13 0xb54f939a67a3a616bf6861780c06d207041ba142        0   0  43097 
 43099  43099 05:34:14 0xf23f210a9943113b652637330cba12443d3e5afb        0   0  43098 
 43100  43100 05:34:15 0xee407ffbae60ecde3a3a8fface15fcc2a96482e8        0   0  43099 
 43101  43101 05:34:16 0x7dbdbaa802823570a03f670c1486a70e72a551a8        0   0  43100 
 43102  43102 05:34:17 0x91d5051b136ffbba695aa928246a271051ab3a73   126000   6  43101 
 43103  43103 05:34:19 0xb54f939a67a3a616bf6861780c06d207041ba142        0   0  43102 2s since last block
 43104  43104 05:34:20 0x6099f3985e19dd8e42dc441660f21f9c9e3e52ba        0   0  43103 
 43105  43105 05:34:21 0xf58ab9a70c7690b821ba06ded018505c6f3a2d85        0   0  43104 
 43106  43106 05:34:22 0xe915c099f647b527617cf163b680d6a2d93e7f0b        0   0  43105 
 43107  43107 05:34:23 0x987603019088b9ab175d5ecc5a22fd92b5d77db1        0   0  43106 
 43108  43108 05:34:24 0xee407ffbae60ecde3a3a8fface15fcc2a96482e8   609000  29  43107 
 43109  43109 05:34:26 0xb54f939a67a3a616bf6861780c06d207041ba142        0   0  43108 2s since last block
 43110  43110 05:34:27 0x91d5051b136ffbba695aa928246a271051ab3a73    42000   2  43109 
 43111  43111 05:34:28 0xb54f939a67a3a616bf6861780c06d207041ba142        0   0  43110 
 43112  43112 05:34:29 0xf23f210a9943113b652637330cba12443d3e5afb        0   0  43111 
 43113  43113 05:34:30 0x987603019088b9ab175d5ecc5a22fd92b5d77db1        0   0  43112 
 43114  43114 05:34:32 0x91d5051b136ffbba695aa928246a271051ab3a73        0   0  43113 2s since last block
 43115  43115 05:34:33 0x987603019088b9ab175d5ecc5a22fd92b5d77db1   567000  27  43114 
 43116  43116 05:34:34 0x7dbdbaa802823570a03f670c1486a70e72a551a8   924000  44  43115 
 43117  43117 05:34:37 0xf58ab9a70c7690b821ba06ded018505c6f3a2d85  2604000 124  43116 3s since last block
 43118  43118 05:34:40 0xf23f210a9943113b652637330cba12443d3e5afb        0   0  43117 3s since last block
 43119  43119 05:34:41 0xd3c0116d5f05b52965da8cd0c37ba8b807cf763a  1239000  59  43118 
 43120  43120 05:34:43 0x987603019088b9ab175d5ecc5a22fd92b5d77db1        0   0  43119 2s since last block
 43121  43121 05:34:44 0x987603019088b9ab175d5ecc5a22fd92b5d77db1        0   0  43120 
 43122  43122 05:34:46 0x6099f3985e19dd8e42dc441660f21f9c9e3e52ba  2940000 140  43121 2s since last block
 43123  43123 05:34:50 0x6099f3985e19dd8e42dc441660f21f9c9e3e52ba   945000  45  43122 4s since last block
 43124  43124 05:34:52 0x987603019088b9ab175d5ecc5a22fd92b5d77db1  4809000 229  43123 2s since last block
 43125  43125 05:34:58 0xf23f210a9943113b652637330cba12443d3e5afb  4116000 196  43124 6s since last block
 43126  43126 05:35:03 0x7dbdbaa802823570a03f670c1486a70e72a551a8  3864000 184  43125 5s since last block
 43127  43127 05:35:07 0xb54f939a67a3a616bf6861780c06d207041ba142  2583000 123  43126 4s since last block
 43128  43128 05:35:10 0x7dbdbaa802823570a03f670c1486a70e72a551a8        0   0  43127 3s since last block
 43129  43129 05:35:11 0xee407ffbae60ecde3a3a8fface15fcc2a96482e8        0   0  43128 
 43130  43130 05:35:12 0xb54f939a67a3a616bf6861780c06d207041ba142        0   0  43129