kopia lustrzana https://github.com/bugout-dev/moonstream
				
				
				
			Cleaned from unused code
							rodzic
							
								
									9da72eac44
								
							
						
					
					
						commit
						759a51db46
					
				| 
						 | 
				
			
			@ -6,7 +6,6 @@ import (
 | 
			
		|||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	bugout "github.com/bugout-dev/bugout-go/pkg"
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
| 
						 | 
				
			
			@ -19,17 +18,6 @@ var (
 | 
			
		|||
	bugoutClient bugout.BugoutClient
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type flagSlice []string
 | 
			
		||||
 | 
			
		||||
func (i *flagSlice) String() string {
 | 
			
		||||
	return strings.Join(*i, ", ")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (i *flagSlice) Set(value string) error {
 | 
			
		||||
	*i = append(*i, value)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Command Line Interface state
 | 
			
		||||
type StateCLI struct {
 | 
			
		||||
	addAccessCmd      *flag.FlagSet
 | 
			
		||||
| 
						 | 
				
			
			@ -310,7 +298,7 @@ func cli() {
 | 
			
		|||
		stateCLI.usersCmd.Parse(os.Args[2:])
 | 
			
		||||
		stateCLI.checkRequirements()
 | 
			
		||||
 | 
			
		||||
		var queryParameters map[string]string
 | 
			
		||||
		queryParameters := make(map[string]string)
 | 
			
		||||
		if stateCLI.userIDFlag != "" {
 | 
			
		||||
			queryParameters["user_id"] = stateCLI.userIDFlag
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,13 +43,10 @@ var (
 | 
			
		|||
 | 
			
		||||
	// Humbug configuration
 | 
			
		||||
	HUMBUG_REPORTER_NB_TOKEN = os.Getenv("HUMBUG_REPORTER_NB_TOKEN")
 | 
			
		||||
 | 
			
		||||
	// Database configuration
 | 
			
		||||
	MOONSTREAM_DB_URI_READ_ONLY         = os.Getenv("MOONSTREAM_DB_URI_READ_ONLY")
 | 
			
		||||
	MOONSTREAM_DB_MAX_IDLE_CONNS    int = 30
 | 
			
		||||
	MOONSTREAM_DB_CONN_MAX_LIFETIME     = 30 * time.Minute
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var ()
 | 
			
		||||
 | 
			
		||||
func CheckEnvVarSet() {
 | 
			
		||||
	if NB_ACCESS_ID_HEADER == "" {
 | 
			
		||||
		NB_ACCESS_ID_HEADER = "x-node-balancer-access-id"
 | 
			
		||||
| 
						 | 
				
			
			@ -94,7 +91,7 @@ func CheckPathExists(path string) (bool, error) {
 | 
			
		|||
		if os.IsNotExist(err) {
 | 
			
		||||
			exists = false
 | 
			
		||||
		} else {
 | 
			
		||||
			return exists, fmt.Errorf("Error due checking file path exists, err: %v", err)
 | 
			
		||||
			return exists, fmt.Errorf("error due checking file path exists, err: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -106,7 +103,7 @@ func GetConfigPath(providedPath string) (*ConfigPlacement, error) {
 | 
			
		|||
	if providedPath == "" {
 | 
			
		||||
		homeDir, err := os.UserHomeDir()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("Unable to find user home directory, %v", err)
 | 
			
		||||
			return nil, fmt.Errorf("unable to find user home directory, %v", err)
 | 
			
		||||
		}
 | 
			
		||||
		configDirPath = fmt.Sprintf("%s/.nodebalancer", homeDir)
 | 
			
		||||
		configPath = fmt.Sprintf("%s/config.json", configDirPath)
 | 
			
		||||
| 
						 | 
				
			
			@ -138,7 +135,7 @@ func GetConfigPath(providedPath string) (*ConfigPlacement, error) {
 | 
			
		|||
func GenerateDefaultConfig(config *ConfigPlacement) error {
 | 
			
		||||
	if !config.ConfigDirExists {
 | 
			
		||||
		if err := os.MkdirAll(config.ConfigDirPath, os.ModePerm); err != nil {
 | 
			
		||||
			return fmt.Errorf("Unable to create directory, %v", err)
 | 
			
		||||
			return fmt.Errorf("unable to create directory, %v", err)
 | 
			
		||||
		}
 | 
			
		||||
		log.Printf("Config directory created at: %s", config.ConfigDirPath)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -149,11 +146,11 @@ func GenerateDefaultConfig(config *ConfigPlacement) error {
 | 
			
		|||
		}
 | 
			
		||||
		tempConfigJson, err := json.Marshal(tempConfig)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Unable to marshal configuration data, err: %v", err)
 | 
			
		||||
			return fmt.Errorf("unable to marshal configuration data, err: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
		err = ioutil.WriteFile(config.ConfigPath, tempConfigJson, os.ModePerm)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("Unable to write default config to file %s, err: %v", config.ConfigPath, err)
 | 
			
		||||
			return fmt.Errorf("unable to write default config to file %s, err: %v", config.ConfigPath, err)
 | 
			
		||||
		}
 | 
			
		||||
		log.Printf("Created default configuration at %s", config.ConfigPath)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,106 +0,0 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	_ "github.com/lib/pq"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	databaseClient DatabaseClient
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type DatabaseClient struct {
 | 
			
		||||
	Client *sql.DB
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Establish connection with database
 | 
			
		||||
func InitDatabaseClient() error {
 | 
			
		||||
	db, err := sql.Open("postgres", MOONSTREAM_DB_URI_READ_ONLY)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("DSN parse error or another database initialization error: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Set the maximum number of concurrently idle connections,
 | 
			
		||||
	// by default sql.DB allows a maximum of 2 idle connections.
 | 
			
		||||
	db.SetMaxIdleConns(MOONSTREAM_DB_MAX_IDLE_CONNS)
 | 
			
		||||
 | 
			
		||||
	// Set the maximum lifetime of a connection.
 | 
			
		||||
	// Longer lifetime increase memory usage.
 | 
			
		||||
	db.SetConnMaxLifetime(MOONSTREAM_DB_CONN_MAX_LIFETIME)
 | 
			
		||||
 | 
			
		||||
	databaseClient = DatabaseClient{
 | 
			
		||||
		Client: db,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Block struct {
 | 
			
		||||
	BlockNumber      uint64      `json:"block_number"`
 | 
			
		||||
	Difficulty       uint64      `json:"difficulty"`
 | 
			
		||||
	ExtraData        string      `json:"extra_data"`
 | 
			
		||||
	GasLimit         uint64      `json:"gas_limit"`
 | 
			
		||||
	GasUsed          uint64      `json:"gas_used"`
 | 
			
		||||
	BaseFeePerGas    interface{} `json:"base_fee_per_gas"`
 | 
			
		||||
	Hash             string      `json:"hash"`
 | 
			
		||||
	LogsBloom        string      `json:"logs_bloom"`
 | 
			
		||||
	Miner            string      `json:"miner"`
 | 
			
		||||
	Nonce            string      `json:"nonce"`
 | 
			
		||||
	ParentHash       string      `json:"parent_hash"`
 | 
			
		||||
	ReceiptRoot      string      `json:"receipt_root"`
 | 
			
		||||
	Uncles           string      `json:"uncles"`
 | 
			
		||||
	Size             float64     `json:"size"`
 | 
			
		||||
	StateRoot        string      `json:"state_root"`
 | 
			
		||||
	Timestamp        uint64      `json:"timestamp"`
 | 
			
		||||
	TotalDifficulty  string      `json:"total_difficulty"`
 | 
			
		||||
	TransactionsRoot string      `json:"transactions_root"`
 | 
			
		||||
 | 
			
		||||
	IndexedAt string `json:"indexed_at"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get block from database
 | 
			
		||||
func (dbc *DatabaseClient) GetBlock(blockchain string, blockNumber uint64) (Block, error) {
 | 
			
		||||
	var block Block
 | 
			
		||||
 | 
			
		||||
	// var tableName string
 | 
			
		||||
	// if blockchain == "ethereum" {
 | 
			
		||||
	// 	tableName = "ethereum_blocks"
 | 
			
		||||
	// } else if blockchain == "polygon" {
 | 
			
		||||
	// 	tableName = "polygon_blocks"
 | 
			
		||||
	// } else {
 | 
			
		||||
	// 	return block, fmt.Errorf("Unsupported blockchain")
 | 
			
		||||
	// }
 | 
			
		||||
	row := dbc.Client.QueryRow(
 | 
			
		||||
		"SELECT block_number,difficulty,extra_data,gas_limit,gas_used,base_fee_per_gas,hash,logs_bloom,miner,nonce,parent_hash,receipt_root,uncles,size,state_root,timestamp,total_difficulty,transactions_root,indexed_at FROM ethereum_blocks WHERE block_number = $1",
 | 
			
		||||
		// tableName,
 | 
			
		||||
		blockNumber,
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	if err := row.Scan(
 | 
			
		||||
		&block.BlockNumber,
 | 
			
		||||
		&block.Difficulty,
 | 
			
		||||
		&block.ExtraData,
 | 
			
		||||
		&block.GasLimit,
 | 
			
		||||
		&block.GasUsed,
 | 
			
		||||
		&block.BaseFeePerGas,
 | 
			
		||||
		&block.Hash,
 | 
			
		||||
		&block.LogsBloom,
 | 
			
		||||
		&block.Miner,
 | 
			
		||||
		&block.Nonce,
 | 
			
		||||
		&block.ParentHash,
 | 
			
		||||
		&block.ReceiptRoot,
 | 
			
		||||
		&block.Uncles,
 | 
			
		||||
		&block.Size,
 | 
			
		||||
		&block.StateRoot,
 | 
			
		||||
		&block.Timestamp,
 | 
			
		||||
		&block.TotalDifficulty,
 | 
			
		||||
		&block.TransactionsRoot,
 | 
			
		||||
		&block.IndexedAt,
 | 
			
		||||
	); err != nil {
 | 
			
		||||
		return block, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return block, nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -176,17 +176,17 @@ func jsonrpcRequestParser(body []byte) ([]JSONRPCRequest, error) {
 | 
			
		|||
	case len(firstByte) > 0 && firstByte[0] == '[':
 | 
			
		||||
		err := json.Unmarshal(body, &jsonrpcRequest)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("Unable to parse body, err: %v", err)
 | 
			
		||||
			return nil, fmt.Errorf("unable to parse body, err: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	case len(firstByte) > 0 && firstByte[0] == '{':
 | 
			
		||||
		var singleJsonrpcRequest JSONRPCRequest
 | 
			
		||||
		err := json.Unmarshal(body, &singleJsonrpcRequest)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("Unable to parse body, err: %v", err)
 | 
			
		||||
			return nil, fmt.Errorf("unable to parse body, err: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
		jsonrpcRequest = []JSONRPCRequest{singleJsonrpcRequest}
 | 
			
		||||
	default:
 | 
			
		||||
		return nil, fmt.Errorf("Incorrect first byte in JSON RPC request")
 | 
			
		||||
		return nil, fmt.Errorf("incorrect first byte in JSON RPC request")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return jsonrpcRequest, nil
 | 
			
		||||
| 
						 | 
				
			
			@ -234,7 +234,7 @@ func logMiddleware(next http.Handler) http.Handler {
 | 
			
		|||
					logStr += fmt.Sprintf(" %s", jsonrpcRequest.Method)
 | 
			
		||||
				}
 | 
			
		||||
				if i == len(jsonrpcRequests)-1 {
 | 
			
		||||
					logStr += fmt.Sprint("]")
 | 
			
		||||
					logStr += "]"
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -10,7 +10,6 @@ import (
 | 
			
		|||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -96,11 +95,11 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string,
 | 
			
		|||
 | 
			
		||||
	switch {
 | 
			
		||||
	case currentClientAccess.dataSource == "blockchain":
 | 
			
		||||
		if currentClientAccess.BlockchainAccess == false {
 | 
			
		||||
		if !currentClientAccess.BlockchainAccess {
 | 
			
		||||
			http.Error(w, "Access to blockchain node not allowed with provided access id", http.StatusForbidden)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		if currentClientAccess.ExtendedMethods == false {
 | 
			
		||||
		if !currentClientAccess.ExtendedMethods {
 | 
			
		||||
			for _, jsonrpcRequest := range jsonrpcRequests {
 | 
			
		||||
				_, exists := ALLOWED_METHODS[jsonrpcRequest.Method]
 | 
			
		||||
				if !exists {
 | 
			
		||||
| 
						 | 
				
			
			@ -117,7 +116,6 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string,
 | 
			
		|||
		node.GethReverseProxy.ServeHTTP(w, r)
 | 
			
		||||
		return
 | 
			
		||||
	case currentClientAccess.dataSource == "database":
 | 
			
		||||
		// lbDatabaseHandler(w, r, blockchain, jsonrpcRequest)
 | 
			
		||||
		http.Error(w, "Database access under development", http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	default:
 | 
			
		||||
| 
						 | 
				
			
			@ -125,22 +123,3 @@ func lbJSONRPCHandler(w http.ResponseWriter, r *http.Request, blockchain string,
 | 
			
		|||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func lbDatabaseHandler(w http.ResponseWriter, r *http.Request, blockchain string, jsonrpcRequest JSONRPCRequest) {
 | 
			
		||||
	switch {
 | 
			
		||||
	case jsonrpcRequest.Method == "eth_getBlockByNumber":
 | 
			
		||||
		var blockNumber uint64
 | 
			
		||||
		blockNumber, _ = strconv.ParseUint(jsonrpcRequest.Params[0].(string), 10, 32)
 | 
			
		||||
 | 
			
		||||
		block, err := databaseClient.GetBlock(blockchain, blockNumber)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Printf("Unable to get block from database, err: %v", err)
 | 
			
		||||
			http.Error(w, fmt.Sprintf("no such block %v", blockNumber), http.StatusBadRequest)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println(block)
 | 
			
		||||
	default:
 | 
			
		||||
		http.Error(w, fmt.Sprintf("Unsupported method %s by database, please use blockchain as data source", jsonrpcRequest.Method), http.StatusBadRequest)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -155,13 +155,6 @@ func Server() {
 | 
			
		|||
		resources.Resources[0].Id, clientAccess.BlockchainAccess, clientAccess.ExtendedMethods,
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	err = InitDatabaseClient()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("Unable to initialize database connection, err: %v", err)
 | 
			
		||||
	} else {
 | 
			
		||||
		log.Printf("Connection with database established")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Fill NodeConfigList with initial nodes from environment variables
 | 
			
		||||
	err = LoadConfig(stateCLI.configPathFlag)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,6 @@ export BUGOUT_BROOD_URL="https://auth.bugout.dev"
 | 
			
		|||
export NB_APPLICATION_ID="<application_id_to_controll_access>"
 | 
			
		||||
export NB_CONTROLLER_TOKEN="<token_of_controller_user>"
 | 
			
		||||
export NB_CONTROLLER_ACCESS_ID="<controller_access_id_for_internal_crawlers>"
 | 
			
		||||
export MOONSTREAM_DB_URI="postgresql://<username>:<password>@<db_host>:<db_port>/<db_name>"
 | 
			
		||||
 | 
			
		||||
# Error humbug reporter
 | 
			
		||||
export HUMBUG_REPORTER_NODE_BALANCER_TOKEN="<bugout_humbug_token_for_crash_reports>"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue