ChainSafe / gossamer

🕸️ Go Implementation of the Polkadot Host
https://chainsafe.github.io/gossamer
GNU Lesser General Public License v3.0
427 stars 110 forks source link

bug(lib/runtime/trie): transaction `sortedKeys` are lost after commit #4057

Closed EclesioMeloJunior closed 1 week ago

EclesioMeloJunior commented 1 week ago

Describe the bug

ensure!(active_recoveries.next().is_none(), Error::<T>::StillActive);

This check is not being activated which is causing the error.

After an investigation I could check that the iter_prefix_values, method used to get the active_recoveries, actually calls the next keys method, so I started doing some speculations and wrote few tests.

The problem happens when you insert few keys and values under a transaction

ts := NewTrieState(inmemory_trie.NewEmptyTrie())

fstKey := []byte("acc:abc123:ddd")
sndKey := []byte("acc:abc123:eee")
thdKey := []byte("acc:abc123:fff")

ts.StartTransaction()
 require.NoError(t, ts.Put(fstKey, []byte("0x10")))
 require.NoError(t, ts.Put(sndKey, []byte("0x11")))
 require.NoError(t, ts.Put(thdKey, []byte("0x12")))
ts.CommitTransaction()

and if you try to get the next key it will return nil instead of a actual valid key

prefix := []byte("acc:abc123")

ts.StartTransaction()
nxtKey := ts.NextKey(prefix)
require.NotNil(nxtKey)
ts.CommitTransaction()