From 40592cee4645ecde4b28f3ddf4061f71f4d5d039 Mon Sep 17 00:00:00 2001 From: chadlagore Date: Fri, 25 Aug 2017 21:50:19 -0700 Subject: [PATCH] Fix up app tests to expect new fixture --- app/app.go | 8 ++++---- app/app_test.go | 39 ++++++++++++++++++++++++++++++--------- app/user.go | 5 ++++- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/app/app.go b/app/app.go index faff9be..de1a6b5 100644 --- a/app/app.go +++ b/app/app.go @@ -284,8 +284,8 @@ func (a *App) HandleWork() { // HandleTransaction handles new instance of TransactionWork. func (a *App) HandleTransaction(txn *blockchain.Transaction) { - validTransaction := a.Pool.Push(txn, a.Chain) - if validTransaction { + code := a.Pool.Push(txn, a.Chain) + if code == consensus.ValidTransaction { log.Debug("Added transaction to pool from address: " + txn.Sender.Repr()) } else { log.Debug("Bad transaction rejected from sender: " + txn.Sender.Repr()) @@ -351,7 +351,7 @@ func (a *App) RunMiner() { miningResult := miner.Mine(a.Chain, blockToMine) if miningResult.Complete { - log.Info("Successfully mined a block!") + // log.Debug("Successfully mined a block!") push := msg.Push{ ResourceType: msg.ResourceBlock, Resource: blockToMine, @@ -457,7 +457,7 @@ func (a *App) makeBlockRequest(currentHead *blockchain.Block, // handleBlockResponse receives a block or nil from the newBlockChan and attempts // to validate it and add it to the blockchain, or it handles a protocol error -// from the errChan. Returns whether the blockchain was modified and wether we +// from the errChan. Returns whether the blockchain was modified and whether we // received an UpToDate response. func (a *App) handleBlockResponse(newBlockChan chan *blockchain.Block, errChan chan *msg.ProtocolError) (changed bool, upToDate bool) { diff --git a/app/app_test.go b/app/app_test.go index 391603d..8ede1f8 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -47,7 +47,7 @@ func TestPushHandlerNewTestTransaction(t *testing.T) { select { case tr, ok := <-a.transactionQueue: assert.True(t, ok) - assert.Equal(t, tr, txn) + assert.ObjectsAreEqual(tr, txn) } } @@ -138,7 +138,7 @@ func TestHandleValidBlock(t *testing.T) { bc, blk := blockchain.NewValidTestChainAndBlock() a.Chain = bc a.HandleBlock(blk) - assert.Equal(t, blk, a.Chain.Blocks[2]) + assert.ObjectsAreEqual(blk, a.Chain.Blocks[2]) // TODO: Assert miner restarted. // TODO: Assert pool appropriately emptied. @@ -226,14 +226,24 @@ func TestHandleBlockResponse(t *testing.T) { a := createNewTestApp() newBlockChan := make(chan *blockchain.Block, 1) errChan := make(chan *msg.ProtocolError, 1) - newBlockChan <- a.Chain.RollBack() + + // Roll the last block off of the chain, add to channel. + lastBlock := a.Chain.RollBack() + assert.NotNil(t, lastBlock) + newBlockChan <- lastBlock + + // Handle the block. changed, upToDate := a.handleBlockResponse(newBlockChan, errChan) assert.True(t, changed) assert.False(t, upToDate) + + // Add an on-chain block to the handler (this shouldn't change the chain). newBlockChan <- a.Chain.Blocks[1] changed, upToDate = a.handleBlockResponse(newBlockChan, errChan) assert.False(t, changed) assert.False(t, upToDate) + + // More stuff happens. errChan <- msg.NewProtocolError(msg.UpToDate, "") changed, upToDate = a.handleBlockResponse(newBlockChan, errChan) assert.False(t, changed) @@ -242,19 +252,30 @@ func TestHandleBlockResponse(t *testing.T) { changed, upToDate = a.handleBlockResponse(newBlockChan, errChan) assert.True(t, changed) assert.False(t, upToDate) - assert.Equal(t, len(a.Chain.Blocks), 1) + assert.Equal(t, len(a.Chain.Blocks), 2) } func TestHandleWork(t *testing.T) { a := createNewTestApp() go a.HandleWork() - a.blockQueue <- a.Chain.RollBack() - time.Sleep(time.Second) + + // Roll the last block off of the chain, add to channel (expect it added back). + lastBlock := a.Chain.RollBack() assert.Equal(t, len(a.Chain.Blocks), 2) + a.blockQueue <- lastBlock + time.Sleep(time.Millisecond * 50) + assert.Equal(t, len(a.Chain.Blocks), 3) + + // Kill the worker. a.quitChan <- true - a.blockQueue <- a.Chain.RollBack() - time.Sleep(time.Second) - assert.Equal(t, len(a.Chain.Blocks), 1) + + // Roll the last block off of the chain, add to channel + // (expect it not to be added back). + lastBlock = a.Chain.RollBack() + assert.Equal(t, len(a.Chain.Blocks), 2) + a.blockQueue <- lastBlock + time.Sleep(time.Millisecond * 50) + assert.Equal(t, len(a.Chain.Blocks), 2) } func TestPay(t *testing.T) { diff --git a/app/user.go b/app/user.go index 80a3909..b139468 100644 --- a/app/user.go +++ b/app/user.go @@ -3,6 +3,8 @@ package app import ( "errors" + "github.com/ubclaunchpad/cumulus/consensus" + "github.com/ubclaunchpad/cumulus/blockchain" "github.com/ubclaunchpad/cumulus/msg" @@ -59,7 +61,8 @@ func (a *App) Pay(to string, amount uint64) error { } // The transaction must be added to the pool. - if !pool.Push(txn, a.Chain) { + code := pool.Push(txn, a.Chain) + if code != consensus.ValidTransaction { return errors.New("transaction validation failed") }