Skip to content

Commit

Permalink
Update Polkadot extrinsic encoding to support spec 1002005 (#3893)
Browse files Browse the repository at this point in the history
* Update Polkadot extrinsic encoding to support spec 1002005

* Update LIBC_PACKAGE_VERSION for Linux CI
  • Loading branch information
doom authored Jun 14, 2024
1 parent a85c7d4 commit 3e8484c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Polkadot/Extrinsic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/chains/Polkadot/ExtrinsicTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tools/install-sys-dependencies-linux
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 3e8484c

Please sign in to comment.