RPC server service, backend, api
This commit is contained in:
parent
72435fbbde
commit
a79d6c36a8
57
pkg/api.go
57
pkg/api.go
@ -1,5 +1,5 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2021 Vulcanize
|
||||
// Copyright © 2022 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
|
||||
@ -15,3 +15,58 @@
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// APIName is the namespace used for the state diffing service API
|
||||
const APIName = "leveldb"
|
||||
|
||||
// APIVersion is the version of the state diffing service API
|
||||
const APIVersion = "0.0.1"
|
||||
|
||||
var (
|
||||
errWriteNotAllowed = errors.New("write endpoints are not enabled")
|
||||
)
|
||||
|
||||
type PublicLevelDBAPI struct {
|
||||
b *LevelDBBackend
|
||||
}
|
||||
|
||||
func NewPublicLevelDBAPI(b *LevelDBBackend) *PublicLevelDBAPI {
|
||||
return &PublicLevelDBAPI{b: b}
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) Has(ctx context.Context, key []byte) (bool, error) {
|
||||
return s.b.Has(key)
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) Get(ctx context.Context, key []byte) ([]byte, error) {
|
||||
return s.b.Get(key)
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) HasAncient(ctx context.Context, kind string, number uint64) (bool, error) {
|
||||
return s.b.HasAncient(kind, number)
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) Ancient(ctx context.Context, kind string, number uint64) ([]byte, error) {
|
||||
return s.b.Ancient(kind, number)
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) ReadAncients(ctx context.Context, kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
||||
return s.b.ReadAncients(kind, start, count, maxBytes)
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) Ancients(ctx context.Context) (uint64, error) {
|
||||
return s.b.Ancients()
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) AncientSize(ctx context.Context, kind string) (uint64, error) {
|
||||
return s.b.AncientSize(kind)
|
||||
}
|
||||
|
||||
func (s *PublicLevelDBAPI) Stat(ctx context.Context, property string) (string, error) {
|
||||
return s.b.Stat(property)
|
||||
}
|
||||
|
115
pkg/backend.go
Normal file
115
pkg/backend.go
Normal file
@ -0,0 +1,115 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2022 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/>.
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/ethdb/leveldb"
|
||||
)
|
||||
|
||||
var _ ethdb.Database = &LevelDBBackend{}
|
||||
|
||||
// NewLevelDBBackend creates a new levelDB RPC server backend
|
||||
func NewLevelDBBackend(conf Config) (*LevelDBBackend, error) {
|
||||
db, err := leveldb.New(conf.File, conf.Cache, conf.Handles, conf.Namespace, conf.Readonly)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
frdb, err := rawdb.NewDatabaseWithFreezer(db, conf.Freezer, conf.Namespace, conf.Readonly)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return &LevelDBBackend{
|
||||
ethDB: frdb,
|
||||
levelDB: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type LevelDBBackend struct {
|
||||
ethDB ethdb.Database
|
||||
levelDB *leveldb.Database
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Has(key []byte) (bool, error) {
|
||||
return s.ethDB.Has(key)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Get(key []byte) ([]byte, error) {
|
||||
return s.ethDB.Get(key)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) HasAncient(kind string, number uint64) (bool, error) {
|
||||
return s.ethDB.HasAncient(kind, number)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Ancient(kind string, number uint64) ([]byte, error) {
|
||||
return s.ethDB.Ancient(kind, number)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) ReadAncients(kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
||||
return s.ethDB.ReadAncients(kind, start, count, maxBytes)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Ancients() (uint64, error) {
|
||||
return s.ethDB.Ancients()
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) AncientSize(kind string) (uint64, error) {
|
||||
return s.ethDB.AncientSize(kind)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Put(key []byte, value []byte) error {
|
||||
return errWriteNotAllowed
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Delete(key []byte) error {
|
||||
return errWriteNotAllowed
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) ModifyAncients(f func(ethdb.AncientWriteOp) error) (int64, error) {
|
||||
return 0, errWriteNotAllowed
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) TruncateAncients(n uint64) error {
|
||||
return errWriteNotAllowed
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Sync() error {
|
||||
return errWriteNotAllowed
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) NewBatch() ethdb.Batch {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Stat(property string) (string, error) {
|
||||
return s.ethDB.Stat(property)
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Compact(start []byte, limit []byte) error {
|
||||
return errWriteNotAllowed
|
||||
}
|
||||
|
||||
func (s *LevelDBBackend) Close() error {
|
||||
return errWriteNotAllowed
|
||||
}
|
15
pkg/env.go
Normal file
15
pkg/env.go
Normal file
@ -0,0 +1,15 @@
|
||||
package pkg
|
||||
|
||||
const (
|
||||
IPC_ENABLED = "IPC_ENABLED"
|
||||
IPC_ENDPOINT = "IPC_ENDPOINT"
|
||||
HTTP_ENABLED = "HTTP_ENABLED"
|
||||
HTTP_ENDPOINT = "HTTP_ENDPOINT"
|
||||
|
||||
LEVELDB_PATH = "LEVELDB_PATH"
|
||||
LEVELDB_CACHE_SIZE = "LEVELDB_CACHE_SIZE"
|
||||
LEVELDB_HANDLER_NUM = "LEVELDB_HANDLER_NUM"
|
||||
LEVELDB_FREEZER_PATH = "LEVELDB_FREEZER_PATH"
|
||||
LEVELDB_NAMESPACE = "LEVELDB_NAMESPACE"
|
||||
LEVELDB_READ_ONLY = "LEVELDB_READ_ONLY"
|
||||
)
|
@ -1,17 +0,0 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2021 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/>.
|
||||
|
||||
package pkg
|
102
pkg/service.go
Normal file
102
pkg/service.go
Normal file
@ -0,0 +1,102 @@
|
||||
// VulcanizeDB
|
||||
// Copyright © 2022 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/>.
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
ethnode "github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/rpc"
|
||||
)
|
||||
|
||||
// Server is the top level interface for exposing a remote RPC wrapper around levelDB ethdb.Database
|
||||
type Server interface {
|
||||
ethnode.Lifecycle
|
||||
APIs() []rpc.API
|
||||
Protocols() []p2p.Protocol
|
||||
Serve(wg *sync.WaitGroup)
|
||||
}
|
||||
|
||||
// Service is the underlying struct for the watcher
|
||||
type Service struct {
|
||||
wg *sync.WaitGroup
|
||||
backend *LevelDBBackend
|
||||
quitChan chan struct{}
|
||||
}
|
||||
|
||||
// NewServer creates a new Server using an underlying Service struct
|
||||
func NewServer(conf Config) (Server, error) {
|
||||
sap := new(Service)
|
||||
sap.quitChan = make(chan struct{})
|
||||
var err error
|
||||
sap.backend, err = NewLevelDBBackend(conf)
|
||||
return sap, err
|
||||
}
|
||||
|
||||
// Protocols exports the services p2p protocols, this service has none
|
||||
func (sap *Service) Protocols() []p2p.Protocol {
|
||||
return []p2p.Protocol{}
|
||||
}
|
||||
|
||||
// APIs returns the RPC descriptors the watcher service offers
|
||||
func (sap *Service) APIs() []rpc.API {
|
||||
return []rpc.API{
|
||||
{
|
||||
Namespace: APIName,
|
||||
Version: APIVersion,
|
||||
Service: NewPublicLevelDBAPI(sap.backend),
|
||||
Public: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Serve is the listening loop
|
||||
func (sap *Service) Serve(wg *sync.WaitGroup) {
|
||||
sap.wg = wg
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
defer wg.Done()
|
||||
for {
|
||||
select {
|
||||
case <-sap.quitChan:
|
||||
log.Info("quiting the levelDB RPC server process")
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
log.Info("levelDB RPC server process successfully spun up")
|
||||
}
|
||||
|
||||
// Start is used to begin the service
|
||||
// This is mostly just to satisfy the node.Service interface
|
||||
func (sap *Service) Start() error {
|
||||
log.Info("starting levelDB RPC server")
|
||||
wg := new(sync.WaitGroup)
|
||||
sap.Serve(wg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop is used to close down the service
|
||||
// This is mostly just to satisfy the node.Service interface
|
||||
func (sap *Service) Stop() error {
|
||||
log.Infof("stopping levelDB RPC server")
|
||||
close(sap.quitChan)
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user