2020-06-03 20:42:11 +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/>.
|
|
|
|
|
2020-06-29 21:11:29 +00:00
|
|
|
package pgipfsethdb_test
|
2020-06-03 20:42:11 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
2021-08-24 05:12:22 +00:00
|
|
|
"time"
|
2020-06-03 20:42:11 +00:00
|
|
|
|
2023-03-30 21:10:38 +00:00
|
|
|
"github.com/cerc-io/ipfs-ethdb/v5/postgres/shared"
|
2023-02-20 18:26:40 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
|
2020-06-03 20:42:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2021-12-29 06:17:52 +00:00
|
|
|
"github.com/mailgun/groupcache/v2"
|
2020-06-03 20:42:11 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
|
2023-03-30 21:10:38 +00:00
|
|
|
pgipfsethdb "github.com/cerc-io/ipfs-ethdb/v5/postgres/v0"
|
2020-06-03 20:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
batch ethdb.Batch
|
|
|
|
testHeader2 = types.Header{Number: big.NewInt(2)}
|
|
|
|
testValue2, _ = rlp.EncodeToBytes(testHeader2)
|
|
|
|
testEthKey2 = testHeader2.Hash().Bytes()
|
2023-02-20 18:26:40 +00:00
|
|
|
testCID2, _ = pgipfsethdb.CIDFromKeccak256(testEthKey2, cid.EthBlock)
|
2020-06-03 20:42:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("Batch", func() {
|
|
|
|
BeforeEach(func() {
|
2023-02-20 18:26:40 +00:00
|
|
|
db, err = shared.TestDB()
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2021-08-24 05:12:22 +00:00
|
|
|
|
|
|
|
cacheConfig := pgipfsethdb.CacheConfig{
|
|
|
|
Name: "db",
|
|
|
|
Size: 3000000, // 3MB
|
|
|
|
ExpiryDuration: time.Hour,
|
|
|
|
}
|
|
|
|
|
|
|
|
database = pgipfsethdb.NewDatabase(db, cacheConfig)
|
2022-05-09 09:20:14 +00:00
|
|
|
|
|
|
|
databaseWithBlock, ok := database.(*pgipfsethdb.Database)
|
|
|
|
Expect(ok).To(BeTrue())
|
|
|
|
(*databaseWithBlock).BlockNumber = testBlockNumber
|
|
|
|
|
2020-06-03 20:42:11 +00:00
|
|
|
batch = database.NewBatch()
|
|
|
|
})
|
|
|
|
AfterEach(func() {
|
2021-12-29 06:17:52 +00:00
|
|
|
groupcache.DeregisterGroup("db")
|
2023-02-20 18:26:40 +00:00
|
|
|
err = shared.ResetTestDB(db)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Put/Write", func() {
|
|
|
|
It("adds the key-value pair to the batch", func() {
|
2023-02-20 18:26:40 +00:00
|
|
|
_, err = database.Get(testCID.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
2023-02-20 18:26:40 +00:00
|
|
|
_, err = database.Get(testCID2.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
|
|
|
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Put(testCID.Bytes(), testValue)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Put(testCID2.Bytes(), testValue2)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
err = batch.Write()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-02-20 18:26:40 +00:00
|
|
|
val, err := database.Get(testCID.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(val).To(Equal(testValue))
|
2023-02-20 18:26:40 +00:00
|
|
|
val2, err := database.Get(testCID2.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(val2).To(Equal(testValue2))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Delete/Reset/Write", func() {
|
|
|
|
It("deletes the key-value pair in the batch", func() {
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Put(testCID.Bytes(), testValue)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Put(testCID2.Bytes(), testValue2)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
err = batch.Write()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
batch.Reset()
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Delete(testCID.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Delete(testCID2.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
err = batch.Write()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-02-20 18:26:40 +00:00
|
|
|
_, err = database.Get(testCID.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
2023-02-20 18:26:40 +00:00
|
|
|
_, err = database.Get(testCID2.Bytes())
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("ValueSize/Reset", func() {
|
|
|
|
It("returns the size of data in the batch queued for write", func() {
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Put(testCID.Bytes(), testValue)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2023-02-20 18:26:40 +00:00
|
|
|
err = batch.Put(testCID2.Bytes(), testValue2)
|
2020-06-03 20:42:11 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
err = batch.Write()
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
size := batch.ValueSize()
|
|
|
|
Expect(size).To(Equal(len(testValue) + len(testValue2)))
|
|
|
|
|
|
|
|
batch.Reset()
|
|
|
|
size = batch.ValueSize()
|
|
|
|
Expect(size).To(Equal(0))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|