Skip to content

Commit

Permalink
feat: invalidate block addition when adding block with invalid prev hash
Browse files Browse the repository at this point in the history
  • Loading branch information
stinkymonkeyph committed Aug 6, 2024
1 parent b30770d commit 55d0620
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ func (bc *Blockchain) AddBlock(b *Block) {

nextBlockHeight := len(bc.Blocks)

if b.PrevHash != bc.LastBlock().Hash() {
log.Panic("Trying to add an invalid block, halting entire process")
}

for index, txn := range b.Transactions {
txn.Status = constants.STATUS_SUCCESS
m[txn.TransactioHash] = true
balance := bc.WalletIndex.CalculateBalance(txn.From)
log.Printf("\n\nsender balance -> %d \n\n", balance)
Expand Down Expand Up @@ -101,7 +106,13 @@ func (bc *Blockchain) LastBlock() *Block {
func (bc *Blockchain) CopyTransactionPool() []*Transaction {
t := make([]*Transaction, 0)
for _, txn := range bc.TransactionPool {
t = append(t, NewTransaction(txn.From, txn.To, txn.Value, txn.Data))
senderBalance := bc.WalletIndex.CalculateBalance(txn.From)
if txn.From != constants.BLOCKCHAIN_AIRDROP_ADDRESS {
if senderBalance < int(txn.Value) {
txn.Status = constants.STATUS_FAILED
}
}
t = append(t, txn)
}
return t
}
Expand All @@ -126,12 +137,10 @@ func (bc *Blockchain) ProofOfWork() (int, []*Transaction) {
}

func (bc *Blockchain) Mining() bool {
log.Println("Start proof of work")
nonce, txns := bc.ProofOfWork()
previousHash := bc.LastBlock().Hash()
block := NewBlock(previousHash, nonce)
block.Transactions = txns
bc.AddBlock(block)
log.Println("Found solution")
return true
}

0 comments on commit 55d0620

Please sign in to comment.