2019-04-26 15:42:40 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2019-02-25 04:26:42 +00:00
|
|
|
package storage_test
|
2019-02-19 21:01:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2019-02-25 04:26:42 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2019-04-25 17:44:14 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2019-02-19 21:01:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Storage queue", func() {
|
2019-04-25 02:49:33 +00:00
|
|
|
var (
|
|
|
|
db *postgres.DB
|
2019-07-29 19:28:20 +00:00
|
|
|
diff utils.StorageDiff
|
2019-04-25 02:49:33 +00:00
|
|
|
queue storage.IStorageQueue
|
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2019-08-16 21:30:26 +00:00
|
|
|
fakeAddr := "0x123456"
|
2019-07-29 19:28:20 +00:00
|
|
|
diff = utils.StorageDiff{
|
2019-09-10 21:26:54 +00:00
|
|
|
HashedAddress: utils.HexToKeccak256Hash(fakeAddr),
|
|
|
|
BlockHash: common.HexToHash("0x678901"),
|
|
|
|
BlockHeight: 987,
|
|
|
|
StorageKey: common.HexToHash("0x654321"),
|
|
|
|
StorageValue: common.HexToHash("0x198765"),
|
2019-02-19 21:01:07 +00:00
|
|
|
}
|
2019-04-25 02:49:33 +00:00
|
|
|
db = test_config.NewTestDB(test_config.NewTestNode())
|
|
|
|
test_config.CleanTestDB(db)
|
|
|
|
queue = storage.NewStorageQueue(db)
|
2019-07-29 19:28:20 +00:00
|
|
|
addErr := queue.Add(diff)
|
2019-02-19 21:01:07 +00:00
|
|
|
Expect(addErr).NotTo(HaveOccurred())
|
2019-04-25 02:49:33 +00:00
|
|
|
})
|
|
|
|
|
2019-06-14 16:26:15 +00:00
|
|
|
Describe("Add", func() {
|
2019-07-29 19:28:20 +00:00
|
|
|
It("adds a storage diff to the db", func() {
|
|
|
|
var result utils.StorageDiff
|
2019-06-14 16:26:15 +00:00
|
|
|
getErr := db.Get(&result, `SELECT contract, block_hash, block_height, storage_key, storage_value FROM public.queued_storage`)
|
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
2019-07-29 19:28:20 +00:00
|
|
|
Expect(result).To(Equal(diff))
|
2019-06-14 16:26:15 +00:00
|
|
|
})
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
It("does not duplicate storage diffs", func() {
|
|
|
|
addErr := queue.Add(diff)
|
2019-06-14 16:26:15 +00:00
|
|
|
Expect(addErr).NotTo(HaveOccurred())
|
|
|
|
var count int
|
|
|
|
getErr := db.Get(&count, `SELECT count(*) FROM public.queued_storage`)
|
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
|
|
|
Expect(count).To(Equal(1))
|
|
|
|
})
|
2019-02-19 21:01:07 +00:00
|
|
|
})
|
2019-04-25 02:49:33 +00:00
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
It("deletes storage diff from db", func() {
|
|
|
|
diffs, getErr := queue.GetAll()
|
2019-04-25 02:49:33 +00:00
|
|
|
Expect(getErr).NotTo(HaveOccurred())
|
2019-07-29 19:28:20 +00:00
|
|
|
Expect(len(diffs)).To(Equal(1))
|
2019-04-25 02:49:33 +00:00
|
|
|
|
2019-10-18 16:16:19 +00:00
|
|
|
err := queue.Delete(diffs[0].ID)
|
2019-04-25 02:49:33 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
remainingRows, secondGetErr := queue.GetAll()
|
|
|
|
Expect(secondGetErr).NotTo(HaveOccurred())
|
|
|
|
Expect(len(remainingRows)).To(BeZero())
|
|
|
|
})
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
It("gets all storage diffs from db", func() {
|
2019-08-16 21:30:26 +00:00
|
|
|
fakeAddr := "0x234567"
|
2019-07-29 19:28:20 +00:00
|
|
|
diffTwo := utils.StorageDiff{
|
2019-09-10 21:26:54 +00:00
|
|
|
HashedAddress: utils.HexToKeccak256Hash(fakeAddr),
|
|
|
|
BlockHash: common.HexToHash("0x678902"),
|
|
|
|
BlockHeight: 988,
|
|
|
|
StorageKey: common.HexToHash("0x654322"),
|
|
|
|
StorageValue: common.HexToHash("0x198766"),
|
2019-04-25 02:49:33 +00:00
|
|
|
}
|
2019-07-29 19:28:20 +00:00
|
|
|
addErr := queue.Add(diffTwo)
|
2019-04-25 02:49:33 +00:00
|
|
|
Expect(addErr).NotTo(HaveOccurred())
|
|
|
|
|
2019-07-29 19:28:20 +00:00
|
|
|
diffs, err := queue.GetAll()
|
2019-04-25 02:49:33 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-07-29 19:28:20 +00:00
|
|
|
Expect(len(diffs)).To(Equal(2))
|
|
|
|
Expect(diffs[0]).NotTo(Equal(diffs[1]))
|
2019-10-18 16:16:19 +00:00
|
|
|
Expect(diffs[0].ID).NotTo(BeZero())
|
2019-09-10 21:26:54 +00:00
|
|
|
Expect(diffs[0].HashedAddress).To(Or(Equal(diff.HashedAddress), Equal(diffTwo.HashedAddress)))
|
2019-07-29 19:28:20 +00:00
|
|
|
Expect(diffs[0].BlockHash).To(Or(Equal(diff.BlockHash), Equal(diffTwo.BlockHash)))
|
|
|
|
Expect(diffs[0].BlockHeight).To(Or(Equal(diff.BlockHeight), Equal(diffTwo.BlockHeight)))
|
|
|
|
Expect(diffs[0].StorageKey).To(Or(Equal(diff.StorageKey), Equal(diffTwo.StorageKey)))
|
|
|
|
Expect(diffs[0].StorageValue).To(Or(Equal(diff.StorageValue), Equal(diffTwo.StorageValue)))
|
2019-10-18 16:16:19 +00:00
|
|
|
Expect(diffs[1].ID).NotTo(BeZero())
|
2019-09-10 21:26:54 +00:00
|
|
|
Expect(diffs[1].HashedAddress).To(Or(Equal(diff.HashedAddress), Equal(diffTwo.HashedAddress)))
|
2019-07-29 19:28:20 +00:00
|
|
|
Expect(diffs[1].BlockHash).To(Or(Equal(diff.BlockHash), Equal(diffTwo.BlockHash)))
|
|
|
|
Expect(diffs[1].BlockHeight).To(Or(Equal(diff.BlockHeight), Equal(diffTwo.BlockHeight)))
|
|
|
|
Expect(diffs[1].StorageKey).To(Or(Equal(diff.StorageKey), Equal(diffTwo.StorageKey)))
|
|
|
|
Expect(diffs[1].StorageValue).To(Or(Equal(diff.StorageValue), Equal(diffTwo.StorageValue)))
|
2019-04-25 02:49:33 +00:00
|
|
|
})
|
2019-02-19 21:01:07 +00:00
|
|
|
})
|