Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extrinsic Encoding differs from initial value #137

Closed
infinitedaremo opened this issue Mar 4, 2021 · 7 comments
Closed

Extrinsic Encoding differs from initial value #137

infinitedaremo opened this issue Mar 4, 2021 · 7 comments

Comments

@infinitedaremo
Copy link

Retrieving block 4013570 from Polkadot mainnet. The raw extrinsic hexs are as follows:

extrinsics := []string{
"0x280403000b409716f37701",
"0x450284004e4a383d2cc6653e9ede079e04c7fc951f39a45f8e8a1aabff8b1f3d158c284c018ea351924d3e4af2e0f61ac8ffefe3a8c3f7f80755c24551bd514eafcd25744bec3713fda140643af9efe06a12c96c3307bd0d48bcbb99a7edcd0194c473798e00a103000503008a4b192ab4dbe097774f392ead0727dfc35e2a990a012a1a23e5862f3dc4c5660b00ced62c891d",
"0x450284004e4a383d2cc6653e9ede079e04c7fc951f39a45f8e8a1aabff8b1f3d158c284c01a4c8d5127b6945a6eadf8b6cfd7c745509eba002a46649a476875f5c4e7d490d5d2dfafdc7a378669a919f70a5069907ba9b816a0363ad28906014c1fdb8498200a503000503003da4fdbd80ad23cf5efe767f626f6558e4985f462ecdeafbb91accccdc015be50b00a846b4ae0d",
"0x41028400dc9fb8f1c1f200f8787fd517e74d4d8cc58e73633b8e82cca603181604e6620900ee111f10a7c09f7ab535a8eddcc3919e8963f5f79b7ffaf9b9c3387aafb4ae6c345c1510b4d10e2b620bea7a4e4473a7256966e6752c93d2f2bf90d4ef5ae00215000000050000b2385eab057a2c1a757466642edc4d48d3838a2d3b6b04e92a81c2997656ab4a070037f87e06"}

Once the extrinsic is decoded to the Go struct. Attempting to encode it again gives differing values:

d, _ := extrinsic.MarshalJSON()
fmt.Println(string(d))
"0x280403000b409716f37701"
"0x410284004a383d2cc6653e9ede079e04c7fc951f39a45f8e8a1aabff8b1f3d158c284c018ea351924d3e4af2e0f61ac8ffefe3a8c3f7f80755c24551bd514eafcd25744bec3713fda140643af9efe06a12c96c3307bd0d48bcbb99a7edcd0194c473798e00a103000503008a4b192ab4dbe097774f392ead0727dfc35e2a990a012a1a23e5862f3dc4c5660b00ced62c891d"
"0x410284004a383d2cc6653e9ede079e04c7fc951f39a45f8e8a1aabff8b1f3d158c284c01a4c8d5127b6945a6eadf8b6cfd7c745509eba002a46649a476875f5c4e7d490d5d2dfafdc7a378669a919f70a5069907ba9b816a0363ad28906014c1fdb8498200a503000503003da4fdbd80ad23cf5efe767f626f6558e4985f462ecdeafbb91accccdc015be50b00a846b4ae0d"
"0x3d0284009fb8f1c1f200f8787fd517e74d4d8cc58e73633b8e82cca603181604e6620900ee111f10a7c09f7ab535a8eddcc3919e8963f5f79b7ffaf9b9c3387aafb4ae6c345c1510b4d10e2b620bea7a4e4473a7256966e6752c93d2f2bf90d4ef5ae00215000000050000b2385eab057a2c1a757466642edc4d48d3838a2d3b6b04e92a81c2997656ab4a070037f87e06"

Note the inherent extrinsic is as expected but the first 2nd, 3rd and 9th characters are different.

@infinitedaremo
Copy link
Author

I think it could potentiialy be down to this line:

func (e *Extrinsic) Decode(decoder scale.Decoder) error {
	// compact length encoding (1, 2, or 4 bytes) (may not be there for Extrinsics older than Jan 11 2019)
	_, err := decoder.DecodeUintCompact()
	if err != nil {
		return err
	}

There is no corresponding encode for a potential length which would explain why only the start of the value is different.

@mikiquantum
Copy link
Contributor

@infinitedaremo The length is prepended at the end of the function:

err = encoder.EncodeUintCompact(*big.NewInt(0).SetUint64(uint64(len(eb))))

The reason why is shorter (missing 5th byte 4e, I believe, is because GSRPC hasn't been updated to use MultiAddresses yet, introduced by paritytech/substrate#7380 and I believe Polkadot is running with them now.

We will support it very soon.

@infinitedaremo
Copy link
Author

Thats good to hear. In the mean time. Is there a way to get the original extrinsic hash from the current code?

@mikiquantum
Copy link
Contributor

@infinitedaremo Can you elaborate a bit more on this? Do you mean from each extrinsic encoded above, get the correspondent extrinsic hash id?

@infinitedaremo
Copy link
Author

Yes. I couldnt find that data anywhere.

@mikiquantum
Copy link
Contributor

mikiquantum commented Mar 7, 2021

@infinitedaremo I believe is just the blake2b256 of the byte array representation of the extrinsic encoded hex:

        h, err := NewBlake2b256(nil)
	assert.NoError(t, err)
	dd := hexutil.MustDecode("0x450284004e4a383d2cc6653e9ede079e04c7fc951f39a45f8e8a1aabff8b1f3d158c284c018ea351924d3e4af2e0f61ac8ffefe3a8c3f7f80755c24551bd514eafcd25744bec3713fda140643af9efe06a12c96c3307bd0d48bcbb99a7edcd0194c473798e00a103000503008a4b192ab4dbe097774f392ead0727dfc35e2a990a012a1a23e5862f3dc4c5660b00ced62c891d")
	_, err = h.Write(dd)
	assert.NoError(t, err)
	r := h.Sum(nil)
	fmt.Printf("%x\n", r)

@cdamian cdamian closed this as completed Dec 5, 2023
@cdamian
Copy link
Contributor

cdamian commented Dec 5, 2023

Please re-open another issue if there are questions related to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants