-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmining.sh
182 lines (125 loc) · 3.31 KB
/
mining.sh
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
#!/bin/bash
# Author: Daniel Lima
# TODO: the tx math is wrong.. not time left to work on it.
# Description: simple solo "mining" example
# setup
difficult=1
blocknumber=1 # genesis
alicebalance=40000
bobbalance=20000
# miner balance
minerjackbalance=0
# first transaction
amount=$RANDOM
blockchain="alice:$amount:bob\nalice:$alicebalance:alice"
# alice after first tx
alicebalance=$((alicebalance - amount))
# ------------------
nextBlockTimestamp() {
timeblock=$(date +%s)
}
nextBlockTimestamp
# genesis block
genesis=$(echo $blocknumber$blockchain$timeblock | shasum | cut -c1-32)
previousblockhash=$genesis
# ------------------
checkBalance() {
echo Alice balance: $alicebalance
echo Bob balance: $bobbalance
}
checkTx() {
# check balance
# global amount=$1
final_balance=$(( alicebalance - amount ))
if [ $final_balance -lt 0 ]
then
echo 1 #out of money
else
echo 0
fi
}
# update balances
sendTx() {
# amount=$2
if [ $( checkTx $amount ) -eq 0 ]; then
alicebalance=$(( alicebalance - amount ))
bobbalance=$(( bobbalance + amount ))
return 0
else
return 1
fi
}
# increase 50% of the rounds
increaseDiff() {
if [ $(( $RANDOM % 2 )) = 0 ] ; then
echo
difficult=$(( difficult + 1 ))
echo -n Increasing difficulty to $difficult ...
sleep 1
echo Done.
fi
}
# 0 00 000 0000 ..
increaseChallenge() {
echo -n Setting up new challenge..
challenge=$(head -c $difficult < /dev/zero | tr '\0' '0')
sleep 1
echo Done.
}
# ------------------
echo
echo "======================================"
echo " Basic cryptomining script "
echo "======================================"
echo
increaseChallenge
mineit() {
echo Starting ..
for i in {0..100000}
do
# SHA256(index + previousHash + timestamp + data + nonce)
hashres=$(echo -n $blocknumber$previousblockhash$timeblock$blockchain$i | shasum | cut -c1-32)
echo "Mine attempt $i (nonce): $hashres"
if [[ ${challenge} == ${hashres:0:$difficult} ]]
then
echo
echo "======================================"
echo " BLOCK $blocknumber MINED! "
echo "======================================"
blocknumber=$(( blocknumber + 1 ))
echo Block found: $hashres
echo
echo Previous blockhash: $previousblockhash
echo Genesis block: $genesis
echo Difficulty: $difficult
echo Timestamp: $timeblock
echo
echo -e "Transactions:\n$blockchain"
echo
nextBlockTimestamp
increaseDiff
increaseChallenge
amount=$RANDOM
echo $amount ...
checkBalance
echo Alice sending $amount to Bob...
if [[ $( sendTx $TX $amount ) -eq 0 ]]
then
echo $amount ...
TX="alice:$amount:bob\nalice:$((alicebalance - amount)):alice"
# update blockchain
blockchain=$blockchain"\n"$TX
fi
echo
/bin/sleep 2
previousblockhash=$hashres
break
fi
done
}
rounds=4
while [ $rounds -gt 0 ]
do
mineit
(( rounds-- ))
done