forked from JoeWeate/Cryptocurrecny
/
miner.go
83 lines (64 loc) · 2.17 KB
/
miner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// miner.go
// Staring template for PointCoint miner.
//
// cs4501: Cryptocurrency Cafe
// University of Virginia, Spring 2015
// Project 2
//
package main
import (
"log"
"github.com/PointCoin/btcjson"
"github.com/PointCoin/btcutil"
)
const (
// This should match your settings in pointcoind.conf
rpcuser = "joeyweate"
rpcpass = "pointcoin"
// This file should exist if pointcoind was setup correctly
cert = "/home/ubuntu/.pointcoind/rpc.cert"
)
func main() {
// Setup the client using application constants, fail horribly if there's a problem
client := setupRpcClient(cert, rpcuser, rpcpass)
for { // Loop forever (you may want to do something smarter!)
// Get a new block template from pointcoind.
log.Printf("Requesting a block template\n")
template, err := client.GetBlockTemplate(&btcjson.TemplateRequest{})
if err != nil {
log.Fatal(err)
}
// The template returned by GetBlockTemplate provides these fields that
// you will need to use to create a new block:
// The hash of the previous block
prevHash := template.PreviousHash
// The difficulty target
difficulty := formatDiff(template.Bits)
// The height of the next block (number of blocks between genesis block and next block)
height := template.Height
// The transactions from the network
txs := formatTransactions(template.Transactions)
// These are configurable parameters to the coinbase transaction
msg := "jfw6fp" // replace with your UVa Computing ID (e.g., "dee2b")
a := "PsVydhvYAcNBPK6Hz51UanuMUsqCJxFkVr" // replace with the address you want mining fees to go to (or leave it like this and Nick gets them)
coinbaseTx := CreateCoinbaseTx(height, a, msg)
txs = prepend(coinbaseTx.MsgTx(), txs)
merkleRoot := createMerkleRoot(txs)
// Finish the miner!
var nonce uint32 = 0
block := CreateBlock(prevHash, merkleRoot, difficulty, nonce, txs)
for i :=0; i < 5000000; i++ {
var hash, _ = block.Header.BlockSha()
if lessThanDiff(hash, difficulty) {
err := client.SubmitBlock(btcutil.NewBlock(block), nil)
if err != nil {
break
}
log.Printf("You got a block!\n")
break
}
block.Header.Nonce += 1
}
}
}