|
| 1 | +// VulcanizeDB |
| 2 | +// Copyright © 2019 Vulcanize |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package storage_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/ethereum/go-ethereum/common" |
| 21 | + "github.com/ethereum/go-ethereum/crypto" |
| 22 | + . "github.com/onsi/ginkgo" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + "github.com/vulcanize/vulcanizedb/libraries/shared/factories/storage" |
| 25 | + "github.com/vulcanize/vulcanizedb/libraries/shared/mocks" |
| 26 | + "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" |
| 27 | + "github.com/vulcanize/vulcanizedb/pkg/fakes" |
| 28 | + "github.com/vulcanize/vulcanizedb/test_config" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("Storage keys lookup", func() { |
| 32 | + var ( |
| 33 | + fakeMetadata = utils.GetStorageValueMetadata("name", map[utils.Key]string{}, utils.Uint256) |
| 34 | + lookup storage.KeysLookup |
| 35 | + loader *mocks.MockStorageKeysLoader |
| 36 | + ) |
| 37 | + |
| 38 | + BeforeEach(func() { |
| 39 | + loader = &mocks.MockStorageKeysLoader{} |
| 40 | + lookup = storage.NewKeysLookup(loader) |
| 41 | + }) |
| 42 | + |
| 43 | + Describe("Lookup", func() { |
| 44 | + Describe("when key not found", func() { |
| 45 | + It("refreshes keys", func() { |
| 46 | + loader.StorageKeyMappings = map[common.Hash]utils.StorageValueMetadata{fakes.FakeHash: fakeMetadata} |
| 47 | + _, err := lookup.Lookup(fakes.FakeHash) |
| 48 | + |
| 49 | + Expect(err).NotTo(HaveOccurred()) |
| 50 | + Expect(loader.LoadMappingsCallCount).To(Equal(1)) |
| 51 | + }) |
| 52 | + |
| 53 | + It("returns error if refreshing keys fails", func() { |
| 54 | + loader.LoadMappingsError = fakes.FakeError |
| 55 | + |
| 56 | + _, err := lookup.Lookup(fakes.FakeHash) |
| 57 | + |
| 58 | + Expect(err).To(HaveOccurred()) |
| 59 | + Expect(err).To(MatchError(fakes.FakeError)) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + Describe("when key found", func() { |
| 64 | + BeforeEach(func() { |
| 65 | + loader.StorageKeyMappings = map[common.Hash]utils.StorageValueMetadata{fakes.FakeHash: fakeMetadata} |
| 66 | + _, err := lookup.Lookup(fakes.FakeHash) |
| 67 | + Expect(err).NotTo(HaveOccurred()) |
| 68 | + Expect(loader.LoadMappingsCallCount).To(Equal(1)) |
| 69 | + }) |
| 70 | + |
| 71 | + It("does not refresh keys", func() { |
| 72 | + _, err := lookup.Lookup(fakes.FakeHash) |
| 73 | + |
| 74 | + Expect(err).NotTo(HaveOccurred()) |
| 75 | + Expect(loader.LoadMappingsCallCount).To(Equal(1)) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + It("returns metadata for loaded static key", func() { |
| 80 | + loader.StorageKeyMappings = map[common.Hash]utils.StorageValueMetadata{fakes.FakeHash: fakeMetadata} |
| 81 | + |
| 82 | + metadata, err := lookup.Lookup(fakes.FakeHash) |
| 83 | + |
| 84 | + Expect(err).NotTo(HaveOccurred()) |
| 85 | + Expect(metadata).To(Equal(fakeMetadata)) |
| 86 | + }) |
| 87 | + |
| 88 | + It("returns metadata for hashed version of key (accommodates keys emitted from Geth)", func() { |
| 89 | + loader.StorageKeyMappings = map[common.Hash]utils.StorageValueMetadata{fakes.FakeHash: fakeMetadata} |
| 90 | + |
| 91 | + hashedKey := common.BytesToHash(crypto.Keccak256(fakes.FakeHash.Bytes())) |
| 92 | + metadata, err := lookup.Lookup(hashedKey) |
| 93 | + |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + Expect(metadata).To(Equal(fakeMetadata)) |
| 96 | + }) |
| 97 | + |
| 98 | + It("returns key not found error if key not found", func() { |
| 99 | + _, err := lookup.Lookup(fakes.FakeHash) |
| 100 | + |
| 101 | + Expect(err).To(HaveOccurred()) |
| 102 | + Expect(err).To(MatchError(utils.ErrStorageKeyNotFound{Key: fakes.FakeHash.Hex()})) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + Describe("SetDB", func() { |
| 107 | + It("sets the db on the loader", func() { |
| 108 | + lookup.SetDB(test_config.NewTestDB(test_config.NewTestNode())) |
| 109 | + |
| 110 | + Expect(loader.SetDBCalled).To(BeTrue()) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments