diff --git a/blockchain/client.go b/blockchain/client.go index c4ab8e7..e241d21 100644 --- a/blockchain/client.go +++ b/blockchain/client.go @@ -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 } diff --git a/controller/relayController.go b/controller/relayController.go index 8731ee4..e510dc3 100644 --- a/controller/relayController.go +++ b/controller/relayController.go @@ -1,36 +1,37 @@ 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") @@ -38,12 +39,12 @@ func (controller *RelayController) SignTransaction(w http.ResponseWriter, r *htt 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 @@ -54,40 +55,40 @@ 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) } } @@ -95,7 +96,7 @@ 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) @@ -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 -} \ No newline at end of file +} diff --git a/gas-relay-signer b/gas-relay-signer index b19cf17..ccd12cf 100755 Binary files a/gas-relay-signer and b/gas-relay-signer differ diff --git a/go.sum b/go.sum index 5d1536f..098c6ff 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/service/relaySignerService.go b/service/relaySignerService.go index 1669acb..5799483 100644 --- a/service/relaySignerService.go +++ b/service/relaySignerService.go @@ -21,7 +21,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" log "github.com/lacchain/gas-relay-signer/audit" @@ -40,13 +39,13 @@ var GAS_LIMIT uint64 = 0 var lock sync.Mutex -//RelaySignerService is the main service +// RelaySignerService is the main service type RelaySignerService struct { // The service's configuration Config *model.Config } -//Init configuration parameters +// Init configuration parameters func (service *RelaySignerService) Init(_config *model.Config) error { service.Config = _config @@ -76,7 +75,7 @@ func (service *RelaySignerService) Init(_config *model.Config) error { return nil } -//SendMetatransaction to blockchain +// SendMetatransaction to blockchain func (service *RelaySignerService) SendMetatransaction(id json.RawMessage, to *common.Address, gasLimit uint64, signingData []byte, v uint8, r, s [32]byte) *rpc.JsonrpcMessage { client := new(bl.Client) err := client.Connect(service.Config.Application.NodeURL) @@ -109,7 +108,7 @@ func (service *RelaySignerService) SendMetatransaction(id json.RawMessage, to *c return result.Response(tx) } -//GetTransactionReceipt from blockchain +// GetTransactionReceipt from blockchain func (service *RelaySignerService) GetTransactionReceipt(id json.RawMessage, transactionID string) *rpc.JsonrpcMessage { client := new(bl.Client) err := client.Connect(service.Config.Application.NodeURL) @@ -128,6 +127,8 @@ func (service *RelaySignerService) GetTransactionReceipt(id json.RawMessage, tra if receipt != nil { d := sha.NewLegacyKeccak256() e := sha.NewLegacyKeccak256() + f := sha.NewLegacyKeccak256() + d.Write([]byte("ContractDeployed(address,address,address)")) eventContractDeployed := hex.EncodeToString(d.Sum(nil)) @@ -135,6 +136,9 @@ func (service *RelaySignerService) GetTransactionReceipt(id json.RawMessage, tra e.Write([]byte("TransactionRelayed(address,address,address,bool,bytes)")) eventTransactionRelayed := hex.EncodeToString(e.Sum(nil)) + f.Write([]byte("BadTransactionSent(address,address,uint8)")) + // eventBadTransaction := hex.EncodeToString(f.Sum(nil)) + fmt.Println("deployed contract eventKeccak:", eventContractDeployed) fmt.Println("transaction relayed eventKeccak:", eventTransactionRelayed) @@ -142,21 +146,32 @@ func (service *RelaySignerService) GetTransactionReceipt(id json.RawMessage, tra if log.Topics[0].Hex() == "0x"+eventContractDeployed { receipt.ContractAddress = common.BytesToAddress(log.Data) } - if log.Topics[0].Hex() == "0x"+eventTransactionRelayed { - executed, output := transactionRelayedFailed(id, log.Data) - if !executed { - receipt.Status = uint64(0) - fmt.Println("Reverse Error:", hexutil.Encode(output)) - - jsonReceipt, err := json.Marshal(receipt) - if err != nil { - HandleError(id, err) - } - - json.Unmarshal(jsonReceipt, &receiptReverted) - receiptReverted["revertReason"] = hexutil.Encode(output) - } - } + /* if log.Topics[0].Hex() == "0x"+eventTransactionRelayed { + executed, output := transactionRelayedFailed(id, log.Data) + if !executed { + receipt.Status = uint64(0) + fmt.Println("Reverse Error:", hexutil.Encode(output)) + + jsonReceipt, err := json.Marshal(receipt) + if err != nil { + HandleError(id, err) + } + + json.Unmarshal(jsonReceipt, &receiptReverted) + receiptReverted["revertReason"] = hexutil.Encode(output) + } + }*/ + /* if log.Topics[0].Hex() == "0x"+eventBadTransaction { + receipt.Status = uint64(0) + jsonReceipt, err := json.Marshal(receipt) + if err != nil { + HandleError(id, err) + } + + json.Unmarshal(jsonReceipt, &receiptReverted) + output := getBadTransaction(id, log.Data) + receiptReverted["revertReason"] = hexutil.Encode(output) + }*/ } } result := new(rpc.JsonrpcMessage) @@ -169,7 +184,7 @@ func (service *RelaySignerService) GetTransactionReceipt(id json.RawMessage, tra } -//GetTransactionCount of account +// GetTransactionCount of account func (service *RelaySignerService) GetTransactionCount(id json.RawMessage, from string) *rpc.JsonrpcMessage { client := new(bl.Client) err := client.Connect(service.Config.Application.NodeURL) @@ -206,7 +221,7 @@ func (service *RelaySignerService) GetTransactionCount(id json.RawMessage, from return result.Response(fmt.Sprintf("0x%x", count)) } -//VerifyGasLimit sent a transaction +// VerifyGasLimit sent a transaction func (service *RelaySignerService) VerifyGasLimit(gasLimit uint64, id json.RawMessage) (bool, error) { client := new(bl.Client) err := client.Connect(service.Config.Application.NodeURL) @@ -215,20 +230,6 @@ func (service *RelaySignerService) VerifyGasLimit(gasLimit uint64, id json.RawMe } defer client.Close() - /*privateKey, err := crypto.HexToECDSA(service.Config.Application.Key) - if err != nil { - return false,err - } - - publicKey := privateKey.Public() - publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) - if !ok { - err := errors.New("error casting public key to ECDSA", -32602) - return false,err - } - - nodeAddress := crypto.PubkeyToAddress(*publicKeyECDSA)*/ - contractAddress := common.HexToAddress(service.Config.Application.ContractAddress) currentGasLimit, err := client.GetCurrentGasLimit(contractAddress) @@ -243,53 +244,10 @@ func (service *RelaySignerService) VerifyGasLimit(gasLimit uint64, id json.RawMe return false, nil } - /*maxBlockGasLimit,err := client.GetMaxBlockGasLimit(contractAddress) - if err != nil { - return false,err - } - - if maxBlockGasLimit != nil{ - log.GeneralLogger.Println("logic block gasLimit:",maxBlockGasLimit.Uint64()) - } - - if (gasLimit > maxBlockGasLimit.Uint64()){ - return false,nil - }*/ - - /* empty,err:=isPoolEmpty(service.Config.Application.NodeURL,id) - if err!=nil{ - return false,err - } - - if empty{ - currentGasLimit,err := client.GetCurrentGasLimit(contractAddress) - if err != nil { - return false,err - } - - if currentGasLimit != nil { - log.GeneralLogger.Println("current gasLimit assigned:",currentGasLimit.Uint64()) - } - if (gasLimit > currentGasLimit.Uint64()){ - return false,nil - } - }else{ - nodeGasLimit,err := client.GetGasLimit(contractAddress, nodeAddress) - if err != nil { - return false,err - } - if nodeGasLimit != nil { - log.GeneralLogger.Println("node gasLimit:",nodeGasLimit.Uint64()) - } - if (gasLimit > nodeGasLimit.Uint64()){ - return false,nil - } - }*/ - return true, nil } -//VerifySender sent a transaction +// VerifySender sent a transaction func (service *RelaySignerService) VerifySender(sender common.Address, id json.RawMessage) (bool, error) { client := new(bl.Client) err := client.Connect(service.Config.Application.NodeURL) @@ -310,7 +268,7 @@ func (service *RelaySignerService) VerifySender(sender common.Address, id json.R return isPermitted, nil } -//DecreaseGasUsed by node +// DecreaseGasUsed by node func (service *RelaySignerService) DecreaseGasUsed(id json.RawMessage) bool { client := new(bl.Client) err := client.Connect(service.Config.Application.NodeURL) @@ -356,12 +314,52 @@ func transactionRelayedFailed(id json.RawMessage, data []byte) (bool, []byte) { err = relayHubAbi.Unpack(&transactionRelayedEvent, "TransactionRelayed", data) if err != nil { - fmt.Println("Failed to unpack") + HandleError(id, err) } return transactionRelayedEvent.Executed, transactionRelayedEvent.Output } +func getBadTransaction(id json.RawMessage, data []byte) []byte { + var badTransactionEvent struct { + Node common.Address + OriginalSender common.Address + ErrorCode uint8 + } + + relayHubAbi, err := abi.JSON(strings.NewReader(RelayABI)) + if err != nil { + HandleError(id, err) + } + + err = relayHubAbi.Unpack(&badTransactionEvent, "BadTransactionSent", data) + + if err != nil { + HandleError(id, err) + } + + switch badTransactionEvent.ErrorCode { + case 0: + return []byte("Max block gas limit overpassed") + case 1: + return []byte("Original sender is different who signed the transaction") + case 2: + return []byte("Bad nonce assigned") + case 3: + return []byte("Not enough gas to process the transaction") + case 4: + return []byte("Destination is an empty contract") + case 5: + return []byte("Your bytecode to deploy is empty") + case 6: + return []byte("Invalid Signature") + case 7: + return []byte("Destination is not allowed") + } + + return nil +} + func (service *RelaySignerService) ProcessNewBlocks(done <-chan interface{}) { fmt.Println("Initiating process BLOCKSSS") client := new(bl.Client) @@ -407,7 +405,7 @@ func decrement() { log.GeneralLogger.Println("gas limit was reseted to 0") } -//HandleError +// HandleError func HandleError(id json.RawMessage, err error) *rpc.JsonrpcMessage { log.GeneralLogger.Println(err.Error()) result := new(rpc.JsonrpcMessage)