ArkEcosystem / core

The ARK Core Blockchain Framework. Check https://learn.ark.dev for more information.
https://ark.io
MIT License
338 stars 285 forks source link

Use AIP11 Serialisation for P2P data exchange #1857

Closed faustbrian closed 5 years ago

vasild commented 5 years ago

I am considering various ways to minimize the traffic, serialization being one of them. Lets take the following example reply of /peer/blocks:

{
    "success": true,
    "blocks": [
        {
            "id": "7701461040436418921",
            "version": 0,
            "timestamp": 62557706,
            "height": 1787805,
            "reward": "200000000",
            "previousBlock": "5414740249060624078",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "0306950dae7158103814e3828b1ab97a87dbb3680db1b4c6998b8208865b2f9db7",
            "blockSignature": "3044022053f0b16714c70bb1f98de65a95afa7700e78bf6bdd4242182241d602b92b019502200a6f688b753db4085c9479343714a74e28bfb298f192984ddf49119b90e08264"
        },
        {
            "id": "15989687548385591524",
            "version": 0,
            "timestamp": 62557714,
            "height": 1787806,
            "reward": "200000000",
            "previousBlock": "7701461040436418921",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "03f08761f99892996c6771761955ec41ee6cdffadd43171228f5f28f8c76423b3d",
            "blockSignature": "304402200ca4976081d6e29f2c391fc9be1f9cef0e9767b5710b3cbb38bb455cb7d16ba5022036ed2bd1a3f92305d18869a2526f6eeaf4335283aab6aac6491475f660eb21f6"
        },
        {
            "id": "2128135995045764554",
            "version": 0,
            "timestamp": 62557722,
            "height": 1787807,
            "reward": "200000000",
            "previousBlock": "15989687548385591524",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "0346e1a1b5cb0720e863e8cf8a91dc8ed827e09fb4a4812f9f4d51f606e5359516",
            "blockSignature": "304402200fab4d00b6992beeed9d79ca8c5e77c1ca5bb8237cd7a4e745c41bd1d4c64d580220040f41cd54eb4c5702e6703f901ea898ad72eef670a41c1e30208bfd675c57de"
        },
        {
            "id": "2094598395260631264",
            "version": 0,
            "timestamp": 62557730,
            "height": 1787808,
            "reward": "200000000",
            "previousBlock": "2128135995045764554",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "02ff842d25fc8eec9e1382e6468188b3fd130ab6246240fc97958ce83d6d147eaf",
            "blockSignature": "304402205f49a014b3d556c01ee89a5ba546cb5f24f34b9f0f83b4665bb86511d1ddac6d02204ba983a9a7b6497b04ec1af388e61445683a139307d838c918e701de73003094"
        },
        {
            "id": "10067656521456205909",
            "version": 0,
            "timestamp": 62557738,
            "height": 1787809,
            "reward": "200000000",
            "previousBlock": "2094598395260631264",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "03d3c6889608074b44155ad2e6577c3368e27e6e129c457418eb3e5ed029544e8d",
            "blockSignature": "304502210087ce4946f8bbac0674a8cb7c54dcea5db7899962cc79dd56e0d165b3c1d9b27602200ebe7887ff979efddd357279fe5cd4885b7a7bc7f9323cb950f397deb5f8b71e"
        },
        {
            "id": "9986798775967579651",
            "version": 0,
            "timestamp": 62557746,
            "height": 1787810,
            "reward": "200000000",
            "previousBlock": "10067656521456205909",
            "numberOfTransactions": 6,
            "totalAmount": "599989995",
            "totalFee": "2750000",
            "payloadLength": 192,
            "payloadHash": "a5a0d3c70cdf5f4c44bd0baa5b0f3554fcabb69ca2844c9c8ea648d2d206e6bf",
            "generatorPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
            "blockSignature": "30440220201793ceeb3494dd78c72345ad47e7510bc7fdea3a8ba388f3f5a0ed918d481d022043d9e7204b9b5015e596562eeef4f5dd7ac98425a3f8300addf2659e6e7ff6f0",
            "transactions": [
                {
                    "version": 1,
                    "network": 30,
                    "type": 0,
                    "timestamp": 62557465,
                    "senderPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
                    "fee": "450000",
                    "amount": "48305893",
                    "vendorFieldHex": "72657365727665",
                    "expiration": 0,
                    "recipientId": "DEFfff5D258XAc7aSjfn9acs4BbXwSXjrr",
                    "signature": "304402205d417049092d7d26d265b6330ca96a0caa6765dc0c8ac4d21755cce767424a5c02206d2ea2c7482b6a66a09c913e209bcd112352828c878a0a60724f21a01742be40",
                    "secondSignature": "304402200c3f7849a22de606985977beae01309efec2d78b8b743ec346e421c7b3cb8f25022034364b4eb83e1fbbdc2fca8bfab7e4d5dab09ffdad62ad75a2b60764cfb67fb9",
                    "vendorField": "reserve",
                    "id": "19a8ac50d92abaa67be25ada1a7aeb02de3a4be190c768e13e247bc8cb067901",
                    "blockId": "9986798775967579651"
                },
                {
                    "version": 1,
                    "network": 30,
                    "type": 0,
                    "timestamp": 62557466,
                    "senderPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
                    "fee": "460000",
                    "amount": "114400356",
                    "vendorFieldHex": "47454f505320506f6f6c20526577617264",
                    "expiration": 0,
                    "recipientId": "DJpFwW39QnQvQRQJF2MCfAoKvsX4DJ28jq",
                    "signature": "3045022100c2e82b7fbe4820d43bfb74b662908cfe46ae8836ea9bb8ea83ec1e8a3ee8183a022013e4fad1a2d3732b7ed767b77ee82ca8f2048bebe6551f181825deecd60fe009",
                    "secondSignature": "3045022100a09e545e8430ff3bcc07c97d5ad02ca786f7e75ec77f7e3e092aed9b4985d195022077da31f531623c08ba1ff33cd1708786b17fa2cabc8a30261bfa3734393c9218",
                    "vendorField": "GEOPS Pool Reward",
                    "id": "738a0472618c63d36e5a4b7f956aa1682c3bd28709305f6d881e361ce5932492",
                    "blockId": "9986798775967579651"
                },
                {
                    "version": 1,
                    "network": 30,
                    "type": 0,
                    "timestamp": 62557466,
                    "senderPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
                    "fee": "460000",
                    "amount": "38656559",
                    "vendorFieldHex": "47454f505320506f6f6c20526577617264",
                    "expiration": 0,
                    "recipientId": "DKDMkjnZY5QjaArNPZEe7xqhk2tH4GrWSB",
                    "signature": "3044022027c63633322049f63f510848649d38a1f9a8e1b75ca591de0b79ce35dbe4375202207eb70d3e8ecb031315921945ff84f71196f3a146621e989c3fa30ea5925733d8",
                    "secondSignature": "30440220288ef1ab19b0976afab77f515eab4e2cedb02471a6631f5fd0d564725569fff5022070c191ca66391771d20baf2b2fafccaf9ebc53ae29374f843168b37cc3b1528e",
                    "vendorField": "GEOPS Pool Reward",
                    "id": "7b8b1b26ad3462c441e21bfd18417100eec24674ae2257aea2ae2df1ce56784e",
                    "blockId": "9986798775967579651"
                },
                {
                    "version": 1,
                    "network": 30,
                    "type": 0,
                    "timestamp": 62557465,
                    "senderPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
                    "fee": "460000",
                    "amount": "200000000",
                    "vendorFieldHex": "47454f505320506f6f6c20526577617264",
                    "expiration": 0,
                    "recipientId": "DEJHR83JFmGpXYkJiaqn7wPGztwjheLAmY",
                    "signature": "3045022100b0c8a23a1b67e9f0b9900aee6d092a2ebbb22e6221a6ab25d3b20fc95488fa9702204b4363deeff3a7534222b985f02467ccca780f19dd56c9b2e369c14786651fbe",
                    "secondSignature": "3045022100d99a5cdd316d918eb4d061b1931782aa6f74d72d51ac9519d78499b66978cdba0220743392a80ef7d6e937d9b3417018f7cd25ca71d0072a1dc053cc92ab7da221e9",
                    "vendorField": "GEOPS Pool Reward",
                    "id": "7c1b94d627da887a87c9d31cad3feb26ff550e27fbbf1361ee7aea22755e5d11",
                    "blockId": "9986798775967579651"
                },
                {
                    "version": 1,
                    "network": 30,
                    "type": 0,
                    "timestamp": 62557466,
                    "senderPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
                    "fee": "460000",
                    "amount": "175541991",
                    "vendorFieldHex": "47454f505320506f6f6c20526577617264",
                    "expiration": 0,
                    "recipientId": "DEFfff5D258XAc7aSjfn9acs4BbXwSXjrr",
                    "signature": "3044022038d190e66e845984996ef68e21b790c2bfe181af3d6c9e8d00132e7ef64b52cc0220201442d9eae1975d0e6b906c29d7338a0ab4a71e65a4b48e299fd604d4fd8d9e",
                    "secondSignature": "3045022100e120a1516a97eba68a9fab61c0d542bb53564e50bff35ab1021bcce0ef6b727e02200ba867146c68d1093bed4a29df8837f97e44d4da9760cd98009c8e30a670c83c",
                    "vendorField": "GEOPS Pool Reward",
                    "id": "d9b6883b8b48ec2078ce68825c65626c8db8a63df3a8e7e7584c2de393eaa688",
                    "blockId": "9986798775967579651"
                },
                {
                    "version": 1,
                    "network": 30,
                    "type": 0,
                    "timestamp": 62557466,
                    "senderPublicKey": "027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582",
                    "fee": "460000",
                    "amount": "23085196",
                    "vendorFieldHex": "47454f505320506f6f6c20526577617264",
                    "expiration": 0,
                    "recipientId": "DBtA1gwdnYZhkg8HJrYLLa4rG7YSm6sCvY",
                    "signature": "3044022030fd06c35b726b5483e240ddd00533fa40d2cdb8f9101764c54d4d3583fd8bec0220661e739cc7b73a08b1185031a70125dbb8fa70bc224e2ca926d7a3cc70505295",
                    "secondSignature": "30440220339b96a9a9b16717ef9ad08df685e2767e38c9191b3ec1e3ec13284480f6fe8402202738b64ccc4f972d8b6db1538d7c57ada7a8393ea2cf66aa7037c878d1650190",
                    "vendorField": "GEOPS Pool Reward",
                    "id": "ecc6202b9b797379ac4daf31d54f7ff3e8d79ab5452c748cf6ed10c97cb05a20",
                    "blockId": "9986798775967579651"
                }
            ]
        },
        {
            "id": "7703070751356602200",
            "version": 0,
            "timestamp": 62557754,
            "height": 1787811,
            "reward": "200000000",
            "previousBlock": "9986798775967579651",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "0304d0c477d634cc85d89c1a4afee8f51168d1747fe8fd79cabc26565e49eb8a7a",
            "blockSignature": "304402204c4b53fa05a6f59c49455a8b2c31cffbf328e27af60f0f6581ca6b491c0f3e01022025a22e3bb3b003f9c57544133dec946c356cca9a2a568cdb2c379f7d9d958b82"
        },
        {
            "id": "12219974313055742171",
            "version": 0,
            "timestamp": 62557762,
            "height": 1787812,
            "reward": "200000000",
            "previousBlock": "7703070751356602200",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "037997a6553ea8073eb199e9f5ff23b8f0892e79433ef35e13966e0a12849d02e3",
            "blockSignature": "30440220309be65b8646f44465ca19a630f6b49d74c2a286173d840ec02de66adc7edfa40220395812485bf1bf7768f9ea76cbb7b51c9278d53fda4ec7ff0280520075b9409f"
        },
        {
            "id": "8933460787925734191",
            "version": 0,
            "timestamp": 62557768,
            "height": 1787813,
            "reward": "200000000",
            "previousBlock": "12219974313055742171",
            "numberOfTransactions": 0,
            "totalAmount": "0",
            "totalFee": "0",
            "payloadLength": 0,
            "payloadHash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "generatorPublicKey": "039b5a3a71335bfa6c72b82498f814123e0678f7cd3d8e7221ec7124918736e01c",
            "blockSignature": "304402205abacc5cd85d3b4ca8c98fb37899f5c6499a15cd3d4924571ac86f8e9ba037ff0220529f215ddd9b2d700f62d920407123799b2ddb37aa913004562344b4c450c93c"
        }
    ]
}

