當前位置: 首頁>>代碼示例>>Golang>>正文


Golang wire.ReadJSON函數代碼示例

本文整理匯總了Golang中github.com/tendermint/tendermint/wire.ReadJSON函數的典型用法代碼示例。如果您正苦於以下問題:Golang ReadJSON函數的具體用法?Golang ReadJSON怎麽用?Golang ReadJSON使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了ReadJSON函數的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: unmarshalValidateTx

func unmarshalValidateTx(amt int64, returnCode []byte) func(string, []byte) error {
	return func(eid string, b []byte) error {
		// unmarshall and assert somethings
		var response ctypes.Response
		var err error
		wire.ReadJSON(&response, b, &err)
		if err != nil {
			return err
		}
		if response.Error != "" {
			return fmt.Errorf(response.Error)
		}
		var data = response.Result.(*ctypes.ResultEvent).Data.(types.EventDataTx)
		if data.Exception != "" {
			return fmt.Errorf(data.Exception)
		}
		tx := data.Tx.(*types.CallTx)
		if !bytes.Equal(tx.Input.Address, user[0].Address) {
			return fmt.Errorf("Senders do not match up! Got %x, expected %x",
				tx.Input.Address, user[0].Address)
		}
		if tx.Input.Amount != amt {
			return fmt.Errorf("Amt does not match up! Got %d, expected %d",
				tx.Input.Amount, amt)
		}
		ret := data.Return
		if !bytes.Equal(ret, returnCode) {
			return fmt.Errorf("Tx did not return correctly. Got %x, expected %x", ret, returnCode)
		}
		return nil
	}
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:32,代碼來源:ws_helpers.go

示例2: unmarshalValidateCall

func unmarshalValidateCall(origin, returnCode []byte, txid *[]byte) func(string, []byte) error {
	return func(eid string, b []byte) error {
		// unmarshall and assert somethings
		var response ctypes.Response
		var err error
		wire.ReadJSON(&response, b, &err)
		if err != nil {
			return err
		}
		if response.Error != "" {
			return fmt.Errorf(response.Error)
		}
		var data = response.Result.(*ctypes.ResultEvent).Data.(types.EventDataCall)
		if data.Exception != "" {
			return fmt.Errorf(data.Exception)
		}
		if !bytes.Equal(data.Origin, origin) {
			return fmt.Errorf("Origin does not match up! Got %x, expected %x",
				data.Origin, origin)
		}
		ret := data.Return
		if !bytes.Equal(ret, returnCode) {
			return fmt.Errorf("Call did not return correctly. Got %x, expected %x", ret, returnCode)
		}
		if !bytes.Equal(data.TxID, *txid) {
			return fmt.Errorf("TxIDs do not match up! Got %x, expected %x",
				data.TxID, *txid)
		}
		return nil
	}
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:31,代碼來源:ws_helpers.go

示例3: unmarshalValidateSend

func unmarshalValidateSend(amt int64, toAddr []byte) func(string, []byte) error {
	return func(eid string, b []byte) error {
		// unmarshal and assert correctness
		var response ctypes.Response
		var err error
		wire.ReadJSON(&response, b, &err)
		if err != nil {
			return err
		}
		if response.Error != "" {
			return fmt.Errorf(response.Error)
		}
		if eid != response.Result.(*ctypes.ResultEvent).Event {
			return fmt.Errorf("Eventid is not correct. Got %s, expected %s", response.Result.(*ctypes.ResultEvent).Event, eid)
		}
		tx := response.Result.(*ctypes.ResultEvent).Data.(types.EventDataTx).Tx.(*types.SendTx)
		if !bytes.Equal(tx.Inputs[0].Address, user[0].Address) {
			return fmt.Errorf("Senders do not match up! Got %x, expected %x", tx.Inputs[0].Address, user[0].Address)
		}
		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.Equal(tx.Outputs[0].Address, toAddr) {
			return fmt.Errorf("Receivers do not match up! Got %x, expected %x", tx.Outputs[0].Address, user[0].Address)
		}
		return nil
	}
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:28,代碼來源:ws_helpers.go

示例4: parseValidateCommandStr

func parseValidateCommandStr(authCommandStr string) (Command, error) {
	var err error
	authCommand := wire.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)
}
開發者ID:huangjiehua,項目名稱:tendermint,代碼行數:9,代碼來源:main.go

示例5: ReadConfig

func ReadConfig(configFilePath string) {
	configJSONBytes, err := ioutil.ReadFile(configFilePath)
	if err != nil {
		Exit(Fmt("Failed to read config file %v. %v\n", configFilePath, err))
	}
	wire.ReadJSON(&Config, configJSONBytes, &err)
	if err != nil {
		Exit(Fmt("Failed to parse config. %v", err))
	}
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:10,代碼來源:main.go

示例6: unmarshalCheckResponse

func unmarshalCheckResponse(body []byte) (response *ctypes.Response, err error) {
	response = new(ctypes.Response)
	wire.ReadJSON(response, body, &err)
	if err != nil {
		return nil, err
	}
	if response.Error != "" {
		return nil, fmt.Errorf(response.Error)
	}
	return response, nil
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:11,代碼來源:client.go

示例7: LoadPrivValidator

func LoadPrivValidator(filePath string) *PrivValidator {
	privValJSONBytes, err := ioutil.ReadFile(filePath)
	if err != nil {
		Exit(err.Error())
	}
	privVal := wire.ReadJSON(&PrivValidator{}, privValJSONBytes, &err).(*PrivValidator)
	if err != nil {
		Exit(Fmt("Error reading PrivValidator from %v: %v\n", filePath, err))
	}
	privVal.filePath = filePath
	return privVal
}
開發者ID:eris-ltd,項目名稱:toadserver,代碼行數:12,代碼來源:priv_validator.go

示例8: unmarshalResponseNameReg

func unmarshalResponseNameReg(b []byte) (*types.NameTx, error) {
	// unmarshall and assert somethings
	var response ctypes.Response
	var err error
	wire.ReadJSON(&response, b, &err)
	if err != nil {
		return nil, err
	}
	if response.Error != "" {
		return nil, fmt.Errorf(response.Error)
	}
	tx := response.Result.(*ctypes.ResultEvent).Data.(types.EventDataTx).Tx.(*types.NameTx)
	return tx, nil
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:14,代碼來源:ws_helpers.go

示例9: unmarshalResponseNewBlock

func unmarshalResponseNewBlock(b []byte) (*types.Block, error) {
	// unmarshall and assert somethings
	var response ctypes.Response
	var err error
	wire.ReadJSON(&response, b, &err)
	if err != nil {
		return nil, err
	}
	if response.Error != "" {
		return nil, fmt.Errorf(response.Error)
	}
	block := response.Result.(*ctypes.ResultEvent).Data.(types.EventDataNewBlock).Block
	return block, nil
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:14,代碼來源:ws_helpers.go

示例10: ReadBarakOptions

// Read options from a file, or stdin if optionsFile is ""
func ReadBarakOptions(optFile string) *BarakOptions {
	var optBytes []byte
	var err error
	if optFile != "" {
		optBytes, err = ioutil.ReadFile(optFile)
	} else {
		optBytes, err = ioutil.ReadAll(os.Stdin)
	}
	if err != nil {
		panic(Fmt("Error reading input: %v", err))
	}
	opt := wire.ReadJSON(&BarakOptions{}, optBytes, &err).(*BarakOptions)
	if err != nil {
		panic(Fmt("Error parsing input: %v", err))
	}
	return opt
}
開發者ID:huangjiehua,項目名稱:tendermint,代碼行數:18,代碼來源:barak.go

示例11: parseValidateCommand

func parseValidateCommand(authCommand AuthCommand) (Command, error) {
	commandJSONStr := authCommand.CommandJSONStr
	signatures := authCommand.Signatures
	// Validate commandJSONStr
	if !validate([]byte(commandJSONStr), barak_.ListValidators(), signatures) {
		fmt.Printf("Failed validation attempt")
		return nil, errors.New("Validation error")
	}
	// Parse command
	var err error
	command := wire.ReadJSON(NoncedCommand{}, []byte(commandJSONStr), &err).(NoncedCommand)
	if err != nil {
		fmt.Printf("Failed to parse command")
		return nil, errors.New("Command parse error")
	}
	// Prevent replays
	barak_.CheckIncrNonce(command.Nonce)
	return command.Command, nil
}
開發者ID:huangjiehua,項目名稱:tendermint,代碼行數:19,代碼來源:main.go

示例12: receiveEventsRoutine

func (wsc *WSClient) receiveEventsRoutine() {
	for {
		_, data, err := wsc.ReadMessage()
		if err != nil {
			log.Info(Fmt("WSClient failed to read message: %v", err))
			wsc.Stop()
			break
		} else {
			var response ctypes.Response
			wire.ReadJSON(&response, data, &err)
			if err != nil {
				log.Info(Fmt("WSClient failed to parse message: %v", err))
				wsc.Stop()
				break
			}
			if strings.HasSuffix(response.Id, "#event") {
				wsc.EventsCh <- *response.Result.(*ctypes.ResultEvent)
			} else {
				wsc.ResultsCh <- response.Result
			}
		}
	}
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:23,代碼來源:ws_client.go

示例13: waitForEvent

// wait for an event; do things that might trigger events, and check them when they are received
// the check function takes an event id and the byte slice read off the ws
func waitForEvent(t *testing.T, con *websocket.Conn, eventid string, dieOnTimeout bool, f func(), check func(string, []byte) error) {
	// go routine to wait for webscoket msg
	goodCh := make(chan []byte)
	errCh := make(chan error)
	quitCh := make(chan struct{})
	defer close(quitCh)

	// Read message
	go func() {
		for {
			_, p, err := con.ReadMessage()
			if err != nil {
				errCh <- err
				break
			} else {
				// if the event id isnt what we're waiting on
				// ignore it
				var response ctypes.Response
				var err error
				wire.ReadJSON(&response, p, &err)
				if err != nil {
					errCh <- err
					break
				}
				event, ok := response.Result.(*ctypes.ResultEvent)
				if ok && event.Event == eventid {
					goodCh <- p
					break
				}
			}
		}
	}()

	// do stuff (transactions)
	f()

	// wait for an event or timeout
	timeout := time.NewTimer(10 * time.Second)
	select {
	case <-timeout.C:
		if dieOnTimeout {
			con.Close()
			t.Fatalf("%s event was not received in time", eventid)
		}
		// else that's great, we didn't hear the event
		// and we shouldn't have
	case p := <-goodCh:
		if dieOnTimeout {
			// message was received and expected
			// run the check
			err := check(eventid, p)
			if err != nil {
				t.Fatal(err)
				panic(err) // Show the stack trace.
			}
		} else {
			con.Close()
			t.Fatalf("%s event was not expected", eventid)
		}
	case err := <-errCh:
		t.Fatal(err)
		panic(err) // Show the stack trace.
	}
}
開發者ID:jsp282,項目名稱:tendermint,代碼行數:66,代碼來源:ws_helpers.go


注:本文中的github.com/tendermint/tendermint/wire.ReadJSON函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。