Skip to content

Commit

Permalink
testing app.Pay, two cases handled
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlagore committed Aug 12, 2017
1 parent 284cd0f commit 33557a3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
15 changes: 15 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,18 @@ func TestHandleTransaction(t *testing.T) {
transactionQueue <- blockchain.NewTestTransaction()
assert.Equal(t, len(transactionQueue), 0)
}

func TestPay(t *testing.T) {
amt := uint64(5)
a := createNewTestApp()
err := a.Pay("badf00d", amt)

// Fail with low balance.
assert.NotNil(t, err)

a.CurrentUser.Wallet.SetBalance(amt)
err = a.Pay("badf00d", amt)

// Fail with bad inputs.
assert.NotNil(t, err)
}
6 changes: 5 additions & 1 deletion app/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,20 @@ func connect(ctx *ishell.Context, a *App) {
}

func createWallet(ctx *ishell.Context, app *App) {
// Create a new wallet and set as CurrentUser's wallet.
wallet := blockchain.NewWallet()
app.CurrentUser.Wallet = wallet
emoji.Println(":credit_card: New wallet created!")

// Give a printout of the address(es).
emoji.Print(":mailbox:")
ctx.Println(" Address: " + wallet.Public().Repr())
emoji.Println(":fist: Emoji Address: " + wallet.Public().Emoji())
ctx.Println("")
}

func createTransaction(ctx *ishell.Context, app *App) {
// Read in the recipient address.
emoji.Print(":credit_card:")
ctx.Println(" Enter recipient wallet address")
toAddress := shell.ReadLine()
Expand All @@ -130,7 +134,7 @@ func createTransaction(ctx *ishell.Context, app *App) {
return
}

// Make payment.
// Try to make a payment.
err = app.Pay(toAddress, amount)
if err != nil {
emoji.Println(":disappointed: Transaction failed!")
Expand Down
25 changes: 19 additions & 6 deletions app/user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"errors"

"github.com/ubclaunchpad/cumulus/blockchain"
"github.com/ubclaunchpad/cumulus/msg"

Expand Down Expand Up @@ -30,11 +32,15 @@ func getCurrentUser() *User {

// Pay pays an amount of coin to an address `to`.
func (a *App) Pay(to string, amount uint64) error {
// Three atomic steps must occur.
// Four atomic steps must occur.
wallet := a.CurrentUser.Wallet
pool := a.Pool

// 1. A legitimate transaction must be built.
tbody := blockchain.TxBody{
Sender: getCurrentUser().Wallet.Public(),
Sender: wallet.Public(),
// TODO: Collect inputs.
Input: blockchain.TxHashPointer{},
Outputs: []blockchain.TxOutput{
blockchain.TxOutput{
Recipient: to,
Expand All @@ -43,16 +49,23 @@ func (a *App) Pay(to string, amount uint64) error {
},
}

// 2. The transaction must be signed and broadcasted.
if txn, err := tbody.Sign(*a.CurrentUser.Wallet, crand.Reader); err != nil {
if txn, err := tbody.Sign(*a.CurrentUser.Wallet, crand.Reader); err == nil {
// 2. The transaction must be signed.
a.PeerStore.Broadcast(msg.Push{
ResourceType: msg.ResourceTransaction,
Resource: txn,
})
a.CurrentUser.Wallet.SetPending(txn)

// 4. The transaction must be broadcasted to the peers.
if err := wallet.SetPending(txn); err != nil {
return err
}

// 3. The transaction must be added to the pool.
a.Pool.Push(txn, a.Chain)
if !pool.Push(txn, a.Chain) {
return errors.New("transaction broadcasted, but could not be added to the pool")
}

} else {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions blockchain/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
crand "crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -173,14 +175,16 @@ func (w *Wallet) SetAllPending(txns []*Transaction) {

// SetPending appends one transaction to the pending set of transaction
// if the wallet effective balance is high enough to accomodate.
func (w *Wallet) SetPending(txn *Transaction) {
func (w *Wallet) SetPending(txn *Transaction) error {
bal := w.GetEffectiveBalance()
spend := txn.GetTotalOutput()
if bal >= spend {
w.PendingTxns = append(w.PendingTxns, txn)
} else {
log.Printf("wallet balance is too low %v < %v", bal, spend)
msg := fmt.Sprintf("wallet balance is too low %v < %v", bal, spend)
return errors.New(msg)
}
return nil
}

// DropAllPending drops pending transactions if they apper in txns.
Expand Down
1 change: 1 addition & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func getIndex(a []*PooledTransaction, target time.Time, low, high int) int {

// Push inserts a transaction into the pool, returning
// true if the Transaction was inserted (was valid).
// TODO: This should return an error if could not add.
func (p *Pool) Push(t *blockchain.Transaction, bc *blockchain.BlockChain) bool {
if ok, err := consensus.VerifyTransaction(bc, t); ok {
p.set(t)
Expand Down

0 comments on commit 33557a3

Please sign in to comment.