Przeglądaj źródła

contrib/localnet/darkfid-small: add scripts to test contract deployment and reorgs

oars 6 miesięcy temu
rodzic
commit
b695b24ff5

+ 6 - 1
contrib/localnet/darkfid-small/clean.sh

@@ -1,2 +1,7 @@
 #!/bin/sh
-rm -rf darkfid0 darkfid1 darkfid2
+rm -rf darkfid0 darkfid1 darkfid2 drk0 drk1 drk2 darkfid0.log darkfid1.log darkfid2.log
+sed -i -e "s|XMRIG_USER0=.*|XMRIG_USER0=\"DZnsGMCvZU5CEzvpuExnxbvz6SEhE2rn89sMcuHsppFE6TjL4SBTrKkf\"|g" tmux_sessions.sh
+sed -i -e "s|XMRIG_USER1=.*|XMRIG_USER1=\"Dae4FtyzrnQ8JNuui5ibZL4jXUR786PbyjwBsq4aj6E1RPPYjtXLfnAf\"|g" tmux_sessions.sh
+sed -i -e "s|XMRIG_USER0=.*|XMRIG_USER0=\"DZnsGMCvZU5CEzvpuExnxbvz6SEhE2rn89sMcuHsppFE6TjL4SBTrKkf\"|g" reorg-test.sh
+sed -i -e "s|XMRIG_USER1=.*|XMRIG_USER1=\"Dae4FtyzrnQ8JNuui5ibZL4jXUR786PbyjwBsq4aj6E1RPPYjtXLfnAf\"|g" reorg-test.sh
+sed -i -e "s|skip_sync =.*|skip_sync = false|g" darkfid1.toml

+ 26 - 0
contrib/localnet/darkfid-small/drk0.toml

@@ -0,0 +1,26 @@
+## drk configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+##
+## The default values are left commented. They can be overridden either by
+## uncommenting, or by using the command-line.
+
+# Blockchain network to use
+network = "localnet"
+
+# Localnet blockchain network configuration
+[network_config."localnet"]
+# Path to blockchain cache database
+cache_path = "drk0/cache"
+# Path to wallet database
+wallet_path = "drk0/wallet.db"
+
+# Password for the wallet database
+wallet_pass = "testing"
+
+# darkfid JSON-RPC endpoint
+endpoint = "tcp://127.0.0.1:48345"
+
+# Path to interactive shell history file
+history_path = "drk0/history.txt"

+ 26 - 0
contrib/localnet/darkfid-small/drk1.toml

@@ -0,0 +1,26 @@
+## drk configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+##
+## The default values are left commented. They can be overridden either by
+## uncommenting, or by using the command-line.
+
+# Blockchain network to use
+network = "localnet"
+
+# Localnet blockchain network configuration
+[network_config."localnet"]
+# Path to blockchain cache database
+cache_path = "drk1/cache"
+# Path to wallet database
+wallet_path = "drk1/wallet.db"
+
+# Password for the wallet database
+wallet_pass = "testing"
+
+# darkfid JSON-RPC endpoint
+endpoint = "tcp://127.0.0.1:48445"
+
+# Path to interactive shell history file
+history_path = "drk1/history.txt"

+ 26 - 0
contrib/localnet/darkfid-small/drk2.toml

@@ -0,0 +1,26 @@
+## drk configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+##
+## The default values are left commented. They can be overridden either by
+## uncommenting, or by using the command-line.
+
+# Blockchain network to use
+network = "localnet"
+
+# Localnet blockchain network configuration
+[network_config."localnet"]
+# Path to blockchain cache database
+cache_path = "drk2/cache"
+# Path to wallet database
+wallet_path = "drk2/wallet.db"
+
+# Password for the wallet database
+wallet_pass = "testing"
+
+# darkfid JSON-RPC endpoint
+endpoint = "tcp://127.0.0.1:48345"
+
+# Path to interactive shell history file
+history_path = "drk2/history.txt"

+ 18 - 0
contrib/localnet/darkfid-small/init-wallets.sh

