2019-12-02 13:47:00 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// splitAndTrim splits input separated by a comma
|
|
|
|
// and trims excessive white space from the substrings.
|
|
|
|
func splitAndTrim(input string) []string {
|
|
|
|
result := strings.Split(input, ",")
|
|
|
|
for i, r := range result {
|
|
|
|
result[i] = strings.TrimSpace(r)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthAPI is a collection of functions that are exposed in the
|
|
|
|
type EthAPI interface {
|
|
|
|
BlockNumber(ctx context.Context) (uint64, error)
|
|
|
|
}
|
|
|
|
|
2019-11-29 08:13:48 +00:00
|
|
|
// EthAPIImpl is implementation of the EthAPI interface based on remote Db access
|
|
|
|
type EthAPIImpl struct {
|
2019-12-02 13:47:00 +00:00
|
|
|
remoteDbAdddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockNumber returns the currently highest block number available in the remote db
|
2019-11-29 08:13:48 +00:00
|
|
|
func (api *EthAPIImpl) BlockNumber(ctx context.Context) (uint64, error) {
|
|
|
|
return 0, nil
|
2019-12-02 13:47:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 08:13:48 +00:00
|
|
|
func daemon(config Config) {
|
2019-12-02 13:47:00 +00:00
|
|
|
vhosts := splitAndTrim(cfg.rpcVirtualHost)
|
|
|
|
cors := splitAndTrim(cfg.rpcCORSDomain)
|
|
|
|
enabledApis := splitAndTrim(cfg.rpcAPI)
|
|
|
|
var rpcAPI = []rpc.API{}
|
|
|
|
for _, enabledAPI := range enabledApis {
|
|
|
|
switch enabledAPI {
|
|
|
|
case "eth":
|
2019-11-29 08:13:48 +00:00
|
|
|
var ethAPI EthAPI = &EthAPIImpl{remoteDbAdddress: cfg.remoteDbAdddress}
|
2019-12-02 13:47:00 +00:00
|
|
|
rpcAPI = append(rpcAPI, rpc.API{
|
|
|
|
Namespace: "eth",
|
|
|
|
Public: true,
|
2019-11-29 08:13:48 +00:00
|
|
|
Service: ethAPI,
|
2019-12-02 13:47:00 +00:00
|
|
|
Version: "1.0",
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
log.Error("Unrecognised", "api", enabledAPI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpEndpoint := fmt.Sprintf("%s:%d", cfg.rpcListenAddress, cfg.rpcPort)
|
2019-11-29 08:13:48 +00:00
|
|
|
listener, _, err := rpc.StartHTTPEndpoint(httpEndpoint, rpcAPI, []string{"test", "eth", "debug", "web3"}, cors, vhosts, rpc.DefaultHTTPTimeouts)
|
2019-12-02 13:47:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not start RPC api", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
extapiURL := fmt.Sprintf("http://%s", httpEndpoint)
|
|
|
|
log.Info("HTTP endpoint opened", "url", extapiURL)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
listener.Close()
|
|
|
|
log.Info("HTTP endpoint closed", "url", httpEndpoint)
|
|
|
|
}()
|
|
|
|
|
2019-11-29 08:13:48 +00:00
|
|
|
abortChan := make(chan os.Signal)
|
2019-12-02 13:47:00 +00:00
|
|
|
signal.Notify(abortChan, os.Interrupt)
|
|
|
|
|
|
|
|
sig := <-abortChan
|
|
|
|
log.Info("Exiting...", "signal", sig)
|
|
|
|
}
|