Skip to content

Commit

Permalink
refactor: increase mining difficulty and log verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
stinkymonkeyph committed Jul 25, 2024
1 parent d1e4667 commit 5c8a881
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (bc *Blockchain) AddBlock(b *Block) {

func (bc *Blockchain) ProofOfWorkMining(minerAddress string) {
nonce := 0
log.Println("Starting Proof of Work")
for {
prevHash := bc.Blocks[len(bc.Blocks)-1].Hash()
guessBlock := NewBlock(prevHash, nonce)
Expand All @@ -68,10 +69,14 @@ func (bc *Blockchain) ProofOfWorkMining(minerAddress string) {
guessHash := guessBlock.Hash()

if zeroes == guessHash[2:2+constants.MINING_DIFFICULTY] {
log.Println("Found solution")
log.Printf("Mining Difficulty is %d", constants.MINING_DIFFICULTY)
log.Printf("Hash Solution: %s", guessHash)
rewardTxn := NewTransaction(constants.BLOCKCHAIN_REWARD_ADDRESS, minerAddress, constants.MINING_REWARD, []byte{})
rewardTxn.Status = constants.STATUS_SUCCESS
guessBlock.Transactions = append(guessBlock.Transactions, rewardTxn)
bc.AddBlock(guessBlock)
log.Printf(bc.ToJSON(), "\n\n")
log.Printf("%s \n\n", bc.ToJSON())
nonce = 0
continue
}
Expand Down
2 changes: 1 addition & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const (
STATUS_SUCCESS = "success"
STATUS_FAILED = "failed"
STATUS_PENDING = "pending"
MINING_DIFFICULTY = 4
MINING_DIFFICULTY = 5
MINING_REWARD = 1200 * DECIMAL
CURRENCY_NAME = "gopher"
DECIMAL = 100
Expand Down

0 comments on commit 5c8a881

Please sign in to comment.