@@ -0,0 +1,18 @@
+#!/bin/sh
+
+# Path to `drk` binary
+DRK="../../../drk"
+DRK0="$DRK -c drk0.toml"
+DRK1="$DRK -c drk1.toml"
+
+initialize() {
+  $1 wallet initialize
+  $1 wallet keygen
+  $1 wallet default-address 1
+  wallet=$($1 wallet address)
+  sed -i -e "s|$2|$wallet|g" tmux_sessions.sh
+  sed -i -e "s|$2|$wallet|g" reorg-test.sh
+}
+
+initialize "$DRK0" "DZnsGMCvZU5CEzvpuExnxbvz6SEhE2rn89sMcuHsppFE6TjL4SBTrKkf"
+initialize "$DRK1" "Dae4FtyzrnQ8JNuui5ibZL4jXUR786PbyjwBsq4aj6E1RPPYjtXLfnAf"

+ 73 - 0
contrib/localnet/darkfid-small/reorg-test.sh

@@ -0,0 +1,73 @@
+#!/bin/sh
+set -e
+
+# Start a tmux session with two mining and a non-mining darkfid nodes.
+# Additionally, start two xmrig daemons.
+#
+
+# Path to used binaries
+XMRIG="xmrig"
+DARKFID="LOG_TARGETS='!sled' ../../../darkfid"
+
+# Dummy mining wallet addresses so mining daemons can start
+XMRIG_USER0="DZnsGMCvZU5CEzvpuExnxbvz6SEhE2rn89sMcuHsppFE6TjL4SBTrKkf"
+XMRIG_USER1="Dae4FtyzrnQ8JNuui5ibZL4jXUR786PbyjwBsq4aj6E1RPPYjtXLfnAf"
+
+# Path to `drk` binary
+DRK="../../../drk"
+DRK0="$DRK -c drk0.toml"
+DRK1="$DRK -c drk1.toml"
+
+# First run the darkfid nodes and the miners:
+#
+#   ./clean.sh
+#   ./init-wallets.sh
+#   ./tmux_sessions.sh
+#
+# Wait for at least 20 blocks to be created.
+# Now you can run this script
+
+session=darkfid-small
+
+if [ "$1" = "-vv" ]; then
+	verbose="-vv"
+	shift
+else
+	verbose=""
+fi
+
+
+echo "=========[Stopping Node1 and Node2]=========="
+# Stop node1 and mine only with node0
+tmux send-keys -t "$session:node1.0" C-c
+tmux send-keys -t "$session:node1.1" C-c
+tmux send-keys -t "$session:node2.0" C-c
+sleep 5
+
+# Create contract txs on node0
+sh ./run-contract-test.sh "$DRK0"
+
+# Stop node0 and mine only with node1
+echo "===========[Stopping Node0]=================="
+tmux send-keys -t "$session:node0.0" C-c
+tmux send-keys -t "$session:node0.1" C-c
+sleep 5
+echo "===========[Restarting Node1]================"
+sed -i -e "s|skip_sync =.*|skip_sync = true|g" darkfid1.toml
+tmux send-keys -t "$session:node1.1" "$DARKFID -c darkfid1.toml $verbose -l darkfid1.log" Enter
+tmux send-keys -t "$session:node1.0" "$XMRIG -u x+1 -r 1000 -R 20 -o 127.0.0.1:48447 -t 2 -u $XMRIG_USER1" Enter
+sleep 2
+
+# Create contract txs on node1
+sh ./run-contract-test.sh "$DRK1"
+
+# Restart node0 and see reorg happening
+echo "=========[Restarting Node0 and Node2]========"
+tmux send-keys -t "$session:node0.1" "$DARKFID -c darkfid0.toml $verbose -l darkfid0.log" Enter
+tmux send-keys -t "$session:node0.0" "$XMRIG -u x+1 -r 1000 -R 20 -o 127.0.0.1:48347 -t 2 -u $XMRIG_USER0" Enter
+tmux send-keys -t "$session:node2.0" "$DARKFID -c darkfid2.toml $verbose -l darkfid2.log" Enter
+sleep 2
+
+# Test to check everything is working fine
+sh ./run-contract-test.sh "$DRK0"
+sh ./run-contract-test.sh "$DRK1"

