Skip to content

Commit

Permalink
Merge pull request #20 from LACNetNetworks/fix/reduce_calls
Browse files Browse the repository at this point in the history
Fix/reduce calls
  • Loading branch information
ccamaleon5 authored Oct 21, 2022
2 parents e721072 + d2f0c95 commit fd21ccd
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 125 deletions.
9 changes: 5 additions & 4 deletions blockchain/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,20 @@ func (ec *Client) ConfigTransaction(key *ecdsa.PrivateKey, gasLimit uint64, pend
}
}

gasPrice, err := ec.client.SuggestGasPrice(context.Background())
//REMOVE
/*gasPrice, err := ec.client.SuggestGasPrice(context.Background())
if err != nil {
msg := "can't get gas price suggested"
err = errors.FailedConfigTransaction.Wrapf(err, msg, -32603)
return nil, err
}
}*/

auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = gasLimit // in units
auth.GasPrice = gasPrice
auth.GasPrice = big.NewInt(0)

log.GeneralLogger.Printf("OptionsTransaction=[From:0x%x,nonce:%d,gasPrice:%s,gasLimit:%d", auth.From, nonce, gasPrice, auth.GasLimit)
log.GeneralLogger.Printf("OptionsTransaction=[From:0x%x,nonce:%d,gasPrice:%s,gasLimit:%d", auth.From, nonce, auth.GasPrice, auth.GasLimit)

return auth, nil
}
Expand Down
77 changes: 39 additions & 38 deletions controller/relayController.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
package controller

import (
"bytes"
"encoding/json"
"sync"
"errors"
"net/http/httputil"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"io/ioutil"
"bytes"
"github.com/lacchain/gas-relay-signer/rpc"
"sync"

log "github.com/lacchain/gas-relay-signer/audit"
"github.com/lacchain/gas-relay-signer/model"
"github.com/lacchain/gas-relay-signer/rpc"
"github.com/lacchain/gas-relay-signer/service"
log "github.com/lacchain/gas-relay-signer/audit"
)

var lock sync.Mutex

//RelayController is the main controller
// RelayController is the main controller
type RelayController struct {
// The controller's configuration
Config *model.Config
Config *model.Config
RelaySignerService *service.RelaySignerService
}

//Init controller
func (controller *RelayController) Init(config *model.Config, relaySignerService *service.RelaySignerService){
// Init controller
func (controller *RelayController) Init(config *model.Config, relaySignerService *service.RelaySignerService) {
controller.Config = config
controller.RelaySignerService = relaySignerService
}

//SignTransaction ...
// SignTransaction ...
func (controller *RelayController) SignTransaction(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

//log.GeneralLogger.Println("Body:", r.Body)

buf, err := ioutil.ReadAll(r.Body)
if err != nil {
handleError(nil,err)
handleError(nil, err)
}
rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf))
rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf))

// log.GeneralLogger.Println("Request body : ", rdr1)
// log.GeneralLogger.Println("Request body : ", rdr1)

var rpcMessage rpc.JsonrpcMessage

Expand All @@ -54,48 +55,48 @@ func (controller *RelayController) SignTransaction(w http.ResponseWriter, r *htt
return
}

log.GeneralLogger.Println("JSON-RPC Method:",rpcMessage.Method);
log.GeneralLogger.Println("JSON-RPC Method:", rpcMessage.Method)

if (rpcMessage.IsPrivTransaction()){
r.Body=rdr2
if rpcMessage.IsPrivTransaction() {
r.Body = rdr2
log.GeneralLogger.Println("Is a private Transaction, forward to Besu->Orion")

serveReverseProxy(controller.Config.Application.NodeURL,w,r)
}else if (rpcMessage.IsPrivRawTransaction()){
r.Body=rdr2
serveReverseProxy(controller.Config.Application.NodeURL, w, r)
} else if rpcMessage.IsPrivRawTransaction() {
r.Body = rdr2
log.GeneralLogger.Println("Is a private send Transaction, decrease gas used")

controller.RelaySignerService.DecreaseGasUsed(rpcMessage.ID)

log.GeneralLogger.Println("forward to Besu->Orion")
serveReverseProxy(controller.Config.Application.NodeURL,w,r)
}else if (rpcMessage.IsRawTransaction()){
processRawTransaction(controller.RelaySignerService,rpcMessage, w)
serveReverseProxy(controller.Config.Application.NodeURL, w, r)
} else if rpcMessage.IsRawTransaction() {
processRawTransaction(controller.RelaySignerService, rpcMessage, w)
return
}else if (rpcMessage.IsGetTransactionReceipt()){
processGetTransactionReceipt(controller.RelaySignerService,rpcMessage, w)
} else if rpcMessage.IsGetTransactionReceipt() {
processGetTransactionReceipt(controller.RelaySignerService, rpcMessage, w)
return
}else if(rpcMessage.IsGetTransactionCount()){
processTransactionCount(controller.RelaySignerService,rpcMessage, w)
} else if rpcMessage.IsGetTransactionCount() {
processTransactionCount(controller.RelaySignerService, rpcMessage, w)
return
//}else if(rpcMessage.IsGetBlockByNumber()){
// processGetBlockByNumber(controller.RelaySignerService,rpcMessage, w)
// return
}else{
// r.Body=rdr2
//}else if(rpcMessage.IsGetBlockByNumber()){
// processGetBlockByNumber(controller.RelaySignerService,rpcMessage, w)
// return
} else {
// r.Body=rdr2
err := errors.New("method is not supported")
data := handleError(rpcMessage.ID, err)
w.Write(data)
return
// serveReverseProxy(controller.Config.Application.NodeURL,w,r)
// serveReverseProxy(controller.Config.Application.NodeURL,w,r)
}
}

func serveReverseProxy(target string, res http.ResponseWriter, req *http.Request) {
// parse the url
url, err := url.Parse(target)
if err != nil {
handleError(nil,err)
handleError(nil, err)
}
// create the reverse proxy
proxy := httputil.NewSingleHostReverseProxy(url)
Expand All @@ -110,12 +111,12 @@ func serveReverseProxy(target string, res http.ResponseWriter, req *http.Request
proxy.ServeHTTP(res, req)
}

func handleError(messageID json.RawMessage, err error) ([]byte) {
// log.GeneralLogger.Println(err)
data, err := json.Marshal(service.HandleError(messageID,err))
func handleError(messageID json.RawMessage, err error) []byte {
// log.GeneralLogger.Println(err)
data, err := json.Marshal(service.HandleError(messageID, err))
if err != nil {
log.GeneralLogger.Println("Error trying to marshall a response to client")
}

return data
}
}
Binary file modified gas-relay-signer
Binary file not shown.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6 h1:Eey/GGQ/E5Xp1P2Lyx1qj007hLZfbi0+CoVeJruGCtI=
github.com/btcsuite/btcd v0.0.0-20171128150713-2e60448ffcc6/go.mod h1:Dmm/EzmjnCiweXmzRIAiUWCInVmPgjkzgv5k4tVyXiQ=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
Expand Down
Loading

0 comments on commit fd21ccd

Please sign in to comment.