aboutsummaryrefslogtreecommitdiff
path: root/lib/_ak_schain
blob: 809a454a31da5e5f785099e3ef007c7745bd23a9 (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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash

source $AK_LIBDIR/_ak_sblock

AK_SCHAINSDIR=$AK_WORKDIR/schains
GENESIS="00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

_ak_schain_latest_cached(){
    if [ ! -f $AK_WORKDIR/schain.latest ]
    then
        _ak_schain_get_latest > $AK_WORKDIR/schain.latest
    fi
    cat $AK_WORKDIR/schain.latest
}

_ak_schain_tidy(){
    SLATEST=$(_ak_schain_get_latest | jq -r '.latest_block')
    if [ ! -d $AK_SCHAINSDIR ]
    then
        mkdir $AK_SCHAINSDIR
    fi
}

_ak_schain_block_latest_block_hash(){
    _ak_schain_get_latest | jq -r '.latest_block'
}

_ak_schain_crawl(){
    if [ ! -z $1 ] && [ -n "$1" ]
    then
        CUR_TARGET="$1"
    else
        _ak_log_warning "No next target found. So long for $1"
        exit 1
    fi

    if [ $counter -eq 0 ]
    then
        echo '['
        counter=$(($counter + 1))
    else
        _ak_log_info "Counter: $counter, LIMIT_ENABLED: $LIMIT_ENABLED, LIMIT: $LIMIT"
        if [ "$LIMIT_ENABLED" == "yes" ]
        then
            if [ $counter -eq $LIMIT ]
            then
                echo ']'
                exit 0
            fi
        fi
        echo ','
        counter=$(($counter + 1))
    fi

    if [ "$1" == "$GENESIS" ]
    then
        echo '{"genesis":"genesis"}' | jq
        echo ']'
        _ak_log_warning "Looks like genesis. Exiting with 0"
        exit 0
    fi

    if [ ! -f "$AK_MINEDBLOCKSDIR/$CUR_TARGET" ]
    then
        _ak_log_warning "Could not find $CUR_TARGET"
    else
        ( echo -n '{"sblock":"'$1'",' &&
        _ak_sblock_show $CUR_TARGET | sed -e 's/^{//g') | jq
        NEXT_TARGET="$(_ak_sblock_show $CUR_TARGET | jq -r '.previous')"
        _ak_log_info "Found previous: $NEXT_TARGET"
        _ak_schain_crawl "$NEXT_TARGET"
    fi
}

_ak_schain_crawl_interface(){
    LIMIT_ENABLED="no"
    LIMIT=0
    counter=0
    if [ -z $1 ] || [ ! -n "$1" ]
    then
        _ak_schain_crawl `_ak_schain_block_latest_block_hash`
    else
        while [ "$#" ]; do
            case "$1" in
                -l)
                    LIMIT_ENABLED="yes"
                    LIMIT=$2
                    shift 2
                    ;;
                *)
                    _ak_schain_crawl "$1"
                    ;;
            esac
        done
    fi
}

_ak_schain_count_schain(){
    echo -n $1 | grep -e '[0-9a-f]\{128\}' > /dev/null
    if [ $? -ne 0 ]
    then
        _ak_log_error "Not a SHA512 hash"
        exit 1
    fi
    echo -n $1 | grep -e '0\{128\}' > /dev/null
    if [ $? -ne 0 ]
    then
        _ak_log_info "Genesis block found!"
    fi
    if [ ! -z $2 ] && [ -n "$2" ]
    then
        printf '%s' "$(echo $(( $(cat $tempcounters/$2) + 1 )))" > $tempcounters/$2
    fi
    _ak_log_info "Accessing file: $1"
    NEXT_TARGET="$(cat "$AK_MINEDBLOCKSDIR/$1" | jq -r '.previous')"
    if [ -n "$NEXT_TARGET" ]
    then
        _ak_log_info "Found previous: $NEXT_TARGET"
        if [ ! -f "$AK_MINEDBLOCKSDIR/$NEXT_TARGET" ]
        then
            _ak_log_warning "Could not find $NEXT_TARGET"
        else
            _ak_schain_count_schain "$NEXT_TARGET" "$2"
        fi
    else
        _ak_log_warning "No next target found. So long for $1"
    fi
}

_ak_sblock_get_latest_from_haystack_of_sblocks(){
    templistblock="$(_ak_make_temp_file)"
    tempcounters="$(_ak_make_temp_directory)"
    max="$(_ak_make_temp_file)"
    max_holder="$(_ak_make_temp_file)"
    echo -n 0 > $max
    echo -n "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" > $max_holder
    find $AK_MINEDBLOCKSDIR -maxdepth 1 -type f | while read line
    do
        basename "${line}" >> $templistblock
    done
    if [ "$(cat $templistblock | wc -l)" -eq 0 ]
    then
        _ak_log_error "No sblocks found"
        max_holder="00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
        printf '{"latest_block":"%s"}' "$max_holder" | tee $AK_WORKDIR/schain.latest
        exit
    fi
    cat $templistblock | while read line
    do
        if [ "$(echo $line | tr -d '\n' | wc -c)" -eq 128 ]
        then
            filename="$(basename $line)"
            _ak_log_info "Investigating $filename..."
            printf '1' > $tempcounters/$filename
            _ak_schain_count_schain "$filename" "$filename"
        else
            _ak_log_warning "Nothing to do with $filename"
        fi
    done
    rm $templistblock
    find $tempcounters -type f | while read line
    do
        _ak_log_debug "Current MAX $(cat $max) on $(cat $max_holder)"
        _ak_log_debug "Current $(cat $line) blocks on $(basename $line)"
        linesum="$(cat $line)"
        if [ $linesum -gt $(cat $max) ]
        then
            cat $line > $max
            basename $line > $max_holder
            _ak_log_info "New MAX $(cat $max) on $(cat $max_holder)"
        fi
    done
    printf '{"latest_block":"%s"}' "$(cat $max_holder)" | tee $AK_WORKDIR/schain.latest
}

_ak_schain_get_latest(){
    _ak_sblock_get_latest_from_haystack_of_sblocks
}