Compare commits

..
Author SHA1 Message Date
i-norden 04ab6b6cfd bump go version 2023-03-30 16:50:34 -05:00
i-norden 288210a68c use stock v1.11.5 geth 2023-03-30 16:24:43 -05:00
Michael 92f55b9712 Merge pull request #54 from cerc-io/rebase-1.11-4-wip
version updates for 1.11.4 statediffing 4.3.8
2023-03-17 09:34:56 -04:00
Michael Shaw f25575e5cb version updates for 1.11.4 statediffing 4.3.8 2023-03-17 09:32:46 -04:00
Michael 6c59c0d1f1 Merge pull request #53 from cerc-io/rebase_1.11.2_wip
updated for geth v1.11.2-statediff-4.3.7-alpha
2023-03-10 12:18:47 -05:00
Michael Shaw 86e51e9518 updated for geth v1.11.2-statediff-4.3.7-alpha 2023-03-10 11:55:20 -05:00
Michael 68a24ab952 Merge pull request #48 from cerc-io/v4.0.10-alpha-wip
dependency updates for 1.10.26 geth release
2022-11-07 10:52:20 -05:00
Michael Shaw 806f5bbe26 dependency updates for 1.10.26 geth release 2022-11-07 10:25:21 -05:00
Michael Shaw 99cbb3b0c1 missed 23->25 change 2022-09-23 13:33:11 -04:00
Michael Shaw e3e8b2fbc1 dependency updates for geth-statediff 1.10.25 2022-09-23 13:26:57 -04:00
Michael 13c752b90e Merge pull request #47 from cerc-io/cerc_refactor
cerc github refactor
2022-09-14 13:54:28 -04:00
Michael Shaw f62c828c86 cerc github refactor 2022-09-14 01:03:07 -04:00
Michael b34f7e0564 Merge pull request #46 from vulcanize/rebase-1.10.23-wip
version update for statediffing geth 1.10.23
2022-09-01 16:11:49 -04:00
Michael Shaw 32edf85e5d version update for statediffing geth 1.10.23 2022-09-01 15:58:38 -04:00
prathamesh0 fd19af396d Log key on db misses (#45) 2022-08-23 12:14:42 +05:30
Michael b706c596c5 Merge pull request #44 from vulcanize/geth1.10.21-update-wip
Exchange.Interface no longer IsOnline and other updates for blockserv…
2022-08-01 17:06:21 -04:00
Michael Shaw 845d37cacf Exchange.Interface no longer IsOnline and other updates for blockservice version update 2022-08-01 17:05:18 -04:00
Michael 963053f23e Merge pull request #43 from vulcanize/geth1.10.21-update-wip
updating go.mod to depend on vulcanize fork of geth to avoid divergin…
2022-08-01 16:15:37 -04:00
Michael Shaw f1c3dfccd3 updating go.mod to depend on vulcanize fork of geth to avoid diverging dependencies that are needed higher up the stack 2022-08-01 15:59:24 -04:00
Michael 4a40d2b14e Merge pull request #42 from vulcanize/geth1.10.21-update-wip
go mod and sum updated for geth 1.10.21
2022-07-29 15:01:21 -04:00
Michael Shaw 77cc282a82 go mod and sum updated for geth 1.10.21 2022-07-29 14:57:24 -04:00
Abdul Rabbani f3de4233b7 Merge pull request #40 from vulcanize/feature/update-geth-1.10.19
Update and test
2022-06-17 08:16:51 -04:00
Abdul Rabbani 23177c8dff Update and test
```
08:09:45:~/GitHub/cerc/ipfs-ethdb go test ./... -v
=== RUN   TestIPFSETHDB
Running Suite: IPFS ethdb test
==============================
Random Seed: 1655467793
Will run 9 of 9 specs

•••••••••
Ran 9 of 9 Specs in 0.000 seconds
SUCCESS! -- 9 Passed | 0 Failed | 0 Pending | 0 Skipped
--- PASS: TestIPFSETHDB (0.00s)
PASS
ok      github.com/vulcanize/ipfs-ethdb/v4      0.177s
=== RUN   TestPGIPFSETHDB
Running Suite: PG-IPFS ethdb test
=================================
Random Seed: 1655467793
Will run 9 of 9 specs

•••••••••
Ran 9 of 9 Specs in 0.922 seconds
SUCCESS! -- 9 Passed | 0 Failed | 0 Pending | 0 Skipped
--- PASS: TestPGIPFSETHDB (0.92s)
PASS
ok      github.com/vulcanize/ipfs-ethdb/v4/postgres     1.157s
```
2022-06-17 08:10:46 -04:00
Abdul Rabbani 1c90257717 Merge pull request #39 from vulcanize/feature/update-go-version-sharding
Feature/update go version sharding
2022-05-31 13:03:11 -04:00
Abdul Rabbani 86b530c3a3 Merge branch 'master' into feature/update-go-version-sharding 2022-05-31 13:02:06 -04:00
Abdul Rabbani 0069033d39 Merge pull request #38 from vulcanize/feature/update-go-version
Use v1.18 go
2022-05-31 12:59:01 -04:00
Abdul Rabbani d22e0f70e2 Use v1.18 go 2022-05-31 12:58:33 -04:00
Abdul Rabbani b220685662 Merge pull request #37 from vulcanize/release-v3.0.2
Update Go version and Fix Interface
2022-05-31 07:35:26 -04:00
Abdul Rabbani 950fb0802d Update Go version and Fix Interface 2022-05-27 13:50:59 -04:00
Ashwin Phatak a19b47d67c Merge pull request #33 from deep-stack/pm-v4-schema-changes
Updates to use v4 schema
2022-05-09 16:30:35 +05:30
prathamesh ed9f8d7c4e Update module path for v4 2022-05-09 16:29:09 +05:30
prathamesh 08bc2f87ab Add block number while making inserts 2022-05-09 15:19:44 +05:30
prathamesh a6b49f89f4 Updates to use v4 schema 2022-05-09 15:19:28 +05:30
Ashwin Phatak 2bc23f4deb Merge pull request #32 from deep-stack/pm-v3-release
Update module path for v3
2022-05-02 19:54:15 +05:30
prathamesh fb4c911adb Update module path for v3 2022-05-02 19:09:05 +05:30
13 changed files with 256 additions and 710 deletions
+3 -3
View File
@@ -34,7 +34,7 @@ Ancient interfaces are used for Ancient/frozen data operations (e.g. rawdb/table
Outside of these primarily auxiliary capabilities, this package satisfies the interfaces required for many state operations using Ethereum data on IPFS. Outside of these primarily auxiliary capabilities, this package satisfies the interfaces required for many state operations using Ethereum data on IPFS.
e.g. e.g.
go-ethereum trie.NodeIterator and state.NodeIterator can be constructed from the ethdb.KeyValueStore and ethdb.Database interfaces, respectively: go-ethereum trie.NodeIterator and state.NodeIterator can be constructed from the ethdb.KeyValueStore and ethdb.Database interfaces, respectively:
```go ```go
@@ -42,7 +42,7 @@ package main
import ( import (
"context" "context"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
@@ -50,7 +50,7 @@ import (
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/fsrepo" "github.com/ipfs/go-ipfs/repo/fsrepo"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/vulcanize/ipfs-ethdb" "github.com/vulcanize/ipfs-ethdb/v4"
) )
func main() { func main() {
+3 -2
View File
@@ -17,6 +17,7 @@
package ipfsethdb package ipfsethdb
import ( import (
"context"
"errors" "errors"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
@@ -103,7 +104,7 @@ func (b *Batch) Write() error {
} }
puts[i] = b puts[i] = b
} }
if err := b.blockService.AddBlocks(puts); err != nil { if err := b.blockService.AddBlocks(context.Background(), puts); err != nil {
return err return err
} }
for _, key := range b.deleteCache.Keys() { for _, key := range b.deleteCache.Keys() {
@@ -112,7 +113,7 @@ func (b *Batch) Write() error {
if err != nil { if err != nil {
return err return err
} }
if err := b.blockService.DeleteBlock(c); err != nil { if err := b.blockService.DeleteBlock(context.Background(), c); err != nil {
return err return err
} }
} }
+1 -1
View File
@@ -25,7 +25,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
ipfsethdb "github.com/vulcanize/ipfs-ethdb" ipfsethdb "github.com/cerc-io/ipfs-ethdb/v4"
) )
var ( var (
+9 -8
View File
@@ -20,7 +20,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
@@ -69,7 +68,7 @@ func (d *Database) Has(key []byte) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
return d.blockService.Blockstore().Has(c) return d.blockService.Blockstore().Has(context.Background(), c)
} }
// Get satisfies the ethdb.KeyValueReader interface // Get satisfies the ethdb.KeyValueReader interface
@@ -95,7 +94,7 @@ func (d *Database) Put(key []byte, value []byte) error {
if err != nil { if err != nil {
return err return err
} }
return d.blockService.AddBlock(b) return d.blockService.AddBlock(context.Background(), b)
} }
// Delete satisfies the ethdb.KeyValueWriter interface // Delete satisfies the ethdb.KeyValueWriter interface
@@ -106,7 +105,7 @@ func (d *Database) Delete(key []byte) error {
if err != nil { if err != nil {
return err return err
} }
return d.blockService.DeleteBlock(c) return d.blockService.DeleteBlock(context.Background(), c)
} }
// DatabaseProperty enum type // DatabaseProperty enum type
@@ -135,9 +134,6 @@ func (d *Database) Stat(property string) (string, error) {
return "", err return "", err
} }
switch prop { switch prop {
case ExchangeOnline:
online := d.blockService.Exchange().IsOnline()
return strconv.FormatBool(online), nil
default: default:
return "", fmt.Errorf("unhandled database property") return "", fmt.Errorf("unhandled database property")
} }
@@ -227,7 +223,7 @@ func (d *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][
} }
// ReadAncients applies the provided AncientReader function // ReadAncients applies the provided AncientReader function
func (d *Database) ReadAncients(fn func(ethdb.AncientReader) error) (err error) { func (d *Database) ReadAncients(fn func(ethdb.AncientReaderOp) error) (err error) {
return errNotSupported return errNotSupported
} }
@@ -260,3 +256,8 @@ func (d *Database) MigrateTable(string, func([]byte) ([]byte, error)) error {
func (d *Database) NewSnapshot() (ethdb.Snapshot, error) { func (d *Database) NewSnapshot() (ethdb.Snapshot, error) {
return nil, errNotSupported return nil, errNotSupported
} }
// AncientDatadir returns an error as we don't have a backing chain freezer.
func (d *Database) AncientDatadir() (string, error) {
return "", errNotSupported
}
+1 -1
View File
@@ -26,7 +26,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
ipfsethdb "github.com/vulcanize/ipfs-ethdb" ipfsethdb "github.com/cerc-io/ipfs-ethdb/v4"
) )
var ( var (
+60 -11
View File
@@ -1,22 +1,71 @@
module github.com/vulcanize/ipfs-ethdb module github.com/cerc-io/ipfs-ethdb/v4
go 1.15 go 1.19
require ( require (
github.com/btcsuite/btcd v0.22.1 // indirect github.com/ethereum/go-ethereum v1.11.5
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/ethereum/go-ethereum v1.10.17
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/ipfs/go-block-format v0.0.3 github.com/ipfs/go-block-format v0.0.3
github.com/ipfs/go-blockservice v0.1.3 github.com/ipfs/go-blockservice v0.4.0
github.com/ipfs/go-cid v0.0.7 github.com/ipfs/go-cid v0.2.0
github.com/ipfs/go-ipfs-blockstore v1.0.1 github.com/ipfs/go-ipfs-blockstore v1.2.0
github.com/ipfs/go-ipfs-ds-help v1.0.0 github.com/ipfs/go-ipfs-ds-help v1.1.0
github.com/ipfs/go-ipfs-exchange-interface v0.0.1 github.com/ipfs/go-ipfs-exchange-interface v0.2.0
github.com/jmoiron/sqlx v1.3.5 github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.5 github.com/lib/pq v1.10.7
github.com/mailgun/groupcache/v2 v2.3.0 github.com/mailgun/groupcache/v2 v2.3.0
github.com/multiformats/go-multihash v0.1.0 github.com/multiformats/go-multihash v0.1.0
github.com/onsi/ginkgo v1.16.5 github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.19.0 github.com/onsi/gomega v1.19.0
github.com/sirupsen/logrus v1.6.0
)
require github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
require (
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-datastore v0.5.0 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipld-format v0.4.0 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-log/v2 v2.3.0 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-verifcid v0.0.1 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.0.3 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multibase v0.0.3 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/segmentio/fasthash v1.0.3 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
lukechampine.com/blake3 v1.1.6 // indirect
) )
+111 -646
View File
File diff suppressed because it is too large Load Diff
+14 -14
View File
@@ -52,27 +52,27 @@ func (mbs *MockBlockservice) Exchange() exchange.Interface {
panic("Exchange: implement me") panic("Exchange: implement me")
} }
func (mbs *MockBlockservice) AddBlock(b blocks.Block) error { func (mbs *MockBlockservice) AddBlock(ctx context.Context, b blocks.Block) error {
return mbs.blockStore.Put(b) return mbs.blockStore.Put(ctx, b)
} }
func (mbs *MockBlockservice) AddBlocks(bs []blocks.Block) error { func (mbs *MockBlockservice) AddBlocks(ctx context.Context, bs []blocks.Block) error {
return mbs.blockStore.PutMany(bs) return mbs.blockStore.PutMany(ctx, bs)
} }
func (mbs *MockBlockservice) DeleteBlock(c cid.Cid) error { func (mbs *MockBlockservice) DeleteBlock(ctx context.Context, c cid.Cid) error {
return mbs.blockStore.DeleteBlock(c) return mbs.blockStore.DeleteBlock(ctx, c)
} }
func (mbs *MockBlockservice) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) { func (mbs *MockBlockservice) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) {
return mbs.blockStore.Get(c) return mbs.blockStore.Get(ctx, c)
} }
func (mbs *MockBlockservice) GetBlocks(ctx context.Context, cs []cid.Cid) <-chan blocks.Block { func (mbs *MockBlockservice) GetBlocks(ctx context.Context, cs []cid.Cid) <-chan blocks.Block {
blockChan := make(chan blocks.Block) blockChan := make(chan blocks.Block)
go func() { go func() {
for _, c := range cs { for _, c := range cs {
if b, err := mbs.blockStore.Get(c); err == nil { if b, err := mbs.blockStore.Get(ctx, c); err == nil {
blockChan <- b blockChan <- b
} }
} }
@@ -93,17 +93,17 @@ type MockBlockstore struct {
err error err error
} }
func (mbs *MockBlockstore) DeleteBlock(c cid.Cid) error { func (mbs *MockBlockstore) DeleteBlock(ctx context.Context, c cid.Cid) error {
delete(mbs.blocks, c.String()) delete(mbs.blocks, c.String())
return mbs.err return mbs.err
} }
func (mbs *MockBlockstore) Has(c cid.Cid) (bool, error) { func (mbs *MockBlockstore) Has(ctx context.Context, c cid.Cid) (bool, error) {
_, ok := mbs.blocks[c.String()] _, ok := mbs.blocks[c.String()]
return ok, mbs.err return ok, mbs.err
} }
func (mbs *MockBlockstore) Get(c cid.Cid) (blocks.Block, error) { func (mbs *MockBlockstore) Get(ctx context.Context, c cid.Cid) (blocks.Block, error) {
obj, ok := mbs.blocks[c.String()] obj, ok := mbs.blocks[c.String()]
if !ok { if !ok {
return nil, blockNotFoundErr return nil, blockNotFoundErr
@@ -111,7 +111,7 @@ func (mbs *MockBlockstore) Get(c cid.Cid) (blocks.Block, error) {
return obj, mbs.err return obj, mbs.err
} }
func (mbs *MockBlockstore) GetSize(c cid.Cid) (int, error) { func (mbs *MockBlockstore) GetSize(ctx context.Context, c cid.Cid) (int, error) {
obj, ok := mbs.blocks[c.String()] obj, ok := mbs.blocks[c.String()]
if !ok { if !ok {
return 0, blockNotFoundErr return 0, blockNotFoundErr
@@ -119,12 +119,12 @@ func (mbs *MockBlockstore) GetSize(c cid.Cid) (int, error) {
return len(obj.RawData()), mbs.err return len(obj.RawData()), mbs.err
} }
func (mbs *MockBlockstore) Put(b blocks.Block) error { func (mbs *MockBlockstore) Put(ctx context.Context, b blocks.Block) error {
mbs.blocks[b.Cid().String()] = b mbs.blocks[b.Cid().String()] = b
return mbs.err return mbs.err
} }
func (mbs *MockBlockstore) PutMany(bs []blocks.Block) error { func (mbs *MockBlockstore) PutMany(ctx context.Context, bs []blocks.Block) error {
for _, b := range bs { for _, b := range bs {
mbs.blocks[b.Cid().String()] = b mbs.blocks[b.Cid().String()] = b
} }
+9 -4
View File
@@ -17,6 +17,8 @@
package pgipfsethdb package pgipfsethdb
import ( import (
"math/big"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
@@ -28,13 +30,16 @@ type Batch struct {
db *sqlx.DB db *sqlx.DB
tx *sqlx.Tx tx *sqlx.Tx
valueSize int valueSize int
blockNumber *big.Int
} }
// NewBatch returns a ethdb.Batch interface for PG-IPFS // NewBatch returns a ethdb.Batch interface for PG-IPFS
func NewBatch(db *sqlx.DB, tx *sqlx.Tx) ethdb.Batch { func NewBatch(db *sqlx.DB, tx *sqlx.Tx, blockNumber *big.Int) ethdb.Batch {
b := &Batch{ b := &Batch{
db: db, db: db,
tx: tx, tx: tx,
blockNumber: blockNumber,
} }
if tx == nil { if tx == nil {
b.Reset() b.Reset()
@@ -50,7 +55,7 @@ func (b *Batch) Put(key []byte, value []byte) (err error) {
if err != nil { if err != nil {
return err return err
} }
if _, err = b.tx.Exec(putPgStr, mhKey, value); err != nil { if _, err = b.tx.Exec(putPgStr, mhKey, value, b.blockNumber.Uint64()); err != nil {
return err return err
} }
b.valueSize += len(value) b.valueSize += len(value)
+6 -1
View File
@@ -27,7 +27,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/postgres" pgipfsethdb "github.com/cerc-io/ipfs-ethdb/v4/postgres"
) )
var ( var (
@@ -49,6 +49,11 @@ var _ = Describe("Batch", func() {
} }
database = pgipfsethdb.NewDatabase(db, cacheConfig) database = pgipfsethdb.NewDatabase(db, cacheConfig)
databaseWithBlock, ok := database.(*pgipfsethdb.Database)
Expect(ok).To(BeTrue())
(*databaseWithBlock).BlockNumber = testBlockNumber
batch = database.NewBatch() batch = database.NewBatch()
}) })
AfterEach(func() { AfterEach(func() {
+23 -8
View File
@@ -18,8 +18,10 @@ package pgipfsethdb
import ( import (
"context" "context"
"database/sql"
"errors" "errors"
"fmt" "fmt"
"math/big"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -27,14 +29,15 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/mailgun/groupcache/v2" "github.com/mailgun/groupcache/v2"
log "github.com/sirupsen/logrus"
) )
var errNotSupported = errors.New("this operation is not supported") var errNotSupported = errors.New("this operation is not supported")
var ( var (
hasPgStr = "SELECT exists(select 1 from public.blocks WHERE key = $1)" hasPgStr = "SELECT exists(select 1 from public.blocks WHERE key = $1 LIMIT 1)"
getPgStr = "SELECT data FROM public.blocks WHERE key = $1" getPgStr = "SELECT data FROM public.blocks WHERE key = $1 LIMIT 1"
putPgStr = "INSERT INTO public.blocks (key, data) VALUES ($1, $2) ON CONFLICT (key) DO NOTHING" putPgStr = "INSERT INTO public.blocks (key, data, block_number) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING"
deletePgStr = "DELETE FROM public.blocks WHERE key = $1" deletePgStr = "DELETE FROM public.blocks WHERE key = $1"
dbSizePgStr = "SELECT pg_database_size(current_database())" dbSizePgStr = "SELECT pg_database_size(current_database())"
) )
@@ -45,6 +48,8 @@ var _ ethdb.Database = &Database{}
type Database struct { type Database struct {
db *sqlx.DB db *sqlx.DB
cache *groupcache.Group cache *groupcache.Group
BlockNumber *big.Int
} }
func (d *Database) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) { func (d *Database) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
@@ -110,7 +115,12 @@ func (d *Database) Has(key []byte) (bool, error) {
// Get retrieves the given key if it's present in the key-value data store // Get retrieves the given key if it's present in the key-value data store
func (d *Database) dbGet(key string) ([]byte, error) { func (d *Database) dbGet(key string) ([]byte, error) {
var data []byte var data []byte
return data, d.db.Get(&data, getPgStr, key) err := d.db.Get(&data, getPgStr, key)
if err == sql.ErrNoRows {
log.Warn("Database miss for key", key)
}
return data, err
} }
// Get satisfies the ethdb.KeyValueReader interface // Get satisfies the ethdb.KeyValueReader interface
@@ -136,7 +146,7 @@ func (d *Database) Put(key []byte, value []byte) error {
if err != nil { if err != nil {
return err return err
} }
_, err = d.db.Exec(putPgStr, mhKey, value) _, err = d.db.Exec(putPgStr, mhKey, value, d.BlockNumber.Uint64())
return err return err
} }
@@ -245,13 +255,13 @@ func (d *Database) Compact(start []byte, limit []byte) error {
// NewBatch creates a write-only database that buffers changes to its host db // NewBatch creates a write-only database that buffers changes to its host db
// until a final write is called // until a final write is called
func (d *Database) NewBatch() ethdb.Batch { func (d *Database) NewBatch() ethdb.Batch {
return NewBatch(d.db, nil) return NewBatch(d.db, nil, d.BlockNumber)
} }
// NewBatchWithSize satisfies the ethdb.Batcher interface. // NewBatchWithSize satisfies the ethdb.Batcher interface.
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer. // NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
func (d *Database) NewBatchWithSize(size int) ethdb.Batch { func (d *Database) NewBatchWithSize(size int) ethdb.Batch {
return NewBatch(d.db, nil) return NewBatch(d.db, nil, d.BlockNumber)
} }
// NewIterator satisfies the ethdb.Iteratee interface // NewIterator satisfies the ethdb.Iteratee interface
@@ -311,7 +321,7 @@ func (d *Database) AncientRange(kind string, start, count, maxBytes uint64) ([][
} }
// ReadAncients applies the provided AncientReader function // ReadAncients applies the provided AncientReader function
func (d *Database) ReadAncients(fn func(ethdb.AncientReader) error) (err error) { func (d *Database) ReadAncients(fn func(ethdb.AncientReaderOp) error) (err error) {
return errNotSupported return errNotSupported
} }
@@ -344,3 +354,8 @@ func (d *Database) MigrateTable(string, func([]byte) ([]byte, error)) error {
func (d *Database) NewSnapshot() (ethdb.Snapshot, error) { func (d *Database) NewSnapshot() (ethdb.Snapshot, error) {
return nil, errNotSupported return nil, errNotSupported
} }
// AncientDatadir returns an error as we don't have a backing chain freezer.
func (d *Database) AncientDatadir() (string, error) {
return "", errNotSupported
}
+15 -10
View File
@@ -28,17 +28,18 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
pgipfsethdb "github.com/vulcanize/ipfs-ethdb/postgres" pgipfsethdb "github.com/cerc-io/ipfs-ethdb/v4/postgres"
) )
var ( var (
database ethdb.Database database ethdb.Database
db *sqlx.DB db *sqlx.DB
err error err error
testHeader = types.Header{Number: big.NewInt(1337)} testBlockNumber = big.NewInt(1337)
testValue, _ = rlp.EncodeToBytes(testHeader) testHeader = types.Header{Number: testBlockNumber}
testEthKey = testHeader.Hash().Bytes() testValue, _ = rlp.EncodeToBytes(testHeader)
testMhKey, _ = pgipfsethdb.MultihashKeyFromKeccak256(testEthKey) testEthKey = testHeader.Hash().Bytes()
testMhKey, _ = pgipfsethdb.MultihashKeyFromKeccak256(testEthKey)
) )
var _ = Describe("Database", func() { var _ = Describe("Database", func() {
@@ -53,6 +54,10 @@ var _ = Describe("Database", func() {
} }
database = pgipfsethdb.NewDatabase(db, cacheConfig) database = pgipfsethdb.NewDatabase(db, cacheConfig)
databaseWithBlock, ok := database.(*pgipfsethdb.Database)
Expect(ok).To(BeTrue())
(*databaseWithBlock).BlockNumber = testBlockNumber
}) })
AfterEach(func() { AfterEach(func() {
groupcache.DeregisterGroup("db") groupcache.DeregisterGroup("db")
@@ -67,7 +72,7 @@ var _ = Describe("Database", func() {
Expect(has).ToNot(BeTrue()) Expect(has).ToNot(BeTrue())
}) })
It("returns true if a key-pair exists in the db", func() { It("returns true if a key-pair exists in the db", func() {
_, err = db.Exec("INSERT into public.blocks (key, data) VALUES ($1, $2)", testMhKey, testValue) _, err = db.Exec("INSERT into public.blocks (key, data, block_number) VALUES ($1, $2, $3)", testMhKey, testValue, testBlockNumber.Uint64())
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
has, err := database.Has(testEthKey) has, err := database.Has(testEthKey)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@@ -82,7 +87,7 @@ var _ = Describe("Database", func() {
Expect(err.Error()).To(ContainSubstring("sql: no rows in result set")) Expect(err.Error()).To(ContainSubstring("sql: no rows in result set"))
}) })
It("returns the value associated with the key, if the pair exists", func() { It("returns the value associated with the key, if the pair exists", func() {
_, err = db.Exec("INSERT into public.blocks (key, data) VALUES ($1, $2)", testMhKey, testValue) _, err = db.Exec("INSERT into public.blocks (key, data, block_number) VALUES ($1, $2, $3)", testMhKey, testValue, testBlockNumber.Uint64())
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
val, err := database.Get(testEthKey) val, err := database.Get(testEthKey)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
+1 -1
View File
@@ -18,7 +18,7 @@ import (
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/vulcanize/ipfs-ethdb/postgres" "github.com/vulcanize/ipfs-ethdb/v4/postgres"
) )
func main() { func main() {