Skip to content

Commit

Permalink
incorp feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlagore committed Jul 12, 2017
1 parent 3f5da68 commit 17b6ced
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
7 changes: 6 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ func RequestHandler(req *msg.Request) msg.Response {
case msg.ResourceBlock:
blockNumber, ok := req.Params["blockNumber"].(uint32)
if ok {
res.Resource = chain.CopyBlockByIndex(blockNumber)
blk, err := chain.CopyBlockByIndex(blockNumber)
if err != nil {
res.Error = paramErr
} else {
res.Resource = blk
}
} else {
res.Error = paramErr
}
Expand Down
16 changes: 10 additions & 6 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blockchain

import (
"encoding/gob"
"errors"
"io"
)

Expand Down Expand Up @@ -73,10 +74,13 @@ func (bc *BlockChain) ContainsTransaction(t *Transaction, start, stop uint32) (b
}

// CopyLocalBlockByIndex returns a copy of a block in the local chain by index.
func (bc *BlockChain) CopyBlockByIndex(i uint32) *Block {
blk := bc.Blocks[i]
b := *blk
b.Transactions = make([]*Transaction, len(blk.Transactions))
copy(b.Transactions, blk.Transactions)
return &b
func (bc *BlockChain) CopyBlockByIndex(i uint32) (*Block, error) {
if i >= 0 && i < uint32(len(bc.Blocks)) {
blk := bc.Blocks[i]
b := *blk
b.Transactions = make([]*Transaction, len(blk.Transactions))
copy(b.Transactions, blk.Transactions)
return &b, nil
}
return nil, errors.New("block request out of bounds")
}
2 changes: 1 addition & 1 deletion blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEncodeDecodeBlockChain(t *testing.T) {

func TestCopyBlock(t *testing.T) {
bc, _ := NewValidTestChainAndBlock()
b := bc.CopyBlockByIndex(0)
b, _ := bc.CopyBlockByIndex(0)

if !reflect.DeepEqual(b, bc.Blocks[0]) {
t.FailNow()
Expand Down
6 changes: 3 additions & 3 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const (
// MiningSuccessful is returned when the miner mines a block.
MiningSuccessful = iota
// MiningNeverStarted is returned when the block header is invalid.
MiningNeverStarted = iota
MiningNeverStarted
// MiningHalted is returned when the app halts the miner.
MiningHalted = iota
MiningHalted
)

// MiningResult contains the result of the mining operation.
Expand All @@ -43,7 +43,7 @@ func RestartMiner(bc *blockchain.BlockChain, b *blockchain.Block) {
func Mine(bc *blockchain.BlockChain, b *blockchain.Block) *MiningResult {
setStart()
if valid, _ := bc.ValidBlock(b); !valid {
log.Error("Invalid block")
log.Error("miner given invalid block")
return &MiningResult{
Complete: false,
Info: MiningNeverStarted,
Expand Down

0 comments on commit 17b6ced

Please sign in to comment.