Skip to content

Commit

Permalink
Add target to new blocks so we can mine
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbachmann committed Aug 13, 2017
1 parent 72c93e2 commit 72da9f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
"os/signal"
"sync"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 72da9f1

Please sign in to comment.