From 1893ff19d8be9f2462d036daa6827a5478e10ebd Mon Sep 17 00:00:00 2001
From: Robert Martin-Legene <robert@nic.ar>
Date: Thu, 28 Jun 2018 18:35:16 -0300
Subject: [PATCH] Moving to use the new libbfa

---
 bin/attach.sh                          |   8 ++
 create.contract => bin/create.contract |  57 ++++-------
 bin/explorer.sh                        |  81 +++++++++++----
 bin/insert.sh                          |   7 +-
 bin/libbfa.sh                          | 131 +++++++++++++------------
 bin/sendether.sh                       |   7 +-
 bin/verify.sh                          |   9 +-
 7 files changed, 163 insertions(+), 137 deletions(-)
 create mode 100755 bin/attach.sh
 rename create.contract => bin/create.contract (53%)

diff --git a/bin/attach.sh b/bin/attach.sh
new file mode 100755
index 0000000..5f42960
--- /dev/null
+++ b/bin/attach.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+# 20180626 Robert Martin-Legene <robert@nic.ar>
+
+if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/libbfa.sh ?" >&2; exit 1; fi
+source ${BFAHOME}/bin/libbfa.sh || exit 1
+
+config node
+geth attach ipc:${BFANODEDIR}/geth.ipc
diff --git a/create.contract b/bin/create.contract
similarity index 53%
rename from create.contract
rename to bin/create.contract
index bb32447..16d0ebb 100755
--- a/create.contract
+++ b/bin/create.contract
@@ -1,36 +1,15 @@
 #!/bin/bash
+# 20180618 Robert Martin-Legene <robert@nic.ar>
 
-cd $( dirname $0 )
-. ./libbfa.sh
-
-function	prereq
-{
-	err=0
-	while [ -n "$1" ]
-	do
-		if !  which $1 > /dev/null
-		then
-			echo "Need $1"
-			err=1
-		fi
-		shift
-	done
-	test $err -eq 0 || exit 1
-}
+if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/libbfa.sh ?" >&2; exit 1; fi
+source ${BFAHOME}/bin/libbfa.sh || exit 1
 
 function	create
 {
     workdir=$(  bfamktemp -p . -d )
-    json=$(	solc --optimize --combined-json abi,bin $filename	)
-    test $? eq 0
-    prefix=$(	echo "$json"	| jq '.contracts|keys[0]'		)
-    test $? eq 0
-    abi=$(	echo "$json"	| jq ".contracts.${prefix}.abi"		)
-    test $? eq 0
-    bin=$(	echo "$json"	| jq ".contracts.${prefix}.bin"		)
-    test $? eq 0
-    # Remove all quote marks "
-    bin="${bin//\"}"
+    json=$(	solc --optimize --combined-json abi,bin $filename	); test $? eq 0
+    abi=$(	echo "$json"	| jq -rM ".contracts.${prefix}.abi"	); test $? eq 0
+    bin=$(	echo "$json"	| jq -rM ".contracts.${prefix}.bin"	); test $? eq 0
     # Save abi for future use
     echo $abi > ${workdir}/abi
     # We could save the bin, but we don't actually use it later, plus
@@ -55,28 +34,32 @@ EOT
     if [ ` wc -l < $outfile ` = 2 -a `tail -1 < $outfile` = "true" ]
     then
         addr=` head -1 < $outfile `
-        mkdir ${network}/contracts
-        mv ${workdir} ${network}/contracts/${addr}
-        echo Your new contract can be found in ${network}/contracts/${addr}
- 	ln -sf ${addr} ${network}/contracts/${contractname}
+        mkdir -p ${BFANETWORKDIR}/contracts
+        mv ${workdir} ${BFANETWORKDIR}/contracts/${addr}
+        echo Your new contract can be found in ${BFANETWORKDIR}/contracts/${addr}
+ 	ln -snf ${addr} ${BFANETWORKDIR}/contracts/${contractname}
     else
+        echo 
+        echo ' *** INPUT ***'
+        echo 
         cat $js
         echo
-        echo ' ***'
+        echo ' *** OUTPUT ***'
         echo
         cat $outfile
     fi
 }
 