+ 125 - 0
contrib/localnet/darkfid-small/run-contract-test.sh

@@ -0,0 +1,125 @@
+#!/bin/sh
+set -e
+set -x
+
+# Accept path to `drk` binary as arg or use default
+DEFAULT_DRK="../../../drk -c drk0.toml"
+DRK="${1:-$DEFAULT_DRK}"
+
+# Path to contract to be deployed
+WASM="../../../../smart-contract/membership.wasm"
+CLIENT="../../../../smart-contract/membership"
+
+# Script configuration
+OUTPUT_FOLDER=/tmp/darkfi
+mkdir -p $OUTPUT_FOLDER
+SLEEP_TIME=5
+
+# First run the darkfid nodes and the miners:
+#
+#   ./clean.sh
+#   ./init-wallets.sh
+#   ./tmux_sessions.sh
+#
+# Set the right path for WASM and CLIENT binaries for your membership contract.
+# Now you can run this script
+
+token_balance() {
+    BALANCE="$($1 wallet balance 2>/dev/null)"
+
+    # No tokens received at all yet
+    if echo "$BALANCE" | grep -q "No unspent balances found"; then
+        echo 0
+        return
+    fi
+
+    BALANCE="$(echo "$BALANCE" | grep -q "$2")"
+    # Not received yet so no entry
+    if [ $? = 1 ]; then
+        echo 0
+        return
+    fi
+
+    # OK we have the token, show the actual balance
+    echo "$BALANCE" | awk '{print $5}'
+}
+
+wait_token() {
+    while [ "$(token_balance "$1" "$2")" = 0 ]; do
+        sleep $SLEEP_TIME
+        sh ./sync-wallet.sh "$1" > /dev/null
+    done
+}
+
+deployment_status() {
+  STATUS="$($1 contract list $2)"
+
+  # contract deployment hasn't been confirmed yet
+  if echo "$STATUS" | grep -q "No history records found"; then
+      echo 0
+      return
+  fi
+
+  echo 1
+}
+
+wait_deployment() {
+    while [ "$(deployment_status "$1" "$2")" = 0 ]; do
+        sleep $SLEEP_TIME
+        sh ./sync-wallet.sh "$1" > /dev/null
+    done
+}
+
+deploy_contract() {
+  ADDRESS="$($1 contract generate-deploy | awk 'NR==3 {print $3}')"
+  $1 contract deploy $ADDRESS $WASM | tee $OUTPUT_FOLDER/deploy-$ADDRESS.tx | $1 broadcast >/dev/null 2>&1
+  echo "$ADDRESS"
+}
+
+
+tx_status() {
+  STATUS="$($1 explorer txs-history | grep $2 | awk '{print $3}')"
+
+  if echo "$STATUS" | grep -q "Broadcasted"; then
+      echo 0
+      return
+  fi
+
+  echo 1
+}
+
+wait_tx() {
+    while [ "$(tx_status "$1" "$2")" = 0 ]; do
+        sleep $SLEEP_TIME
+        sh ./sync-wallet.sh "$1" > /dev/null
+    done
+}
+
+generate_key() {
+  SECRET_KEY="$($CLIENT generate | awk 'NR==1 {print $3}')"
+  echo "$SECRET_KEY"
+}
+
+register_call() {
+  TX_ID="$($CLIENT register $2  $3 | tee $OUTPUT_FOLDER/register-$2-$3.call | $1 tx-from-calls | tee $OUTPUT_FOLDER/register-$2-$3.tx | $1 broadcast | awk 'NR==4 {print $3}')"
+  echo "$TX_ID"
+}
+
+deregister_call() {
+  TX_ID="$($CLIENT deregister $2  $3 | tee $OUTPUT_FOLDER/deregister-$2-$3.call | $1 tx-from-calls | tee $OUTPUT_FOLDER/deregister-$2-$3.tx | $1 broadcast | awk 'NR==4 {print $3}')"
+  echo "$TX_ID"
+}
+
+
+wait_token "$DRK" DRK
+CONTRACT_ADDRESS="$(deploy_contract "$DRK")"
+wait_deployment "$DRK" "$CONTRACT_ADDRESS"
+
+wait_token "$DRK" DRK
+SECRET_KEY="$(generate_key "$DRK" "$CONTRACT_ADDRESS")"
+
+TX_ID="$(register_call "$DRK" "$CONTRACT_ADDRESS" "$SECRET_KEY")"
+wait_tx "$DRK" "$TX_ID"
+
+TX_ID="$(deregister_call "$DRK" "$CONTRACT_ADDRESS" "$SECRET_KEY")"
+wait_tx "$DRK" "$TX_ID"

