func (service *NameService) NameAllocation(r *http.Request, args *NameAllocationArgs, reply *NameAllocationReply) 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") } random, err := hex.DecodeString(args.Random) if err != nil { return err } tx, err := transaction.NewNameAllocation(args.Name, random) if err != nil { return err } 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()) service.srv.Router.Pub(txe, "/transaction") return nil }
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") }
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} }
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) }
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 }
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()) }
func (srv *NameService) NameAllocation(r *http.Request, args *NameAllocationArgs, reply *NameAllocationReply) error { key, err := env.DB.GetKey(args.Alias) if err != nil { return err } if key == nil { return errors.New("can't find the key") } random, err := hex.DecodeString(args.Random) if err != nil { return err } tx, err := transaction.NewNameAllocation(args.Name, random, key) if err != nil { return err } reply.Id = hex.EncodeToString(tx.Hash()) router.Send("/transaction", make(chan transaction.T), tx) return nil }
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()) }