Skip to content

Commit

Permalink
refactor: prevent disconnect of block and blockchain struct during bl…
Browse files Browse the repository at this point in the history
…ock production
  • Loading branch information
stinkymonkeyph committed Aug 12, 2024
1 parent c7ad50d commit 1ea604a
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ func (bc *Blockchain) AddTransactionToTransactionPool(txn *Transaction) {
}
}

func (bc *Blockchain) AddBlock(b *Block) {
m := map[string]bool{}

func (bc *Blockchain) AddBlock(nonce int, txns []*Transaction) {
nextBlockNumber := len(bc.Blocks)

b := NewBlock(bc.LastBlock().Hash(), nextBlockNumber, nonce, txns)

m := map[string]bool{}

if b.PrevHash != bc.LastBlock().Hash() {
log.Panic("Trying to add an invalid block, halting entire process")
}
Expand Down Expand Up @@ -182,8 +184,6 @@ func (bc *Blockchain) ProofOfWork() (int, []*Transaction) {

func (bc *Blockchain) Mining() bool {
nonce, txns := bc.ProofOfWork()
previousHash := bc.LastBlock().Hash()
block := NewBlock(previousHash, len(bc.Blocks), nonce, txns)
bc.AddBlock(block)
bc.AddBlock(nonce, txns)
return true
}

0 comments on commit 1ea604a

Please sign in to comment.