예제 #1
0
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")
}
예제 #2
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}
}
예제 #3
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)
}
예제 #4
0
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
}
예제 #5
0
func TestNewBlock(t *testing.T) {
	privateKey := generateKey(t)
	txn1, rand := trans.NewNameReservation("my-new-repository", &privateKey.PublicKey)
	txn2, _ := trans.NewNameAllocation("my-new-repository", rand, privateKey)
	txn3, _ := trans.NewNameDeallocation("my-new-repository", privateKey)

	transactions := []trans.T{txn1, txn2, txn3}
	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())
}
예제 #6
0
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())
}