From 7a06397ae63c26bc81b890230333aa5926edf675 Mon Sep 17 00:00:00 2001 From: protolambda Date: Thu, 8 Sep 2022 11:00:19 +0200 Subject: [PATCH] op-chain-ops: MergeStorage deterministic storage results order --- op-chain-ops/state/state.go | 13 ++++++++----- op-chain-ops/state/state_test.go | 6 +----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/op-chain-ops/state/state.go b/op-chain-ops/state/state.go index 227cbeae530bf..19251152ce6ba 100644 --- a/op-chain-ops/state/state.go +++ b/op-chain-ops/state/state.go @@ -76,18 +76,21 @@ func ComputeStorageSlots(layout *solc.StorageLayout, values StorageValues) ([]*E // of the produced storage slots have a matching key, if so use a // binary or to add the storage values together func MergeStorage(storage []*EncodedStorage) []*EncodedStorage { - encoded := make(map[common.Hash]common.Hash) + encodedKV := make(map[common.Hash]common.Hash) + var encodedKeys []common.Hash // for deterministic result order for _, storage := range storage { - if prev, ok := encoded[storage.Key]; ok { + if prev, ok := encodedKV[storage.Key]; ok { combined := new(big.Int).Or(prev.Big(), storage.Value.Big()) - encoded[storage.Key] = common.BigToHash(combined) + encodedKV[storage.Key] = common.BigToHash(combined) } else { - encoded[storage.Key] = storage.Value + encodedKV[storage.Key] = storage.Value + encodedKeys = append(encodedKeys, storage.Key) } } results := make([]*EncodedStorage, 0) - for key, val := range encoded { + for _, key := range encodedKeys { + val := encodedKV[key] results = append(results, &EncodedStorage{key, val}) } return results diff --git a/op-chain-ops/state/state_test.go b/op-chain-ops/state/state_test.go index c491202f0e77e..25c5c971f3e34 100644 --- a/op-chain-ops/state/state_test.go +++ b/op-chain-ops/state/state_test.go @@ -276,11 +276,7 @@ func TestMergeStorage(t *testing.T) { for _, test := range cases { got := state.MergeStorage(test.input) - // deep equal check - require.Equal(t, len(got), len(test.expect)) - for i := range got { - require.Equal(t, *got[i], *test.expect[i]) - } + require.Equal(t, test.expect, got) } }