That is 13504 bytes. If we just remove the whitespace it becomes 9474 (30% reduction).

If serialized, then it would look like this:

{
    "success": true,
    "blocks": [
        "000000000a8eba039d471b004b2505916844c6ce000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550306950dae7158103814e3828b1ab97a87dbb3680db1b4c6998b8208865b2f9db73044022053f0b16714c70bb1f98de65a95afa7700e78bf6bdd4242182241d602b92b019502200a6f688b753db4085c9479343714a74e28bfb298f192984ddf49119b90e08264",
        "00000000128eba039e471b006ae116031eb91969000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85503f08761f99892996c6771761955ec41ee6cdffadd43171228f5f28f8c76423b3d304402200ca4976081d6e29f2c391fc9be1f9cef0e9767b5710b3cbb38bb455cb7d16ba5022036ed2bd1a3f92305d18869a2526f6eeaf4335283aab6aac6491475f660eb21f6",
        "000000001a8eba039f471b00dde6c81bb70280e4000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550346e1a1b5cb0720e863e8cf8a91dc8ed827e09fb4a4812f9f4d51f606e5359516304402200fab4d00b6992beeed9d79ca8c5e77c1ca5bb8237cd7a4e745c41bd1d4c64d580220040f41cd54eb4c5702e6703f901ea898ad72eef670a41c1e30208bfd675c57de",
        "00000000228eba03a0471b001d88a869826cddca000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85502ff842d25fc8eec9e1382e6468188b3fd130ab6246240fc97958ce83d6d147eaf304402205f49a014b3d556c01ee89a5ba546cb5f24f34b9f0f83b4665bb86511d1ddac6d02204ba983a9a7b6497b04ec1af388e61445683a139307d838c918e701de73003094",
        "000000002a8eba03a1471b001d118224909cf8e0000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85503d3c6889608074b44155ad2e6577c3368e27e6e129c457418eb3e5ed029544e8d304502210087ce4946f8bbac0674a8cb7c54dcea5db7899962cc79dd56e0d165b3c1d9b27602200ebe7887ff979efddd357279fe5cd4885b7a7bc7f9323cb950f397deb5f8b71e",
        "00000000328eba03a2471b008bb78043fa0ac05506000000eb1ec3230000000030f629000000000000c2eb0b00000000c0000000a5a0d3c70cdf5f4c44bd0baa5b0f3554fcabb69ca2844c9c8ea648d2d206e6bf027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda915658230440220201793ceeb3494dd78c72345ad47e7510bc7fdea3a8ba388f3f5a0ed918d481d022043d9e7204b9b5015e596562eeef4f5dd7ac98425a3f8300addf2659e6e7ff6f0e6000000f2000000f0000000f2000000f1000000f0000000ff011e00198dba03027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582d0dd0600000000000772657365727665e516e10200000000000000001e63f74804d659a2ccaab80f0dd99b073c0f3f6490304402205d417049092d7d26d265b6330ca96a0caa6765dc0c8ac4d21755cce767424a5c02206d2ea2c7482b6a66a09c913e209bcd112352828c878a0a60724f21a01742be40304402200c3f7849a22de606985977beae01309efec2d78b8b743ec346e421c7b3cb8f25022034364b4eb83e1fbbdc2fca8bfab7e4d5dab09ffdad62ad75a2b60764cfb67fb9ff011e001a8dba03027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582e0040700000000001147454f505320506f6f6c20526577617264649cd10600000000000000001e9601b47dafa59a9119b5b8c9ef066cf210f2ac0f3045022100c2e82b7fbe4820d43bfb74b662908cfe46ae8836ea9bb8ea83ec1e8a3ee8183a022013e4fad1a2d3732b7ed767b77ee82ca8f2048bebe6551f181825deecd60fe0093045022100a09e545e8430ff3bcc07c97d5ad02ca786f7e75ec77f7e3e092aed9b4985d195022077da31f531623c08ba1ff33cd1708786b17fa2cabc8a30261bfa3734393c9218ff011e001a8dba03027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582e0040700000000001147454f505320506f6f6c205265776172642fda4d0200000000000000001e9a602034e6a326de7385779149c8a0cfa30e4a203044022027c63633322049f63f510848649d38a1f9a8e1b75ca591de0b79ce35dbe4375202207eb70d3e8ecb031315921945ff84f71196f3a146621e989c3fa30ea5925733d830440220288ef1ab19b0976afab77f515eab4e2cedb02471a6631f5fd0d564725569fff5022070c191ca66391771d20baf2b2fafccaf9ebc53ae29374f843168b37cc3b1528eff011e00198dba03027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582e0040700000000001147454f505320506f6f6c2052657761726400c2eb0b00000000000000001e6475f4912aac496b7e4feb214c3bf07e7bf59baa3045022100b0c8a23a1b67e9f0b9900aee6d092a2ebbb22e6221a6ab25d3b20fc95488fa9702204b4363deeff3a7534222b985f02467ccca780f19dd56c9b2e369c14786651fbe3045022100d99a5cdd316d918eb4d061b1931782aa6f74d72d51ac9519d78499b66978cdba0220743392a80ef7d6e937d9b3417018f7cd25ca71d0072a1dc053cc92ab7da221e9ff011e001a8dba03027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582e0040700000000001147454f505320506f6f6c20526577617264e78e760a00000000000000001e63f74804d659a2ccaab80f0dd99b073c0f3f64903044022038d190e66e845984996ef68e21b790c2bfe181af3d6c9e8d00132e7ef64b52cc0220201442d9eae1975d0e6b906c29d7338a0ab4a71e65a4b48e299fd604d4fd8d9e3045022100e120a1516a97eba68a9fab61c0d542bb53564e50bff35ab1021bcce0ef6b727e02200ba867146c68d1093bed4a29df8837f97e44d4da9760cd98009c8e30a670c83cff011e001a8dba03027716e659220085e41389efc7cf6a05f7f7c659cf3db9126caabce6cda9156582e0040700000000001147454f505320506f6f6c205265776172648c40600100000000000000001e49f58a460b459159c76639a7f192ddbeff2c2b933044022030fd06c35b726b5483e240ddd00533fa40d2cdb8f9101764c54d4d3583fd8bec0220661e739cc7b73a08b1185031a70125dbb8fa70bc224e2ca926d7a3cc7050529530440220339b96a9a9b16717ef9ad08df685e2767e38c9191b3ec1e3ec13284480f6fe8402202738b64ccc4f972d8b6db1538d7c57ada7a8393ea2cf66aa7037c878d1650190",
        "000000003a8eba03a3471b008a983c930cda7e03000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550304d0c477d634cc85d89c1a4afee8f51168d1747fe8fd79cabc26565e49eb8a7a304402204c4b53fa05a6f59c49455a8b2c31cffbf328e27af60f0f6581ca6b491c0f3e01022025a22e3bb3b003f9c57544133dec946c356cca9a2a568cdb2c379f7d9d958b82",
        "00000000428eba03a4471b006ae6ce09264fc758000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855037997a6553ea8073eb199e9f5ff23b8f0892e79433ef35e13966e0a12849d02e330440220309be65b8646f44465ca19a630f6b49d74c2a286173d840ec02de66adc7edfa40220395812485bf1bf7768f9ea76cbb7b51c9278d53fda4ec7ff0280520075b9409f",
        "00000000488eba03a5471b00a99611e4be15e8db000000000000000000000000000000000000000000c2eb0b0000000000000000e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855039b5a3a71335bfa6c72b82498f814123e0678f7cd3d8e7221ec7124918736e01c304402205abacc5cd85d3b4ca8c98fb37899f5c6499a15cd3d4924571ac86f8e9ba037ff0220529f215ddd9b2d700f62d920407123799b2ddb37aa913004562344b4c450c93c"
    ]
}

