-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.sh
134 lines (108 loc) · 3.69 KB
/
sim.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
#!/bin/bash
# Set the threshold for the line count
THRESHOLD=201
MINING_START_THRESHOLD=1
NUMBER_OF_SIMULATIONS=100
NUMBER_OF_MACHINES_RUNNING=37
SLEEP_BEFORE_MINING=20
TIME_BETWEEN_TESTS=20
CHECK_INTERVAL=0.1
GOQUAI_PATH=~/go-quai
MINER_PATH=~/quai-cpu-miner
# Function to call the first API and get the block number
getBlockNumber() {
RESPONSE=$(curl -s --location 'http://localhost:8610/' \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"method": "quai_blockNumber",
"params": [],
"id": 1
}')
echo $RESPONSE | jq -r '.result'
}
getBlockByNumber() {
BLOCK_NUMBER=$1
RESPONSE=$(curl -s --location 'http://localhost:8610/' \
--header 'Content-Type: application/json' \
--data "{
\"jsonrpc\": \"2.0\",
\"method\": \"quai_getBlockByNumber\",
\"params\": [\"$BLOCK_NUMBER\", true],
\"id\": 1
}")
echo $RESPONSE | jq -r '.result'
}
# Clean the database and nodelogs before starting the Simulation
# clean the db and nodelogs
echo "Removing the Database and the nodelogs"
pkill -9 quai
rm -rf $GOQUAI_PATH/nodelogs
# create the static files
cp -r quai ~/.quai
# start go-quai
make run
sleep $SLEEP_BEFORE_MINING
cd $MINER_PATH && make run-mine-background region=0 zone=0
cd $GOQUAI_PATH
start=1
end=$NUMBER_OF_SIMULATIONS
# Main loop to run the experiment 100 times
for i in $(seq $start $end); do
> simulations/$i.csv
while true; do
blockNumber=$(($(getBlockNumber)))
# Check if the count has reached the threshold
if [ "$blockNumber" -ge "$THRESHOLD" ]; then
# Call the APIs and store the block number
echo "Simulation, $i, Num of Nodes, $NUMBER_OF_MACHINES_RUNNING" >> simulations/$i.csv
echo "Block Height, Block Hash, Time Stamp, Received Time Stamp, Append Time, Extra Bits" >> simulations/$i.csv
###### Stop, clear data, and restart the process #####
echo "Stop the Miner"
# stop the miner
cd $MINER_PATH && make stop
echo "Miner stopped"
# stop the node
echo "Stop the Node"
cd $GOQUAI_PATH
start=1
end=$THRESHOLD
for j in $(seq $start $end); do
index_hex=$(printf "0x%x" $j)
block_j=$(getBlockByNumber "$index_hex")
timestamp=$(($(echo $block_j | jq -r '.timestamp')))
hash=$(echo $block_j | jq -r '.hash')
entropy=$(echo $block_j | jq -r '.extraBits')
receivedtime=$(cat nodelogs/zone-0-0.log | grep 'Appended' | grep $hash | awk '{print $21}' | awk -F= '{print $2}')
appendtime=$(cat nodelogs/zone-0-0.log | grep 'Appended' | grep $hash | awk '{print $20}' | awk -F= '{print $2}')
echo $j, $hash, $timestamp, $receivedtime, $appendtime, $entropy >> simulations/$i.csv
done
sleep 5
make stop
echo "Node stopped"
# clean the db and nodelogs
echo "Removing the Database and the nodelogs"
rm -rf ~/.quai $GOQUAI_PATH/nodelogs
# create the static files
cp -r quai ~/.quai
echo "Sleep for $TIME_BETWEEN_TESTS"
# SLEEP BEFORE NEXT TEST
sleep $TIME_BETWEEN_TESTS
echo "Start Node"
make run
sleep $SLEEP_BEFORE_MINING
echo "Starting the Miner on 0-0"
cd $MINER_PATH && make run-mine-background region=0 zone=0
echo "Going back to go-quai"
cd $GOQUAI_PATH
break
else
sleep $CHECK_INTERVAL
fi
done
done
cd $MINER_PATH
make stop
cd $GOQUAI_PATH
make stop
pkill -f ./sim.sh