Skip to content

Commit

Permalink
add scripts for test AH API methods (get_history_account, get_ops_in_…
Browse files Browse the repository at this point in the history
…block)
  • Loading branch information
mkochanowicz authored and mvandeberg committed Mar 21, 2018
1 parent 67c577c commit a8cd243
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/scripts/create_account_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3
"""
Create list of all steem accounts in file.
Usage: create_account_list.py <server_address> <output_filename>
"""
import sys
import json
import requests

def main():
if len( sys.argv ) != 3:
exit( "Usage: create_account_list.py <server_address> <output_filename>" )

url = sys.argv[1] + "/rpc"
print( url )
filename = sys.argv[2]

try: file = open( filename, "w" )
except: exit( "Cannot open file " + filename )

headers = { 'content-type': 'application/json' }
last_account = ""
end = False
accounts_count = 0

while end == False:
request = {
"jsonrpc": "2.0",
"id": 0,
"method": "database_api.list_accounts",
"params": { "start": last_account, "limit": 1000, "order": "by_name" }
}

try:
response = requests.post( url, data=json.dumps(request), headers=headers).json()

accounts = response["result"]["accounts"]
except:
print( "rpc failed for last_account: " + last_account )
print( response )
end = True
continue

if last_account != "":
assert accounts[0]["name"] == last_account
del accounts[0]

if len( accounts ) == 0:
end = True
continue

last_account = accounts[-1]["name"]
accounts_count += len( accounts )
for account in accounts:
file.write( account["name"] + "\n" )

# while end == False
file.close()
print( str(accounts_count) + " accounts")


if __name__ == "__main__":
main()
126 changes: 126 additions & 0 deletions tests/scripts/test_ah_get_account_history.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/bash

