leonardocustodio / polkadart

Polkadart provides developers the ability to query a node and interact with the Polkadot based chains using Dart.
https://polkadart.dev
Apache License 2.0
39 stars 16 forks source link

Wrong storage hash key for storage value #373

Closed clangenb closed 1 year ago

clangenb commented 1 year ago

In our generated code we have this:

  final _i1.StorageValue<int> _endorsementTicketsPerBootstrapper = const _i1.StorageValue<int>(
    prefix: 'EncointerCeremonies',
    storage: 'EndorsementTicketsPerBootstrapper',
    valueCodec: _i4.U8Codec.codec,
  );

The generated key hash:

    final hashedKey = _endorsementTicketsPerBootstrapper.hashedKey();

    // hashedKey: 0xa7d291a8132b2cc65c41da45f4de76796ae3d97cb4a9938c6ebddc6491b5bc06
    print("hashedKey: 0x${hex.encode(hashedKey)}");

Is wrong, the correct key hash according to polkadot-js/apps is:

image

So the method is wrong in this case, whereas the prefix is correct. The method has 33 characters, so my guess is that some magic happens on substrate side for prefixes > 32 bytes.

leonardocustodio commented 1 year ago

Ok, found the issue it is in the xxh64 algorithm actually (that is used by twox). cc @justkawal

leonardocustodio commented 1 year ago

Fixed on version 0.2.4

clangenb commented 1 year ago

I can confirm that it is fixed on our end. Thanks for the blazingly fast fix!

clangenb commented 1 year ago

I have to correct my statement, the hash seems to be wrong in other cases now, example:

  final _i1.StorageValue<_i3.CeremonyPhaseType> _currentPhase = const _i1.StorageValue<_i3.CeremonyPhaseType>(
    prefix: 'EncointerScheduler',
    storage: 'CurrentPhase',
    valueCodec: _i3.CeremonyPhaseType.codec,
  );

// wrong result: 
// 26dc3fa0a77526a8494180a4789f6883d9764401941df7f77a47ba7db64a6e0a
// actual value:
// 26dc3fa0a77526a8494180a4789f6883d9764401941df7f707a47ba7db64a6ea

Actual value:

image

It only seems to affect the last bytes.

I can confirm that the storage hash was correct before #375.

clangenb commented 1 year ago

It seems I can't reopen the issue unfortunately. Shall I create a new one?

leonardocustodio commented 1 year ago

They are pretty close though haha. I wish dart itself had those hashing algorithms. I will check it later.

leonardocustodio commented 1 year ago

Totally my fault this one. When converting to hex it did not use signed 2's complement. It should be fixed now, also added this string to the tests.

leonardocustodio commented 1 year ago

Released under version: polkadart: ^0.2.5

clangenb commented 1 year ago

Nice, our integration tests work again, seems to be fine, thanks a lot for this quick reaction again! 👍