func (c *ClientJSON) GetStorage(address []byte, key []byte) (*ctypes.ResponseGetStorage, error) { request := rpctypes.RPCRequest{ JSONRPC: "2.0", Method: reverseFuncMap["GetStorage"], Params: []interface{}{address, key}, Id: 0, } body, err := c.RequestResponse(request) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseGetStorage `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func (c *ClientHTTP) BlockchainInfo(minHeight uint, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error) { values, err := argsToURLValues([]string{"minHeight", "maxHeight"}, minHeight, maxHeight) if err != nil { return nil, err } resp, err := http.PostForm(c.addr+reverseFuncMap["BlockchainInfo"], values) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseBlockchainInfo `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func (c *ClientHTTP) ListValidators() (*ctypes.ResponseListValidators, error) { values, err := argsToURLValues(nil) if err != nil { return nil, err } resp, err := http.PostForm(c.addr+reverseFuncMap["ListValidators"], values) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseListValidators `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func (c *ClientJSON) Status() (*ctypes.ResponseStatus, error) { request := rpctypes.RPCRequest{ JSONRPC: "2.0", Method: reverseFuncMap["Status"], Params: []interface{}{}, Id: 0, } body, err := c.RequestResponse(request) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseStatus `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func (c *Crawler) consumeMessage(wsMsg *rpcclient.WSMsg, node *Node) error { if wsMsg.Error != nil { return wsMsg.Error } // unmarshal block event var response struct { Event string Data *types.Block Error string } var err error binary.ReadJSON(&response, wsMsg.Data, &err) if err != nil { return err } if response.Error != "" { return fmt.Errorf(response.Error) } block := response.Data node.LastSeen = time.Now() node.BlockHeight = block.Height node.BlockHistory[block.Height] = node.LastSeen return nil }
func (c *ClientHTTP) Call(address []byte, data []byte) (*ctypes.ResponseCall, error) { values, err := argsToURLValues([]string{"address", "data"}, address, data) if err != nil { return nil, err } resp, err := http.PostForm(c.addr+reverseFuncMap["Call"], values) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseCall `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func unmarshalValidateSend(amt uint64, toAddr []byte) func(string, []byte) error { return func(eid string, b []byte) error { // unmarshal and assert correctness var response struct { Event string `json:"event"` Data types.SendTx `json:"data"` Error string `json:"error"` } var err error binary.ReadJSON(&response, b, &err) if err != nil { return err } if response.Error != "" { return fmt.Errorf(response.Error) } if eid != response.Event { return fmt.Errorf("Eventid is not correct. Got %s, expected %s", response.Event, eid) } tx := response.Data if bytes.Compare(tx.Inputs[0].Address, userByteAddr) != 0 { return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Inputs[0].Address, userByteAddr) } if tx.Inputs[0].Amount != amt { return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Inputs[0].Amount, amt) } if bytes.Compare(tx.Outputs[0].Address, toAddr) != 0 { return fmt.Errorf("Receivers do not match up! Got %x, expected %x", tx.Outputs[0].Address, userByteAddr) } return nil } }
func unmarshalValidateCallCall(origin, returnCode []byte, txid *[]byte) func(string, []byte) error { return func(eid string, b []byte) error { // unmarshall and assert somethings var response struct { Event string `json:"event"` Data types.EventMsgCall `json:"data"` Error string `json:"error"` } var err error binary.ReadJSON(&response, b, &err) if err != nil { return err } if response.Error != "" { return fmt.Errorf(response.Error) } if response.Data.Exception != "" { return fmt.Errorf(response.Data.Exception) } if bytes.Compare(response.Data.Origin, origin) != 0 { return fmt.Errorf("Origin does not match up! Got %x, expected %x", response.Data.Origin, origin) } ret := response.Data.Return if bytes.Compare(ret, returnCode) != 0 { return fmt.Errorf("Call did not return correctly. Got %x, expected %x", ret, returnCode) } if bytes.Compare(response.Data.TxId, *txid) != 0 { return fmt.Errorf("TxIds do not match up! Got %x, expected %x", response.Data.TxId, *txid) } return nil } }
func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*ctypes.ResponseBroadcastTx, error) { values, err := argsToURLValues([]string{"tx"}, tx) if err != nil { return nil, err } resp, err := http.PostForm(c.addr+reverseFuncMap["BroadcastTx"], values) if err != nil { return nil, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseBroadcastTx `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func (c *ClientJSON) BlockchainInfo(minHeight uint, maxHeight uint) (*ctypes.ResponseBlockchainInfo, error) { request := rpctypes.RPCRequest{ JSONRPC: "2.0", Method: reverseFuncMap["BlockchainInfo"], Params: []interface{}{minHeight, maxHeight}, Id: 0, } body, err := c.RequestResponse(request) if err != nil { return nil, err } var response struct { Result *ctypes.ResponseBlockchainInfo `json:"result"` Error string `json:"error"` Id string `json:"id"` JSONRPC string `json:"jsonrpc"` } binary.ReadJSON(&response, body, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } return response.Result, nil }
func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) { var err error binary.ReadJSON(&genState, jsonBlob, &err) if err != nil { panic(Fmt("Couldn't read GenesisDoc: %v", err)) } return }
func parseValidateCommandStr(authCommandStr string) (Command, error) { var err error authCommand := binary.ReadJSON(AuthCommand{}, []byte(authCommandStr), &err).(AuthCommand) if err != nil { fmt.Printf("Failed to parse auth_command") return nil, errors.New("AuthCommand parse error") } return parseValidateCommand(authCommand) }
func _jsonStringToArg(ty reflect.Type, arg string) (reflect.Value, error) { var err error v := reflect.New(ty) binary.ReadJSON(v.Interface(), []byte(arg), &err) if err != nil { return v, err } v = v.Elem() return v, nil }
func ReadConfig(configFilePath string) { configJSONBytes, err := ioutil.ReadFile(configFilePath) if err != nil { Exit(Fmt("Failed to read config file %v. %v\n", configFilePath, err)) } binary.ReadJSON(&Config, configJSONBytes, &err) if err != nil { Exit(Fmt("Failed to parse config. %v", err)) } }
func LoadPrivValidator(filePath string) *PrivValidator { privValJSONBytes, err := ioutil.ReadFile(filePath) if err != nil { panic(err) } privVal := binary.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator) if err != nil { Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err)) } privVal.filePath = filePath return privVal }
func unmarshalResponseNewBlock(b []byte) (*types.Block, error) { // unmarshall and assert somethings var response struct { Event string `json:"event"` Data *types.Block `json:"data"` Error string `json:"error"` } var err error binary.ReadJSON(&response, b, &err) if err != nil { return nil, err } if response.Error != "" { return nil, fmt.Errorf(response.Error) } block := response.Data return block, nil }
func parseValidateCommand(authCommand AuthCommand) (Command, error) { commandJSONStr := authCommand.CommandJSONStr signatures := authCommand.Signatures // Validate commandJSONStr if !validate([]byte(commandJSONStr), barak.validators, signatures) { fmt.Printf("Failed validation attempt") return nil, errors.New("Validation error") } // Parse command var err error command := binary.ReadJSON(NoncedCommand{}, []byte(commandJSONStr), &err).(NoncedCommand) if err != nil { fmt.Printf("Failed to parse command") return nil, errors.New("Command parse error") } // Prevent replays if barak.nonce+1 != command.Nonce { return nil, errors.New("Replay error") } else { barak.nonce += 1 } return command.Command, nil }
func unmarshalValidateCall(amt uint64, returnCode []byte) func(string, []byte) error { return func(eid string, b []byte) error { // unmarshall and assert somethings var response struct { Event string `json:"event"` Data struct { Tx types.CallTx `json:"tx"` Return []byte `json:"return"` Exception string `json:"exception"` } `json:"data"` Error string `json:"error"` } var err error binary.ReadJSON(&response, b, &err) if err != nil { return err } if response.Error != "" { return fmt.Errorf(response.Error) } if response.Data.Exception != "" { return fmt.Errorf(response.Data.Exception) } tx := response.Data.Tx if bytes.Compare(tx.Input.Address, userByteAddr) != 0 { return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Input.Address, userByteAddr) } if tx.Input.Amount != amt { return fmt.Errorf("Amt does not match up! Got %d, expected %d", tx.Input.Amount, amt) } ret := response.Data.Return if bytes.Compare(ret, returnCode) != 0 { return fmt.Errorf("Call did not return correctly. Got %x, expected %x", ret, returnCode) } return nil } }
func main() { fmt.Printf("New Barak Process (PID: %d)\n", os.Getpid()) // Apply bare tendermint/* configuration. cfg.ApplyConfig(cfg.MapConfig(map[string]interface{}{"log_level": "info"})) // read flags to change options file. var optionsBytes []byte var optionsFile string var err error flag.StringVar(&optionsFile, "options-file", "", "Read options from file instead of stdin") flag.Parse() if optionsFile != "" { optionsBytes, err = ioutil.ReadFile(optionsFile) } else { optionsBytes, err = ioutil.ReadAll(os.Stdin) } if err != nil { panic(Fmt("Error reading input: %v", err)) } options := binary.ReadJSON(&Options{}, optionsBytes, &err).(*Options) if err != nil { panic(Fmt("Error parsing input: %v", err)) } barak.nonce = options.StartNonce barak.validators = options.Validators barak.rootDir = os.Getenv("BRKROOT") if barak.rootDir == "" { barak.rootDir = os.Getenv("HOME") + "/.barak" } err = EnsureDir(barak.rootDir) if err != nil { panic(Fmt("Error creating barak rootDir: %v", err)) } barak.registries = options.Registries // Debug. fmt.Printf("Options: %v\n", options) fmt.Printf("Barak: %v\n", barak) // Start rpc server. mux := http.NewServeMux() mux.HandleFunc("/download", ServeFile) mux.HandleFunc("/register", Register) // TODO: mux.HandleFunc("/upload", UploadFile) rpcserver.RegisterRPCFuncs(mux, Routes) rpcserver.StartHTTPServer(options.ListenAddress, mux) // Register this barak with central listener for _, registry := range barak.registries { go func(registry string) { for { resp, err := http.Get(registry + "/register") if err != nil { fmt.Printf("Error registering to registry %v:\n %v\n", registry, err) time.Sleep(1 * time.Hour) continue } body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("Successfully registered with registry %v\n %v\n", registry, string(body)) return } }(registry) } // Write pid to file. This should be the last thing before TrapSignal. err = WriteFileAtomic(barak.rootDir+"/pidfile", []byte(Fmt("%v", barak.pid))) if err != nil { panic(Fmt("Error writing pidfile: %v", err)) } TrapSignal(func() { fmt.Println("Barak shutting down") }) }