and would be 6440 (52% reduction).

In summary: original: 13504 whitespace removed: 9474 (30% reduction) serialized: 6440 (52% reduction) serialized, whitespace removed: 6340 (53% reduction) whitespace removed, gzip: 3324 (75% reduction) whitespace removed, xz: 3080 (77% reduction) serialized, whitespace removed, gzip: 2603 (81% reduction) serialized, whitespace removed, xz: 2464 (82% reduction)

The problem with serialized is that each byte is represented as 2 characters and occupies 2 bytes in the output which is 2x bloat - something to avoid if one is concerned about space. If we are to avoid that and introduce a custom binary format, which will not be valid JSON, for example { "foo": "... binary data here..." } we would shrink the size to:

serialized with binary data, whitespace removed: 3196 (76% reduction) serialized with binary data, whitespace removed, gzip: 2315 (83% reduction) serialized with binary data, whitespace removed, xz: 2308 (83% reduction)

base64 would be somewhere in between and would keep it as valid JSON.

Now we find a balance between:

  1. implementation cost (amount of changes in the code to implement this), affects when this will be ready
  2. maintenance cost (complexity of the implementation), affects how easy it would be to change it in the future and how buggy it will be
  3. network traffic generated

Notice that 1. is pay once, 2. and 3. are permanent costs.

