Beispiel #1
0
func (a *ApiPort) sendMoney(w http.ResponseWriter, r *http.Request) {
	b, _ := ioutil.ReadAll(r.Body)

	v := make(map[string]string)
	err := json.Unmarshal(b, &v)
	if err != nil {
		io.WriteString(w, err.Error())
	}

	source := v["Source"]
	destination := v["Destination"]
	amount, _ := strconv.ParseUint(v["Amount"], 10, 64)

	t := libGFC.NewTransferUpdate(source, destination, amount)

	c := make(chan error)

	a.s.TransactionChannel <- TransactionError{t, a, c}

	err = <-c
	if err == nil {
		io.WriteString(w, "true")
	} else {
		io.WriteString(w, err.Error())
	}

	return
}
Beispiel #2
0
func (s *Server) produceBlock() (block libytc.Block) {

	//Find our entry
	var location *libGFC.FileChainRecord = nil
	for _, l := range s.state["GFC"].State {
		if l.Location[0] == s.GetLocation() {
			location = l
			break
		}
	}

	//If we aren't in the map, add us
	if location == nil {
		key, r := libGFC.NewHost(s.GetLocation())
		t := libGFC.NewHostUpdate(r)
		t.Sign(key)
		location = r
		s.Keys[r.Id] = key
		s.SeenTransactions[t.String()] = t
	}

	//If we are bootstrapping, destroy the default entry
	if s.state["GFC"].Revision == 0 {
		key, r := libGFC.OriginHostRecord()
		t := libGFC.NewTransferUpdate(r.Id, location.Id, r.Balance)
		t.Sign(key)
		s.SeenTransactions[t.String()] = t
	}

	update := make([]libytc.Update, len(s.SeenTransactions))

	i := uint(0)
	for _, v := range s.SeenTransactions {
		update[i] = v
		i++
	}

	block = libGFC.NewGFCBlock(s.state["GFC"].Revision+1, update)
	return

}
Beispiel #3
0
func TestNetworkPortSimple(t *testing.T) {
	t.Log("Make stuff")
	a := NewNetworkPort(NewServer(nil))
	b := NewNetworkPort(NewServer(nil))
	t.Log("Done making")

	a.s.event = make(chan string)

	c := make(chan error)
	go func() {
		c <- nil
		err := a.ListenNetwork("127.0.0.1:1777")
		if err != nil {
			t.Fatal(err)
		}
	}()

	t.Log("Before Block")
	<-c

	t.Log("Connecting")
	_, err := b.ConnectAddress("127.0.0.1:1777")
	if err != nil {
		t.Fatal(err)
	}

	t.Log("Connected")

	<-a.s.event

	t.Log("Recieved event")

	if len(a.s.ports) != 1 {
		t.Log(a.s.ports)
		t.Fatal("Failure to connect")
	}

	b.s.event = a.s.event
	a.s.event = nil

	priv, _ := libytc.DeterministicKey(0)
	_, h := libGFC.NewHost("destination")
	record := libGFC.NewHostUpdate(h)

	c = make(chan error)
	a.s.TransactionChannel <- TransactionError{record, nil, c}
	err = <-c
	if err != nil {
		t.Fatal(err)
	}
	o := <-b.s.event
	t.Log(o)

	u := libGFC.NewTransferUpdate("Origin", "Origin", 1)
	u.Sign(priv)

	if len(u.Signature.M) != 1 {
		t.Fatal("SignatureMap is not created")
	}

	c = make(chan error)
	a.s.TransactionChannel <- TransactionError{u, nil, c}
	err = <-c
	if err != nil {
		t.Fatal(err)
	}
	o = <-b.s.event
	t.Log(o)

	if len(b.s.SeenTransactions) != 2 {
		t.Fatal("Length is not 2, length is", len(b.s.SeenTransactions), b.s.SeenTransactions)
	}

}