aboutsummaryrefslogtreecommitdiff
path: root/bin/ak-find-latest-mined-sblock
blob: 8ef104c16496725872151a22c426c32c09792d85 (plain)
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
#!/bin/bash
cd $AK_MINEDBLOCKSDIR
PROGRAM="$(basename $0)"
declare -A counters
source $AK_LIBDIR/_ak_log
source $AK_LIBDIR/_ak_ipfs

if [ ! -z $1 ] && [ -n "$1" ]
then
    if [ "$1" == "-h" ] || [ "$1" == "--help" ]
    then
        echo "$PROGRAM - Finds longest schain from the $AK_MINEDBLOCKSDIR dir">&2
        exit 2
    fi
fi

further(){
	# logit "INFO" "Diving into $1"
	lookfor $1
}

lookfor(){
	echo -n $1 | grep -e '[0-9a-f]\{128\}' > /dev/null
	if [ ! $? = 0 ]
	then
		logit "ERROR" "Oops!!! The argument passed, does not match the regular expression!"
	else
		counters[$CHAIN_PARENT]="$(expr ${counters[$CHAIN_PARENT]} + 1)"
		if [ ! $? = 0 ]
		then
			echo "$1, $counters[$1]"
			exit 1
		fi
		logit "INFO" "Accessing file: $1"
	#	echo "$1 file:"
	#	cat "$1" | jq
		NEXT_TARGET="$(cat "$1" | jq | grep previous | tr -d ' ' | sed -e 's/previous//g; s/[",:]//g;')"
		if [ ! "$NEXT_TARGET" = "" ]
		then
			logit "INFO" "Found previous: $NEXT_TARGET"
			if [ ! -f "$NEXT_TARGET" ]
			then
				logit "WARNING" "Could not find $NEXT_TARGET"
			else
				further "$NEXT_TARGET"
			fi
		else
			logit "WARNING" "No next target found. So long for $1"
		fi
	fi
}

templistblock="$(mktemp)"
find . -type f | sed 's/^\.\/000/000/g' | grep '^000'  | sort > $templistblock
while IFS="" read -r p || [ -n "$p" ]
do
#	if [ ! "$p" = "" ] && [ ! "$p" = "tmplistblock" ]
	if [ "$(echo $p | tr -d '\n' | wc -c)" = 128 ]
	then
        filename="$(basename $p)"
		logit "INFO" "Investigating $filename..."
		export CHAIN_PARENT="$filename"
		counters[$CHAIN_PARENT]=1
		lookfor "$filename"
	else
		logit "WARNING" "Nothing to do with $filename"
	fi
done < $templistblock
rm $templistblock
max=0
max_holder=0
for value in "${!counters[@]}"
do
    #	echo "${value} : ${counters[${value}]}"
    if [ ${counters[${value}]} -gt $max  ]
    then
        max="${counters[${value}]}"
        max_holder="${value}"
        logit "INFO" "New MAX $max on $max_holder"
    fi
done
echo '{"latest_block":"'$max_holder'"}'