Пример #1
0
func NewServer(ports []Port) (s *Server) {
	s = new(Server)
	s.state = make(map[string]*libGFC.GFCChain)
	s.state["GFC"] = libGFC.NewGFCChain()

	s.encoder = make(map[string]libytc.Encoder)
	s.encoder["GFC"] = libGFC.GFCEncoder{}

	s.BlockChannel = make(chan BlockError)
	s.TransactionChannel = make(chan TransactionError)
	s.KeyChannel = make(chan KeyError)
	s.SeenTransactions = make(map[string]libytc.Update)
	s.calculateBlock = time.Tick(1 * time.Minute)

	s.Keys = make(map[string]*ecdsa.PrivateKey)
	key, host := libGFC.OriginHostRecord()
	s.Keys[host.Id] = key

	go s.handleChannels()

	for _, p := range ports {
		s.AddPort(p)
	}

	return
}
Пример #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")
	}

}
Пример #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

}