github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/validation.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package endpoints 19 20 import ( 21 "context" 22 "encoding/json" 23 "fmt" 24 "net/http" 25 "time" 26 27 "github.com/gin-gonic/gin" 28 "github.com/mysteriumnetwork/go-rest/apierror" 29 30 "github.com/ethereum/go-ethereum/ethclient" 31 32 "github.com/mysteriumnetwork/node/config" 33 ) 34 35 type validationEndpoints struct { 36 } 37 38 // ValidateRPCChain2URLS validates list of RPC Chain2 urls 39 // swagger:operation GET /validation/validate-rpc-chain2-urls 40 // 41 // --- 42 // summary: validates list of RPC Chain2 urls 43 // description: validates list of RPC Chain2 urls 44 // responses: 45 // 200: 46 // description: Validation success 47 // 400: 48 // description: Failed to parse or request validation failed 49 // schema: 50 // "$ref": "#/definitions/APIError" 51 func (e validationEndpoints) ValidateRPCChain2URLS(c *gin.Context) { 52 var rpcURLS []string 53 err := json.NewDecoder(c.Request.Body).Decode(&rpcURLS) 54 if err != nil { 55 c.Error(apierror.ParseFailed()) 56 return 57 } 58 59 for _, rpc := range rpcURLS { 60 ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) 61 defer cancel() 62 63 client, err := ethclient.DialContext(ctx, rpc) 64 if err != nil { 65 c.Error(err) 66 return 67 } 68 69 rpcURLChainID, err := client.ChainID(ctx) 70 if err != nil { 71 c.Error(err) 72 return 73 } 74 75 chain2ID := config.GetInt64(config.FlagChain2ChainID) 76 if rpcURLChainID.Int64() != chain2ID { 77 c.Error(apierror.BadRequest(fmt.Sprintf("URL: %s chainID missmatch - expected: %d but got: %d", rpc, chain2ID, rpcURLChainID), apierror.ValidateErrInvalidVal)) 78 return 79 } 80 } 81 c.Status(http.StatusOK) 82 } 83 84 // AddRoutesForValidator register /validation endpoint 85 func AddRoutesForValidator(e *gin.Engine) error { 86 validatorEndpoints := &validationEndpoints{} 87 e.POST("/validation/validate-rpc-chain2-urls", validatorEndpoints.ValidateRPCChain2URLS) 88 return nil 89 }