コード例 #1
0
ファイル: rpc_name.go プロジェクト: linhua55/gitchain
func (service *NameService) NameReservation(r *http.Request, args *NameReservationArgs, reply *NameReservationReply) error {
	log := service.log.New("cmp", "api_name")
	key, err := service.srv.DB.GetKey(args.Alias)
	if err != nil {
		return err
	}
	if key == nil {
		return errors.New("can't find the key")
	}
	tx, random := transaction.NewNameReservation(args.Name)

	hash, err := service.srv.DB.GetPreviousEnvelopeHashForPublicKey(&key.PublicKey)
	if err != nil {
		log.Error("error while preparing transaction", "err", err)
	}
	txe := transaction.NewEnvelope(hash, tx)
	txe.Sign(key)

	reply.Id = hex.EncodeToString(txe.Hash())
	reply.Random = hex.EncodeToString(random)
	// We save sha(random+name)=txhash to scraps to be able to find
	// the transaction hash by random and number during allocation
	service.srv.DB.PutScrap(util.SHA256(append(random, []byte(args.Name)...)), txe.Hash())
	service.srv.Router.Pub(txe, "/transaction")
	return nil
}
コード例 #2
0
ファイル: block_test.go プロジェクト: linhua55/gitchain
func TestEncodeDecode(t *testing.T) {
	privateKey := generateKey(t)
	txn1, rand := trans.NewNameReservation("my-new-repository")
	txn1e := trans.NewEnvelope(types.EmptyHash(), txn1)
	txn2, _ := trans.NewNameAllocation("my-new-repository", rand)
	txn2e := trans.NewEnvelope(types.EmptyHash(), txn2)
	txn3, _ := trans.NewNameDeallocation("my-new-repository")
	txn3e := trans.NewEnvelope(types.EmptyHash(), txn3)

	txn1e.Sign(privateKey)
	txn2e.Sign(privateKey)
	txn3e.Sign(privateKey)

	transactions := []*trans.Envelope{txn1e, txn2e, txn3e}
	block, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)
	if err != nil {
		t.Errorf("can't create a block because of %v", err)
	}

	enc, err := block.Encode()
	if err != nil {
		t.Errorf("error while encoding block: %v", err)
	}

	block1, err := Decode(enc)
	if err != nil {
		t.Errorf("error while encoding block: %v", err)
	}

	assert.Equal(t, block, block1, "encoded and decoded block should be identical to the original one")
}
コード例 #3
0
ファイル: db_test.go プロジェクト: Jaspper/gitchain
func fixtureSampleTransactions(t *testing.T) []transaction.T {
	privateKey := generateECDSAKey(t)
	txn1, rand := transaction.NewNameReservation("my-new-repository", &privateKey.PublicKey)
	txn2, _ := transaction.NewNameAllocation("my-new-repository", rand, privateKey)
	txn3, _ := transaction.NewNameDeallocation("my-new-repository", privateKey)

	return []transaction.T{txn1, txn2, txn3}
}
コード例 #4
0
ファイル: db_test.go プロジェクト: Jaspper/gitchain
func TestPutGetBlock(t *testing.T) {
	privateKey := generateECDSAKey(t)
	txn1, rand := transaction.NewNameReservation("my-new-repository", &privateKey.PublicKey)
	txn2, _ := transaction.NewNameAllocation("my-new-repository", rand, privateKey)
	txn3, _ := transaction.NewNameDeallocation("my-new-repository", privateKey)

	transactions := []transaction.T{txn1, txn2, txn3}
	block, err := block.NewBlock(types.EmptyHash(), block.HIGHEST_TARGET, transactions)
	if err != nil {
		t.Errorf("can't create a block because of %v", err)
	}

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

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}
	err = db.PutBlock(block, false)
	if err != nil {
		t.Errorf("error putting block: %v", err)
	}
	block1, err := db.GetBlock(block.Hash())
	if err != nil {
		t.Errorf("error getting block: %v", err)
	}
	if block1 == nil {
		t.Errorf("error getting block %v", block.Hash())
	}
	assert.Equal(t, block, block1)

	// Attempt fetching the last one
	block1, err = db.GetLastBlock()
	if err != nil {
		t.Errorf("error getting block: %v", err)
	}
	if block1 != nil {
		t.Errorf("error getting block, there should be no last block")
	}

	// Set the last one
	err = db.PutBlock(block, true)
	if err != nil {
		t.Errorf("error putting block: %v", err)
	}
	block1, err = db.GetLastBlock()
	if err != nil {
		t.Errorf("error getting last block: %v", err)
	}
	if block1 == nil {
		t.Errorf("error getting block, there should be a last block")
	}
	assert.Equal(t, block, block1)
}
コード例 #5
0
ファイル: rpc_name.go プロジェクト: josephyzhou/gitchain
func (srv *NameService) NameReservation(r *http.Request, args *NameReservationArgs, reply *NameReservationReply) error {
	key, err := env.DB.GetKey(args.Alias)
	if err != nil {
		return err
	}
	tx, random := transaction.NewNameReservation(args.Name, &key.PublicKey)
	reply.Id = hex.EncodeToString(tx.Hash())
	reply.Random = hex.EncodeToString(random)
	server.BroadcastTransaction(tx)
	return nil
}
コード例 #6
0
ファイル: db_test.go プロジェクト: jeffslofish/gitchain
func fixtureSampleTransactions(t *testing.T) ([]*transaction.Envelope, *ecdsa.PrivateKey) {
	privateKey := generateECDSAKey(t)
	txn1, rand := transaction.NewNameReservation("my-new-repository")
	txn1e := transaction.NewEnvelope(types.EmptyHash(), txn1)
	txn1e.Sign(privateKey)
	txn2, _ := transaction.NewNameAllocation("my-new-repository", rand)
	txn2e := transaction.NewEnvelope(txn1e.Hash(), txn2)
	txn2e.Sign(privateKey)
	txn3, _ := transaction.NewNameDeallocation("my-new-repository")
	txn3e := transaction.NewEnvelope(txn2e.Hash(), txn3)
	txn3e.Sign(privateKey)

	return []*transaction.Envelope{txn1e, txn2e, txn3e}, privateKey
}
コード例 #7
0
ファイル: rpc_name.go プロジェクト: Jaspper/gitchain
func (srv *NameService) NameReservation(r *http.Request, args *NameReservationArgs, reply *NameReservationReply) error {
	key, err := env.DB.GetKey(args.Alias)
	if err != nil {
		return err
	}
	if key == nil {
		return errors.New("can't find the key")
	}
	tx, random := transaction.NewNameReservation(args.Name, &key.PublicKey)
	reply.Id = hex.EncodeToString(tx.Hash())
	reply.Random = hex.EncodeToString(random)
	router.Send("/transaction", make(chan transaction.T), tx)
	return nil
}
コード例 #8
0
ファイル: block_test.go プロジェクト: josephyzhou/gitchain
func TestNewBlockSingleTx(t *testing.T) {
	privateKey := generateKey(t)
	txn1, _ := trans.NewNameReservation("my-new-repository", &privateKey.PublicKey)

	transactions := []trans.T{txn1}
	block1, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)

	if err != nil {
		t.Errorf("can't create a block because of %v", err)
		return
	}

	assert.Equal(t, transactions, block1.Transactions)
	assert.NotEqual(t, block1.MerkleRootHash, types.EmptyHash())
}
コード例 #9
0
ファイル: db_test.go プロジェクト: Jaspper/gitchain
func TestPutGetTransaction(t *testing.T) {
	privateKey := generateECDSAKey(t)
	txn, _ := transaction.NewNameReservation("my-new-repository", &privateKey.PublicKey)
	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}
	err = db.PutTransaction(txn)
	if err != nil {
		t.Errorf("error putting transaction: %v", err)
	}
	txn1, err := db.GetTransaction(txn.Hash())
	if err != nil {
		t.Errorf("error getting transaction: %v", err)
	}
	assert.Equal(t, txn, txn1)
}
コード例 #10
0
ファイル: transaction_test.go プロジェクト: linhua55/gitchain
func TestPutGetDeleteTransaction(t *testing.T) {
	privateKey := generateECDSAKey(t)
	txn1, _ := transaction.NewNameReservation("my-new-repository")
	txn1e := transaction.NewEnvelope(types.EmptyHash(), txn1)
	txn1e.Sign(privateKey)

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

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

	err = db.PutTransaction(txn1e)
	if err != nil {
		t.Errorf("error putting transaction: %v", err)
	}

	tx, err := db.GetTransaction(txn1e.Hash())
	if err != nil {
		t.Errorf("error getting transaction: %v", err)
	}

	assert.Equal(t, tx, txn1e)

	err = db.DeleteTransaction(txn1e.Hash())
	if err != nil {
		t.Errorf("error getting transaction: %v", err)
	}

	tx, err = db.GetTransaction(txn1e.Hash())
	if err != nil {
		t.Errorf("error getting transaction: %v", err)
	}

	assert.Nil(t, tx)

}
コード例 #11
0
ファイル: block_test.go プロジェクト: linhua55/gitchain
func TestNewBlock(t *testing.T) {
	privateKey := generateKey(t)
	txn1, rand := trans.NewNameReservation("my-new-repository")
	txn1e := trans.NewEnvelope(types.EmptyHash(), txn1)
	txn2, _ := trans.NewNameAllocation("my-new-repository", rand)
	txn2e := trans.NewEnvelope(types.EmptyHash(), txn2)
	txn3, _ := trans.NewNameDeallocation("my-new-repository")
	txn3e := trans.NewEnvelope(types.EmptyHash(), txn3)

	txn1e.Sign(privateKey)
	txn2e.Sign(privateKey)
	txn3e.Sign(privateKey)

	transactions := []*trans.Envelope{txn1e, txn2e, txn3e}
	block1, err := NewBlock(types.EmptyHash(), HIGHEST_TARGET, transactions)

	if err != nil {
		t.Errorf("can't create a block because of %v", err)
	}

	assert.Equal(t, transactions, block1.Transactions)
	assert.NotEqual(t, block1.MerkleRootHash, types.EmptyHash())
}