+ 20 - 0
contrib/localnet/darkfid-small/sync-wallet.sh

@@ -0,0 +1,20 @@
+#!/bin/sh
+set -e
+set -x
+
+# Accept path to `drk` binary as arg or use default
+DEFAULT_DRK="../../../drk -c drk0.toml"
+DRK="${1:-$DEFAULT_DRK}"
+
+sync_wallet() {
+  while true; do
+      if $1 ping 2> /dev/null; then
+          break
+      fi
+      sleep 1
+  done
+
+  $1 scan
+}
+
+sync_wallet "$DRK"

+ 9 - 8
contrib/localnet/darkfid-small/tmux_sessions.sh

@@ -6,10 +6,11 @@ set -e
 
 # Path to used binaries
 XMRIG="xmrig"
-DARKFID="LOG_TARGETS='!runtime,!sled' ../../../darkfid"
+DARKFID="LOG_TARGETS='!sled' ../../../darkfid"
 
-# Dummy mining wallet address so mining daemons can start
-XMRIG_USER="DZnsGMCvZU5CEzvpuExnxbvz6SEhE2rn89sMcuHsppFE6TjL4SBTrKkf"
+# Dummy mining wallet addresses so mining daemons can start
+XMRIG_USER0="DZnsGMCvZU5CEzvpuExnxbvz6SEhE2rn89sMcuHsppFE6TjL4SBTrKkf"
+XMRIG_USER1="Dae4FtyzrnQ8JNuui5ibZL4jXUR786PbyjwBsq4aj6E1RPPYjtXLfnAf"
 
 session=darkfid-small
 
@@ -21,13 +22,13 @@ else
 fi
 
 tmux new-session -d -s $session -n "node0"
-tmux send-keys -t $session "$XMRIG -u x+1 -r 1000 -R 20 -o 127.0.0.1:48347 -t 2 -u $XMRIG_USER" Enter
+tmux send-keys -t $session "$XMRIG -u x+1 -r 1000 -R 20 -o 127.0.0.1:48347 -t 2 -u $XMRIG_USER0" Enter
 tmux split-window -t $session -v -l 80%
-tmux send-keys -t $session "$DARKFID -c darkfid0.toml $verbose" Enter
+tmux send-keys -t $session "$DARKFID -c darkfid0.toml $verbose -l darkfid0.log" Enter
 tmux new-window -t $session -n "node1"
-tmux send-keys -t $session "$XMRIG -u x+1 -r 1000 -R 20 -o 127.0.0.1:48447 -t 2 -u $XMRIG_USER" Enter
+tmux send-keys -t $session "$XMRIG -u x+1 -r 1000 -R 20 -o 127.0.0.1:48447 -t 2 -u $XMRIG_USER1" Enter
 tmux split-window -t $session -v -l 80%
-tmux send-keys -t $session "$DARKFID -c darkfid1.toml $verbose" Enter
+tmux send-keys -t $session "$DARKFID -c darkfid1.toml $verbose -l darkfid1.log" Enter
 tmux new-window -t $session -n "node2"
-tmux send-keys -t $session "$DARKFID -c darkfid2.toml $verbose" Enter
+tmux send-keys -t $session "$DARKFID -c darkfid2.toml $verbose -l darkfid2.log" Enter
 tmux attach -t $session