BlockKeeperEpochContract.sol
Overview
BlockKeeperEpochContract.sol implements the BlockKeeperEpoch contract, which manages the lifecycle, staking, and slashing mechanisms for a block keeper node during a specific epoch. It controls the node's stake, reputation, and operational parameters, and interacts with related contracts to coordinate epoch transitions, stake continuation, and penalties for misbehavior.
This contract maintains epoch state data, verifies caller permissions, manages stakes including continuation stakes, handles slashing (penalties), and coordinates the transition to a "cooler" contract after epoch completion. It is central to the block keeper node's participation in consensus during an epoch, providing methods to update stake, handle epoch lifecycle events, and enforce rules.
Contract Structure and State Variables
The contract stores crucial epoch-specific data such as public keys, stake amounts, reputation coefficients, IP addresses, licenses, and epoch timing parameters.
_owner_pubkey(staticuint256): Owner’s public key, immutable on deployment._root(address): Address of the root contract managing block keepers._seqNoStart,_seqNoFinish(uint64): Start and finish sequence numbers for the epoch._bls_pubkey(bytes): BLS public key used for cryptographic signing._stakeand_stakeContinue(uint128): Stake amounts for current and continuation epochs._licensesand_licensesContinue(LicenseStake[]): License stakes for this epoch and continuation._virtualStake and _virtualStakeContinue (
optional(uint128)): Optional virtual stakes._sumReputationCoef,_sumReputationCoefContinue(uint128): Sum reputation coefficients._signerIndex,_signerIndexContinue(uint16): Signer indices for current and continuation epochs._proxyListContinue(mapping(uint8 => string)): Proxy list for continuation epoch._myIp(string): IP address associated with the node._reward_sum(uint128): Sum of rewards for this epoch._isContinue(bool): Indicates if the epoch is continuing from a previous one._is_full_slashing(bool): Flag indicating if full stake slashing is active._is_touching(bool): Indicates if the epoch is being "touched" (epoch finalization triggered)._slash_type(optional(uint8)): Type of slashing pending._busy_seqno(uint64): Sequence number marking busy state for epoch touch._gparam(uint256): General parameter for configuration or tracking._nodeVersion, _nodeVersionContinue (
optional(string)): Node software version strings._code(mapping(uint8 => TvmCell)): Mapping holding contract codes by identifier.
Constructor
constructor (
uint64 waitStep,
uint64 epochDuration,
bytes bls_pubkey,
mapping(uint8 => TvmCell) code,
uint128 sumReputationCoef,
uint16 signerIndex,
LicenseStake[] licenses,
optional(uint128) virtualStake,
uint128 reward_sum,
string myIp,
bool isContinue,
optional(uint32) timeStart,
optional(string) nodeVersion
)
Description
Initializes a new BlockKeeperEpoch instance with epoch timing, stake, cryptographic keys, licenses, and other parameters. It verifies the contract code hashes for security, calculates the owner address, and ensures the caller is authorized based on whether this is a continuation epoch or not.
It also informs the root contract about the active block keeper and updates the node wallet with locking stake details.
Parameters
waitStep: Delay or wait step between operations.epochDuration: Duration of the epoch in blockchain sequence numbers.bls_pubkey: BLS public key for signing.code: Mapping of contract code cells used by this contract.sumReputationCoef: Reputation coefficient sum for the node.signerIndex: Index of the signer in the validator set.licenses: Array of license stakes representing delegated stakes.virtualStake: Optional virtual stake amount.reward_sum: Sum of rewards accumulated for this epoch.myIp: Node IP address as string.isContinue: Flag indicating if this is a continuation from a previous epoch.timeStart: Optional UNIX timestamp marking epoch start.nodeVersion: Optional node software version.
Usage Example
new BlockKeeperEpoch(
waitStep,
epochDuration,
bls_pubkey,
code,
sumReputationCoef,
signerIndex,
licenses,
virtualStake,
reward_sum,
myIp,
false, // isContinue flag
optional.empty, // timeStart
optional.empty // nodeVersion
);
Key Functions
setStake
function setStake(uint128 numberOfActiveBlockKeepers, uint256 gparam) public senderIs(_root) accept
Updates the number of active block keepers and a general parameter. Only callable by the root contract.
Parameters:
numberOfActiveBlockKeepers: Number of active nodes participating.gparam: Arbitrary configuration or state parameter.
Usage: Used by root contract to update epoch state during operation.
continueStake
function continueStake(
bytes bls_pubkey,
uint16 signerIndex,
LicenseStake[] licenses,
optional(uint128) virtualStake,
mapping(uint8 => string) ProxyList,
uint128 sumReputationCoef,
optional(string) nodeVersionContinue
) public senderIs(_owner_address) accept
Allows the owner to continue staking into the next epoch, setting continuation parameters.
Parameters:
bls_pubkey: New BLS public key for continuation.signerIndex: Signer index for the continued epoch.licenses: License stakes for continuation.virtualStake: Optional virtual stake for continuation.ProxyList: Proxy list mapping for the continuation epoch.sumReputationCoef: Reputation coefficient for continuation.nodeVersionContinue: Optional software version for continuation.
If already continued, it calls
continueStakeNotAccepton the node wallet.Sets continuation-related state variables and calls
continueStakeAccepton the node wallet.
cancelContinueStake
function cancelContinueStake() public senderIs(_owner_address) accept
Allows the owner to cancel a previously set continuation stake, reverting continuation state.
Requires continuation to be active.
Calls internal
cancelContinueStakeInto update state and notify node wallet.
slash
function slash(uint8 slash_type) public senderIs(_owner_address) accept
Implements slashing (penalty) logic based on slash_type:
If touching (epoch finalization) is in progress, stores slash type for deferred execution.
If
FULL_STAKE_SLASH, triggers full stake slashing viadestroy_full_slash().Otherwise, performs partial slashing via
part_slash(slash_type).
touch
function touch() public accept
Triggers epoch finalization actions if the epoch has ended:
Validates epoch completion and busy state.
Requires licenses to be set.
If no full slashing is active, sets touching flag and calls root to decrease active block keeper count.
cantDelete
function cantDelete() public senderIs(_root) accept
Called by root contract if deletion of the epoch contract is not permitted:
Resets touching flag.
If a slash type is pending, processes slashing accordingly.
canDelete
function canDelete(uint256 reward, uint64 epochDuration, uint64 waitStep, uint128 reward_sum) public view senderIs(_root) accept
Initiates destruction of this epoch contract and transition to cooling phase.
Calls
destroy()with continuation parameters.
destroy
function destroy(uint64 epochDurationContinue, uint64 waitStepContinue, uint128 reward_sum_continue) public view senderIs(address(this)) accept
Transitions the epoch to a "cooler" contract managing post-epoch cooldown and stake finalization.
Constructs state initialization data.
Updates or destroys the proxy list contract depending on continuation state.
Instantiates a new
BlockKeeperCoolercontract with current and continuation state parameters.
destroy_full_slash
function destroy_full_slash() public view senderIs(address(this)) accept
Executes full stake slashing and destroys this epoch contract immediately.
Notifies node wallet to slash full stake.
Destroys proxy list contract.
Decreases active block keeper count in root contract with full slashing flag.
part_slash
function part_slash(uint8 slash_type) private
Handles partial slashing by reducing stakes proportionally across licenses and virtual stake.
Iterates over
_licenses, reducing each license stake proportionally.Calls node wallet to apply partial slashing.
Adjusts total stake and virtual stake accordingly.
Decreases stakes in root contract.
Resets continuation flags if continuation was active.
slashPartHelper
function slashPartHelper(uint i, uint8 slash_type, uint128 slash_stake) private returns(uint128)
Helper function to calculate and update the slashed amount for a specific license.
Decreases license stake by proportional amount.
Returns updated total slashed stake.
Private Utility Methods
ensureBalance
function ensureBalance() private pure
Ensures the contract has sufficient balance to cover deployment fees or operations. Mints currency if balance is below threshold.
Fallback Function
receive() external
Empty fallback function to accept incoming transfers.
Getters
getDetails()returns detailed epoch state including keys, addresses, sequence numbers, stake, continuation status, and IP.getProxyListContinue()returns the continuation proxy list.getEpochCoolerCodeHash()returns the code hash of the epoch cooler contract.getVersion()returns contract version and name.
Interactions with Other Contracts
BlockKeeperContractRoot: Manages global block keeper state (e.g., active block keeper count, stake changes).
AckiNackiBlockKeeperNodeWallet: Node wallet contract, updates lock stake and handles continuation and slashing notifications.
BlockKeeperEpochProxyList: Manages proxy list for epoch, supporting update or destruction on epoch transitions.
BlockKeeperCooler: Contract instantiated after epoch completion to handle cooldown and final stake settlement.
This contract heavily relies on these contracts to delegate responsibilities and maintain the block keeper network's integrity and state consistency.
Important Implementation Details
Authorization: Functions use
senderIs()modifier to restrict calls to authorized entities (e.g., root or owner).Epoch Timing: Uses blockchain sequence numbers (
block.seqno) and timestamps to manage epoch lifecycle.Stake Continuation: Supports continuation of stakes across epochs with separate continuation state variables.
Slashing: Supports both full and partial slashing with deferred execution if needed.
Proxy List Management: Updates or destroys proxy list contracts during epoch transitions.
Balance Management: Ensures contract balance is sufficient to cover operations by minting when needed.
Diagram: Contract Structure and Core Methods
classDiagram
class BlockKeeperEpoch {
-_owner_pubkey: uint256
-_root: address
-_seqNoStart: uint64
-_seqNoFinish: uint64
-_bls_pubkey: bytes
-_stake: uint128
-_licenses: LicenseStake[]
-_sumReputationCoef: uint128
-_signerIndex: uint16
-_isContinue: bool
-_stakeContinue: uint128
-_licensesContinue: LicenseStake[]
-_sumReputationCoefContinue: uint128
-_signerIndexContinue: uint16
-_proxyListContinue: mapping(uint8 => string)
-_myIp: string
-_reward_sum: uint128
-_is_full_slashing: bool
-_is_touching: bool
-_slash_type: optional(uint8)
+constructor()
+setStake()
+continueStake()
+cancelContinueStake()
+slash()
+touch()
+cantDelete()
+canDelete()
+destroy()
+destroy_full_slash()
+part_slash()
+slashPartHelper()
+getDetails()
+getProxyListContinue()
+getEpochCoolerCodeHash()
+getVersion()
}
This class diagram outlines the primary state variables and publicly accessible methods of the BlockKeeperEpoch contract.
For related concepts on staking, epoch management, and slashing algorithms, refer to Staking Mechanisms, Epoch Lifecycle, and Slashing Protocols. The contract’s interactions with node wallets and root contracts relate closely to Node Wallet Management and BlockKeeper Root Contract.