moonstream/nodes/node_balancer/cmd/nodebalancer/blockchain.go

106 wiersze
2.8 KiB
Go

package main
import (
"io/ioutil"
"log"
"strconv"
"strings"
configs "github.com/bugout-dev/moonstream/nodes/node_balancer/configs"
)
var (
nodeConfigs NodeConfigs
ALLOWED_METHODS = map[string]bool{
"eth_blockNumber": true,
"eth_call": true,
"eth_chainId": true,
"eth_estimateGas": true,
"eth_feeHistory": true,
"eth_gasPrice": true,
"eth_getBalance": true,
"eth_getBlockByHash": true,
"eth_getBlockByNumber": true,
"eth_getBlockTransactionCountByHash": true,
"eth_getBlockTransactionCountByNumber": true,
"eth_getCode": true,
"eth_getLogs": true,
"eth_getStorageAt": true,
"eth_getTransactionByHash": true,
"eth_getTransactionByBlockHashAndIndex": true,
"eth_getTransactionByBlockNumberAndIndex": true,
"eth_getTransactionCount": true,
"eth_getTransactionReceipt": true,
"eth_getUncleByBlockHashAndIndex": true,
"eth_getUncleByBlockNumberAndIndex": true,
"eth_getUncleCountByBlockHash": true,
"eth_getUncleCountByBlockNumber": true,
"eth_getWork": true,
"eth_mining": true,
// "eth_sendRawTransaction": true,
"eth_protocolVersion": true,
"eth_syncing": true,
"net_listening": true,
"net_peerCount": true,
"net_version": true,
"web3_clientVersion": true,
}
)
type JSONRPCRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
ID uint64 `json:"id"`
}
// Node conf
type BlockchainConfig struct {
Blockchain string
IPs []string
Port string
}
type NodeConfig struct {
Blockchain string
Addr string
Port uint16
}
type NodeConfigs struct {
NodeConfigs []NodeConfig
}
// Return list of NodeConfig structures
func (nc *NodeConfigs) InitNodeConfigList(configPath string) {
configs.CheckEnvVarSet()
rawBytes, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatalf("Unable to read config file, %v", err)
}
text := string(rawBytes)
lines := strings.Split(text, "\n")
// Define available blockchain nodes
for _, line := range lines {
fields := strings.Split(line, ",")
if len(fields) == 3 {
port, err := strconv.ParseInt(fields[2], 0, 16)
if err != nil {
log.Printf("Unable to parse port number, %v", err)
continue
}
nc.NodeConfigs = append(nc.NodeConfigs, NodeConfig{
Blockchain: fields[0],
Addr: fields[1],
Port: uint16(port),
})
}
}
}