forked from febe19/bazo-miner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproofofstake.go
208 lines (170 loc) · 5.75 KB
/
proofofstake.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
package miner
import (
"bytes"
"encoding/binary"
"errors"
"github.com/julwil/bazo-miner/crypto"
"sync"
"time"
"github.com/julwil/bazo-miner/protocol"
"github.com/julwil/bazo-miner/storage"
"golang.org/x/crypto/sha3"
)
var validateMutex = sync.Mutex{}
//Tests whether the first diff bits are zero
func validateProofOfStake(diff uint8,
prevProofs [][crypto.COMM_PROOF_LENGTH]byte,
height uint32,
balance uint64,
commitmentProof [crypto.COMM_PROOF_LENGTH]byte,
timestamp int64) bool {
validateMutex.Lock()
defer validateMutex.Unlock()
var (
heightBuf [4]byte
timestampBuf [8]byte
hashArgs []byte
)
// allocate memory
// n * COMM_PROOF_LENGTH bytes (prevProofs) + COMM_PROOF_LENGTH bytes (commitmentProof)+ 4 bytes (height) + 8 bytes (count)
hashArgs = make([]byte, len(prevProofs)*crypto.COMM_PROOF_LENGTH+crypto.COMM_PROOF_LENGTH+4+8)
binary.BigEndian.PutUint32(heightBuf[:], height)
binary.BigEndian.PutUint64(timestampBuf[:], uint64(timestamp))
index := 0
for _, prevProof := range prevProofs {
copy(hashArgs[index:index+crypto.COMM_PROOF_LENGTH], prevProof[:])
index += crypto.COMM_PROOF_LENGTH
}
copy(hashArgs[index:index+crypto.COMM_PROOF_LENGTH], commitmentProof[:]) // COMM_KEY_LENGTH bytes
index += crypto.COMM_PROOF_LENGTH
copy(hashArgs[index:index+4], heightBuf[:]) // 4 bytes
index += 4
copy(hashArgs[index:index+8], timestampBuf[:])
//calculate the hash
pos := sha3.Sum256(hashArgs[:])
data := binary.BigEndian.Uint64(pos[:])
data = data / balance
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, data)
copy(pos[0:32], buf.Bytes())
var byteNr uint8
//Bytes check
for byteNr = 0; byteNr < (uint8)(diff/8); byteNr++ {
if pos[byteNr] != 0 {
return false
}
}
//Bits check
if diff%8 != 0 && pos[byteNr] >= 1<<(8-diff%8) {
return false
}
return true
}
//diff and partialHash is needed to calculate a valid PoS, prevHash is needed to check whether we should stop
//PoS calculation because another block has been validated meanwhile
func proofOfStake(diff uint8,
prevHash [32]byte,
prevProofs [][crypto.COMM_PROOF_LENGTH]byte,
height uint32,
balance uint64,
commitmentProof [crypto.COMM_PROOF_LENGTH]byte) (int64, error) {
var (
pos [32]byte
byteNr uint8
abort bool
timestampBuf [8]byte
heightBuf [4]byte
timestamp int64
hashArgs []byte
)
// allocate memory
// n * COMM_KEY_LENGTH bytes (prevProofs) + COMM_KEY_LENGTH bytes (localCommPubKey)+ 4 bytes (height) + 8 bytes (count)
hashArgs = make([]byte, len(prevProofs)*crypto.COMM_PROOF_LENGTH+crypto.COMM_PROOF_LENGTH+4+8)
binary.BigEndian.PutUint32(heightBuf[:], height)
// all required parameters are concatenated in the following order:
// ([PrevProofs] ⋅ CommitmentProof ⋅ CurrentBlockHeight ⋅ Seconds)
index := 0
for _, prevProof := range prevProofs {
copy(hashArgs[index:index+crypto.COMM_PROOF_LENGTH], prevProof[:])
index += crypto.COMM_PROOF_LENGTH
}
copy(hashArgs[index:index+crypto.COMM_PROOF_LENGTH], commitmentProof[:]) // COMM_KEY_LENGTH bytes
index += crypto.COMM_PROOF_LENGTH
copy(hashArgs[index:index+4], heightBuf[:]) // 4 bytes
index += 4
timestampBufIndexStart := index
timestampBufIndexEnd := index + 8
cnt := 0
for range time.Tick(time.Second) {
// lastBlock is a global variable which points to the last block. This check makes sure we abort if another
// block has been validated
cnt = cnt + 1
logger.Printf("Try Block with Time: %v and cnt: %v", time.Now().Format("030405"), cnt)
//If 30 blocks should have been received, break
if cnt >= 30*BLOCK_INTERVAL {
logger.Printf("Mined %v sec and no block validated...? --> Strange... ", 30*BLOCK_INTERVAL)
return -1, errors.New("Abort mining, Mined too long")
}
if lastBlock == nil {
lastBlock = storage.ReadLastClosedBlock()
}
if lastBlock == nil {
return -1, errors.New("Abort mining, No Last Block Found")
}
if prevHash != lastBlock.Hash {
//Error code -2 initiates that probably a aggTx Should be deleted from open storage.
logger.Printf("Abort mining, another block has been successfully validated in the meantime --> LastBlock: %x", lastBlock.Hash[0:8])
return -2, errors.New("Abort mining, another block has been successfully validated in the meantime:")
}
abort = false
//add the number of seconds that have passed since the Unix epoch (00:00:00 UTC, 1 January 1970)
timestamp = time.Now().Unix()
binary.BigEndian.PutUint64(timestampBuf[:], uint64(timestamp))
copy(hashArgs[timestampBufIndexStart:timestampBufIndexEnd], timestampBuf[:]) //8 bytes
//calculate the hash
pos = sha3.Sum256(hashArgs[:])
//divide the hash by the balance (should not happen but possible in a testing environment)
data := binary.BigEndian.Uint64(pos[:])
if balance == 0 {
return -1, errors.New("Zero division: Account owns 0 coins.")
}
data = data / balance
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, data)
copy(pos[0:32], buf.Bytes())
//Byte check
for byteNr = 0; byteNr < (uint8)(diff/8); byteNr++ {
if pos[byteNr] != 0 {
//continue begins the next iteration of the innermost
abort = true
break
}
}
if abort {
continue
}
//Bit check
if diff%8 != 0 && pos[byteNr] >= 1<<(8-diff%8) {
continue
}
break
}
cnt = 0
return timestamp, nil
}
func GetLatestProofs(n int, block *protocol.Block) (prevProofs [][crypto.COMM_PROOF_LENGTH]byte) {
for block.Height > 0 && n > 0 {
//try to read block from 'closedblocks' and 'closedblockswithouttx' bucket.
closedBlock := storage.ReadClosedBlock(block.PrevHash)
if closedBlock == nil {
closedBlock = storage.ReadClosedBlockWithoutTx(block.PrevHashWithoutTx)
}
if closedBlock == nil {
return
}
prevProofs = append(prevProofs, closedBlock.CommitmentProof)
n -= 1
block = closedBlock
}
return prevProofs
}