# Stake Wars III **Published by:** [John Boone](https://paragraph.com/@cryptojohnboone/) **Published on:** 2022-07-27 **URL:** https://paragraph.com/@cryptojohnboone/stake-wars-iii ## Content Stake Wars: Episode III. Challenge 001Published on: 2022–07–27Updated on: 2022–07–27Submitted by: Crjobo I will guide you through the challenges of Stake War III. We will start by creating your Shardnet wallet & deploy the NEAR CLI. It will give you the opportunity to understand how staking on NEAR works.Challenge submissionNo submission si required for this challenge; it will be evaluated together with the next one (002).Useful linksWallet: https://wallet.shardnet.near.org/ Explorer: https://explorer.shardnet.near.org/Create a walletFirst step is to create a wallet. Head here and follow the steps. Do NOT FORGET to note down your secret phrase on paper. https://wallet.shardnet.near.org/Setup a Server on HetznerHetzner ServerHead up to hetzner.com and buy a Server to deploy your node and validator node. I recommend: https://www.hetzner.com/dedicated-rootserver/ax41-nvme/configurator#/ The setup will take up to 15 minutes depending on how you pay and how fast your transaction will be. It will cost you ca 40e a month When done, go to you favorite Command Line Interface (CLI) and connect to your server through ssh. The form should be sudo ssh root@YOUR-IP Then enter the password you have received from Hetzner.Setup NEAR-CLINEAR-CLI is a command-line interface that communicates with the NEAR blockchain via remote procedure calls (RPC):Setup and Installation NEAR CLIView Validator StatsNote: For security reasons, it is recommended that NEAR-CLI be installed on a different computer than your validator node and that no full access keys be kept on your validator node. Also if you configure both on the same server you will run into an issue later in the guide as you node and your validator node will want to run in // and will conflict. You will have then to kill the node before launching the validator node. First, let's make sure the linux machine is up-to-date.sudo apt update && sudo apt upgrade -y Install developer tools, Node.js, and npmThen, we will start with installing Node.js and npm:curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install build-essential nodejs PATH="$PATH" Check Node.js and npm version:node -v v18.x.xnpm -v 8.x.xInstall NEAR-CLIHere's the Github Repository for NEAR CLI.: https://github.com/near/near-cli. To install NEAR-CLI, unless you are logged in as root, which is not recommended you will need to use sudo to install NEAR-CLI so that the near binary is saved to /usr/local/binsudo npm install -g near-cli Validator StatsNow that NEAR-CLI is installed, let's test out the CLI and use the following commands to interact with the blockchain as well as to view validator stats. There are three reports used to monitor validator status:EnvironmentThe environment will need to be set each time a new shell is launched to select the correct network. Networks:GuildNetTestNetMainNetShardnet (this is the network we will use for Stake Wars) Command:export NEAR_ENV=shardnet You can also run this command to set the Near testnet Environment persistent:echo 'export NEAR_ENV=shardnet' >> ~/.bashrc NEAR CLI Commands Guide:ProposalsA proposal by a validator indicates they would like to enter the validator set, in order for a proposal to be accepted it must meet the minimum seat price. Command:Validators CurrentThis shows a list of active validators in the current epoch, the number of blocks produced, number of blocks expected, and online rate. Used to monitor if a validator is having issues. Command:near validators current Validators NextThis shows validators whose proposal was accepted one epoch ago, and that will enter the validator set in the next epoch. Command:near validators next - -We continue ?Congrats you have managed the first step in you adventure. Let's go for step 2.Stake Wars: Episode III. Challenge 002Published on: 2022-07-26 Updated on: 2022-07-15 Submitted by: Crjobo Reward: 30 Unlocked NEAR Points (UNP) This challenge is focused on deploying a node (nearcore), downloading a snapshot, syncing it to the actual state of the network, then activating the node as a validator. Useful links Wallet: https://wallet.shardnet.near.org/ Explorer: https://explorer.shardnet.near.org/Setup your nodeServer Requirements Please see the hardware requirement below:Hardware Chunk-Only Producer Specifications CPU 4-Core CPU with AVX support RAM 8GB DDR4 Storage 500GB SSDInstall required software & set the configurationPrerequisites: Before you start, you may want to confirm that your machine has the right CPU features.lscpu | grep -P '(?=.*avx )(?=.*sse4.2 )(?=.*cx16 )(?=.*popcnt )' > /dev/null && echo "Supported" || echo "Not supported" Supported Install developer tools: sudo apt install -y git binutils-dev libcurl4-openssl-dev zlib1g-dev libdw-dev libiberty-dev cmake gcc g++ python docker.io protobuf-compiler libssl-dev pkg-config clang llvm cargo Install Python pip: sudo apt install python3-pip Set the configuration:USER_BASE_BIN=$(python3 -m site --user-base)/bin export PATH="$USER_BASE_BIN:$PATH" Install Building envsudo apt install clang build-essential make Install Rust & Cargo curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh Press 1 and press enter. Source the environmentsource $HOME/.cargo/env Clone nearcore project from GitHubFirst, clone the nearcore repository.git clone https://github.com/near/nearcore cd nearcore git fetch Checkout to the commit needed. Please refer to the commit defined in this file. git checkout Compile nearcore binary In the nearcore folder run the following commands: cargo build -p neard --release --features shardnet The binary path is target/release/neard. If you are seeing issues, it is possible that cargo command is not found. Compiling nearcore binary may take a little while. Initialize working directory In order to work properly, the NEAR node requires a working directory and a couple of configuration files. Generate the initial required working directory by running: ./target/release/neard --home ~/.near init --chain-id shardnet --download-genesis img This command will create the directory structure and will generate config.json, node_key.json, and genesis.json on the network you have passed. config.json - Configuration parameters which are responsive for how the node will work. The config.json contains needed information for a node to run on the network, how to communicate with peers, and how to reach consensus. Although some options are configurable. In general validators have opted to use the default config.json provided. genesis.json - A file with all the data the network started with at genesis. This contains initial accounts, contracts, access keys, and other records which represents the initial state of the blockchain. The genesis.json file is a snapshot of the network state at a point in time. In contacts accounts, balances, active validators, and other information about the network. node_key.json - A file which contains a public and private key for the node. Also includes an optional account_id parameter which is required to run a validator node (not covered in this doc). data/ - A folder in which a NEAR node will write it's state. Replace the config.json From the generated config.json, there two parameters to modify: boot_nodes: If you had not specify the boot nodes to use during init in Step 3, the generated config.json shows an empty array, so we will need to replace it with a full one specifying the boot nodes. tracked_shards: In the generated config.json, this field is an empty. You will have to replace it to "tracked_shards": [0] rm ~/.near/config.json wget -O ~/.near/config.json https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/shardnet/config.json Get latest snapshot IMPORTANT: NOT REQUIRED TO GET SNAPSHOT AFTER HARDFORK ON SHARDNET DURING 2022-07-18 Install AWS Cli sudo apt-get install awscli -y Download snapshot (genesis.json) // IMPORTANT: NOT REQUIRED TO GET SNAPSHOT AFTER HARDFORK ON SHARDNET DURING 2022-07-18 cd ~/.near wget https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/shardnet/genesis.json If the above fails, AWS CLI may be oudated in your distribution repository. Instead, try: pip3 install awscli --upgrade Run the node To start your node simply run the following command: cd ~/nearcore ./target/release/neard --home ~/.near run img The node is now running you can see log outputs in your console. Your node should be find peers, download headers to 100%, and then download blocks. Activating the node as validator Authorize Wallet Locally A full access key needs to be installed locally to be able to sign transactions via NEAR-CLI. You need to run this command: near login Note: This command launches a web browser allowing for the authorization of a full access key to be copied locally. 1 – Copy the link in your browser 2 – Grant Access to Near CLI img 3 – After Grant, you will see a page like this, go back to console img 4 – Enter your wallet and press Enter img Check the validator_key.json Run the following command: cat ~/.near/validator_key.json Note: If a validator_key.json is not present, follow these steps to create one Create a validator_key.json Generate the Key file: near generate-key ---> xx.factory.shardnet.near WHERE xx is you pool name Copy the file generated to shardnet folder: Make sure to replace by your accountId cp ~/.near-credentials/shardnet/YOUR_WALLET.json ~/.near/validator_key.json Edit “account_id” => xx.factory.shardnet.near, where xx is your PoolName Change private_key to secret_key Note: The account_id must match the staking pool contract name or you will not be able to sign blocks.\ File content must be in the following pattern: { "account_id": "xx.factory.shardnet.near", "public_key": "ed25519:HeaBJ3xLgvZacQWmEctTeUqyfSU4SDEnEwckWxd92W2G", "secret_key": "ed25519:****" } Start the validator node target/release/neard run Setup Systemd Command: sudo vi /etc/systemd/system/neard.service Paste: [Unit] Description=NEARd Daemon Service [Service] Type=simple User= #Group=near WorkingDirectory=/home//.near ExecStart=/home//nearcore/target/release/neard run Restart=on-failure RestartSec=30 KillSignal=SIGINT TimeoutStopSec=45 KillMode=mixed [Install] WantedBy=multi-user.target Note: Change USER to your paths Command: sudo systemctl enable neard Command: sudo systemctl start neard If you need to make a change to service because of an error in the file. It has to be reloaded: sudo systemctl reload neard Watch logs Command: journalctl -n 100 -f -u neard Make log output in pretty print Command: sudo apt install ccze View Logs with color Command: journalctl -n 100 -f -u neard | ccze -A Becoming a Validator In order to become a validator and enter the validator set, a minimum set of success criteria must be met. The node must be fully synced The validator_key.json must be in place The contract must be initialized with the public_key in validator_key.json The account_id must be set to the staking pool contract id There must be enough delegations to meet the minimum seat price. See the seat price here. A proposal must be submitted by pinging the contract Once a proposal is accepted a validator must wait 2-3 epoch to enter the validator set Once in the validator set the validator must produce great than 90% of assigned blocks Check running status of validator node. If “Validator” is showing up, your pool is selected in the current validators list. Let's go to challenge 3 Stake Wars: Episode III. Challenge 003 Published on: 2022-07-27 Updated on: 2022-07-27 Submitted by: Crjobo Rewards: 10 Unlocked NEAR Points (UNP) Deploy a new staking pool for your validator. Do operations on your staking pool to delegate and stake NEAR. Useful links Wallet: https://wallet.shardnet.near.org/ Explorer: https://explorer.shardnet.near.org/ Mounting a staking pool NEAR uses a staking pool factory with a whitelisted staking contract to ensure delegators’ funds are safe. In order to run a validator on NEAR, a staking pool must be deployed to a NEAR account and integrated into a NEAR validator node. Delegators must use a UI or the command line to stake to the pool. A staking pool is a smart contract that is deployed to a NEAR account. Deploy a Staking Pool Contract Deploy a Staking Pool Calls the staking pool factory, creates a new staking pool with the specified name, and deploys it to the indicated accountId. near call factory.shardnet.near create_staking_pool '{"staking_pool_id": "", "owner_id": "", "stake_public_key": "", "reward_fee_fraction": {"numerator": 5, "denominator": 100}, "code_hash":"DD428g9eqLL8fWUxv8QSpVFzyHi1Qd16P8ephYCTmMSZ"}' --accountId="" --amount=30 --gas=300000000000000 From the example above, you need to replace: Pool ID: Staking pool name, the factory automatically adds its name to this parameter, creating {pool_id}.{staking_pool_factory} Examples: If pool id is stakewars will create : stakewars.factory.shardnet.near Owner ID: The SHARDNET account (i.e. stakewares.shardnet.near) that will manage the staking pool. Public Key: The public key in your validator_key.json file. 5: The fee the pool will charge (e.g. in this case 5 over 100 is 5% of fees). Account Id: The SHARDNET account deploying and signing the mount tx. Usually the same as the Owner ID. Be sure to have at least 30 NEAR available, it is the minimum required for storage. Example : near call stake_wars_validator.factory.shardnet.near --amount 30 --accountId stakewars.shardnet.near --gas=300000000000000 To change the pool parameters, such as changing the amount of commission charged to 1% in the example below, use this command: near call update_reward_fee_fraction '{"reward_fee_fraction": {"numerator": 1, "denominator": 100}}' --accountId --gas=300000000000000 If there is a “True” at the End. Your pool is created. You have now configure your Staking pool. Check your pool is now visible on https://explorer.shardnet.near.org/nodes/validators Transactions Guide Deposit and Stake NEAR Command: near call deposit_and_stake --amount --accountId --gas=300000000000000 Unstake NEAR Amount in yoctoNEAR. Run the following command to unstake: near call unstake '{"amount": ""}' --accountId --gas=300000000000000 To unstake all you can run this one: near call unstake_all --accountId --gas=300000000000000 Withdraw Unstaking takes 2-3 epochs to complete, after that period you can withdraw in YoctoNEAR from pool. Command: near call withdraw '{"amount": ""}' --accountId --gas=300000000000000 Command to withdraw all: near call withdraw_all --accountId --gas=300000000000000 Ping A ping issues a new proposal and updates the staking balances for your delegators. A ping should be issued each epoch to keep reported rewards current. Command: near call ping '{}' --accountId --gas=300000000000000 Balances Total Balance Command: near view get_account_total_balance '{"account_id": ""}' Staked Balance Command: near view get_account_staked_balance '{"account_id": ""}' Unstaked Balance Command: near view get_account_unstaked_balance '{"account_id": ""}' Available for Withdrawal You can only withdraw funds from a contract if they are unlocked. Command: near view is_account_unstaked_balance_available '{"account_id": ""}' Pause / Resume Staking Pause Command: near call pause_staking '{}' --accountId Resume Command: near call resume_staking '{}' --accountId Let's go to challenge 4 Stake Wars: Episode III. Challenge 004 Published on: 2022-07-27 Updated on: 2022-07-27 Submitted by: Crjobo Rewards: 15 Unlocked NEAR Points (UNP) Setup tools for monitoring node status. Install and use RPC on port 3030 to get useful information for keep your node working. Usefull links Wallet: https://wallet.shardnet.near.org/ Explorer: https://explorer.shardnet.near.org/ Monitor and make alerts An email notification can make it more comfortable to maintain a validator up and running. Achieve to be a validator confirming transactions on testnet and get >95% of uptime. Log Files The log file is stored either in the ~/.nearup/logs directory or in systemd depending on your setup. Systemd Command: journalctl -n 100 -f -u neard | ccze -A Log file sample: Validator | 1 validator INFO stats: #85079829 H1GUabkB7TW2K2yhZqZ7G47gnpS7ESqicDMNyb9EE6tf Validator 73 validators 30 peers ⬇ 506.1kiB/s ⬆ 428.3kiB/s 1.20 bps 62.08 Tgas/s CPU: 23%, Mem: 7.4 GiB Validator: A “Validator” will indicate you are an active validator 73 validators: Total 73 validators on the network 30 peers: You current have 30 peers. You need at least 3 peers to reach consensus and start validating #46199418: block – Look to ensure blocks are moving RPC Any node within the network offers RPC services on port 3030 as long as the port is open in the nodes firewall. The NEAR-CLI uses RPC calls behind the scenes. Common uses for RPC are to check on validator stats, node version and to see delegator stake, although it can be used to interact with the blockchain, accounts and contracts overall. Find many commands and how to use them in more detail here: https://docs.near.org/api/rpc/introduction Command: sudo apt install curl jq Common Commands: ####### Check your node version: Command: curl -s http://127.0.0.1:3030/status | jq .version ####### Check Delegators and Stake Command: near view .factory.shardnet.near get_accounts '{"from_index": 0, "limit": 10}' --accountId .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 (""))' | 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 ("POOL_ID"))' And here we are!!!! I hope this guide has helped you to enter the world of NEAR. ## Publication Information - [John Boone](https://paragraph.com/@cryptojohnboone/): Publication homepage - [All Posts](https://paragraph.com/@cryptojohnboone/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@cryptojohnboone): Subscribe to updates - [Twitter](https://twitter.com/Cryptojohnboone): Follow on Twitter