From 3e8484cb15a82a01ab88909c3e32732fe82a1818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=22Doom=22=20Doumergue?= Date: Fri, 14 Jun 2024 04:58:43 +0200 Subject: [PATCH] Update Polkadot extrinsic encoding to support spec 1002005 (#3893) * Update Polkadot extrinsic encoding to support spec 1002005 * Update LIBC_PACKAGE_VERSION for Linux CI --- src/Polkadot/Extrinsic.cpp | 18 +++++++++++++++ tests/chains/Polkadot/ExtrinsicTests.cpp | 29 ++++++++++++++++++++++++ tools/install-sys-dependencies-linux | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Polkadot/Extrinsic.cpp b/src/Polkadot/Extrinsic.cpp index ebfbe67d2b0..8ec0b509c6e 100644 --- a/src/Polkadot/Extrinsic.cpp +++ b/src/Polkadot/Extrinsic.cpp @@ -388,8 +388,15 @@ Data Extrinsic::encodeIdentityAddAuthorization(const Proto::Identity::AddAuthori return data; } +static bool requires_new_spec_compatbility(uint32_t network, uint32_t specVersion) noexcept { + // version 1002005 introduces a breaking change for Polkadot and Kusama + return ((network == 0 || network == 2) && specVersion >= 1002005); +} + Data Extrinsic::encodePayload() const { Data data; + auto use_new_spec = requires_new_spec_compatbility(network, specVersion); + // call append(data, call); // era / nonce / tip @@ -398,6 +405,12 @@ Data Extrinsic::encodePayload() const { if (!feeAssetId.empty()) { append(data, feeAssetId); } + + if (use_new_spec) { + // mode (currently always 0) + data.push_back(0x00); + } + // specVersion encode32LE(specVersion, data); // transactionVersion @@ -406,6 +419,11 @@ Data Extrinsic::encodePayload() const { append(data, genesisHash); // block hash append(data, blockHash); + + if (use_new_spec) { + // empty metadata hash + data.push_back(0x00); + } return data; } diff --git a/tests/chains/Polkadot/ExtrinsicTests.cpp b/tests/chains/Polkadot/ExtrinsicTests.cpp index dd921a64343..4f5c969722a 100644 --- a/tests/chains/Polkadot/ExtrinsicTests.cpp +++ b/tests/chains/Polkadot/ExtrinsicTests.cpp @@ -159,4 +159,33 @@ TEST(PolkadotExtrinsic, Kusama_encodeAssetTransferNoCallIndices) { EXPECT_THROW(Polkadot::Extrinsic(input).encodeCall(input), std::invalid_argument); } +TEST(PolkadotExtrinsic, Polkadot_EncodePayloadWithNewSpec) { + Polkadot::Proto::SigningInput input; + input.set_network(0); + input.set_multi_address(true); + + auto* transfer = input.mutable_balance_call()->mutable_asset_transfer(); + transfer->set_to_address("14ixj163bkk2UEKLEXsEWosuFNuijpqEWZbX5JzN4yMHbUVD"); + auto* callIndices = transfer->mutable_call_indices()->mutable_custom(); + callIndices->set_module_index(0x32); + callIndices->set_method_index(0x05); + + auto value = store(999500000); + transfer->set_value(std::string(value.begin(), value.end())); + transfer->set_asset_id(1984); + + input.set_spec_version(1002000); // breaking change happens at version 1002005 + auto result = Polkadot::Extrinsic(input).encodePayload(); + EXPECT_EQ(hex(result), "3205011f00a4b558a0342ae6e379a7ed00d23ff505f1101646cb279844496ad608943eda0d82a34cee00000000104a0f0000000000"); + + input.set_spec_version(1002005); // >= 1002005 + result = Polkadot::Extrinsic(input).encodePayload(); + EXPECT_EQ(hex(result), "3205011f00a4b558a0342ae6e379a7ed00d23ff505f1101646cb279844496ad608943eda0d82a34cee0000000000154a0f000000000000"); + + input.set_spec_version(1002006); // >= 1002005 + result = Polkadot::Extrinsic(input).encodePayload(); + EXPECT_EQ(hex(result), "3205011f00a4b558a0342ae6e379a7ed00d23ff505f1101646cb279844496ad608943eda0d82a34cee0000000000164a0f000000000000"); +} + + } // namespace TW::Polkadot::tests diff --git a/tools/install-sys-dependencies-linux b/tools/install-sys-dependencies-linux index eec6524cd06..2f1e7db8e44 100755 --- a/tools/install-sys-dependencies-linux +++ b/tools/install-sys-dependencies-linux @@ -6,7 +6,7 @@ set -e if [[ "$1" == "ci" ]]; then LIBSTD_PACKAGE_VERSION="12.3.0-1ubuntu1~22.04" # Bump this version if the CI has been broken due to the packages update. - LIBC_PACKAGE_VERSION="2.35-0ubuntu3.7" + LIBC_PACKAGE_VERSION="2.35-0ubuntu3.8" echo "Remove GCC 13 from runner image - runner-images/8659 workaround" echo "NOTE: Bump $LIBC_PACKAGE_VERSION version if the CI has been broken due to the packages update"