Skip to content

Commit

Permalink
refactor...
Browse files Browse the repository at this point in the history
  • Loading branch information
baabeetaa committed Jul 20, 2023
1 parent 8d4506c commit 0b61ef2
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test:

test-evmos:
@echo "Testing subnode with evmos config"
@go test -mod readonly --timeout=10m -ldflags '$(LD_FLAGS) -X github.com/notional-labs/subnode/test.Chain=evmos' ./test
@go test -p 1 -mod readonly --timeout=10m -ldflags '$(LD_FLAGS) -X github.com/notional-labs/subnode/test.Chain=evmos' ./test

lint:
@echo "Running golangci-lint"
Expand Down
9 changes: 9 additions & 0 deletions aggerator/eth_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,27 @@ func Eth_getBlockTransactionCountByHash(w http.ResponseWriter, jsonBody []byte)
}

func Eth_getBlockByHash(w http.ResponseWriter, jsonBody []byte) {
//log.Println("Eth_getBlockByHash")
for i, s := range state.PoolEth {
ethUrl := s.Backend.Eth
//log.Println("Eth_getBlockByHash ethUrl=", ethUrl)
body, err := utils.FetchJsonRpcOverHttp(ethUrl, jsonBody)
if err != nil {
//log.Println("Eth_getBlockByHash Error, continue", err)
continue
}

var j0 interface{}
err = json.Unmarshal(body, &j0)
if err != nil {
//log.Println("Eth_getBlockByHash SendError", err)
_ = utils.SendError(w)
return
}

if m0, ok := j0.(map[string]interface{}); ok {
if (m0["result"] != nil) || (i >= len(state.PoolEth)-1) { // found result or last node, send it
//log.Println("Eth_getBlockByHash SendResult")
_ = utils.SendResult(w, body)
return
}
Expand All @@ -95,8 +100,10 @@ func Eth_getBlockByHash(w http.ResponseWriter, jsonBody []byte) {
}

func Eth_getTransactionByHash(w http.ResponseWriter, jsonBody []byte) {
//log.Println("Eth_getTransactionByHash")
for i, s := range state.PoolEth {
ethUrl := s.Backend.Eth
//log.Println("Eth_getTransactionByHash ethUrl=", ethUrl)
body, err := utils.FetchJsonRpcOverHttp(ethUrl, jsonBody)
if err != nil {
continue
Expand All @@ -105,12 +112,14 @@ func Eth_getTransactionByHash(w http.ResponseWriter, jsonBody []byte) {
var j0 interface{}
err = json.Unmarshal(body, &j0)
if err != nil {
//log.Println("Eth_getTransactionByHash SendError")
_ = utils.SendError(w)
return
}

if m0, ok := j0.(map[string]interface{}); ok {
if (m0["result"] != nil) || (i >= len(state.PoolEth)-1) { // found result or last node, send it
//log.Println("Eth_getTransactionByHash SendResult")
_ = utils.SendResult(w, body)
return
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/cmd_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func startCmd() *cobra.Command {
Short: "start subnode server",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
server.Start()
node := server.NewNode()
node.Start()

return nil
},
Expand Down
24 changes: 15 additions & 9 deletions server/api_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ import (
"strconv"
)

var apiServer *http.Server
type ApiServer struct {
apiServer *http.Server
}

func NewApiServer() *ApiServer {
newItem := &ApiServer{}
return newItem
}

func StartApiServer() {
func (m *ApiServer) StartApiServer() {
fmt.Println("StartApiServer...")

handler := func(w http.ResponseWriter, r *http.Request) {
prunedNode := state.SelectPrunedNode(config.ProtocolTypeApi)

Expand Down Expand Up @@ -46,19 +54,17 @@ func StartApiServer() {
r.Host = r.URL.Host
state.ProxyMapApi[selectedHost].ServeHTTP(w, r)
}
// handle all requests to your server using the proxy
//http.HandleFunc("/", handler)

serverMux := http.NewServeMux()
serverMux.HandleFunc("/", handler)
go func() {
//log.Fatal(http.ListenAndServe(":1337", serverMux))
apiServer = &http.Server{Addr: ":1337", Handler: serverMux}
log.Fatal(apiServer.ListenAndServe())
m.apiServer = &http.Server{Addr: ":1337", Handler: serverMux}
log.Fatal(m.apiServer.ListenAndServe())
}()
}

func ShutdownApiServer() {
if err := apiServer.Shutdown(context.Background()); err != nil {
func (m *ApiServer) ShutdownApiServer() {
if err := m.apiServer.Shutdown(context.Background()); err != nil {
log.Printf("apiServer Shutdown: %v", err)
}
}
42 changes: 24 additions & 18 deletions server/eth_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ import (
"strings"
)

var ethServer *http.Server
type EthServer struct {
ethServer *http.Server
}

func NewEthServer() *EthServer {
newItem := &EthServer{}
return newItem
}

func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
func (m *EthServer) ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
prunedNode := state.SelectPrunedNode(config.ProtocolTypeEth)
selectedHost := prunedNode.Backend.Eth // default to pruned node

Expand Down Expand Up @@ -74,7 +81,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
//} else

if method == "eth_getBalance" {
height := getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)

if height >= 0 {
node, err := state.SelectMatchedBackend(height, config.ProtocolTypeEth)
Expand Down Expand Up @@ -117,7 +124,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
selectedHost = node.Backend.Eth
}
} else if method == "eth_getTransactionCount" {
height := getHeightFromEthJsonrpcParams(m0["params"], 3, 2, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 3, 2, w)

if height >= 0 {
node, err := state.SelectMatchedBackend(height, config.ProtocolTypeEth)
Expand All @@ -129,7 +136,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
selectedHost = node.Backend.Eth
}
} else if method == "eth_getBlockTransactionCountByNumber" {
height := getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)

if height >= 0 {
node, err := state.SelectMatchedBackend(height, config.ProtocolTypeEth)
Expand All @@ -141,7 +148,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
selectedHost = node.Backend.Eth
}
} else if method == "eth_getCode" {
height := getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)

if height >= 0 {
node, err := state.SelectMatchedBackend(height, config.ProtocolTypeEth)
Expand All @@ -153,7 +160,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
selectedHost = node.Backend.Eth
}
} else if method == "eth_call" {
height := getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 2, 1, w)

if height >= 0 {
node, err := state.SelectMatchedBackend(height, config.ProtocolTypeEth)
Expand All @@ -165,7 +172,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
selectedHost = node.Backend.Eth
}
} else if method == "eth_getBlockByNumber" {
height := getHeightFromEthJsonrpcParams(m0["params"], 2, 0, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 2, 0, w)

fmt.Println("height=", height)

Expand All @@ -179,7 +186,7 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
selectedHost = node.Backend.Eth
}
} else if method == "eth_getProof" {
height := getHeightFromEthJsonrpcParams(m0["params"], 3, 1, w)
height := m.getHeightFromEthJsonrpcParams(m0["params"], 3, 1, w)

fmt.Println("height=", height)

Expand Down Expand Up @@ -219,33 +226,32 @@ func ethJsonRpcOverHttp(w http.ResponseWriter, r *http.Request) {
state.ProxyMapEth[selectedHost].ServeHTTP(w, r)
}

func StartEthServer() {
func (m *EthServer) StartEthServer() {
fmt.Println("StartEthServer...")
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { // JSONRPC over HTTP
ethJsonRpcOverHttp(w, r)
m.ethJsonRpcOverHttp(w, r)
} else {
_ = utils.SendError(w)
return
}
}
// handle all requests to your server using the proxy
//http.HandleFunc("/", handler)

serverMux := http.NewServeMux()
serverMux.HandleFunc("/", handler)
go func() {
ethServer = &http.Server{Addr: ":8545", Handler: serverMux}
log.Fatal(ethServer.ListenAndServe())
m.ethServer = &http.Server{Addr: ":8545", Handler: serverMux}
log.Fatal(m.ethServer.ListenAndServe())
}()
}

func ShutdownEthServer() {
if err := ethServer.Shutdown(context.Background()); err != nil {
func (m *EthServer) ShutdownEthServer() {
if err := m.ethServer.Shutdown(context.Background()); err != nil {
log.Printf("ethServer Shutdown: %v", err)
}
}

func getHeightFromEthJsonrpcParams(params interface{}, paramsLen int, posHeight int, w http.ResponseWriter) (height int64) {
func (m *EthServer) getHeightFromEthJsonrpcParams(params interface{}, paramsLen int, posHeight int, w http.ResponseWriter) (height int64) {
height = int64(-1)

positionalParams, ok := params.([]interface{})
Expand Down
Loading

0 comments on commit 0b61ef2

Please sign in to comment.