Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(blockchain): clear redundancy code #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func (bc *Blockchain) AddBlock(block *Block) {
log.Panic(err)
}

lastHash := b.Get([]byte("l"))
lastBlockData := b.Get(lastHash)
lastBlockData := b.Get(bc.tip)
lastBlock := DeserializeBlock(lastBlockData)

if block.Height > lastBlock.Height {
Expand Down Expand Up @@ -197,7 +196,7 @@ func (bc *Blockchain) FindUTXO() map[string]TXOutputs {
return UTXO
}

// Iterator returns a BlockchainIterat
// Iterator returns a BlockchainIterator
func (bc *Blockchain) Iterator() *BlockchainIterator {
bci := &BlockchainIterator{bc.tip, bc.db}

Expand All @@ -210,8 +209,7 @@ func (bc *Blockchain) GetBestHeight() int {

err := bc.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
lastHash := b.Get([]byte("l"))
blockData := b.Get(lastHash)
blockData := b.Get(bc.tip)
lastBlock = *DeserializeBlock(blockData)

return nil
Expand Down Expand Up @@ -277,24 +275,10 @@ func (bc *Blockchain) MineBlock(transactions []*Transaction) *Block {
}
}

err := bc.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
lastHash = b.Get([]byte("l"))

blockData := b.Get(lastHash)
block := DeserializeBlock(blockData)

lastHeight = block.Height

return nil
})
if err != nil {
log.Panic(err)
}

lastHeight = bc.GetBestHeight()
newBlock := NewBlock(transactions, lastHash, lastHeight+1)

err = bc.db.Update(func(tx *bolt.Tx) error {
err := bc.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
err := b.Put(newBlock.Hash, newBlock.Serialize())
if err != nil {
Expand Down