-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddblock.sh
More file actions
executable file
·58 lines (44 loc) · 1.75 KB
/
addblock.sh
File metadata and controls
executable file
·58 lines (44 loc) · 1.75 KB
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
#!/bin/zsh
# Read the number from the file
number=$(ls -1 ./blk | wc -l | tr -d '\ ')
# Print the number
echo "Adding Block: $number"
# Set the target difficulty for the block
# this sets the highest value of the hash
difficulty="000000FFFFFF0000000000000000000000000000000000000000000000000000"
# Create a temporary file to add to the blockchain
file=$1:t
TMPFILE=`mktemp -q /tmp/$file.XXXXXX`
if [ $? -ne 0 ]; then
echo "$0: Can't create temp file, exiting..."
exit 1
fi
filename=$TMPFILE:t
# add the prior block hash to the new proposed blcok
echo -n "<block>\n<number>" >> $TMPFILE
echo -n $number >> $TMPFILE
echo -n "</number>\n" >> $TMPFILE
# add the timestamp to the proposed block file
echo -n "<timestamp>" >> $TMPFILE
date +%s | tr -d '\n'>> $TMPFILE
echo -n "</timestamp>\n" >> $TMPFILE
# add the prior block hash to the proposed block file
echo -n "<priorBlockHash>\n<hash>" >> $TMPFILE
tail -n1 chain | cut -d ' ' -f 1|tr -d '\n' >> $TMPFILE
echo -n "</hash>\n<file>" >> $TMPFILE
tail -n1 chain | cut -d ' ' -f 3|tr -d '\n' >> $TMPFILE
echo "<file/>\n</priorBlockHash>" >> $TMPFILE
# add the sha256 sum of the contents to the proposed block file
echo -n "<contentHash>" >> $TMPFILE
shasum -a 256 $1 | cut -d ' ' -f1 |tr -d '\n' >> $TMPFILE
echo -n "</contentHash>\n</block>\n" >> $TMPFILE
# add the contents of the file to the proposed block file
# to improve performance, we will not add the contents of the file
#echo -n "<fileContents>" >> $TMPFILE
#cat $1 >> $TMPFILE
#echo "</fileContents>" >> $TMPFILE
# Mine the block and output to the block directory
echo ./simpleMiner -i $TMPFILE -f $difficulty -o ./blk/$number.blk
./simpleMiner -i $TMPFILE -f $difficulty -o ./blk/$number.blk
#Add the block to the chain
shasum -a 256 ./blk/$number.blk >> chain