Example #1
0
func (a *ApiPort) newWallet(w http.ResponseWriter, r *http.Request) {

	b := make([]byte, 8)
	rand.Read(b)

	priv, host := libGFC.NewHost("foo")
	h := libGFC.NewHostUpdate(host)
	h.Sign(priv)

	c := make(chan error)

	a.s.KeyChannel <- KeyError{host.Id, priv, c}
	err := <-c
	if err != nil {
		w.WriteHeader(500)
		io.WriteString(w, err.Error())
		return
	}

	c = make(chan error)
	a.s.TransactionChannel <- TransactionError{h, a, c}

	err = <-c
	if err == nil {
		io.WriteString(w, host.Id)
	} else {
		w.WriteHeader(400)
		io.WriteString(w, err.Error())
	}
}
Example #2
0
func TestBlockGeneration(t *testing.T) {
	t.Log("Create server")
	s := NewServer(nil)
	s.event = make(chan string)

	e := make(chan time.Time)
	s.calculateBlock = (<-chan time.Time)(e)

	_, h := libGFC.NewHost("destination")
	record := libGFC.NewHostUpdate(h)

	c := make(chan error)

	s.TransactionChannel <- TransactionError{record, nil, c}
	<-c
	<-s.event

	if _, found := s.Keys[s.state["GFC"].NextHost().Id]; !found {
		t.Log(s.state["GFC"].NextHost())
		t.Log(s.state["GFC"].NextHost().Id)
		t.Fatal("Next host is not us?")
	}

	e <- time.Now()
	<-s.event
	<-s.event

	if len(s.SeenTransactions) != 0 {
		t.Fatal("transaction still in queue", s.SeenTransactions)
	}

	if s.state["GFC"].Revision != 1 {
		t.Fatal("Wrong revision number")
	}

	_, o := libGFC.OriginHostRecord()

	if s.state["GFC"].State[o.Id].Balance != 0 {
		t.Fatal("Incorrect balance")
	}

	e <- time.Now()
	<-s.event
	<-s.event

	if s.state["GFC"].Revision != 2 {
		t.Fatal("Wrong revision number")
	}

}
Example #3
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

}
Example #4
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)
	}

}