diff --git a/README.md b/README.md index d0d9d9a..8fff5e3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ ## pg-ipfs-ethdb +[![Go Report Card](https://goreportcard.com/badge/github.com/vulcanize/pg-ipfs-ethdb)](https://goreportcard.com/report/github.com/vulcanize/pg-ipfs-ethdb) > go-ethereum ethdb interfaces for Ethereum state data stored in Postgres-backed IPFS @@ -29,6 +30,8 @@ with a few exceptions: package main import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/trie" "github.com/jmoiron/sqlx" "github.com/vulcanize/pg-ipfs-ethdb" @@ -40,8 +43,12 @@ func main() { kvs := ipfsethdb.NewKeyValueStore(db) trieDB := trie.NewDatabase(kvs) + t, _ := trie.New(common.Hash{}, trieDB) + // do stuff with trie or trieDB - // do stuff + database := ipfsethdb.NewDatabase(db) + stateDatabase := state.NewDatabase(database) + // do stuff with the state database } ``` @@ -50,6 +57,43 @@ EXCEPTIONS: AncientReader, AncientWriter, and Iteratee interfaces are not functi Ancient data does not currently have a representation on IPFS, and recapitulation of the database key iterator is complicated since go-ethereum types that use this interface expect the iterator to iterate over keccak256 hash keys, whereas the keys for Ethereum data on IPFS are derived from that hash but not the hash itself. +Iteratee interface is only used in Geth for various tests, in trie/sync_bloom.go (for fast sync), and for rawdb.InspectDatabase while the Ancient interfaces are only used for Ancient data operations, +so we don't need these interfaces for the majority of state operations. + +The ethdb.Iteratee/ethdb.Iterator interfaces should not be confused with the trie.NodeIterator or state.NodeIterator. +These can be constructed from the ethdb.KeyValueStore and ethdb.Database interfaces, respectively: + +```go +package main + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/trie" + "github.com/jmoiron/sqlx" + "github.com/vulcanize/pg-ipfs-ethdb" +) + +func main() { + connectStr := "postgresql://localhost:5432/vulcanize_testing?sslmode=disable" + db, _ := sqlx.Connect("postgres", connectStr) + + kvs := ipfsethdb.NewKeyValueStore(db) + trieDB := trie.NewDatabase(kvs) + t, _ := trie.New(common.Hash{}, trieDB) + trieNodeIterator := t.NodeIterator([]byte{}) + // do stuff with trie node iterator + + database := ipfsethdb.NewDatabase(db) + stateDatabase := state.NewDatabase(database) + snapshotTree := snapshot.New(kvs, trieDB, 1, common.Hash{}, false) + stateDB, _ := state.New(common.Hash{}, stateDatabase, snapshotTree) + stateDBNodeIterator := state.NewNodeIterator(stateDB) + // do stuff with the statedb node iterator +} +``` + ## Maintainers @vulcanize @AFDudley diff --git a/batch.go b/batch.go index f90f52a..de7f14d 100644 --- a/batch.go +++ b/batch.go @@ -21,12 +21,14 @@ import ( "github.com/jmoiron/sqlx" ) +// Batch is the type that satisfies the ethdb.Batch interface for PG-IPFS Ethereum data type Batch struct { db *sqlx.DB tx *sqlx.Tx size int } +// NewBatch returns a ethdb.Batch interface for PG-IPFS func NewBatch(db *sqlx.DB) ethdb.Batch { return &Batch{ db: db, diff --git a/database.go b/database.go index e8d9028..4ee318a 100644 --- a/database.go +++ b/database.go @@ -35,16 +35,19 @@ var ( dbSizePgStr = "SELECT pg_database_size(current_database())" ) +// Database is the type that satisfies the ethdb.Database and ethdb.KeyValueStore interfaces for PG-IPFS Ethereum data type Database struct { db *sqlx.DB } +// NewKeyValueStore returns a ethdb.KeyValueStore interface for PG-IPFS func NewKeyValueStore(db *sqlx.DB) ethdb.KeyValueStore { return &Database{ db: db, } } +// NewDatabase returns a ethdb.Database interface for PG-IPFS func NewDatabase(db *sqlx.DB) ethdb.Database { return &Database{ db: db, @@ -190,7 +193,7 @@ func (d *Database) NewBatch() ethdb.Batch { // Note: This method assumes that the prefix is NOT part of the start, so there's // no need for the caller to prepend the prefix to the start func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator { - return NewIterator([]byte{}, []byte{}, d.db) + return NewIterator(start, prefix, d.db) } // Close satisfies the io.Closer interface diff --git a/iterator.go b/iterator.go index 3336eeb..a1e45dd 100644 --- a/iterator.go +++ b/iterator.go @@ -21,14 +21,18 @@ import ( "github.com/jmoiron/sqlx" ) -// We don't need these iterator interfaces right now +// Iterator is the type that satisfies the ethdb.Iterator interface for PG-IPFS Ethereum data // Iteratee interface is only used in Geth for various tests, trie/sync_bloom.go (for fast sync), and rawdb.InspectDatabase +// Don't need this interface for the majority of state operations +// This should not be confused with trie.NodeIterator or state.NodeIteraor (which can be constructed from the ethdb.KeyValueStore and ethdb.Database interfaces) +// ethdb.KeyValueStore => trie.Database => trie.Trie => trie.NodeIterator type Iterator struct { db *sqlx.DB currentKey, prefix []byte err error } +// NewIterator returns a ethdb.Iterator interface for PG-IPFS func NewIterator(start, prefix []byte, db *sqlx.DB) ethdb.Iterator { return &Iterator{ db: db,