aboutsummaryrefslogtreecommitdiff
path: root/bin/ak-zchain-chk
blob: 02d82529ffdc6edcb2fe31303726ec1d2fc6c948 (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
#!/bin/bash
PROGRAM="$(basename $0)"
source $AK_LIBDIR/_ak_logit

fix="0"
usage(){
    echo "zchain-chk - Check and fix zchain"
    echo "---------------------------------"
    echo "Usage:"
    echo "    --help, -h                             Print this help and exit"
    echo "    --chain <ipns-link>, -n <ipns-link>    Crawl specified chain"
    echo "    --fix                                  #TODO Fix your chain"
    echo ""
    echo "Note that combined flags don't work for now"
    echo "Running with no flags crawls your chain"
}

if [ ! -z "$1" ] && [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
    usage
    exit
elif [ ! -z "$1" ] && [ "$1" == "-f" ] || [ "$1" == "--fix" ]
then
    fix="1"
    entrance="$(cat $AK_ZLATEST)"
elif [ ! -z "$1" ] && [ "$1" == "-n" ] || [ "$1" == "--chain" ]
then
    entrance="$(ak-ipns-resolve $2)"
elif [ ! -z "$1" ]
then
    entrance="$1"
else
    # By default we ak-enter from the latest block
    # We can alter this by changing this value
    entrance="$(cat $AK_ZLATEST)"
fi

# We assign the IPFS CIDv0 of an empty file as this is used
# as our GENESIS block, hence the "seed" that the tree grows
# from.
seed="$(cat $AK_ZGENESIS)"

# We assume that we found the entrance inside a block, hence
# ZBLOCK is labeled as previous
zblock="$entrance"
declare -A blocks_found

# Enter temp folder
TEMPASSIN="$(ak-tempassin)"
cd $TEMPASSIN
counter=0

# The loop
# We break the loop from inside the loop
while true
do
    if [ $counter == 0 ]
    then
        echo 'Start checking'
    fi
    counter=$(expr $counter + 1)

    # Check if $zblock exists as variable
    if [ ! -v $zblock ]
    then
        # Check if it is not our seed cause if it is we skip this part
        if [ "$zblock" != "$seed" ]
        then
            # Reset timestamp since it's introduced later
            timestamp=''
            # Announce to stdout which ZBLOCK is being read at the moment
            logit "INFO" "Examining $zblock"

            # We create files named after each ZBLOCK IPFS CID for later
            # reference. Files are empty.
            touch $AK_ZBLOCKDIR/$zblock
            logit "INFO" "Created reference"

            # We concatenate the zblock's contents, pipe them through filter
            # ak-json2bash and output them to tmp-file
            ak-ipfs-cat $zblock | ak-json2bash > tmp-zblock
            logit "INFO" "ZBLOCK $zblock READ"

            # Supposingly you are on a safe environment and you only have
            # access to your chain, I would consider mild secure to source
            # the files into your bash.
            # File an issue/pull request if you think it can be done better!!
            source tmp-zblock
            logit "INFO" "ZBLOCK SOURCED"

            # Same as above applies to BLOCK and DATA subparts of each ZBLOCK
            # BLOCKS
            ak-ipfs-cat $block | ak-json2bash > tmp-block
            source tmp-block
            logit "INFO" "BLOCK $block SOURCED"
            touch $AK_BLOCKDIR/$block
            logit "INFO" "BLOCK REFERENCED"
            module="$(echo $action | sed -e 's/\// /g' | awk '{ print $1 }')"
            logit "INFO" "DATA is $module module."
            command="$(echo $action | sed -e 's/\// /g' | awk '{ print $2 }')"
            logit "INFO" "COMMAND is $command"
            if [ ! -v $timestamp ]
            then
                echo "$timestamp : $zblock -> $block -> $previous"
                blocks_found[$counter]="$block"
            fi
            touch $AK_DATADIR/$data

            # Now, since we sourced the BLOCK to our terminal, we can search
            # for $previous variable. In case we don't find one, we exit with
            # code 0
            if [ -v $previous ]
            then
                logit "ERROR" "Block $block has no previous zblock"
                echo "Chain with no genesis"
                if [ "$fix" == "1" ]
                then
                    echo "LOL"
                else
                    echo "Blocks found and need repacking..."
                    for value in ${blocks_found[@]}
                    do
                        echo $value
                        ak-ipfs-cat $value | jq -M
                    done
                fi
                exit 0
            # Otherwise, we inform of the sequence
            else
                zblock=$previous
            fi
        # Now check if it is equal to the seed
        # which apparently means we reached the seed.
        elif [ "$zblock" == "$seed" ]
        then
            echo "Chain is OK with GENESIS block = $seed"
            logit "INFO" "Counter $counter"
            exit 0
        fi
    # And finally, if nothing is there exit with error
    else
        logit "ERROR" "Check not passed... No previous IPFS CID"
        exit 1
    fi
done