Stake Wars III — Your own shardnet validator is Near

2pilot
10 min readAug 17, 2022

--

Episodes

  1. ___________Phantom near
  2. _______Attack of the referrals
  3. ______Revenge of the near gith
  4. ____A new staking pool validator
  5. __Ping.sh and crontab -e strike back
  6. _Return of the concepts consolidation
  1. Phantom near

To create your shardnet (testnet) wallet, simply go to shardnet wallet page and press Create Account. After that type your preferred account name and press “Reserve My Account ID” if the name is available.

Mine is already taken, so I’ll use another one as an example.

Next generate your wallet passphrase.

Copy seed phrase, save it securely and press continue. After that insert word number “N” from the 12 words sid phrase you copied previously ( 4th word in my case ) and press Verify & Complete.

Once account it is verified and created your would need to paste the whole seed phrase and login

You will see the screen like this

Keep in mind

Note that your balance might be zero. You can still continue in that case to the episode 4 — staking pool creation. Also it might worth checking out official discord #stake-wars-announcements #stake-wars-tokens_delegation channels.

And that’s it for wallet creation, lets continue.

2. Attack of the referrals

For this testnet I’m using Hetzner as a server provider. It offers great service, recommended by the challenge organizers and using this link to register will give you 20 euro signup bonus. Once registered make sure you selected Cloud in the drop down list top of the page

Add new project by clicking NEW PROJECT and give it some name, for example

Once it is done click ADD SERVER, leave defaults options at the top

And choose a server and press press CREATE & BUY NOW

Currently minimum system requirements according to official docs are 4CPU and 8GB Ram, but from my observation at the moment of writing having less than 16GB Ram won’t let you process chunks in time. So I would go with CX51. Good news is that you do not pay in advance, but according to your usage time. So if you host it for 15 days you will only pay half of the price — approximately 18 euro. Also there is always an option to downgrade your existing server to cheaper version as long as storage size doesn’t change.

Once it’s is created and ready you will get an email with ip and password of your server.

To access it you would need to run a terminal on mac

Or download terminal emulator for windows, for example MobaXterm or Putty.

Once terminal is open connect to your server by running

ssh root@your_ip

type yes and hit enter if this is your first connection to the server, type your password and hit enter. Note, while typing your password it will look like nothing happens and you won’t see it’s length, but that’s fine, type it, trust your instincts and let the near guide you

For example

Next let’s install all required software to your newly created machine.

3. Revenge of the near gith

To install near software, first let’s update preinstalled software by typing

sudo apt update && sudo apt upgrade -y

This could take several minutes to complete and will look something like

Next let’s install near software and all it’s dependencies (copy the whole command)

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash - && sudo apt install build-essential nodejs -y && PATH="$PATH" && sudo npm install -g near-cli && echo 'export NEAR_ENV=shardnet' >> ~/.bashrc && echo 'export NEAR_ENV=shardnet' >> ~/.bash_profile && source $HOME/.bash_profile

Check components installed correctly by running:

node -v
near proposals

Should give an output like this

We will discuss later (in episode 6) what this means.

Now let’s install even moreee software that will help us run different commands later

sudo apt install -y git binutils-dev libcurl4-openssl-dev zlib1g-dev libdw-dev libiberty-dev cmake gcc g++ python3 docker.io protobuf-compiler libssl-dev pkg-config clang llvm cargo python3-pip clang build-essential make jq && USER_BASE_BIN=$(python3 -m site --user-base)/bin && export PATH="$USER_BASE_BIN:$PATH" && export RUSTUP_INIT_SKIP_PATH_CHECK=yes && curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh && source $HOME/.cargo/env

Once you see this screen hit enter to continue with the default installation

Download near node source code with the latest recommended updates

git clone https://github.com/near/nearcore && cd nearcore && git fetch && commit=$(curl https://raw.githubusercontent.com/near/stakewars-iii/main/commit.md) && git checkout $commit

Build and initialize source code

