From 72da9f152a5e7744fdd6129441de02f146e48b83 Mon Sep 17 00:00:00 2001 From: Bruno Bachmann Date: Sun, 13 Aug 2017 15:35:53 -0700 Subject: [PATCH] Add target to new blocks so we can mine --- app/app.go | 12 +++++++++++- miner/miner.go | 3 +-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/app.go b/app/app.go index 1ada502..68f75fc 100644 --- a/app/app.go +++ b/app/app.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "math/big" "os" "os/signal" "sync" @@ -13,6 +14,7 @@ import ( "github.com/google/uuid" "github.com/ubclaunchpad/cumulus/blockchain" + "github.com/ubclaunchpad/cumulus/common/constants" "github.com/ubclaunchpad/cumulus/conf" "github.com/ubclaunchpad/cumulus/conn" "github.com/ubclaunchpad/cumulus/consensus" @@ -243,8 +245,12 @@ func createBlockchain(user *User) *blockchain.BlockChain { Head: blockchain.NilHash, } + // TODO: update when we have adjustable difficulty + target := new(big.Int).Div(constants.MaxTarget, big.NewInt(2<<24)) + targetHash := blockchain.BigIntToHash(target) + genesisBlock := blockchain.Genesis(user.Wallet.Public(), - consensus.CurrentTarget(), consensus.StartingBlockReward, []byte{}) + targetHash, consensus.StartingBlockReward, []byte{}) bc.AppendBlock(genesisBlock) return &bc @@ -323,7 +329,11 @@ func (a *App) RunMiner() { // Make a new block form the transactions in the transaction pool blockToMine := a.Pool.NextBlock(a.Chain, a.CurrentUser.Wallet.Public(), a.CurrentUser.BlockSize) + blockToMine.Target = a.Chain.Blocks[0].Target + + // TODO: update this when we have adjustable difficulty miningResult := miner.Mine(a.Chain, blockToMine) + if miningResult.Complete { log.Info("Sucessfully mined a block!") push := msg.Push{ diff --git a/miner/miner.go b/miner/miner.go index 7d94401..9518c1e 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -37,8 +37,7 @@ func RestartMiner(bc *blockchain.BlockChain, b *blockchain.Block) { } // Mine continuously increases the nonce and tries to verify the proof of work -// until the puzzle is solved -// TODO: Make Mine take an interface with a callback as an arguement. +// until the puzzle is solved. func Mine(bc *blockchain.BlockChain, b *blockchain.Block) *MiningResult { setStart()