-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
251 lines (218 loc) · 6.2 KB
/
block.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// (block.go) - Contains the Block struct and Block commands in ./mfc
// Copyright (C) 2021 MaxflowO2, the only author of Max Flow Chain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"math"
"math/big"
"time"
"bytes"
"io/ioutil"
"encoding/hex"
"encoding/json"
"github.com/MaxflowO2/mfc/K12"
// "github.com/boltdb/bolt"
)
// Block struct {}
// Used throughout code
type Block struct {
Timestamp int64
Transactions []*Transaction
PrevBlockHash []byte
Hash []byte
Nonce int
Height int
Difficulty int
HashBy string
Signed []byte
}
// NewBlock(trans *Transaction, prevBlockHash []Byte)
// Adds Timestamp, *Transaction, BlockHash, Hash, Nonce to *Block{}
// Preforms PoW
// Returns *Block{}
func NewBlock(trans []*Transaction, lastHash []byte, lastHeight int, newTarget int) *Block {
block := &Block{time.Now().Unix(), trans, lastHash, []byte{}, 0, lastHeight, newTarget, LoadAddress(), []byte{}}
pow := NewProofOfWork(block)
nonce, hash, diff := pow.Run()
lastHeight++
block.Height = lastHeight
block.Hash = hash
block.Nonce = nonce
block.Difficulty = diff
block.Signed = Sign(block.Hash)
block.ToFile()
return block
}
// NewGenesisBlock()
// Obviously the start
// **Future Update** Genesis struct{} and pass gen Genesis{} to this
// Why? For --alphanet --betanet --mainnet (ect ect) from .JSON file
// Returns *Block{}
func NewGenesisBlock() *Block {
var genesis []*Transaction
return NewBlock(genesis, []byte{}, 0, 16)
}
func AlphaGenesisBlock() *Block {
var alphaTrans []*Transaction
theOne := AlphaGenesis()
alphaTrans = append(alphaTrans, theOne)
alpha := &Block{1623289682, alphaTrans, []byte{}, []byte{}, 0, 1, 0, "", []byte{}}
// New K12.Sum256 values
alpha.Hash = []byte{0, 0, 131, 168, 228, 219, 228, 184, 223, 179, 126, 55, 55, 36, 55, 171, 23, 131, 204, 236, 181, 229, 18, 188, 113, 30, 105, 184, 71, 38, 246, 130}
alpha.Nonce = 62078
alpha.Difficulty = 16
// Old sha3.Sum256 values
//alpha.Hash = []byte{0, 0, 91, 237, 75, 239, 186, 156, 203, 254, 5, 66, 134, 202, 179, 200, 24, 123, 177, 62, 127, 223, 166, 39, 79, 139, 178, 237, 146, 253, 100, 214}
//alpha.Nonce = 55995
//alpha.Difficulty = 16
alpha.ToFile()
return alpha
}
// PoW functions below
//const targetBits = 16
// Variable set throughout pow.go
var (
maxNonce = math.MaxInt64
)
// ProofOfWork {} struct
// Used for pow functions below
type ProofOfWork struct {
block *Block
target *big.Int
}
// NewProofOfWork(b *Block)
// Uses targetBits to set difficulty
// Returns *ProofOfWork{}
func NewProofOfWork(b *Block) *ProofOfWork {
target := big.NewInt(1)
newTargetBits := b.Difficulty
target.Lsh(target, uint(256-newTargetBits))
pow := &ProofOfWork{b, target}
return pow
}
// This is a bullshit version of MerkleRoot.
// sliceHash([]*Trans)
// Gets the bytes of all hashes, in []*Transaction
// Returns Sha3.Sum256 of []*Transaction
func (pow *ProofOfWork) sliceHash() []byte {
max := len(pow.block.Transactions)
var temp *Transaction
var toHash []byte
for i := 0; i < max; i++ {
temp = pow.block.Transactions[i]
toHash = bytes.Join(
[][]byte{
toHash,
temp.Hash,
},
[]byte{},
)
// add a "toblock bool" - add to struct
// add a "database addition" - Main database
// add a "database withdrawl" - Mempool
}
resultpre := K12.Sum256(toHash)
result := resultpre[:]
return result
}
// pow.prepareData(nonce int)
// Joins block data into []byte
// Returns []byte
func (pow *ProofOfWork) prepareData(nonce int, mroot []byte) []byte {
data := bytes.Join(
[][]byte{
pow.block.PrevBlockHash,
mroot,
IntToHex(pow.block.Timestamp),
IntToHex(int64(pow.block.Difficulty)),
IntToHex(int64(nonce)),
[]byte(pow.block.HashBy),
},
[]byte{},
)
return data
}
// pow.Run()
// Preforms Sha3.Sum256 hash of block data
// Returns Nonce, Hash
func (pow *ProofOfWork) Run() (int, []byte, int) {
var hashInt big.Int
var hash []byte
nonce := 0
mroot := pow.sliceHash()
// fmt.Printf("Mining the block containing:\n \"%v\"\n", pow.block.Transactions)
for nonce < maxNonce {
data := pow.prepareData(nonce, mroot)
hash = K12.Sum256(data)
// fmt.Printf("\r%x", hash)
hashInt.SetBytes(hash)
if hashInt.Cmp(pow.target) == -1 {
break
} else {
nonce++
}
}
fmt.Printf("Block #%v mined\n\n", pow.block.Height)
return nonce, hash[:], pow.block.Difficulty
}
// pow.Validate()
// Validates Hash
// Returns bool
func (pow *ProofOfWork) Validate() bool {
var hashInt big.Int
data := pow.prepareData(pow.block.Nonce, pow.sliceHash())
hash := K12.Sum256(data)
hashInt.SetBytes(hash)
isValid := hashInt.Cmp(pow.target) == -1
return isValid
}
// Bolt.DB functions
// b.ToFile()
// Saves block to alpha/block as (Hash).block
func (b *Block) ToFile() {
header := "alpha/block/"
dotblock := ".block"
filename := header + hex.EncodeToString(b.Hash) + dotblock
file, err := json.MarshalIndent(b, "", " ")
if err != nil {
fmt.Errorf("%s did not Marshal\n", filename)
}
err = ioutil.WriteFile(filename, file, 0644)
if err != nil {
fmt.Errorf("%s did not save\n", filename)
}
}
// b.Serialize()
// Serialized block for Bolt.DB
// Returns []byte
func (b *Block) Serialize() []byte {
value, err := json.Marshal(b)
if err != nil {
fmt.Errorf("%v did not Marshal\n", b)
}
return value
}
// DeserializeBlock(d []byte)
// Deserialize a block
// Returns *Block
func DeserializeBlock(d []byte) *Block {
var block Block
err := json.Unmarshal(d, &block)
if err != nil {
fmt.Errorf("Could not Unmarshal\n")
}
return &block
}