Merge pull request #6 from vulcanize/statedb

Add go report card and some additional detail/comments
This commit is contained in:
Ian Norden 2020-06-22 13:51:34 -05:00 committed by GitHub
commit 7d6c705aa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 3 deletions

View File

@ -1,5 +1,6 @@
## pg-ipfs-ethdb ## 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 > go-ethereum ethdb interfaces for Ethereum state data stored in Postgres-backed IPFS
@ -29,6 +30,8 @@ with a few exceptions:
package main package main
import ( import (
"github.com/ethereum/go-ethereum/common"
"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/pg-ipfs-ethdb" "github.com/vulcanize/pg-ipfs-ethdb"
@ -40,8 +43,12 @@ func main() {
kvs := ipfsethdb.NewKeyValueStore(db) kvs := ipfsethdb.NewKeyValueStore(db)
trieDB := trie.NewDatabase(kvs) 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 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. 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 ## Maintainers
@vulcanize @vulcanize
@AFDudley @AFDudley

View File

@ -21,12 +21,14 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
) )
// Batch is the type that satisfies the ethdb.Batch interface for PG-IPFS Ethereum data
type Batch struct { type Batch struct {
db *sqlx.DB db *sqlx.DB
tx *sqlx.Tx tx *sqlx.Tx
size int size int
} }
// NewBatch returns a ethdb.Batch interface for PG-IPFS
func NewBatch(db *sqlx.DB) ethdb.Batch { func NewBatch(db *sqlx.DB) ethdb.Batch {
return &Batch{ return &Batch{
db: db, db: db,

View File

@ -35,16 +35,19 @@ var (
dbSizePgStr = "SELECT pg_database_size(current_database())" 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 { type Database struct {
db *sqlx.DB db *sqlx.DB
} }
// NewKeyValueStore returns a ethdb.KeyValueStore interface for PG-IPFS
func NewKeyValueStore(db *sqlx.DB) ethdb.KeyValueStore { func NewKeyValueStore(db *sqlx.DB) ethdb.KeyValueStore {
return &Database{ return &Database{
db: db, db: db,
} }
} }
// NewDatabase returns a ethdb.Database interface for PG-IPFS
func NewDatabase(db *sqlx.DB) ethdb.Database { func NewDatabase(db *sqlx.DB) ethdb.Database {
return &Database{ return &Database{
db: db, 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 // 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 // no need for the caller to prepend the prefix to the start
func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator { 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 // Close satisfies the io.Closer interface

View File

@ -21,14 +21,18 @@ import (
"github.com/jmoiron/sqlx" "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 // 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 { type Iterator struct {
db *sqlx.DB db *sqlx.DB
currentKey, prefix []byte currentKey, prefix []byte
err error err error
} }
// NewIterator returns a ethdb.Iterator interface for PG-IPFS
func NewIterator(start, prefix []byte, db *sqlx.DB) ethdb.Iterator { func NewIterator(start, prefix []byte, db *sqlx.DB) ethdb.Iterator {
return &Iterator{ return &Iterator{
db: db, db: db,