staking.sh

Overview

staking.sh is a Bash script designed to manage staking operations for a blockchain node wallet interacting with smart contracts in a staking system. It automates the processes of placing new stakes, continuing existing stakes, handling epoch transitions, and managing cooler epochs related to the staking lifecycle. It also supports graceful shutdown and staking continuation cancellation via UNIX signals.

The script interacts primarily with blockchain contracts via the tvm-cli command-line interface, querying contract details and submitting transactions. It manages wallet state, licenses, and stake statuses, ensuring the node participates properly in staking epochs. It also handles balance calculations and BLS key updates to maintain staking eligibility.

Key Constants and Variables

Command-line Arguments and Options

Logging

The log() function timestamps and outputs messages to the log file specified by -l. Old logs are archived before starting.

Signal Handling

Functions

trigger_stopping_staking()

trigger_stopping_continue_staking()

update_bls_keys(bKeysFile)

place_stake()

place_continue_stake(seqNoStartOld)

calculate_reward(coolerAddress, stakeHex)

process_cooler_epoch(coolerAddress, stakeHex)

process_epoch()

Main Execution Flow

  1. Parses options and arguments.

  2. Validates node owner key, BLS keys file, and node IP.

  3. Waits for the blockchain network endpoint and wallet address availability.

  4. Retrieves wallet address and initial wallet state.

  5. If no active stakes, places a stake.

  6. If daemon mode is enabled (-d), loops continuously calling process_epoch() with sleep intervals.

  7. Otherwise, runs process_epoch() once.

Interaction with Other System Components

Important Implementation Details


Mermaid Flowchart of Main Functions and Interactions

flowchart TD
Start --> ParseArgs[Parse Args & Options]
ParseArgs --> ValidateFiles[Validate Keys & IP]
ValidateFiles --> WaitNetwork[Wait for Network Endpoint]
WaitNetwork --> GetWalletAddr[Get Wallet Address]
GetWalletAddr --> GetWalletDetails[Get Wallet Details]
GetWalletDetails --> CheckActiveStakes{Active Stakes?}
CheckActiveStakes -- No --> PlaceStake["place_stake()"]
PlaceStake --> DaemonCheck{Daemon Mode?}
CheckActiveStakes -- Yes --> DaemonCheck
DaemonCheck -- No --> ProcessEpoch["process_epoch()"]
DaemonCheck -- Yes --> Loop["Loop: process_epoch() + Sleep"]
Loop --> Loop
ProcessEpoch --> ActiveStakesLoop[For each active stake]
ActiveStakesLoop -->|status 0| HandlePreEpoch[Pre-epoch: wait & touch]
ActiveStakesLoop -->|status 1| HandleEpoch[Active epoch: continue stake, touch]
ActiveStakesLoop -->|status 2| HandleCooler[Cooler epoch: process rewards]
%% Signal handlers
Start -->|SIGINT/SIGTERM| TriggerStopStaking["trigger_stopping_staking()"]
Start -->|SIGHUP| TriggerStopContinue["trigger_stopping_continue_staking()"]
%% Other functions
PlaceStake --> UpdateBLSKeys["update_bls_keys()"]
HandleEpoch --> PlaceContinueStake["place_continue_stake()"]

Usage Examples


Parameters and Return Values Detail

Function

Parameters

Return Value

Description

trigger_stopping_staking

None

Exits script

Gracefully stops staking by touching epoch contract.

trigger_stopping_continue_staking

None

None

Cancels staking continuation upon SIGHUP signal.

update_bls_keys

bKeysFile (string)

Exits on failure

Updates BLS keys and signals node process to reload keys.

place_stake

None

None

Places a new stake using available licenses and balance.

place_continue_stake

seqNoStartOld (string)

None

Places continuation stake for an existing epoch.

calculate_reward

coolerAddress, stakeHex

None (sets REWARD)

Calculates the reward based on cooler contract balance.

process_cooler_epoch

coolerAddress, stakeHex

None

Checks cooler epoch finish and calculates rewards if needed.

process_epoch

None

None

Main loop handling stake statuses and epoch transitions.


Important Notes on Algorithms and Logic


Interaction with External Components


Environment and Prerequisites


End of staking.sh documentation