github.com/aergoio/aergo@v1.3.1/consensus/impl/raftv2/test/test_common.sh (about)

     1  #!/usr/bin/env bash
     2  echo "include test_common.sh"
     3  export INCLUDE_TEST_COMMON="YES"
     4  
     5  declare -A nodenames ports svrports svrname httpports peerids
     6  
     7  ALLIPS="10001 10002 10003 10004 10005 10006 10007"
     8  
     9  for i in {1..7} ; do
    10  	nodename="aergo$i"
    11  	nodenames[$i]=$nodename
    12  
    13  	ports[$nodename]=$((10000 + $i))
    14  	svrport=$((11000 + $i))
    15  	svrports[$nodename]=$svrport
    16  	svrname[$nodename]="BP$svrport"
    17  
    18  	httpports[$nodename]=$((11000 + $i))
    19  
    20  	if [ -e "$TEST_RAFT_INSTANCE/$svrport.id" ]; then 
    21  		peerids[$nodename]=`cat $TEST_RAFT_INSTANCE/$svrport.id`
    22  		echo "peerids="${peerids[$nodename]}
    23  	fi
    24  done
    25  
    26  function RUN_TEST_SCRIPT() {
    27  	shell="/usr/bin/env bash"
    28  	echo "run scripts> $*"
    29  
    30  	$shell -c "$*"
    31  	if [ $? != 0 ]; then
    32  		echo "Test Failed"
    33  		exit 1000
    34  	fi
    35  
    36  	echo "Test Succeeded"
    37  }
    38  
    39  
    40  function existProcess() {
    41      local port=$1
    42  
    43      local proc=$(ps  -ef|grep aergosvr | grep $port | awk '{print $2 }')
    44      if [ "$proc" = "" ]; then
    45          return "0"
    46      fi
    47  
    48      return "1"
    49  }
    50  
    51  function getHeight() {
    52      local port=$1
    53  
    54      local serverport=$(($port + 1000))
    55  
    56  	#echo "port=$1, serverport=$serverport"
    57  	echo "port=$1"
    58  
    59      existProcess $serverport
    60      if [ "$?" = "0" ]; then
    61  		echo "no process $serverport"
    62  		eval "$2=0"
    63  		return
    64      fi
    65  
    66      local _height_=$(aergocli -p $port blockchain | jq .Height)
    67  #	echo "getHeight _height_=$_height_"
    68  
    69  	if [ "$_height_" = "" ]; then
    70  		_height_=0
    71  	fi
    72  
    73      eval "$2=$_height_"
    74  }
    75  
    76  function getHash() {
    77      local port=$1
    78      local height=$2
    79  
    80  	if [ $# != 3 ];then
    81  		echo "usage: getHash port height retHash"
    82  		exit 100
    83  	fi
    84  
    85      local serverport=$(($port + 1000))
    86  	echo "serverport=$serverport"
    87  
    88      existProcess $serverport
    89      if [ "$?" = "0" ]; then
    90  		"no process $serverport"
    91  		exit 100
    92      fi
    93  
    94  	echo "aergocli -p $port getblock --number $height | jq .Hash"
    95      local hash=$(aergocli -p $port getblock --number $height | jq .Hash)
    96  
    97      eval "$3=$hash"
    98  }
    99  
   100  function getleader() {
   101  	local curleader=""
   102  	local i=0
   103  	for (( ; ; )); do
   104  		for i in 10001 10002 10003 10004 10005 ; do
   105  			getLeaderOf $i curleader
   106  			if [[ "$curleader" == aergo* ]]; then
   107  				break
   108  			fi
   109  		done
   110  
   111  		echo "curleader=$curleader"
   112  
   113  		if [[ "$curleader" != aergo* ]]; then
   114  			echo "<get leader failed>"
   115  			eval "$1="
   116  			sleep 3
   117  			continue
   118  		fi
   119  
   120  		echo "leader=$curleader"
   121  
   122  		eval "$1=$curleader"
   123  		return
   124  	done
   125  }
   126  
   127  
   128  function getLeaderOf() {
   129  	local _leader_=""
   130  	if [ $# -ne 2 ]; then
   131  		echo "usage: getLeaderOf rpcport retvalue"
   132  		exit 100
   133  	fi
   134  
   135  	local myport=$1
   136  
   137  	_leader_=$(aergocli -p $myport blockchain | jq .ConsensusInfo.Status.Leader)
   138  	_leader_=${_leader_//\"/}
   139  
   140  	if [[ "$_leader_" == aergo* ]]; then
   141  		eval "$2=$_leader_"
   142  	fi
   143  }
   144  
   145  function HasLeader() {
   146  	if [ $# -ne 2 ]; then
   147  		echo "usage: HasLeader rpcport retvalue"
   148  		exit 100
   149  	fi
   150  
   151  	local myport=$1
   152  	local myRet
   153  	getLeaderOf $myport myRet
   154  
   155  	if [[ "$myRet" == aergo* ]]; then
   156  		echo "leader exist in $myport"
   157  		eval "$2=1"
   158  	else
   159  		echo "leader not exist in $myport"
   160  		eval "$2=0"
   161  	fi
   162  }
   163  
   164  function getRaftID() {
   165  	if [ $# != 3 ]; then
   166  		echo "getRafTID leaderport name outRaftID"
   167  		exit 100
   168  	fi
   169  
   170  	local _leaderport=$1
   171  	local name=$2
   172  	local pattern=".Bps|.[]|select(.Name==\"$name\")|.RaftID"
   173  	local _raftID=
   174  
   175  	echo "aergocli -p $_leaderport getconsensusinfo | jq $pattern"
   176  	_raftID=`aergocli -p $_leaderport getconsensusinfo | jq $pattern`
   177  	ret=$?
   178  	if [ "$_raftID" == "" ]; then
   179  		echo "failed to get raftID for $name"
   180  		exit 200
   181  		eval $3=""
   182  	fi
   183  
   184  	echo "<raftid=$_raftID>"
   185  	eval "$3=$_raftID"
   186  }
   187  
   188  function getRaftState() {
   189  	local name=$1
   190  	
   191  	if [ "$#" != 2 ]; then 
   192  		echo "Usage: getRaftState servername outStateVar"
   193  		exit 100
   194  	fi
   195  
   196  	local leaderPort=
   197  	getLeaderPort leaderPort
   198  	if [ $? -ne 0 ];then
   199  		echo "failed to get leader port"
   200  		exit 100
   201  	fi
   202  
   203  	# getRaftID
   204  	getRaftID $leaderPort $name raftID
   205  
   206  	# getRaftStatus
   207  	local pattern=".Info.Status.progress[\"$raftID\"].state"
   208  
   209  	local _raftState=$(aergocli -p $leaderPort getconsensusinfo | jq $pattern)
   210  
   211  	echo "<raftState=$_raftState>"
   212  
   213  	eval "$2=$_raftState"
   214  }
   215  
   216  
   217  function getLeaderPort() {
   218  	if [ $# != 1 ]; then
   219  		echo "Usage: getLeaderPort leaderport"
   220  		exit 100
   221  	fi
   222  
   223  	local _leader=""
   224  	getleader _leader
   225  
   226  	local _leaderport=${ports[$_leader]}
   227  
   228  	if [ "$_leaderport" == "" ];then
   229  		echo "failed to get leader port"
   230  		exit 100
   231  	fi
   232  
   233  	echo "leader port=$_leaderport"
   234  
   235  	eval "$1=$_leaderport"
   236  }
   237  
   238  function isStableLeader() {
   239  	if [ $# -ne 1 ]; then
   240  		echo 'Usage: isStableLeader timeout. return value=$?'
   241  		exit 100
   242  	fi
   243  
   244  	mydate=$(date)
   245  	echo "$mydate> check if leader is stable"
   246  	timeout=$1
   247  
   248  	local _prevleader=""
   249  	local _tmpLeader=""
   250  
   251  	getleader _prevleader
   252  	getleader _tmpLeader
   253  
   254  	for ((i=1;i<=$timeout;i++))
   255  	do 
   256  		if [ "$_prevleader" != "$_tmpLeader" ]; then
   257  			echo "Fail: leader changed prev=$_prevleader, cur=$_tmpLeader" 
   258  			return 0
   259  		fi
   260  
   261  		sleep 1
   262  	done
   263  
   264  	return 1
   265  }
   266  
   267  function changeLeader() {
   268  	if [ "$#" != 0 ];then
   269  		echo "Usage: changeLeader"
   270  		exit 100
   271  	fi
   272  
   273  	local leaderName
   274  
   275  	getleader leaderName
   276  
   277  	echo "cur leader: $leaderName"
   278  	
   279  	local leaderPort="" 
   280  	leaderPort=${svrports[$leaderName]}
   281  	echo "leaderport=$leaderPort"
   282  
   283  	kill_svr.sh $leaderPort
   284  	sleep 2
   285  	DEBUG_CHAIN_BP_SLEEP=$chainSleep run_svr.sh $leaderPort
   286  	sleep 2
   287  
   288  	leaderName=""
   289  	getleader leaderName
   290  	echo "new leader: $leaderName"
   291  }
   292  
   293  function isChainHang() {
   294  	# "isChainHang: return 1 if true"
   295  	if [ "$#" != "2" ];then
   296  		echo "Usage: isChainHang targetRpcPort timeout"
   297  		exit 100
   298  	fi
   299  
   300  	# 아무노드나 골라서 5초동안 chain이 증가하고 있는지 확인
   301  	local srcPort=$1
   302  	local timeout=$2
   303  	local heightStart=""
   304  	local heightEnd=""
   305  
   306  	local tryHangAgain=10
   307  
   308  	echo "isChainHang($timeout) from $srcPort"
   309  
   310  	for ((i=1;i<=$tryHangAgain;i++))
   311  	do 
   312  		getHeight $srcPort heightStart
   313  
   314  		sleep $timeout
   315  
   316  		getHeight $srcPort heightEnd
   317  
   318  		echo "start:$heightStart ~ end:$heightEnd"
   319  		if [ "$heightEnd" != "$heightStart" ];then
   320  			echo "check succed"
   321  			return 0
   322  		fi
   323  
   324  		echo "chain is hanged. and retry[$i]"
   325  		sleep 1
   326  	done
   327  
   328  	return 1
   329  }
   330  
   331  function checkReorg() {
   332  	reorgCount=$(egrep 'reorg' ./*.log | wc -l | awk '{print $1}')
   333  
   334  	if [ "$reorgCount" != "0" ];then
   335  		echo "failed: reorg occured"
   336  		exit 100
   337  	fi
   338  }
   339  
   340  
   341  # 모든 노드의 leader가 0이 아닌 valid node를 가르키는지
   342  function checkLeaderValid() {
   343  	local curleader=""
   344  	for i in $ALLIPS ; do
   345  		local _svrport=$(($i + 1000))
   346  		existProcess $_svrport
   347  		if [ "$?" = "0" ]; then
   348  			continue
   349  		fi
   350  
   351  		curleader=$(aergocli -p $i blockchain | jq .ConsensusInfo.Status.Leader)
   352  		curleader=${curleader//\"/}
   353  		if [[ "$curleader" != aergo* ]]; then
   354  			echo "failed: leader of $i is $curleader"
   355  			exit 100
   356  		fi
   357  	done
   358  }
   359  
   360  function checkSync() {
   361  	local srcPort=$1
   362  	local curPort=$2
   363  	local timeout=$3
   364  	local _srcHeight=
   365  	local _curHeight=
   366  
   367  	echo "============ checkSync $srcPort vs $curPort . timeout=$3sec ==========="
   368  	echo "src=$srcPort, curPort=$curPort, time=$timeout"
   369  
   370  	for ((i = 1; i<= $3; i++)); do
   371  		sleep 1
   372  
   373  		_srcHeight=""
   374  		_curHeight=""
   375  		getHeight $srcPort _srcHeight
   376  		
   377  		getHeight $curPort _curHeight
   378  
   379  		echo "srcno=$_srcHeight, curno=$_curHeight"
   380  
   381  		if [ "$_srcHeight" = "0" ] || [ "$_curHeight" = "0" ] || [ "$_srcHeight" = "255" ] || [ "$_curHeight" = "255" ]; then
   382  			continue
   383  		fi
   384  
   385  		targetNo=$((_curHeight + 3))
   386  		if [ $targetNo -gt $_srcHeight ]; then
   387  			echo "sync succeed"
   388  			isChainHang $curPort 3
   389  			echo ""
   390  			echo ""
   391  			hang=$?
   392  			if [ $hang = 1 ];then
   393  				echo "========= hang after sync ============"
   394  				exit 100
   395  			fi
   396  			return
   397  		fi
   398  	done
   399  
   400  	echo "========= sync failed ============"
   401  	exit 100
   402  }
   403  
   404  function checkSyncWithLeader() {
   405  	local _curPort=$1
   406  	local _timeout=$2
   407  
   408  	echo "============ checkSync with Leader $_curPort . timeout=$2sec ==========="
   409  
   410  	local leaderport=
   411  	getLeaderPort leaderport
   412  	if [ $? -ne 0 -o "$leaderport" = "" ];then
   413  		echo "failed to get leader port"
   414  		exit 100
   415  	fi
   416  
   417  	checkSync $leaderport $_curPort $_timeout
   418  	if [ $? -ne 0 ];then
   419  		echo "failed to sync with leader $leaderport"
   420  		exit 100
   421  	fi
   422  }
   423  
   424  # 현재 sync가 정상적으로 진행중인지 검사
   425  # 현재 best가 remote 에 connect되어 있는 지 확인
   426  function checkSyncRunning() {
   427      local srcPort=$1
   428      local curPort=$2
   429      local try=$3
   430  
   431      local srcHash
   432      local curHash
   433      local curHeight
   434  
   435      echo "============ checkSyncRunning $srcPort vs $curPort . try=$3 nums ==========="
   436  
   437      for ((i = 1; i<= $try; i++)); do
   438          curHeight=""
   439  
   440          getHeight $curPort curHeight
   441  
   442          curHash=""
   443          getHash $curPort $curHeight curHash
   444  
   445          srcHash=""
   446          getHash $srcPort $curHeight srcHash
   447  
   448  		mydate=$(date)
   449          echo "$mydate> curHeight=$curHeight, srchash=$srcHash, curhash=$curHash"
   450  
   451          if [ "$curHeight" = "-1" ] || [ "$curHash" = "-1" ] || [ "$srcHash" = "-1" ]; then
   452  			echo "========= sync failed ============"
   453  			exit 100 
   454          fi
   455  
   456          if [ "$curHash" != "$srcHash"  ]; then
   457  			echo "========= sync failed ============"
   458  			exit 100
   459          fi
   460  
   461          sleep 1
   462  	done
   463  
   464  	mydate=$(date)
   465  	echo "$mydate =========== sync is running well =========="
   466  	return 0
   467  }
   468  
   469  
   470  # copy BP1100[N].toml and _genesis.* to $TEST_RAFT_INSTANCE 
   471  function prepareConfig() {
   472  	if [ $# != "1" ];then
   473  		echo "Usage: $0 configMax"
   474  		exit 100
   475  	fi
   476  
   477  	if [ "$TEST_RAFT_INSTANCE" = "" ];then
   478  		echo "TEST_RAFT_INSTANCE is not set"
   479  		exit 100
   480  	fi
   481  
   482  
   483  	configMax=$1
   484  	echo "prepare config files ($configMax)"
   485  
   486  	for i in $(seq 1 $configMax); do
   487  		echo "cp  $TEST_RAFT_INSTANCE_CONF/BP1100$i.toml  $TEST_RAFT_INSTANCE"
   488  		cp  $TEST_RAFT_INSTANCE_CONF/BP1100$i.toml  $TEST_RAFT_INSTANCE
   489  	done
   490  	echo "cp  $TEST_RAFT_INSTANCE_CONF/_genesis.* $TEST_RAFT_INSTANCE"
   491  	cp  $TEST_RAFT_INSTANCE_CONF/_genesis.* $TEST_RAFT_INSTANCE
   492  }
   493  
   494  function getAdminUnlocked() {
   495  	if [ $# -ne 3 ]; then
   496  		echo "Usage: $0 rpcport genesis.json retAddress"
   497  		exit 100
   498  	fi
   499  
   500  	rpcport=$1
   501  	_genesis="$2"
   502  
   503  	if [ ! -e  "$_genesis" ];then
   504  		echo "not exit genesis_wallet.txt ($_genesis)"
   505  		exit 100
   506  	fi
   507  
   508  	_admin=`cat $_genesis`
   509  
   510  	echo "aergocli -p $rpcport account unlock --address $_admin --password 1234"
   511  	_ret=`aergocli -p $rpcport account unlock --address $_admin --password 1234`
   512  
   513  	if [ "$_admin" != "$_ret" ];then
   514  		echo "failed to unlock $_admin"
   515  	fi
   516  
   517      eval "$3=$_admin"
   518  }
   519  
   520  function makeAddMemberJson() {
   521  	# return valude is printed by echo
   522  	if [ $# -ne 1 ]; then
   523  		#echo "Usage: $0 nodename"
   524  		exit 100
   525  	fi
   526  
   527  	_nodename=$1
   528  	if [[ "$_nodename " != aergo* ]]; then
   529  		#echo "Usage: $0 nodename"
   530  		exit 100
   531  	fi
   532  
   533  	memberJson='[ { "command": "add", "name": "'$_nodename'", "address": "/ip4/127.0.0.1/tcp/'${httpports[$_nodename]}'", "peerid":"'${peerids[$_nodename]}'" } ]'
   534  
   535  	echo $memberJson
   536  }
   537  
   538  function makeRemoveMemberJson() {
   539  	# return valude is printed by echo
   540  	if [ $# -ne 1 ]; then
   541  		#echo "Usage: $0 raftID"
   542  		exit 100
   543  	fi
   544  
   545  	_raftID=$1
   546  	_memberJson='[ { "command": "remove", "id":"'$_raftID'" } ]'
   547  
   548  	echo $_memberJson
   549  }
   550  
   551  function getClusterTotal() {
   552  	_chkPort=$1
   553  	_total=`aergocli -p $_chkPort blockchain | jq .ConsensusInfo.Status.Total`
   554  	echo $(printf %d $_total)
   555  }
   556  
   557  function waitClusterTotal() {
   558  	if [ $# -lt 2 -o ! $1 -ge 0 ];then
   559  		echo "Usage: waitClusterTotal totalcount tryCount(every 3 second) reqport"
   560  		exit 100
   561  	fi
   562  
   563  	reqCount=$1
   564  	tryCnt=$2
   565  	reqPort=$3
   566  	local i
   567  	echo "Wait cluster: reqCount=$1 tryCnt=$2 reqPort=$3"
   568  
   569  	local leaderport=$reqPort
   570  
   571  	for ((i=1; i<= $tryCnt ; i++)); do
   572  		if [ "$reqport" = "" ];then
   573  			getLeaderPort leaderport
   574  
   575  			if [ $? -ne 0 -o "$leaderport" = "" ];then
   576  				echo "failed to get leader port"
   577  				sleep 3
   578  				continue
   579  			fi
   580  		fi
   581  
   582  		total=`aergocli -p $leaderport blockchain | jq .ConsensusInfo.Status.Total`
   583  		echo "i=$i, total=$total, req=$reqCount"
   584  		if [ "$total" = "$reqCount" ];then
   585  			return 1	
   586  		fi
   587  
   588  		sleep 3 
   589  		echo $i
   590  	done
   591  		
   592  
   593  	echo "failed waitClusterTotal($total)"
   594  	return 0
   595  }
   596  
   597  function WaitPeerConnect() {
   598  	if [ $# -ne 2 ]; then
   599  		echo "Usage:$0 expectPeerCount Timeout(sec)"
   600  		exit 100
   601  	fi 
   602  
   603  	_reqcnt=$1
   604  	_timeout=$2
   605  	local _res
   606  
   607  	for ((i = 1; i<= $_timeout; i++)); do
   608  		_res=$(aergocli -p 10001 getpeers | jq ".|length")
   609  
   610  		if [[ "$_res" == fail* ]]; then
   611  			continue
   612  		fi
   613  
   614  		if [ "$_res" != "" -a $_res -gt 0 -a $_res -ge $_reqcnt ];then
   615  			echo "peer[$_res] connected"
   616  			return 1
   617  		fi
   618  
   619  		sleep 1
   620  	done
   621  
   622  	echo "failed peer connection: peer req=$_reqcnt, res=$_res connected."
   623  	return 0
   624  }
   625  
   626  
   627  function WaitShutdown() {
   628  	if [ $# -ne 2 ]; then
   629  		echo "Usage:$0 pattern Timeout(sec)"
   630  		exit 100
   631  	fi 
   632  
   633  	_pattern=$1
   634  	_timeout=$2
   635  	local _res
   636  
   637  	for ((i = 1; i<= $_timeout; i++)); do
   638  		existProcess $_pattern
   639  		if [ "$?" == "0" ]; then
   640  			echo "no process $_pattern"
   641  			return 1
   642  		fi
   643  
   644  		sleep 1
   645  	done
   646  
   647  	echo "failed to wait shutdown(pattern=$_pattern)"
   648  	return 0
   649  }