diff --git a/libraries/shared/mocks/storage_queue.go b/libraries/shared/mocks/storage_queue.go index ae41f687..729219a8 100644 --- a/libraries/shared/mocks/storage_queue.go +++ b/libraries/shared/mocks/storage_queue.go @@ -31,3 +31,11 @@ func (queue *MockStorageQueue) Add(row utils.StorageDiffRow) error { queue.PassedRow = row return queue.AddError } + +func (queue *MockStorageQueue) Delete(id int) error { + panic("implement me") +} + +func (queue *MockStorageQueue) GetAll() ([]utils.StorageDiffRow, error) { + panic("implement me") +} diff --git a/libraries/shared/storage/storage_queue.go b/libraries/shared/storage/storage_queue.go index a077a4f9..5fe67630 100644 --- a/libraries/shared/storage/storage_queue.go +++ b/libraries/shared/storage/storage_queue.go @@ -23,6 +23,8 @@ import ( type IStorageQueue interface { Add(row utils.StorageDiffRow) error + Delete(id int) error + GetAll() ([]utils.StorageDiffRow, error) } type StorageQueue struct { @@ -40,3 +42,14 @@ func (queue StorageQueue) Add(row utils.StorageDiffRow) error { row.BlockHeight, row.StorageKey.Bytes(), row.StorageValue.Bytes()) return err } + +func (queue StorageQueue) Delete(id int) error { + _, err := queue.db.Exec(`DELETE FROM public.queued_storage WHERE id = $1`, id) + return err +} + +func (queue StorageQueue) GetAll() ([]utils.StorageDiffRow, error) { + var result []utils.StorageDiffRow + err := queue.db.Select(&result, `SELECT * FROM public.queued_storage`) + return result, err +} diff --git a/libraries/shared/storage/storage_queue_test.go b/libraries/shared/storage/storage_queue_test.go index 9157fcda..ca05f234 100644 --- a/libraries/shared/storage/storage_queue_test.go +++ b/libraries/shared/storage/storage_queue_test.go @@ -4,6 +4,7 @@ import ( "github.com/ethereum/go-ethereum/common" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/libraries/shared/storage" "github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils" @@ -11,23 +12,74 @@ import ( ) var _ = Describe("Storage queue", func() { - It("adds a storage row to the db", func() { - row := utils.StorageDiffRow{ + var ( + db *postgres.DB + row utils.StorageDiffRow + queue storage.IStorageQueue + ) + + BeforeEach(func() { + row = utils.StorageDiffRow{ Contract: common.HexToAddress("0x123456"), BlockHash: common.HexToHash("0x678901"), BlockHeight: 987, StorageKey: common.HexToHash("0x654321"), StorageValue: common.HexToHash("0x198765"), } - db := test_config.NewTestDB(test_config.NewTestNode()) - queue := storage.NewStorageQueue(db) - + db = test_config.NewTestDB(test_config.NewTestNode()) + test_config.CleanTestDB(db) + queue = storage.NewStorageQueue(db) addErr := queue.Add(row) - Expect(addErr).NotTo(HaveOccurred()) + }) + + It("adds a storage row to the db", func() { var result utils.StorageDiffRow getErr := db.Get(&result, `SELECT contract, block_hash, block_height, storage_key, storage_value FROM public.queued_storage`) Expect(getErr).NotTo(HaveOccurred()) Expect(result).To(Equal(row)) }) + + It("deletes storage row from db", func() { + rows, getErr := queue.GetAll() + Expect(getErr).NotTo(HaveOccurred()) + Expect(len(rows)).To(Equal(1)) + + err := queue.Delete(rows[0].Id) + + Expect(err).NotTo(HaveOccurred()) + remainingRows, secondGetErr := queue.GetAll() + Expect(secondGetErr).NotTo(HaveOccurred()) + Expect(len(remainingRows)).To(BeZero()) + }) + + It("gets all storage rows from db", func() { + rowTwo := utils.StorageDiffRow{ + Contract: common.HexToAddress("0x123456"), + BlockHash: common.HexToHash("0x678902"), + BlockHeight: 988, + StorageKey: common.HexToHash("0x654322"), + StorageValue: common.HexToHash("0x198766"), + } + addErr := queue.Add(rowTwo) + Expect(addErr).NotTo(HaveOccurred()) + + rows, err := queue.GetAll() + + Expect(err).NotTo(HaveOccurred()) + Expect(len(rows)).To(Equal(2)) + Expect(rows[0]).NotTo(Equal(rows[1])) + Expect(rows[0].Id).NotTo(BeZero()) + Expect(rows[0].Contract).To(Or(Equal(row.Contract), Equal(rowTwo.Contract))) + Expect(rows[0].BlockHash).To(Or(Equal(row.BlockHash), Equal(rowTwo.BlockHash))) + Expect(rows[0].BlockHeight).To(Or(Equal(row.BlockHeight), Equal(rowTwo.BlockHeight))) + Expect(rows[0].StorageKey).To(Or(Equal(row.StorageKey), Equal(rowTwo.StorageKey))) + Expect(rows[0].StorageValue).To(Or(Equal(row.StorageValue), Equal(rowTwo.StorageValue))) + Expect(rows[1].Id).NotTo(BeZero()) + Expect(rows[1].Contract).To(Or(Equal(row.Contract), Equal(rowTwo.Contract))) + Expect(rows[1].BlockHash).To(Or(Equal(row.BlockHash), Equal(rowTwo.BlockHash))) + Expect(rows[1].BlockHeight).To(Or(Equal(row.BlockHeight), Equal(rowTwo.BlockHeight))) + Expect(rows[1].StorageKey).To(Or(Equal(row.StorageKey), Equal(rowTwo.StorageKey))) + Expect(rows[1].StorageValue).To(Or(Equal(row.StorageValue), Equal(rowTwo.StorageValue))) + }) }) diff --git a/libraries/shared/storage/utils/row.go b/libraries/shared/storage/utils/row.go index dddb4e8f..70606fa5 100644 --- a/libraries/shared/storage/utils/row.go +++ b/libraries/shared/storage/utils/row.go @@ -25,6 +25,7 @@ import ( const ExpectedRowLength = 5 type StorageDiffRow struct { + Id int Contract common.Address BlockHash common.Hash `db:"block_hash"` BlockHeight int `db:"block_height"`