github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/qmanager/utils/functions.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "errors" 7 "github.com/ethereum/go-ethereum/common" 8 "github.com/ethereum/go-ethereum/config" 9 "github.com/ethereum/go-ethereum/log" 10 "github.com/ethereum/go-ethereum/qmanager/global" 11 "net" 12 "net/http" 13 "strings" 14 ) 15 16 var ( 17 ActiveQmanager string 18 ) 19 20 func CheckQmanagerStatus() bool { 21 var QManagerAddresses= config.Config.QManagers 22 23 for _, qman := range QManagerAddresses{ 24 split := strings.Split(qman, "@") 25 QManager := split[1] 26 ActiveQmanager = QManager 27 //timeout := 1 * time.Second 28 //conn, err := net.DialTimeout("http", qman + "/Ping" , timeout) 29 //if err != nil { 30 // log.Info("QManager Not Available", "ADDR", QManager ) 31 //} else { 32 // conn.Close() 33 // ActiveQmanager = QManager 34 //} 35 } 36 split := strings.Split(ActiveQmanager, ":") 37 Address := split[0] 38 trial := net.ParseIP(Address) 39 if trial == nil { 40 log.Error("QManager Adrress Error", "Invalid Address ", trial ) 41 return false 42 } else { 43 return true 44 } 45 46 } 47 48 func CheckAddressValidity() bool { 49 var QManagerAddresses= config.Config.QManagers 50 if len(QManagerAddresses) == 0 { 51 log.Error("QManager Connection Error", "Address Not Found ", "Please insert qmanager address into Config.json" ) 52 return false 53 } else{ 54 if CheckQmanagerStatus(){ 55 return true 56 }else { 57 return false 58 } 59 } 60 } 61 62 func RequestExtraData(Proposer string) (common.ValidatorInfos, error) { 63 if CheckAddressValidity(){ 64 65 requestStruct := global.RequestStruct{ 66 Proposer: Proposer, 67 } 68 69 bytesRepresentation, err := json.Marshal(requestStruct) 70 if err != nil { 71 log.Error(err.Error()) 72 } 73 74 log.Info("Get ExtraData", "QMANAGER Address : ", "http://"+ ActiveQmanager + "/ExtraData") 75 76 resp, err := http.Post("http://"+ ActiveQmanager + "/ExtraData", "application/json", bytes.NewBuffer(bytesRepresentation)) 77 if err != nil { 78 log.Error(err.Error()) 79 return nil, err 80 } 81 82 var result []common.ValidatorInfo 83 json.NewDecoder(resp.Body).Decode(&result) 84 //log.Info("VALIDATOR LIST", "Full List : ", result) 85 //log.Info("VALIDATOR LIST", "Full BODY : ", resp) 86 87 return result, nil 88 89 }else { 90 return nil, errors.New("Unavailable QManager Address") 91 } 92 } 93 94 func BootNodeToQmanager(NodeData global.QManDBStruct) error { 95 if CheckAddressValidity(){ 96 bytesRepresentation, err := json.Marshal(NodeData) 97 if err != nil { 98 log.Error(err.Error()) 99 } 100 resp, err := http.Post("http://"+ ActiveQmanager + "/BootNodeSendData", "application/json", bytes.NewBuffer(bytesRepresentation)) 101 if err != nil { 102 log.Error(err.Error()) 103 return err 104 } 105 106 var result global.Message 107 108 json.NewDecoder(resp.Body).Decode(&result) 109 110 log.Info("Bootnode To Qmanager", "Send Status : ", result.Message) 111 return nil 112 }else { 113 return errors.New("Unavailable QManager Address") 114 } 115 } 116 117 func CoordinatorConfirmation(coordiReq global.RequestCoordiStruct) (bool, error) { 118 if CheckAddressValidity(){ 119 bytesRepresentation, err := json.Marshal(coordiReq) 120 if err != nil { 121 log.Error(err.Error()) 122 return false, err 123 } 124 125 resp, err := http.Post("http://"+ ActiveQmanager + "/CoordinatorConfirmation", "application/json", bytes.NewBuffer(bytesRepresentation)) 126 if err != nil { 127 log.Error(err.Error()) 128 return false, err 129 } 130 131 var result global.CoordiDecideStruct 132 133 json.NewDecoder(resp.Body).Decode(&result) 134 135 return result.Status, nil 136 137 }else { 138 return false, errors.New("Unavailable QManager Address") 139 } 140 141 } 142 143