/
test.go
227 lines (176 loc) · 5.7 KB
/
test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"math/big"
"strconv"
"time"
log "github.com/sirupsen/logrus"
)
// proofOfWorkStruct - Used to get POW
// The targetHash works with difficulty to get number of leading 0s you want
type proofOfWorkStruct struct {
block *blockStruct
targetHash *big.Int
}
// blockStruct is your block
type blockStruct struct {
Index int `json:"index"`
Timestamp string `json:"timestamp"`
Transactions []transactionStruct `json:"transactions"`
Hash string `json:"hash"`
PrevHash string `json:"prevhash"`
Difficulty int `json:"difficulty"`
Nonce string `json:"nonce"`
}
var lockedBlock = blockStruct{}
// transactionStruct is your transaction - To be place in block
type transactionStruct struct {
ID int64 `json:"ID"`
Inputs []txInputStruct `json:"inputs"`
Outputs []txOutputStruct `json:"outputs"`
}
// txInput
type txInputStruct struct {
TXID int64 `json:"txID"`
ReferenceTXID int64 `json:"referenceTXID"`
Signature string `json:"signature"`
}
// txOutput - This is where the money is stored
type txOutputStruct struct {
Address string `json:"jeffCoinAddress"`
Value int64 `json:"value"`
}
func checkErr(err error) {
if err != nil {
fmt.Printf("Error is %+v\n", err)
log.Fatal("ERROR:", err)
}
}
// MINING ************************************************************************************************************
func mineLockedBlock() {
// GET THE BLOCK TO MINE - lockedBlock
// Already have lockedBlock
// LOAD POW STRUCT (With Block and targetHash)
targetHash := big.NewInt(1)
difficulty := lockedBlock.Difficulty
targetHash.Lsh(targetHash, uint(256-difficulty))
fmt.Printf("The target hash will have %d leading 0s\n", difficulty)
// powStruct := &proofOfWorkStruct{&lockedBlock, targetHash}
// MINE - FIND THE NONCE and return hash
foundNonce, hashHex, iterations := findTheNonce(&lockedBlock, targetHash)
fmt.Printf("The nonce is %d\n", foundNonce)
fmt.Printf("The hash is %v (%v bytes)\n", hashHex, len(hashHex))
fmt.Printf("This took %v iterations \n", iterations)
status := verifyTheNonce(&lockedBlock, targetHash, hashHex, foundNonce)
fmt.Println("Verification status", status)
fmt.Printf("The binary is\n")
// JUST THE BINARY OF THE BEGINING
//hashByte, _ := hex.DecodeString(hashHex)
for _, v := range hashHex {
ui, _ := strconv.ParseUint(string(v), 16, 64)
format := fmt.Sprintf("%%0%db", 4)
bin := fmt.Sprintf(format, ui)
//fmt.Printf("%d, %c, %s\n", i, v, bin)
fmt.Printf("%s", bin)
}
fmt.Println("")
// VALIDATE FOR FUN
data := prepareData(&lockedBlock, targetHash, foundNonce)
hashBytew := sha256.Sum256([]byte(data))
fmt.Printf("The nonce is %v\n", foundNonce)
fmt.Printf("The hash is %v\n", hex.EncodeToString(hashBytew[:]))
}
// FindTheNonce - Find the nonce so you have the correct number of leading 0s (difficulty)
func findTheNonce(block *blockStruct, targetHash *big.Int) (int, string, int) {
var hashInt big.Int
var hashByte [32]byte
nonce := 0
i := 0
// START AT nonce 0 and increment until you find the correct number of leading 0s
for nonce < math.MaxInt64 {
// GET DATA WITH INSERTED NONCE & HASH
data := prepareData(block, targetHash, nonce)
hashByte = sha256.Sum256([]byte(data))
fmt.Println(hex.EncodeToString(hashByte[:]))
// fmt.Printf("\n The nonce is: %v", nonce)
// fmt.Printf("\n The hash is: %x", hash)
// CHECK IF HASH HAS targetHash Leading 0 bits
hashInt.SetBytes(hashByte[:])
if hashInt.Cmp(targetHash) == -1 {
break
} else {
nonce++
}
i++
}
hashHex := hex.EncodeToString(hashByte[:])
return nonce, hashHex, i
}
func verifyTheNonce(block *blockStruct, targetHash *big.Int, verifyHash string, nonce int) bool {
// GET DATA WITH INSERTED NONCE & HASH
data := prepareData(block, targetHash, nonce)
hashByte := sha256.Sum256([]byte(data))
fmt.Println(hex.EncodeToString(hashByte[:]))
// HASH
hashByte = sha256.Sum256([]byte(data))
hashHex := hex.EncodeToString(hashByte[:])
// COMPARE HASH
if hashHex == verifyHash {
return true
}
return false
}
func prepareData(block *blockStruct, targetHash *big.Int, nonce int) string {
// GET ALL THE TRANSACTIONS/DATA
transactionBytes := []byte{}
for _, transaction := range block.Transactions {
transactionBytes, _ = json.Marshal(transaction)
}
// DATA YOU WANT TO HASH
data := strconv.Itoa(block.Index) + block.Timestamp + string(transactionBytes) + block.PrevHash + string(block.Difficulty) + strconv.Itoa(nonce)
return data
}
func main() {
t := time.Now()
JeffCoinAddress := "99dgh9999"
firstTransaction := `
{
"ID": 0,
"inputs": [
{
"txID": 0,
"referenceTXID": -1,
"signature": ""
}
],
"outputs": [
{
"jeffCoinAddress": "` + JeffCoinAddress + `",
"value": 1000
}
]
}`
// Place data in struct
firstTransactionByte := []byte(firstTransaction)
var theTransactionStruct transactionStruct
err := json.Unmarshal(firstTransactionByte, &theTransactionStruct)
checkErr(err)
// Place transaction in slice
var transactionSlice []transactionStruct
transactionSlice = append(transactionSlice, theTransactionStruct)
lockedBlock.Index = 0
lockedBlock.Timestamp = t.String()
lockedBlock.Transactions = transactionSlice
lockedBlock.PrevHash = lockedBlock.Hash
lockedBlock.Difficulty = 16
lockedBlock.Hash = ""
lockedBlock.Nonce = ""
// js, _ := json.MarshalIndent(lockedBlock, "", " ")
// s := string(js)
// fmt.Printf("The lockBlock is:\n%v", s)
mineLockedBlock()
}