-trap "echo Argh;exit 1" ERR
-test -n "$1" || ( echo "Specify a filename of a contract you wish to compile."; false )
 filename="$1"
-test -r "$filename"
+if [ -z "$filename" -o ! -r "$filename" ]
+then
+    echo "Specify a filename of a contract you wish to compile."
+    false
+fi
 contractname=${filename%%.sol}
 contractname=${contractname##*/}
 contractname=${contractname##contract.}
 contractname=${contractname%.*}
-account=` ls -1dl network5445/node1/keystore/* | head -1 | sed 's/.*--//' `
-test -n "$account"
+config max
 prereq jq solc geth
 create
diff --git a/bin/explorer.sh b/bin/explorer.sh
index f59d45a..39cc647 100755
--- a/bin/explorer.sh
+++ b/bin/explorer.sh
@@ -1,25 +1,24 @@
 #!/bin/bash
 # 20180619 Robert Martin-Legene <robert@nic.ar>
 
-if [ ! -d "${BFAHOME}" ]
-then
-    echo "\$BFAHOME does not point to a directory. Did you source \$BFAHOME/bin/env ?" >&2
-    exit 1
-fi
+if [ -z "${BFAHOME}" ]; then echo "\$BFAHOME not set. Did you source bfa/bin/libbfa.sh ?" >&2; exit 1; fi
+source ${BFAHOME}/bin/libbfa.sh || exit 1
 
-trap "echo Argh;exit 1" ERR
+config max
+prereq tput
+# rpcport is set per node, in a file. Default is port 8545.
 rpcport=8545
-
-cd $(           dirname $0          )
-test -r "node1/rpcport" &&
-  rpcport=$(    cat node1/rpcport   )
+cd "${BFANETWORKDIR}"
+test -r "$BFANODEDIR/rpcport" &&
+  rpcport=$(    cat "$BFANODEDIR/rpcport"   )
 test -n "$rpcport"
 width=$(	tput cols           )
 height=$(	tput lines          )
-block=${1:-0}
 
 function	showblock
 {
+        test "$1" = "latest" &&
+            set $maxblock
 	hexblock=$( printf '%x' $1 )
 	printf '\e[H\e[JBlock %d (0x%x)\n' $block $block
 	curl -H 'Content-type: application/json' -X POST --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x'${hexblock}'", true],"id":1}' http://127.0.0.1:$rpcport 2>/dev/null |
@@ -27,21 +26,61 @@ function	showblock
 		fold --width=$width |
 		head -$(( $height - 2 ))
 	printf '\e[m'
+        maxblock=$( latest )
+}
+
+function    latest
+{
+    curl    \
+        -H 'Content-type: application/json'                         \
+        -X POST                                                     \
+        --data '{"jsonrpc":"2.0","method":"eth_getNumber","id":1}'  \
+        http://127.0.0.1:$rpcport                                   \
+        2>/dev/null |
+    jq -rM .result
 }
 
+
+block=$1
+maxblock=$( latest )
+if [ -z "$block" ]
+then
+    block=latest
+fi
+test -n "$block"
+
 showblock $block
-while read -r -s -n 1 -p "j=up k=down q=quit "
+timeout=
+while read -r -s -n 1 ${timeout} -p "j=up k=down q=quit "
 do
-	if   [ "${REPLY,,}" = "q" ]
-	then
+        case "${REPLY,,}" in
+	    q)
 		echo
 		exit 0
-	elif [ "${REPLY,,}" = "k" -a $block -gt 0 ]
-	then
-		let block--
-	elif [ "${REPLY,,}" = "j" ]
-	then
-		let block++ || true
-	fi
+                ;;
+            k)
+                if [ "$block" = "latest" -a $maxblock -gt 0 ]
+                then
+                    block=$(( $maxblock - 1 ))
+                else
+                    if [ $block -gt 0 ]
+                    then
+		        block=$(( $block - 1 ))
+                    fi
+                fi
+                ;;
+            j)
+                if [ "$block" != "latest" ]
+                then
+		    block=$(( $block + 1 ))
+                fi
+	        ;;
+            *)
+                continue
+                ;;
+        esac
 	showblock $block
