forked from cerc-io/ipfs-ethdb
blockservice ethdb tests
This commit is contained in:
+10
-12
@@ -19,8 +19,6 @@ package pgipfsethdb
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/vulcanize/pg-ipfs-ethdb"
|
||||
)
|
||||
|
||||
// Batch is the type that satisfies the ethdb.Batch interface for PG-IPFS Ethereum data using a direct Postgres connection
|
||||
@@ -31,20 +29,23 @@ type Batch struct {
|
||||
}
|
||||
|
||||
// NewBatch returns a ethdb.Batch interface for PG-IPFS
|
||||
func NewBatch(db *sqlx.DB) ethdb.Batch {
|
||||
return &Batch{
|
||||
//
|
||||
func NewBatch(db *sqlx.DB, tx *sqlx.Tx) ethdb.Batch {
|
||||
b := &Batch{
|
||||
db: db,
|
||||
tx: tx,
|
||||
}
|
||||
if tx == nil {
|
||||
b.Reset()
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Put satisfies the ethdb.Batch interface
|
||||
// Put inserts the given value into the key-value data store
|
||||
// Key is expected to be the keccak256 hash of value
|
||||
func (b *Batch) Put(key []byte, value []byte) (err error) {
|
||||
if b.tx == nil {
|
||||
b.Reset()
|
||||
}
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(key)
|
||||
mhKey, err := MultihashKeyFromKeccak256(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -58,10 +59,7 @@ func (b *Batch) Put(key []byte, value []byte) (err error) {
|
||||
// Delete satisfies the ethdb.Batch interface
|
||||
// Delete removes the key from the key-value data store
|
||||
func (b *Batch) Delete(key []byte) (err error) {
|
||||
if b.tx == nil {
|
||||
b.Reset()
|
||||
}
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(key)
|
||||
mhKey, err := MultihashKeyFromKeccak256(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/pg-ipfs-ethdb"
|
||||
"github.com/vulcanize/pg-ipfs-ethdb/postgres"
|
||||
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -38,13 +37,13 @@ var (
|
||||
|
||||
var _ = Describe("Batch", func() {
|
||||
BeforeEach(func() {
|
||||
db, err = ipfsethdb.TestDB()
|
||||
db, err = pgipfsethdb.TestDB()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
database = pgipfsethdb.NewDatabase(db)
|
||||
batch = database.NewBatch()
|
||||
})
|
||||
AfterEach(func() {
|
||||
err = ipfsethdb.ResetTestDB(db)
|
||||
err = pgipfsethdb.ResetTestDB(db)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
@@ -75,7 +74,6 @@ var _ = Describe("Batch", func() {
|
||||
|
||||
Describe("Delete/Reset/Write", func() {
|
||||
It("deletes the key-value pair in the batch", func() {
|
||||
_, err = db.Exec("INSERT into public.blocks (key, data) VALUES ($1, $2)", testMhKey, testValue)
|
||||
err = batch.Put(testEthKey, testValue)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
err = batch.Put(testEthKey2, testValue2)
|
||||
|
||||
@@ -23,8 +23,6 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/vulcanize/pg-ipfs-ethdb"
|
||||
)
|
||||
|
||||
var errNotSupported = errors.New("this operation is not supported")
|
||||
@@ -59,7 +57,7 @@ func NewDatabase(db *sqlx.DB) ethdb.Database {
|
||||
// Has satisfies the ethdb.KeyValueReader interface
|
||||
// Has retrieves if a key is present in the key-value data store
|
||||
func (d *Database) Has(key []byte) (bool, error) {
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(key)
|
||||
mhKey, err := MultihashKeyFromKeccak256(key)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -70,7 +68,7 @@ func (d *Database) Has(key []byte) (bool, error) {
|
||||
// Get satisfies the ethdb.KeyValueReader interface
|
||||
// Get retrieves the given key if it's present in the key-value data store
|
||||
func (d *Database) Get(key []byte) ([]byte, error) {
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(key)
|
||||
mhKey, err := MultihashKeyFromKeccak256(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -82,7 +80,7 @@ func (d *Database) Get(key []byte) ([]byte, error) {
|
||||
// Put inserts the given value into the key-value data store
|
||||
// Key is expected to be the keccak256 hash of value
|
||||
func (d *Database) Put(key []byte, value []byte) error {
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(key)
|
||||
mhKey, err := MultihashKeyFromKeccak256(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -93,7 +91,7 @@ func (d *Database) Put(key []byte, value []byte) error {
|
||||
// Delete satisfies the ethdb.KeyValueWriter interface
|
||||
// Delete removes the key from the key-value data store
|
||||
func (d *Database) Delete(key []byte) error {
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(key)
|
||||
mhKey, err := MultihashKeyFromKeccak256(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@ import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/vulcanize/pg-ipfs-ethdb"
|
||||
"github.com/vulcanize/pg-ipfs-ethdb/postgres"
|
||||
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -37,17 +36,17 @@ var (
|
||||
testHeader = types.Header{Number: big.NewInt(1337)}
|
||||
testValue, _ = rlp.EncodeToBytes(testHeader)
|
||||
testEthKey = testHeader.Hash().Bytes()
|
||||
testMhKey, _ = ipfsethdb.MultihashKeyFromKeccak256(testEthKey)
|
||||
testMhKey, _ = pgipfsethdb.MultihashKeyFromKeccak256(testEthKey)
|
||||
)
|
||||
|
||||
var _ = Describe("Database", func() {
|
||||
BeforeEach(func() {
|
||||
db, err = ipfsethdb.TestDB()
|
||||
db, err = pgipfsethdb.TestDB()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
database = pgipfsethdb.NewDatabase(db)
|
||||
})
|
||||
AfterEach(func() {
|
||||
err = ipfsethdb.ResetTestDB(db)
|
||||
err = pgipfsethdb.ResetTestDB(db)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
|
||||
@@ -19,8 +19,6 @@ package pgipfsethdb
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/vulcanize/pg-ipfs-ethdb"
|
||||
)
|
||||
|
||||
// Iterator is the type that satisfies the ethdb.Iterator interface for PG-IPFS Ethereum data using a direct Postgres connection
|
||||
@@ -72,7 +70,7 @@ func (i *Iterator) Key() []byte {
|
||||
// The caller should not modify the contents of the returned slice
|
||||
// and its contents may change on the next call to Next
|
||||
func (i *Iterator) Value() []byte {
|
||||
mhKey, err := ipfsethdb.MultihashKeyFromKeccak256(i.currentKey)
|
||||
mhKey, err := MultihashKeyFromKeccak256(i.currentKey)
|
||||
if err != nil {
|
||||
i.err = err
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// 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/>.
|
||||
|
||||
package pgipfsethdb
|
||||
|
||||
import (
|
||||
"github.com/ipfs/go-ipfs-blockstore"
|
||||
"github.com/ipfs/go-ipfs-ds-help"
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq" //postgres driver
|
||||
"github.com/multiformats/go-multihash"
|
||||
)
|
||||
|
||||
// MultihashKeyFromKeccak256 converts keccak256 hash bytes into a blockstore-prefixed multihash db key string
|
||||
func MultihashKeyFromKeccak256(h []byte) (string, error) {
|
||||
mh, err := multihash.Encode(h, multihash.KECCAK_256)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dbKey := dshelp.MultihashToDsKey(mh)
|
||||
return blockstore.BlockPrefix.String() + dbKey.String(), nil
|
||||
}
|
||||
|
||||
// TestDB connect to the testing database
|
||||
// it assumes the database has the IPFS public.blocks table present
|
||||
// DO NOT use a production db for the test db, as it will remove all contents of the public.blocks table
|
||||
func TestDB() (*sqlx.DB, error) {
|
||||
connectStr := "postgresql://localhost:5432/vulcanize_testing?sslmode=disable"
|
||||
return sqlx.Connect("postgres", connectStr)
|
||||
}
|
||||
|
||||
// ResetTestDB drops all rows in the test db public.blocks table
|
||||
func ResetTestDB(db *sqlx.DB) error {
|
||||
_, err := db.Exec("TRUNCATE public.blocks")
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user