From 435d56e2fcdecae1ca436574d159403d3225ad71 Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Fri, 27 Mar 2020 16:46:34 +0100 Subject: [PATCH] Timestamps must be sorted prior to appending into thin history index (#411) * Timestamps must be sorted prior to appending into thin history index * Skip unstable test --- accounts/keystore/account_cache_test.go | 2 ++ ethdb/mutation.go | 26 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index f38e761f9..99802d481 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -318,6 +318,8 @@ func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error { // TestUpdatedKeyfileContents tests that updating the contents of a keystore file // is noticed by the watcher, and the account cache is updated accordingly func TestUpdatedKeyfileContents(t *testing.T) { + t.Skip("This test is unstable") + t.Parallel() // Create a temporary kesytore to test with diff --git a/ethdb/mutation.go b/ethdb/mutation.go index 4c2a9f314..fa65b66e0 100644 --- a/ethdb/mutation.go +++ b/ethdb/mutation.go @@ -379,7 +379,17 @@ func (m *mutation) Commit() (uint64, error) { m.mu.Lock() defer m.mu.Unlock() - for timestamp, changes := range m.accountChangeSetByBlock { + // we need sorted timestamps for thin history index + accountTimestamps := make([]uint64, 0) + for ts := range m.accountChangeSetByBlock { + accountTimestamps = append(accountTimestamps, ts) + } + sort.Slice(accountTimestamps, func(i, j int) bool { return accountTimestamps[i] < accountTimestamps[j] }) + + for _, timestamp := range accountTimestamps { + changes := m.accountChangeSetByBlock[timestamp] + sort.Sort(changes) + if debug.IsThinHistory() { changedKeys := changes.ChangedKeys() for k := range changedKeys { @@ -393,7 +403,7 @@ func (m *mutation) Commit() (uint64, error) { m.puts.Set(dbutils.AccountsHistoryBucket, key, *index) } } - sort.Sort(changes) + var ( dat []byte err error @@ -410,12 +420,20 @@ func (m *mutation) Commit() (uint64, error) { m.puts.Set(dbutils.AccountChangeSetBucket, dbutils.EncodeTimestamp(timestamp), dat) } - for timestamp, changes := range m.storageChangeSetByBlock { + storageTimestamps := make([]uint64, 0) + for ts := range m.storageChangeSetByBlock { + storageTimestamps = append(storageTimestamps, ts) + } + sort.Slice(storageTimestamps, func(i, j int) bool { return storageTimestamps[i] < storageTimestamps[j] }) + + for _, timestamp := range storageTimestamps { + changes := m.storageChangeSetByBlock[timestamp] + sort.Sort(changes) + var ( dat []byte err error ) - sort.Sort(changes) if debug.IsThinHistory() { changedKeys := changes.ChangedKeys()