2020-05-30 17:09:37 +00:00
// VulcanizeDB
// Copyright © 2020 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/>.
2020-05-30 17:20:00 +00:00
package ipfsethdb
2020-05-30 17:09:37 +00:00
import (
2020-06-29 21:11:29 +00:00
"context"
2020-05-30 17:09:37 +00:00
"errors"
"fmt"
2020-06-29 21:11:29 +00:00
"strconv"
2020-05-30 17:09:37 +00:00
"strings"
"github.com/ethereum/go-ethereum/ethdb"
2020-06-29 21:11:29 +00:00
"github.com/ipfs/go-blockservice"
2020-05-30 17:09:37 +00:00
)
var (
2020-07-12 23:16:42 +00:00
stateTrieCodec uint64 = 0x96
defaultBatchCapacity = 1024
errNotSupported = errors . New ( "this operation is not supported" )
2020-05-30 17:09:37 +00:00
)
2020-06-29 21:11:29 +00:00
// Database is the type that satisfies the ethdb.Database and ethdb.KeyValueStore interfaces for IPFS Ethereum data
// This is ipfs-backing-datastore agnostic but must operate through a configured ipfs node (and so is subject to lockfile contention with e.g. an ipfs daemon)
// If blockservice block exchange is configured the blockservice can fetch data that are missing locally from IPFS peers
2020-05-30 17:09:37 +00:00
type Database struct {
2020-06-29 21:11:29 +00:00
blockService blockservice . BlockService
2020-05-30 17:09:37 +00:00
}
2020-06-29 21:11:29 +00:00
// NewKeyValueStore returns a ethdb.KeyValueStore interface for IPFS
func NewKeyValueStore ( bs blockservice . BlockService ) ethdb . KeyValueStore {
2020-05-30 17:09:37 +00:00
return & Database {
2020-06-29 21:11:29 +00:00
blockService : bs ,
2020-05-30 17:09:37 +00:00
}
}
2020-06-29 21:11:29 +00:00
// NewDatabase returns a ethdb.Database interface for IPFS
func NewDatabase ( bs blockservice . BlockService ) ethdb . Database {
2020-05-30 17:09:37 +00:00
return & Database {
2020-06-29 21:11:29 +00:00
blockService : bs ,
2020-05-30 17:09:37 +00:00
}
}
// Has satisfies the ethdb.KeyValueReader interface
// Has retrieves if a key is present in the key-value data store
2020-06-29 21:11:29 +00:00
// This only operates on the local blockstore not through the exchange
2020-05-30 17:09:37 +00:00
func ( d * Database ) Has ( key [ ] byte ) ( bool , error ) {
2020-07-09 19:49:01 +00:00
// we are using state codec because we don't know the codec and at this level the codec doesn't matter, the datastore key is multihash-only derived
2020-07-12 23:16:42 +00:00
c , err := Keccak256ToCid ( key , stateTrieCodec )
2020-05-30 17:09:37 +00:00
if err != nil {
return false , err
}
2020-06-29 21:11:29 +00:00
return d . blockService . Blockstore ( ) . Has ( c )
2020-05-30 17:09:37 +00:00
}
// 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 ) {
2020-07-09 19:49:01 +00:00
// we are using state codec because we don't know the codec and at this level the codec doesn't matter, the datastore key is multihash-only derived
2020-07-12 23:16:42 +00:00
c , err := Keccak256ToCid ( key , stateTrieCodec )
2020-05-30 17:09:37 +00:00
if err != nil {
return nil , err
}
2020-06-29 21:11:29 +00:00
block , err := d . blockService . GetBlock ( context . Background ( ) , c )
2020-07-09 19:49:01 +00:00
if err != nil {
return nil , err
}
return block . RawData ( ) , nil
2020-05-30 17:09:37 +00:00
}
// Put satisfies the ethdb.KeyValueWriter interface
// Put inserts the given value into the key-value data store
2020-06-29 21:11:29 +00:00
// Key is expected to be the keccak256 hash of value
2020-05-30 17:09:37 +00:00
func ( d * Database ) Put ( key [ ] byte , value [ ] byte ) error {
2020-06-29 21:11:29 +00:00
b , err := NewBlock ( key , value )
2020-05-30 17:09:37 +00:00
if err != nil {
return err
}
2020-06-29 21:11:29 +00:00
return d . blockService . AddBlock ( b )
2020-05-30 17:09:37 +00:00
}
// Delete satisfies the ethdb.KeyValueWriter interface
// Delete removes the key from the key-value data store
func ( d * Database ) Delete ( key [ ] byte ) error {
2020-07-09 19:49:01 +00:00
// we are using state codec because we don't know the codec and at this level the codec doesn't matter, the datastore key is multihash-only derived
2020-07-12 23:16:42 +00:00
c , err := Keccak256ToCid ( key , stateTrieCodec )
2020-05-30 17:09:37 +00:00
if err != nil {
return err
}
2020-06-29 21:11:29 +00:00
return d . blockService . DeleteBlock ( c )
2020-05-30 17:09:37 +00:00
}
// DatabaseProperty enum type
type DatabaseProperty int
const (
Unknown DatabaseProperty = iota
2020-06-29 21:11:29 +00:00
ExchangeOnline
2020-05-30 17:09:37 +00:00
)
// DatabasePropertyFromString helper function
func DatabasePropertyFromString ( property string ) ( DatabaseProperty , error ) {
switch strings . ToLower ( property ) {
2020-06-29 21:11:29 +00:00
case "exchange" , "online" :
return ExchangeOnline , nil
2020-05-30 17:09:37 +00:00
default :
return Unknown , fmt . Errorf ( "unknown database property" )
}
}
// Stat satisfies the ethdb.Stater interface
// Stat returns a particular internal stat of the database
func ( d * Database ) Stat ( property string ) ( string , error ) {
prop , err := DatabasePropertyFromString ( property )
if err != nil {
return "" , err
}
switch prop {
2020-06-29 21:11:29 +00:00
case ExchangeOnline :
online := d . blockService . Exchange ( ) . IsOnline ( )
return strconv . FormatBool ( online ) , nil
2020-05-30 17:09:37 +00:00
default :
return "" , fmt . Errorf ( "unhandled database property" )
}
}
// Compact satisfies the ethdb.Compacter interface
// Compact flattens the underlying data store for the given key range
func ( d * Database ) Compact ( start [ ] byte , limit [ ] byte ) error {
return errNotSupported
}
// NewBatch satisfies the ethdb.Batcher interface
// NewBatch creates a write-only database that buffers changes to its host db
// until a final write is called
func ( d * Database ) NewBatch ( ) ethdb . Batch {
2020-06-29 21:11:29 +00:00
b , err := NewBatch ( d . blockService , defaultBatchCapacity )
if err != nil {
panic ( err )
}
return b
2020-05-30 17:09:37 +00:00
}
2020-09-08 21:14:27 +00:00
// NewIterator creates a binary-alphabetical iterator over the entire keyspace
// contained within the key-value database.
func ( d * Database ) NewIterator ( ) ethdb . Iterator {
return NewIterator ( [ ] byte { } , [ ] byte { } , d . blockService )
}
// NewIteratorWithStart creates a binary-alphabetical iterator over a subset of
// database content starting at a particular initial key (or after, if it does
// not exist).
func ( d * Database ) NewIteratorWithStart ( start [ ] byte ) ethdb . Iterator {
return NewIterator ( start , [ ] byte { } , d . blockService )
}
// NewIteratorWithPrefix creates a binary-alphabetical iterator over a subset
// of database content with a particular key prefix.
func ( d * Database ) NewIteratorWithPrefix ( prefix [ ] byte ) ethdb . Iterator {
return NewIterator ( [ ] byte { } , prefix , d . blockService )
2020-05-30 17:09:37 +00:00
}
// Close satisfies the io.Closer interface
// Close closes the db connection
func ( d * Database ) Close ( ) error {
2020-06-29 21:11:29 +00:00
return d . blockService . Close ( )
2020-05-30 17:09:37 +00:00
}
// HasAncient satisfies the ethdb.AncientReader interface
// HasAncient returns an indicator whether the specified data exists in the ancient store
func ( d * Database ) HasAncient ( kind string , number uint64 ) ( bool , error ) {
return false , errNotSupported
}
// Ancient satisfies the ethdb.AncientReader interface
// Ancient retrieves an ancient binary blob from the append-only immutable files
func ( d * Database ) Ancient ( kind string , number uint64 ) ( [ ] byte , error ) {
return nil , errNotSupported
}
// Ancients satisfies the ethdb.AncientReader interface
// Ancients returns the ancient item numbers in the ancient store
func ( d * Database ) Ancients ( ) ( uint64 , error ) {
return 0 , errNotSupported
}
// AncientSize satisfies the ethdb.AncientReader interface
// AncientSize returns the ancient size of the specified category
func ( d * Database ) AncientSize ( kind string ) ( uint64 , error ) {
return 0 , errNotSupported
}
// AppendAncient satisfies the ethdb.AncientWriter interface
// AppendAncient injects all binary blobs belong to block at the end of the append-only immutable table files
func ( d * Database ) AppendAncient ( number uint64 , hash , header , body , receipt , td [ ] byte ) error {
return errNotSupported
}
// TruncateAncients satisfies the ethdb.AncientWriter interface
// TruncateAncients discards all but the first n ancient data from the ancient store
func ( d * Database ) TruncateAncients ( n uint64 ) error {
return errNotSupported
}
// Sync satisfies the ethdb.AncientWriter interface
// Sync flushes all in-memory ancient store data to disk
func ( d * Database ) Sync ( ) error {
return errNotSupported
2020-05-30 17:10:48 +00:00
}