if [ $# -ne 6 ]
then
echo Usage: jobs 1st_address 1st_port 2nd_address 2nd_port working_dir
echo Example: 100 127.0.0.1 8090 ec2-34-235-166-184.compute-1.amazonaws.com 8090 logs
exit -1
fi

JOBS=$1
NODE1=http://$2:$3
NODE2=http://$4:$5
WDIR=$6

rm -fr $WDIR
mkdir $WDIR

./create_account_list.py $NODE1 $WDIR/accounts

EXTRACT_RESULT="python -c \
'import sys, json; \
response=json.load(sys.stdin); \
result=response[\"result\"]; \
print(json.dumps(result, sort_keys=True, indent=2))'"

ERRORS=0

# account
get_account_history ()
{
echo Comparing account history for $account

local account=$1
local START=-1
local LIMIT=10000

while true; do

local GET_AH="curl -s --data \
\"{ \\\"jsonrpc\\\": \\\"2.0\\\", \\\"id\\\": 0, \
\\\"method\\\": \\\"account_history_api.get_account_history\\\", \
\\\"params\\\": { \\\"account\\\": \\\"$account\\\", \\\"start\\\": $START, \\\"limit\\\": $LIMIT } }\""

for tries in {1..10}
do
local JSON1=$(eval $GET_AH $NODE1)
if [[ $? -eq 0 && $JSON1 != "" ]]; then
JSON1=$(echo $JSON1 | python -c \
'import sys, json; \
response=json.load(sys.stdin); \
result=response["result"]; \
print(json.dumps(result, sort_keys=True, indent=2))')
if [[ $? -eq 0 && $JSON1 != "" ]]; then
break
fi
fi
done
[[ $? -ne 0 || $JSON1 == "" ]] && ((ERRORS++)) && echo ERROR: Failed to get history account for account from node $NODE1 >$WDIR/$account && return

for tries in {1..10}
do
local JSON2=$(eval $GET_AH $NODE2)
if [[ $? -eq 0 && $JSON2 != "" ]]; then
JSON2=$(echo $JSON2 | python -c \
'import sys, json; \
response=json.load(sys.stdin); \
result=response["result"]; \
print(json.dumps(result, sort_keys=True, indent=2))')
if [[ $? -eq 0 && $JSON2 != "" ]]; then
break
fi
fi
done
[[ $? -ne 0 || $JSON2 == "" ]] && ((ERRORS++)) && echo ERROR: Failed to get history account for $account from node $NODE2 >$WDIR/$account && return

if [[ "$JSON1" != "$JSON2" ]]
then
echo ERROR: Comparison failed >$WDIR/$account
echo $NODE1 response: >>$WDIR/$account
echo "$JSON1" >>$WDIR/$account
echo $NODE2 response: >>$WDIR/$account
echo "$JSON2" >>$WDIR/$account
((ERRORS++))
return
fi

LAST=$(echo $JSON1 \
| python -c \
'import sys, json; \
result=json.load(sys.stdin); \
history=result["history"]; \
print(history[0][0] if len(history) else 0)')

if [ "$LAST" -eq 0 ]; then
break
fi

START=$LAST
if [ $LAST -gt 10000 ]; then
LIMIT=10000
else
LIMIT=$LAST
fi
done # while true; do
} # get_account_history ()

CURRENT_JOBS=0

for account in $( <$WDIR/accounts ); do

if [ $ERRORS -ne 0 ]; then
wait
exit -1
fi

if [ $CURRENT_JOBS -eq $JOBS ]; then
wait
CURRENT_JOBS=0
else
((CURRENT_JOBS++))
get_account_history $account &
fi

done # for account in $( <accounts ); do

wait
150 changes: 150 additions & 0 deletions tests/scripts/test_ah_get_ops_in_block.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/bin/bash

if [[ $# -lt 5 || $# -gt 7 ]]
then
echo Usage: jobs 1st_address 1st_port 2nd_address 2nd_port [last_block [first_block]]
echo if jobs == 0 script detect processor count and use it
echo if last_block not passed or 0 will be read from steemd
echo if first_block not passed will be 0
echo Example: 127.0.0.1 8090 ec2-34-235-166-184.compute-1.amazonaws.com 8090
exit -1
fi

JOBS=$1
NODE1=http://$2:$3
NODE2=http://$4:$5
FIRST_BLOCK=0
LAST_BLOCK=0

if [ $# -eq 6 ]
then
LAST_BLOCK=$6
fi

if [ $# -eq 7 ]
then
FIRST_BLOCK=$7
fi

if [ $JOBS -eq 0 ]
then
$JOBS=$(nproc -all)
fi

if [ $LAST_BLOCK -eq 0 ]
then
for tries in {1..10}
do
LAST_BLOCK=$(curl -s --data '{ "jsonrpc": "2.0", "id": 0, "method": "database_api.get_dynamic_global_properties", "params": {} }' $NODE1\
| python -c \
'import sys, json;\
print(json.load(sys.stdin)["result"]["head_block_number"])')
if [[ $? -eq 0 && $LAST_BLOCK != "" ]]; then
break
fi
done
[[ $? -ne 0 || $LAST_BLOCK == "" ]] && echo FATAL: database_api.get_dynamic_global_properties on $NODE1 failed && exit -1

for tries in {1..10}
do
LAST_BLOCK2=$(curl -s --data '{ "jsonrpc": "2.0", "id": 0, "method": "database_api.get_dynamic_global_properties", "params": {} }' $NODE2\
| python -c \
'import sys, json;\
print(json.load(sys.stdin)["result"]["head_block_number"])')
if [[ $? -eq 0 && $LAST_BLOCK != "" ]]; then
break
fi
done
[[ $? -ne 0 || $LAST_BLOCK2 == "" ]] && echo FATAL: database_api.get_dynamic_global_properties on $NODE2 failed && exit -1

if [ $LAST_BLOCK -ne $LAST_BLOCK2 ]
then
echo FATAL: $NODE1 head_block_number $LAST_BLOCK is different than $NODE2 head_block_number $LAST_BLOCK2
exit -1
fi
fi

# node block
get_ops_in_block ()
{
local NODE=$1
local BLOCK=$2
local JSON=""

for tries in {1..10}; do
JSON=$(curl -s --data "{ \"jsonrpc\": \"2.0\", \"id\": \"$BLOCK\", \"method\": \"account_history_api.get_ops_in_block\", \"params\": { \"block_num\": \"$BLOCK\", \"only_virtual\": false } }" $NODE)

if [[ $? -eq 0 && $JSON != "" ]]; then
JSON=$(echo $JSON \
| python -c \
'import sys, json; \
response=json.load(sys.stdin); \
result=response["result"]; \
print(json.dumps(result, sort_keys=True, indent=2))')
if [[ $? -eq 0 && $JSON != "" ]]; then
break
fi
fi
done

echo $JSON
} # get_ops_in_block ()

# args: first_block last_block output
test_blocks ()
{
local BLOCK=$1
local OUTPUT=$3
local JSON1=""
local JSON2=""

echo Blocks range: [ $1 : $2 ] >$OUTPUT
echo >>$OUTPUT

while [ $BLOCK -le $2 ]
do
echo Comparing block number $BLOCK

JSON1=$(get_ops_in_block $NODE1 $BLOCK &)

JSON2=$(get_ops_in_block $NODE2 $BLOCK &)

wait

[[ $JSON1 == "" ]] && echo ERROR: Failed to get block $BLOCK from node $NODE1 >>$OUTPUT && exit -1
[[ $JSON2 == "" ]] && echo ERROR: Failed to get block $BLOCK from node $NODE2 >>$OUTPUT && exit -1

if [[ "$JSON1" != "$JSON2" ]]
then
echo ERROR: Comparison failed at block $BLOCK >>$OUTPUT
echo $NODE1 response: >>$OUTPUT
echo "$JSON1" >>$OUTPUT
echo $NODE2 response: >>$OUTPUT
echo "$JSON2" >>$OUTPUT
return
fi

((BLOCK++))
done

echo SUCCESS! >>$OUTPUT
} # test_blocks ()

if [ $JOBS -gt 1 ]
then
BLOCKS=$(($LAST_BLOCK-$FIRST_BLOCK+1))
BLOCKS_PER_JOB=$(($BLOCKS/$JOBS))

for ((JOB=1;JOB<JOBS;++JOB))
do
test_blocks $FIRST_BLOCK $(($FIRST_BLOCK+$BLOCKS_PER_JOB-1)) "get_ops_in_block$JOB.log" &
FIRST_BLOCK=$(($FIRST_BLOCK+$BLOCKS_PER_JOB))
done
OUTPUT="get_ops_in_block$JOBS.log"
else
OUTPUT=get_ops_in_block.log
fi

test_blocks $FIRST_BLOCK $LAST_BLOCK $OUTPUT

wait

0 comments on commit a8cd243

Please sign in to comment.