CPU load to serialize/deserialize and compress/decompress is to be assessed too.

kristjank commented 5 years ago

Let's do step 1 - serialization. first and complete this task, also to get this into 2.3

Step 2 - we can use codec from snapshots module to reduce this even more. But only after 1 is completed. MsgPack would be usefull here. But this is for later, ignore compression for now

geopsllc commented 5 years ago

Did you consider processing overhead on these options? Sending more data to reduce the amount of processing needed on both sides is the better choice considering the kind of VMs most people run on.

vasild commented 5 years ago

Actually, the JSON is already minimized when being sent (whitespace removed). So we have:

original: 9474 bytes
gzip: 3324 (65% reduction)
xz: 3080 (67% reduction)

serialized to base64: 4242 (55% reduction)

serialized to hex: 6340 (33% reduction)
serialized to hex, gzip: 2603 (73% reduction)
serialized to hex, xz: 2464 (74% reduction)

@geopsllc no, see the last sentence of my previous comment

vasild commented 5 years ago

After studying this a little bit more I realized that the data is already being compressed. A snippet from tcpdump(1):

Request:
--------
GET /peer/blocks?lastBlockHeight=1819552 HTTP/1.1
user-agent: got/9.6.0 (https://github.com/sindresorhus/got)
version: 2.2.1
port: 4002
nethash: 2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867
content-type: application/json
hashid: 264bd69b
accept-encoding: gzip, deflate   <--------------- [1]
Host: 35.221.179.218:4002
Connection: close

Response:
---------
HTTP/1.1 200 OK
X-RateLimit-UserLimit: 20
X-RateLimit-UserRemaining: 19
X-RateLimit-UserReset: 1552916765228
nethash: 2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867
milestonehash: 3b7ee0793e2ddf23
version: 2.1.0 
port: 4002
os: linux
height: 1819559
hashid: 90f40149
content-type: application/json; charset=utf-8
cache-control: no-cache
vary: accept-encoding
content-encoding: gzip    <--------------- [2]
Date: Mon, 18 Mar 2019 13:46:04 GMT
Connection: close
Transfer-Encoding: chunked

... binary data in gzip format ...

This is due to meaningful defaults in the libraries we use: [1] https://github.com/sindresorhus/got#decompress [2] https://hapijs.com/api#-serveroptionscompression

This means that we are already here:

gzip: 3324 (65% reduction)

Given the above I think it does not make sense to pursue further reduction by serialization, given the little benefit it is going to bring, the extra cost on CPU it is going to induce (in both serialization and deserialization) and complexity in the code.

@faustbrian @supaiku0 kill this issue?

spkjp commented 5 years ago

We already have the transactions serialized in memory (tx.serialized.toString("hex")) so there would be no overhead in that regards (except for the deserialization on the receiver's end which is a bit slower)

But yeah I think the numbers speak for itself and there is no real benefit at the moment of pursuing it further.

fix commented 5 years ago

@vasild there are other room of optimisation the aip11 is inducing: