ipfs-ethdb/postgres/doc.md

45 lines
1.9 KiB
Markdown
Raw Normal View History

2020-07-09 19:49:28 +00:00
## ipfs-ethdb
2023-03-30 15:38:23 +00:00
IPFS has been [extended](https://github.com/cerc-io/go-ipfs/releases/tag/v0.4.22-alpha) to [use Postgres](https://github.com/cerc-io/go-ipfs-config/releases/tag/v0.0.8-alpha) as a backing [datastore](https://github.com/ipfs/go-ds-sql/tree/master/postgres).
2020-07-09 19:49:28 +00:00
Interfacing directly with the IPFS-backing Postgres database has some advantages over using the blockservice interface.
Namely, batching of IPFS writes with other Postgres writes and avoiding lock contention on the ipfs repository (lockfile located at the `IPFS_PATH`).
The downside is that we forgo the block-exchange capabilities of the blockservice, and are only able to fetch data contained in the local datastore.
## Usage
To use this module import it and build an ethdb interface around an instance of [sqlx.DB](https://github.com/jmoiron/sqlx), you can then
employ it as you would the blockservice-based ethdbs.
```go
package main
import (
2020-07-13 04:28:05 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie"
"github.com/jmoiron/sqlx"
2023-03-30 15:38:23 +00:00
"github.com/cerc-io/ipfs-ethdb/v5/postgres/v1"
2020-07-09 19:49:28 +00:00
)
func main() {
connectStr := "postgresql://vdbm:password@localhost:8077/cerc_testing?sslmode=disable"
2020-07-09 19:49:28 +00:00
db, _ := sqlx.Connect("postgres", connectStr)
kvs := pgipfsethdb.NewKeyValueStore(db)
trieDB := trie.NewDatabase(kvs)
t, _ := trie.New(common.Hash{}, trieDB)
trieNodeIterator := t.NodeIterator([]byte{})
// do stuff with trie node iterator
2023-03-22 04:53:33 +00:00
database := pgipfsethdb.NewDatabase(db, pgipfsethdb.CacheConfig{
Name: "db",
Size: 3000000, // 3MB
ExpiryDuration: time.Hour,
})
2020-07-09 19:49:28 +00:00
stateDatabase := state.NewDatabase(database)
stateDB, _ := state.New(common.Hash{}, stateDatabase, nil)
stateDBNodeIterator := state.NewNodeIterator(stateDB)
// do stuff with the statedb node iterator
}
```