cargo build -p neard --release --features shardnet && ./target/release/neard --home ~/.near init --chain-id shardnet --download-genesis && rm ~/.near/config.json && wget -O ~/.near/config.json https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/shardnet/config.json && cd ..

Add public address to config to improve validators to validator communication

public_addr="$(jq -r .public_key .near/node_key.json)@$(wget -U curl -qO- ifconfig.me):24567" && new_config=$(jq '.network=(.network + {"public_addrs": ["'$public_addr'"]})' .near/config.json) && echo -E "${new_config}" > .near/config.json

Link your validator to your wallet by typing

near login

Copy generated link and paste it to your browser

You will see the following page, click next

Click connect

Enter your account id and press Confirm

Wait until you see

Go back to your terminal and insert your account id one more time in the terminal and hit enter

Check key for your account is created by typing

ls -lah .near-credentials/shardnet/

Next lets start your validator

4. A new staking pool validator

To start validator, first let’s save your account alias to reuse it in scripts later. As an example for this article we will use dewit but you should replace it with your own alias.

export NEAR_ACCOUNT_ALIAS=dewit

Next create validator key json file

near generate-key $NEAR_ACCOUNT_ALIAS.factory.shardnet.near && sed -i "s/$NEAR_ACCOUNT_ALIAS.shardnet.near/$NEAR_ACCOUNT_ALIAS.factory.shardnet.near/" ~/.near-credentials/shardnet/$NEAR_ACCOUNT_ALIAS.shardnet.near.json && cp ~/.near-credentials/shardnet/$NEAR_ACCOUNT_ALIAS.shardnet.near.json ~/.near/validator_key.json

Now, when everything is configured let’s create a near service to control your node in a generic way, attempt to restart it on failure and start after server reboot automatically

wget -O /etc/systemd/system/neard.service https://raw.githubusercontent.com/stanisloe/near-stake-wars/main/neard.service && systemctl enable neard

To start your node run

systemctl start neard

You should give it some time to sync, to check the logs you can type

journalctl -n 100 -f -u neard

and check that there is no downloading headers message like on a screenshot above. To exit press

control + c

on mac

ctrl + c

on windows.

If you have enough near in your wallet — 30 in the current case, you can create your staking pool validator with 5% commission

near call factory.shardnet.near create_staking_pool '{"staking_pool_id": "'$NEAR_ACCOUNT_ALIAS'", "owner_id": "'$NEAR_ACCOUNT_ALIAS.shardnet.near'", "stake_public_key": "'$(cat ~/.near/validator_key.json | jq -r '.public_key')'", "reward_fee_fraction": {"numerator": 5, "denominator": 100}, "code_hash":"DD428g9eqLL8fWUxv8QSpVFzyHi1Qd16P8ephYCTmMSZ"}' --accountId=$NEAR_ACCOUNT_ALIAS.shardnet.near --gas=300000000000000 --amount=30

Wait for transaction to propagate and if it was successful write this command to check if your proposal was accepted.

near proposals | grep $(echo $NEAR_ACCOUNT_ALIAS)

To make your validator active-first run this command to get info about seat price

near validators current | grep "seat price"

This should give you output like this

In our case the price is 200, so let’s save it like

export SEAT_PRICE=200

and delegate tokens to your pool

near call $NEAR_ACCOUNT_ALIAS.factory.shardnet.near deposit_and_stake --deposit $SEAT_PRICE --accountId $NEAR_ACCOUNT_ALIAS.shardnet.near

Note the seat price is dynamic, so if you have more credits available-use them, but leave something for commission.

If everything was successful you should find yourself in the output here

near validators current | grep $(echo $NEAR_ACCOUNT_ALIAS)

5. Ping.sh and crontab -e strike back

Lets learn how to keep your validator up and running.

Create directories

mkdir /root/scripts /root/logs

Create ping script

wget -O /root/scripts/ping.sh https://raw.githubusercontent.com/stanisloe/near-stake-wars/main/ping-template.sh && sed -i "s/%alias%/'$NEAR_ACCOUNT_ALIAS'/" /root/scripts/ping.sh && chmod 777 /root/scripts/ping.sh

