This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
proofofblockstake.go
195 lines (168 loc) · 5.94 KB
/
proofofblockstake.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
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
"math/big"
"math/rand"
"strconv"
"time"
//"io"
)
var BLOCKDELAY = 2000 //to calculate the stakemodifier
var PERIOD = 60 //period time in seconds on average between two blocks
var DIFF_REFRESH = 500 //number of blocks period to refresh before refresh the diff
var DIFF_STRENGTH = 500
type hash [32]byte
type blockchainitem struct {
blockhash hash
timestamp int64
}
type UTXO struct {
blockHeight uint32
index uint32
amount uint64
}
type wallet struct {
UTXOs []UTXO
}
type BSParameters struct {
difficulty big.Int
stakemodifier hash
}
func main() {
//test the functions
var bsparameters BSParameters
timestamp := time.Now().Unix()
blockchain := CalcGenesisBlocks(timestamp)
bsparameters.UpdateStakeModifier(blockchain)
const NUMOFBCN = 20
var Wallets [NUMOFBCN]wallet
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
NumOfHashes := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil)
var TotalBS uint64
numof := 0
for j := 0; j < NUMOFBCN; j++ {
numin := r.Intn(5) + 1
for i := 0; i < numin; i++ {
newUTXO := UTXO{amount: uint64(r.Intn(100) * 100), index: uint32(numof), blockHeight: uint32(0)}
Wallets[j].UTXOs = append(Wallets[j].UTXOs, newUTXO)
numof++
TotalBS += Wallets[j].UTXOs[i].amount
}
}
fmt.Println("TotalBS", TotalBS)
StartDifficulty := *new(big.Int).Div(new(big.Int).Div(NumOfHashes, big.NewInt(int64(PERIOD))), big.NewInt(int64(TotalBS)))
bsparameters.difficulty = StartDifficulty
counter := 0
blockhigh := 0
for i := 0; i < (3600 * 24 * 300); i++ {
blockfound := false
numofbcns := NUMOFBCN
if i > (120000) {
// numofbcns -= 5
}
if i > (500000) {
// numofbcns -= 10
}
TotalBS = 0
for j := 0; j < numofbcns; j++ {
winner := Wallets[j].CheckWinner(timestamp+int64(i), bsparameters)
for k := 0; k < len(Wallets[j].UTXOs); k++ {
TotalBS += Wallets[j].UTXOs[k].amount
}
if winner >= 0 {
//fmt.Println("BCN", j, "won with UTXO :", winner)
Wallets[j].UTXOs[winner].index = 3
Wallets[j].UTXOs[winner].blockHeight = uint32(blockhigh)
blockhigh++
blockfound = true
}
}
if blockfound {
var buffer bytes.Buffer
buffer.WriteString("random" + strconv.Itoa(i))
blockchain = append(blockchain, blockchainitem{sha256.Sum256(buffer.Bytes()), timestamp + int64(i)})
bsparameters.UpdateStakeModifier(blockchain)
//fmt.Println("block found in second: ", i)
counter++
if counter > DIFF_STRENGTH && counter%DIFF_REFRESH == 0 {
bsparameters.UpdateDifficulty(NumOfHashes, PERIOD, DIFF_STRENGTH, blockchain)
fmt.Println(counter, ",", i/PERIOD, ",", i/PERIOD-counter, ",", TotalBS)
}
}
}
fmt.Println("num of blocks found", counter, "not unique", blockhigh)
}
//calculate the stakemodifier out of the blockchain. automatically takes the last block as reference.
func (b *BSParameters) UpdateStakeModifier(blockchain []blockchainitem) {
if len(blockchain) < (BLOCKDELAY + 255) {
panic("blockchain is not long enough")
}
j := len(blockchain) - BLOCKDELAY
for i := 0; i < 256; i++ {
if i%8 == 0 {
b.stakemodifier[i/8] = 0
}
b.stakemodifier[i/8] += blockchain[j-i].blockhash[i/8] & (1 << (uint)(i%8))
}
}
func (b *BSParameters) UpdateDifficulty(NumOfHashes *big.Int, period int, diffstrenght int, blockchain []blockchainitem) {
if len(blockchain) < (diffstrenght) {
panic("blockchain is not long enough")
}
diff := blockchain[len(blockchain)-1].timestamp - blockchain[len(blockchain)-diffstrenght-1].timestamp
totalBScalc := *new(big.Int).Div(new(big.Int).Div(new(big.Int).Mul(NumOfHashes, big.NewInt(int64(diffstrenght))), big.NewInt(int64(diff))), &b.difficulty)
b.difficulty = *new(big.Int).Div(NumOfHashes, new(big.Int).Mul(&totalBScalc, big.NewInt(int64(period))))
Ratio := big.NewRat(diff, int64(period*diffstrenght))
// b.difficulty = *new(big.Int).Div(new(big.Int).Mul(&b.difficulty, big.NewInt(diff)), new(big.Int).Mul(big.NewInt(int64(period)), big.NewInt(int64(diffstrenght))))
// totalBScalc := *new(big.Int).Div(new(big.Int).Div( , big.NewInt(int64(period))), &b.difficulty)
fmt.Print(totalBScalc.String())
fmt.Print(",", Ratio.RatString(), ",", float64(diff)/float64(period)/float64(diffstrenght), ",")
}
//checks if this wallet has a winner for this timestamp
//returns -1 if no winner , otherwise winner is the index of the utxo who won with the biggest amount
func (w *wallet) CheckWinner(timestamp int64, bsparam BSParameters) int {
var biggestamoutwon uint64
var result int
result = -1
for index, utxo := range w.UTXOs {
if BlockCreation(bsparam.stakemodifier, utxo.blockHeight, utxo.index, timestamp, utxo.amount, &bsparam.difficulty) {
if biggestamoutwon < utxo.amount {
result = index
biggestamoutwon = utxo.amount
}
}
}
return result
}
//calculates the blockcreation criterium, returns true if the criterium is met.
func BlockCreation(stakemodifier hash, blocknumber uint32, indexutxo uint32, timestamp int64, stakeamout uint64, difficulty *big.Int) bool {
if stakeamout == 0 {
return false
}
k := new(bytes.Buffer)
binary.Write(k, binary.LittleEndian, blocknumber)
index := new(bytes.Buffer)
binary.Write(index, binary.LittleEndian, indexutxo)
ts := new(bytes.Buffer)
binary.Write(ts, binary.LittleEndian, timestamp)
dat := append(append(append(stakemodifier[:], k.Bytes()...), index.Bytes()...), ts.Bytes()...)
hash := sha256.Sum256(dat)
res := new(big.Int).Div(new(big.Int).SetBytes(hash[:]), big.NewInt(int64(stakeamout)))
if res.Cmp(difficulty) < 0 {
return true
}
return false
}
//calculates the hashes of the blocks before the genesis block in a deterministic way.
func CalcGenesisBlocks(timestamp int64) []blockchainitem {
var blockchain []blockchainitem
for block := -(BLOCKDELAY + 255); block < 1; block++ {
var buffer bytes.Buffer
buffer.WriteString("genesis" + strconv.Itoa(block))
blockchain = append(blockchain, blockchainitem{sha256.Sum256(buffer.Bytes()), timestamp})
}
return blockchain
}