+        timeout=
+        test "$block" = "latest" &&
+            timeout="-t 1"
 done
diff --git a/bin/insert.sh b/bin/insert.sh
index 9cc261f..8aa2f3b 100755
--- a/bin/insert.sh
+++ b/bin/insert.sh
@@ -1,10 +1,7 @@
 #!/bin/bash
 
-function	cleanup
-{
-	rmfiles="$rmfiles $@"
-    	trap "rm -rf ${rmfiles}" EXIT
-}
+# $BFAHOME must point to a directory. Did you source $BFAHOME/bin/env ?
+source ${BFAHOME}/bin/libbfa.sh || exit 1
 
 trap "echo Argh;exit 1" ERR
 cd $( dirname $0 )
diff --git a/bin/libbfa.sh b/bin/libbfa.sh
index 4499b83..a4ab1d6 100644
--- a/bin/libbfa.sh
+++ b/bin/libbfa.sh
@@ -1,4 +1,4 @@
-# Probably, this should only be sourced, not executed directly.
+# This should only be sourced, not executed directly.
 # 20180626 Robert Martin-Legene <robert@nic.ar>
 
 fatal()
@@ -13,6 +13,11 @@ test "$( basename $SHELL 2>/dev/null )" = "bash"		||
 test "$( caller 2>/dev/null | awk '{print $1}' )" != "0"	||
     fatal "This file must be source(d), not executed."
 