Now let’s practice some jedi tricks:

crontab -e

If you run this command for the first time, the following message will pop up

Press 2 and hit enter.

To go to the end of file press:

Shift + g

Press letter o, to move your cursor to the new line and start editing file

Copy this line

0 */2 * * * sh /root/scripts/ping.sh

And press control + v to paste it. To exit and save changes — press Esc and the following characters from your keyboard sequentially

:wq

Should look something like this

Hit enter and the job is done.

If this didn’t work for you and you feel something like this

you can always fallback to nano editor by typing

select-editor

and choosing the first option.

Some of the useful for troubleshooting commands:

To check the logs

journalctl -n 100 -f -u neard

Check your node version: Command:

curl -s http://127.0.0.1:3030/status | jq .versio

Check Delegators and Stake Command:

near view $NEAR_ACCOUNT_ALIAS.factory.shardnet.near get_accounts '{"from_index": 0, "limit": 50}' --accountId $NEAR_ACCOUNT_ALIAS.shardnet.near

Check Reason Validator Kicked Command:

curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' 127.0.0.1:3030 | jq -c '.result.prev_epoch_kickout[] | select(.account_id | contains ("'$NEAR_ACCOUNT_ALIAS'"))' | jq .reason

Check Blocks Produced / Expected Command:

curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' 127.0.0.1:3030 | jq -c '.result.current_validators[] | select(.account_id | contains ("'$NEAR_ACCOUNT_ALIAS'.factory.shardnet.near"))'

6. Return of the concepts consolidation

Lets recap what we just did and reinforce our knowledge.

After we rented a server and installed all necessary software there we used this command to check if near is installed correctly

near proposals

This command shows all active and non active validators that created a staking pool and executed ping. Each of them can have one of the following status:

Proposal(Accepted) — it could be either active or non active that is waiting for his seat price to satisfy current requirement.

Rollover — this is an active validator that was selected to a new epoch but haven’t executed ping command. We configured to run ping command every 2 hours, so this shouldn’t be an issue. Keep in mind that for each ping command there is a commission so you need some balance on your wallet.

Proposal(Declined) — this means you will be kicked out in the next epoch. There could be several reasons: you didn’t ping your node during the whole epoch, your seat price is lower than the price in next epoch, you missed a lot of chunks like this guy

Epoch — after producing specific amount of blocks epoch number is increased by 1. From my observations for the current testnet it takes about 7–11 hours to update epoch number. Some actions like becoming an active validator, unstake tokens from a delegator require several epochs to complete since transaction creation.

In episode 3 we inserted some weird link to the browser to s̶h̶a̶r̶e̶ ̶w̶i̶t̶h̶ ̶e̶v̶e̶r̶y̶o̶n̶e̶ ̶y̶o̶u̶r̶ ̶b̶r̶o̶w̶s̶i̶n̶g̶ ̶h̶i̶s̶t̶o̶r̶y̶ actually link your wallet with your server so that you can perform any actions there are on ui from your terminal: stake near, send near etc.

Also in the same episode we converted near code to something your server can understand and run. When the new code with fixes and improvements from the team is available (recommended commit is updated) we should repeat the same procedure. This could be done with the following command:

cd /root/nearcore && git fetch && commit=$(curl https://raw.githubusercontent.com/near/stakewars-iii/main/commit.md) && git checkout $commit && cargo build -p neard --release --features shardnet && cd ..

wait until it is finished and restart your node

systemctl restart neard.service

Note that sometimes this won’t be enough and extra actions should be done like updating config file, removing data folder etc.

And that’s it for now. Hopefully after going through the article near will be always with and you will embrace the shardnet side of the chain. If you found it useful don’t be shy to hit the 👏 button or delegate some test near tokens to my staking pool: dewit.factory.shardnet.near

If noticed some issues or a question you have, a comments section or my discord: sanchous#5793 can use.

--

--