Esempio n. 1
0
func TestListRepository(t *testing.T) {

	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}

	repo := repository.NewRepository("test", repository.PENDING, types.EmptyHash())
	err = db.PutRepository(repo)
	if err != nil {
		t.Errorf("error putting repository: %v", err)
	}

	repo1 := repository.NewRepository("hello_world", repository.ACTIVE, types.EmptyHash())
	err = db.PutRepository(repo1)
	if err != nil {
		t.Errorf("error putting repository: %v", err)
	}

	actualRepositories := db.ListRepositories()
	sort.Strings(actualRepositories)
	expectedRepositories := []string{"test", "hello_world"}
	sort.Strings(expectedRepositories)

	assert.Equal(t, actualRepositories, expectedRepositories)
}
Esempio n. 2
0
func TestListRefs(t *testing.T) {

	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}

	repo := repository.NewRepository("myrepo", repository.ACTIVE, types.EmptyHash())
	err = db.PutRepository(repo)
	if err != nil {
		t.Errorf("error putting repository: %v", err)
	}

	ref := util.SHA160([]byte("random"))
	err = db.PutRef("myrepo", "refs/heads/master", ref)
	if err != nil {
		t.Errorf("error putting repository ref: %v", err)
	}
	err = db.PutRef("myrepo", "refs/heads/next", ref)
	if err != nil {
		t.Errorf("error putting repository ref: %v", err)
	}

	refs, err := db.ListRefs("myrepo")
	if err != nil {
		t.Errorf("error listing repository refs: %v", err)
	}

	assert.Equal(t, refs, []string{"refs/heads/master", "refs/heads/next"})
}
Esempio n. 3
0
func TestPutGetRepository(t *testing.T) {

	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}

	repo := repository.NewRepository("test", repository.PENDING, types.EmptyHash())
	err = db.PutRepository(repo)
	if err != nil {
		t.Errorf("error putting repository: %v", err)
	}
	repo1, err := db.GetRepository("test")
	if err != nil {
		t.Errorf("error getting repository: %v", err)
	}
	if repo1 == nil {
		t.Errorf("error getting repository `test'")
	}
	assert.Equal(t, repo, repo1)
}
Esempio n. 4
0
func TestPutGetRef(t *testing.T) {

	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}

	repo := repository.NewRepository("myrepo", repository.ACTIVE, types.EmptyHash())
	err = db.PutRepository(repo)
	if err != nil {
		t.Errorf("error putting repository: %v", err)
	}

	// before the ref is set...
	ref0, err := db.GetRef("myrepo", "refs/heads/master")
	if err != nil {
		t.Errorf("error getting repository ref: %v", err)
	}
	assert.True(t, ref0.Equals(repository.EmptyRef()))

	ref := util.SHA160([]byte("random"))
	err = db.PutRef("myrepo", "refs/heads/master", ref)
	if err != nil {
		t.Errorf("error putting repository ref: %v", err)
	}
	ref1, err := db.GetRef("myrepo", "refs/heads/master")
	if err != nil {
		t.Errorf("error getting repository ref: %v", err)
	}
	if ref1 == nil {
		t.Errorf("error getting repository ref `refs/heads/master'")
	}
	assert.True(t, bytes.Compare(ref, ref1) == 0)
}
Esempio n. 5
0
func NameRegistrar(srv *context.T) {
	log := srv.Log.New("cmp", "name")
	ch := srv.Router.Sub("/block/last")
loop:
	select {
	case blki := <-ch:
		if blk, ok := blki.(*block.Block); ok {
			processPendingAllocations(srv, log)
			for i := range blk.Transactions {
				tx0 := blk.Transactions[i]
				tx := tx0.Transaction
				switch tx.(type) {
				case *transaction.NameAllocation:
					log.Debug("processing name allocation transaction", "txn", tx0)
					tx1 := tx.(*transaction.NameAllocation)
					// 1. find the reservation
					// 1.1. check if it was done with this server and there's a reference
					//      in scrap records
					reservation, err := srv.DB.GetScrap(util.SHA256(append(tx1.Rand, []byte(tx1.Name)...)))
					var reservationTx *transaction.Envelope
					if reservation == nil || err != nil {
						// 1.2 no scrap found, so try searching throughout database
						curBlock, err := srv.DB.GetLastBlock()
						if err != nil {
							log.Error("can't find last block during name allocation attempt")
							break
						}
						for curBlock != nil {
							for i := range curBlock.Transactions {
								if isValidReservation(curBlock.Transactions[i], tx0) {
									reservationTx = curBlock.Transactions[i]
								}
							}

							h := curBlock.PreviousBlockHash
							curBlock, err = srv.DB.GetBlock(h)
							if err != nil {
								log.Error("can't find block during name allocation attempt", "txn", h, "err", err)
								break
							}
						}

					} else {
						blk, err := srv.DB.GetTransactionBlock(reservation)
						if err != nil {
							log.Error("can't find block for name reservation", "txn", reservationTx, "err", err)
							break
						}
						for i := range blk.Transactions {
							if isValidReservation(blk.Transactions[i], tx0) {
								reservationTx = blk.Transactions[i]
								break
							}
						}

					}

					if reservationTx == nil {
						log.Error("can't find corresponding name reservation for allocation", "txn", tx0)
						break
					}

					// 2. verify its maturity
					confirmations, err := srv.DB.GetTransactionConfirmations(reservationTx.Hash())
					if err != nil {
						log.Error("can't compute number of confirmations for reservation", "txn", reservationTx.Hash(), "err", err)
						break
					}

					if confirmations >= RESERVATION_CONFIRMATIONS_REQUIRED {
						// this reservation is confirmed
						srv.DB.PutRepository(repository.NewRepository(tx1.Name, repository.PENDING, tx0.Hash()))
						log.Info("created pending repository", "repo", tx1.Name, "alloc_txn", tx0.Hash())
					} else {
						// this allocation is wasted as the distance is not long enough
					}
				default:
					// ignore all other transactions
				}
			}
		}
	}
	goto loop
}