+function    stderr
+{
+    echo "$@" >&2
+}
+
 function    cleanup
 {
     if  [ $# -gt 0 ]
@@ -45,82 +50,82 @@ function    geth_exec_js
     echo    $out
 }
 
-function    config_environment
+function    networkdir
 {
-    creating=0
-    if      [ -z "${networkdir}" ]
+    test -z "${BFAHOME}" -o ! -d "${BFAHOME}" ||
+        fatal "\$BFAHOME in your environment must point to a directory."
+    local   creating=0
+    # If no BFANETWORKDIR variable has been set, we will try to guess
+    # the network directory based on the directory we are in.
+    if [ -n "${BFANETWORKDIR}" ]
     then
+        test -d "${BFANETWORKDIR}" ||
+            fatal   "\$BFANETWORKDIR (\"${BFANETWORKDIR}\") not found."
+    else
         local   basedir=$( basename $PWD )
-        if      [ -n "${networkdir}" -a ! -d "${networkdir}" ]
+        if    $( echo "${basedir}" | grep -qE '^network[0-9]+$' )
         then
-            # Networkdir is set and we can't find it. Let's see if
-            # we happen to already be in that directory.
-            if      [ "${basedir}" -ne "${networkdir}" ]
-            then
-                fatal "Your \$networkdir environment variable conflicts with" \
-                    "the name of the directory you are currently in, and I can" \
-                    "not find the networkdir you specified (if any)."
-            fi
-        elif    $( echo "${basedir}" | grep -qE '^network[0-9]+$' )
-        then
-            networkdir="$basedir"
+            BFANETWORKDIR="$basedir"
         fi
-        local   n
-        for     n in $( ls -ld network* 2>/dev/null | grep -qE '^network[0-9]+$' )
-        do
-            if      [ -n "${networkdir}" ]
-            then
-                fatal "If you have more than one work listed in your" \
-                    "current directory, you must specify which one to" \
-                    "use, using the \"networkdir\" environment variable."
-            fi
-            networkdir=${n}
-        done
-        if      [ -z "${networkdir}" ]
+        if      [ -z "${BFANETWORKDIR}" ]
         then
-            echo "I can't find your networkdir."
-            REPLY=
-            while   [ "$REPLY" != "y" -a "$REPLY" != "n" ]
+            local   num=0
+            while   [ $num -lt 9876 ]
             do
-                read    -n 1 -p "Would you like me to create a random one for you? [yN]: "
-                if      [ -z "$REPLY" ]
-                then
-                    REPLY=n
-                else
-                    if  [ "${REPLY,,}" = "y" ]
-                    then
-                        creating=1
-                    fi
-                fi
+                num=$(( $RANDOM * $RANDOM ))
             done
-            if [ $creating = 1 ]
-            then
-                local   num=0
-                while   [ $num -lt 123456 ]
-                do
-                    num=$(( $RANDOM * $RANDOM ))
-                done
-                networkdir="network${num}"
-                echo "You had no network defined, so I am creating network ${network} for you in ${networkdir}/ ."
-                mkdir "${networkdir}"
-            fi
+            stderr  "I can't find your BFANETWORKDIR."
+            fatal   "Consider running: mkdir ${BFAHOME}/network${num}"
         fi
     fi
+}
+
+function    nodedir
+{
+    networkdir
     # set defaults
-    true ${nodedir:=node1}
-    test -n "${nodedir}"
-    if [ ! -d "${nodedir}" -a $creating = 1 ]
-    then
-        mkdir ${networkdir}/${nodedir}
-    else
-        test -d "${nodedir}"
-    fi
+    true ${BFANODEDIR:=${BFAHOME}/node1}
+    test -n "${BFANODEDIR}"
+    test -d "${BFANODEDIR}"
+}
+
+function    account
+{
+    nodedir
     if [ -z "$account" ]
     then
-        test -d "${nodedir}/keystore"
-        account=$( ls -1dl "${nodedir}"/keystore/* | head -1 | sed 's/.*--//' )
+        test -d "${BFANODEDIR}/keystore"
+        account=$( ls -1dl "${BFANODEDIR}"/keystore/*--* | head -1 | sed 's/.*--//' )
     fi
     test -n "$account"
 }
 
-config_environment
+function    config
+{
+    case "$1" in
+        network)
+            networkdir
+            ;;
+        node)
+            nodedir
+            ;;
+        account|max)
+            account
+            ;;
+    esac
+}
+
+function	prereq
+{
+	err=0
+	while [ -n "$1" ]
+	do
+		if !  which $1 > /dev/null
+		then
+			echo "Need $1"
+			err=1
+		fi
+		shift
+	done
+	test $err -eq 0
+}
diff --git a/bin/sendether.sh b/bin/sendether.sh
index 10b7c61..c51eb8d 100644
--- a/bin/sendether.sh
+++ b/bin/sendether.sh
@@ -1,10 +1,7 @@
 #!/bin/sh
 
-funtion fatal
-{
-    echo "$@" >&2
-    exit 1
-}
+# $BFAHOME must point to a directory. Did you source $BFAHOME/bin/env ?
+source ${BFAHOME}/bin/libbfa.sh || exit 1
 
 test -n "$1" ||
     fatal "Send to whom?"
diff --git a/bin/verify.sh b/bin/verify.sh
index 136baca..850690a 100755
--- a/bin/verify.sh
+++ b/bin/verify.sh
@@ -1,12 +1,9 @@
 #!/bin/bash
+# 20180619 Robert Martin-Legene <robert@nic.ar>
 
-function	cleanup
-{
-	rmfiles="$rmfiles $@"
-    	trap "rm -rf ${rmfiles}" EXIT
-}
+# $BFAHOME must point to a directory. Did you source $BFAHOME/bin/env ?
+source ${BFAHOME}/bin/libbfa.sh || exit 1
 
-trap "echo Argh;exit 1" ERR
 in=$1
 test -n "$in" || in="Hello World!"
 cd $( dirname $0 )
-- 
GitLab