Esempio n. 1
0
// Handle incoming requests.
func (this *ServerServer) handleFunc(c *gin.Context) {
	log.Debug("Incoming message")
	r := c.Request
	var buf bytes.Buffer
	n, errR := buf.ReadFrom(r.Body)
	if errR != nil || n == 0 {
		http.Error(c.Writer, "Bad request.", 400)
		return
	}
	bts := buf.Bytes()
	var errDC error
	reqData := &RequestData{}
	wire.ReadJSON(reqData, bts, &errDC)
	if errDC != nil {
		http.Error(c.Writer, "Failed to decode json.", 400)
		return
	}
	log.Debug("Starting to add.")
	resp, errA := this.serverManager.add(reqData)
	if errA != nil {
		http.Error(c.Writer, "Internal error: "+errA.Error(), 500)
		return
	}
	log.Debug("Work done.", "PORT", resp.Port)
	w := c.Writer
	enc := json.NewEncoder(w)
	enc.Encode(resp)
	w.WriteHeader(200)

}
Esempio n. 2
0
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
	}
}
Esempio n. 3
0
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
	}
}
Esempio n. 4
0
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
	}
}
Esempio n. 5
0
// Decode from an io.Reader.
func (this *TCodec) Decode(v interface{}, r io.Reader) error {
	bts, errR := ioutil.ReadAll(r)
	if errR != nil {
		return errR
	}
	var err error
	wire.ReadJSON(v, bts, &err)
	return err
}
Esempio n. 6
0
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
}
Esempio n. 7
0
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
}
Esempio n. 8
0
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
}
Esempio n. 9
0
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
}
Esempio n. 10
0
func (wsc *WSClient) receiveEventsRoutine() {
	for {
		_, data, err := wsc.ReadMessage()
		if err != nil {
			log.Info("WSClient failed to read message", "error", err, "data", string(data))
			wsc.Stop()
			break
		} else {
			var response ctypes.Response
			wire.ReadJSON(&response, data, &err)
			if err != nil {
				log.Info("WSClient failed to parse message", "error", err)
				wsc.Stop()
				break
			}
			if strings.HasSuffix(response.ID, "#event") {
				wsc.EventsCh <- *response.Result.(*ctypes.ResultEvent)
			} else {
				wsc.ResultsCh <- response.Result
			}
		}
	}
}
Esempio n. 11
0
// Decode from a byte array.
func (this *TCodec) DecodeBytes(v interface{}, bts []byte) error {
	var err error
	wire.ReadJSON(v, bts, &err)
	return err
}
Esempio n. 12
0
// 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.
	}
}