Skip to content

Instantly share code, notes, and snippets.

@arno01
Last active August 2, 2022 09:25
Show Gist options
  • Save arno01/32b7bb5c6c072c07ee6b57e93fc709ef to your computer and use it in GitHub Desktop.
Save arno01/32b7bb5c6c072c07ee6b57e93fc709ef to your computer and use it in GitHub Desktop.
Akash provider safeguard script against freeloaders

Akash Provider safeguard script against freeloaders

This script issues akash tx market lease withdraw only against leases which ran out of balance.

Motivation

The deployment gets automatically closed when it runs out of balance in escrow account. But the balance settlement occurs only after a lease withdraw transaction.

Without timely withdrawal from the escrow accounts freeloaders can deploy an expensive deployment which will ran out of 5 AKT deposit by far and still be up & running until provider withdraws the lease.

The default --withdrawal-period is set to 24h which is too much for large providers and, at the same time, setting this parameter to a low value would cause a lease withdrawal TX for every deployment causing unnecessary fee spending.

How it works?

It takes the active leases (akash query market lease list) per provider you set, then it checks balance remaining per every deployment. After that it withdraws from the escrow account (akash tx market lease withdraw) only those dseq's which ran out of balance.

It should run on a provider side or wherever it's got provider's key (for issuing akash tx market lease withdraw) and the access to Akash RPC node for broadcasting that TX.

I suggest to wrap it into a shell script and add it to crontab to run every 1 minute.

# revision 6

# Note: [as per Adam] this script assumes that there is only one lease per deployment.

export AKASH_OUTPUT=json
export AKASH_NODE=http://....
PROVIDER=akash1vky0uh4wayh9npd74uqesglpaxwymynnspf6a4

HEIGHT=$(akash query block | jq -r '.block.header.height')
akash query market lease list \
  --provider $PROVIDER \
  --gseq 0 --oseq 0 \
  --state active --page 1 --limit 5000 \
  | jq -r '.leases[].lease | [(.lease_id | .owner, (.dseq|tonumber), (.gseq|tonumber), (.oseq|tonumber), .provider)] | @tsv | gsub("\\t";",")' \
    | while IFS=, read owner dseq gseq oseq provider; do \
      REMAINING=$(akash query escrow blocks-remaining --dseq $dseq --owner $owner | jq -r '.balance_remaining')
      
      ## FOR DEBUGGING/INFORMATIONAL PURPOSES
      echo "INFO: $owner/$dseq/$gseq/$oseq balance remaining $REMAINING"

      if (( $(echo "$REMAINING < 0" | bc -l) )); then

        ## UNCOMMENT WHEN READY
        ## echo "( sleep 2s; cat key-pass.txt; cat key-pass.txt ) | akash tx market lease withdraw --provider $provider --owner $owner --dseq $dseq --oseq $oseq --gseq $gseq --gas-prices=0.025uakt --gas=auto --gas-adjustment=1.3 -y;
        ## sleep 10
        ## TODO: sleep 10 is necessary as a safeguard against account sequence re-use.
        ## BUG: this script needs NOT to run at the same time provider withdraws the lease.

        ## FOR DEBUGGING PURPOSES, COMMENT WHEN READY
        echo "INFO: akash tx market lease withdraw --provider $provider --owner $owner --dseq $dseq --oseq $oseq --gseq $gseq --gas-prices=0.025uakt --gas=auto --gas-adjustment=1.3 -y";

      fi
      done
@andy108369
Copy link

@88plug
Copy link

88plug commented Aug 1, 2022

# revision 7

# Note: [as per Adam] this script assumes that there is only one lease per deployment.
apt-get install -y bc jq
export AKASH_OUTPUT=json
export AKASH_NODE=http://rpc.xeon.computer:26657
PROVIDER=akash19yhu3jgw8h0320av98h8n5qczje3pj3u9u2amp

HEIGHT=$(akash query block | jq -r '.block.header.height')
akash query market lease list \
  --provider $PROVIDER \
  --gseq 0 --oseq 0 \
  --state active --page 1 --limit 5000 \
  | jq -r '.leases[].lease | [(.lease_id | .owner, (.dseq|tonumber), (.gseq|tonumber), (.oseq|tonumber), .provider)] | @tsv | gsub("\\t";",")' \
    | while IFS=, read owner dseq gseq oseq provider; do \
      REMAINING=$(akash query escrow blocks-remaining --dseq $dseq --owner $owner | jq -r '.balance_remaining')
      ## FOR DEBUGGING/INFORMATIONAL PURPOSES
      echo "INFO: $owner/$dseq/$gseq/$oseq balance remaining $REMAINING"

      if (( $(echo "$REMAINING < 0" | bc -l) )); then

        ## UNCOMMENT WHEN READY
        ( sleep 2s; cat key-pass.txt; cat key-pass.txt ) | akash tx market lease withdraw --provider $provider --owner $owner --dseq $dseq --oseq $oseq --gseq $gseq --gas-prices=0.025uakt --gas=auto --gas-adjustment=1.3 -y --from $provider
        sleep 10
        ## TODO: sleep 10 is necessary as a safeguard against account sequence re-use.
        ## BUG: this script needs NOT to run at the same time provider withdraws the lease.

        ## FOR DEBUGGING PURPOSES, COMMENT WHEN READY
        #echo "INFO: akash tx market lease withdraw --provider $provider --owner $owner --dseq $dseq --oseq $oseq --gseq $gseq --gas-prices=0.025uakt --gas=auto --gas-adjustment=1.3 -y";

      fi
      done

Needed to add dependencies + --owner and remove "echo" to make it work.

@andy108369
Copy link

andy108369 commented Aug 1, 2022

@88plug I think you don't need this script since akash provider v0.16.4.

PR akash-network/node#1576

Tested here https://github.com/ovrclk/engineering/issues/271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment