2019-02-25 04:26:42 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-02-25 04:26:42 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package storage_test
|
|
|
|
|
|
|
|
import (
|
2019-10-31 16:04:53 +00:00
|
|
|
"math/rand"
|
|
|
|
|
2019-02-25 04:26:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/factories/storage"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/mocks"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/fakes"
|
2019-02-25 04:26:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Storage transformer", func() {
|
|
|
|
var (
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup *mocks.MockStorageKeysLookup
|
|
|
|
repository *mocks.MockStorageRepository
|
|
|
|
t storage.Transformer
|
2019-02-25 04:26:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup = &mocks.MockStorageKeysLookup{}
|
2019-02-25 04:26:42 +00:00
|
|
|
repository = &mocks.MockStorageRepository{}
|
|
|
|
t = storage.Transformer{
|
2019-10-21 22:08:09 +00:00
|
|
|
HashedAddress: common.Hash{},
|
|
|
|
StorageKeysLookup: storageKeysLookup,
|
|
|
|
Repository: repository,
|
2019-02-25 04:26:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns the contract address being watched", func() {
|
2019-08-16 21:30:26 +00:00
|
|
|
fakeAddress := utils.HexToKeccak256Hash("0x12345")
|
2019-08-16 21:01:01 +00:00
|
|
|
t.HashedAddress = fakeAddress
|
2019-02-25 04:26:42 +00:00
|
|
|
|
2019-08-16 21:01:01 +00:00
|
|
|
Expect(t.KeccakContractAddress()).To(Equal(fakeAddress))
|
2019-02-25 04:26:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("looks up metadata for storage key", func() {
|
2019-10-31 16:04:53 +00:00
|
|
|
t.Execute(utils.PersistedStorageDiff{})
|
2019-02-25 04:26:42 +00:00
|
|
|
|
2019-10-21 22:08:09 +00:00
|
|
|
Expect(storageKeysLookup.LookupCalled).To(BeTrue())
|
2019-02-25 04:26:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if lookup fails", func() {
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup.LookupErr = fakes.FakeError
|
2019-02-25 04:26:42 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
err := t.Execute(utils.PersistedStorageDiff{})
|
2019-02-25 04:26:42 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("creates storage row with decoded data", func() {
|
|
|
|
fakeMetadata := utils.StorageValueMetadata{Type: utils.Address}
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup.Metadata = fakeMetadata
|
2019-02-25 04:26:42 +00:00
|
|
|
rawValue := common.HexToAddress("0x12345")
|
|
|
|
fakeBlockNumber := 123
|
|
|
|
fakeBlockHash := "0x67890"
|
2019-10-31 16:04:53 +00:00
|
|
|
fakeRow := utils.PersistedStorageDiff{
|
|
|
|
ID: rand.Int63(),
|
|
|
|
StorageDiffInput: utils.StorageDiffInput{
|
|
|
|
HashedAddress: common.Hash{},
|
|
|
|
BlockHash: common.HexToHash(fakeBlockHash),
|
|
|
|
BlockHeight: fakeBlockNumber,
|
|
|
|
StorageKey: common.Hash{},
|
|
|
|
StorageValue: rawValue.Hash(),
|
|
|
|
},
|
2019-02-25 04:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := t.Execute(fakeRow)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-10-31 16:04:53 +00:00
|
|
|
Expect(repository.PassedDiffID).To(Equal(fakeRow.ID))
|
2019-02-25 04:26:42 +00:00
|
|
|
Expect(repository.PassedMetadata).To(Equal(fakeMetadata))
|
|
|
|
Expect(repository.PassedValue.(string)).To(Equal(rawValue.Hex()))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if creating row fails", func() {
|
|
|
|
rawValue := common.HexToAddress("0x12345")
|
|
|
|
fakeMetadata := utils.StorageValueMetadata{Type: utils.Address}
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup.Metadata = fakeMetadata
|
2019-02-25 04:26:42 +00:00
|
|
|
repository.CreateErr = fakes.FakeError
|
2019-10-31 16:04:53 +00:00
|
|
|
diff := utils.PersistedStorageDiff{StorageDiffInput: utils.StorageDiffInput{StorageValue: rawValue.Hash()}}
|
2019-02-25 04:26:42 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
err := t.Execute(diff)
|
2019-02-25 04:26:42 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
2019-07-18 22:09:07 +00:00
|
|
|
|
|
|
|
Describe("when a storage row contains more than one item packed in storage", func() {
|
|
|
|
var (
|
2019-07-19 14:56:21 +00:00
|
|
|
rawValue = common.HexToAddress("000000000000000000000000000000000000000000000002a300000000002a30")
|
2019-07-18 22:09:07 +00:00
|
|
|
fakeBlockNumber = 123
|
2019-07-19 14:56:21 +00:00
|
|
|
fakeBlockHash = "0x67890"
|
|
|
|
packedTypes = make(map[int]utils.ValueType)
|
2019-07-18 22:09:07 +00:00
|
|
|
)
|
|
|
|
packedTypes[0] = utils.Uint48
|
|
|
|
packedTypes[1] = utils.Uint48
|
|
|
|
|
|
|
|
var fakeMetadata = utils.StorageValueMetadata{
|
|
|
|
Name: "",
|
|
|
|
Keys: nil,
|
|
|
|
Type: utils.PackedSlot,
|
|
|
|
PackedTypes: packedTypes,
|
|
|
|
}
|
|
|
|
|
|
|
|
It("passes the decoded data items to the repository", func() {
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup.Metadata = fakeMetadata
|
2019-10-31 16:04:53 +00:00
|
|
|
fakeRow := utils.PersistedStorageDiff{
|
|
|
|
ID: rand.Int63(),
|
|
|
|
StorageDiffInput: utils.StorageDiffInput{
|
|
|
|
HashedAddress: common.Hash{},
|
|
|
|
BlockHash: common.HexToHash(fakeBlockHash),
|
|
|
|
BlockHeight: fakeBlockNumber,
|
|
|
|
StorageKey: common.Hash{},
|
|
|
|
StorageValue: rawValue.Hash(),
|
|
|
|
},
|
2019-07-18 22:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := t.Execute(fakeRow)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-10-31 16:04:53 +00:00
|
|
|
Expect(repository.PassedDiffID).To(Equal(fakeRow.ID))
|
2019-07-18 22:09:07 +00:00
|
|
|
Expect(repository.PassedMetadata).To(Equal(fakeMetadata))
|
|
|
|
expectedPassedValue := make(map[int]string)
|
2019-07-19 14:56:21 +00:00
|
|
|
expectedPassedValue[0] = "10800"
|
|
|
|
expectedPassedValue[1] = "172800"
|
2019-07-18 22:09:07 +00:00
|
|
|
Expect(repository.PassedValue.(map[int]string)).To(Equal(expectedPassedValue))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns error if creating a row fails", func() {
|
2019-10-21 22:08:09 +00:00
|
|
|
storageKeysLookup.Metadata = fakeMetadata
|
2019-07-18 22:09:07 +00:00
|
|
|
repository.CreateErr = fakes.FakeError
|
2019-10-31 16:04:53 +00:00
|
|
|
diff := utils.PersistedStorageDiff{StorageDiffInput: utils.StorageDiffInput{StorageValue: rawValue.Hash()}}
|
2019-07-18 22:09:07 +00:00
|
|
|
|
2019-10-31 16:04:53 +00:00
|
|
|
err := t.Execute(diff)
|
2019-07-18 22:09:07 +00:00
|
|
|
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err).To(MatchError(fakes.FakeError))
|
|
|
|
})
|
|
|
|
})
|
2019-02-25 04:26:42 +00:00
|
|
|
})
|