Skip to content

Commit

Permalink
Allows reset Epoch during staking auction and adds function to update…
Browse files Browse the repository at this point in the history
… networking address (#227)

* removing added field

* remove address check

* remove access node requirement

* adds approved list and node id hex restriction

* reset epoch during staking auction

* adds updateNetworkingAddress function to all contracts

* fix error message
  • Loading branch information
joshuahannan authored Aug 4, 2021
1 parent 6c18941 commit 6e64b6a
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 20 deletions.
19 changes: 18 additions & 1 deletion contracts/FlowIDTableStaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub contract FlowIDTableStaking {
pub event RewardsPaid(nodeID: String, amount: UFix64)
pub event UnstakedTokensWithdrawn(nodeID: String, amount: UFix64)
pub event RewardTokensWithdrawn(nodeID: String, amount: UFix64)
pub event NetworkingAddressUpdated(nodeID: String, newAddress: String)

/// Delegator Events
pub event NewDelegatorCreated(nodeID: String, delegatorID: UInt32)
Expand Down Expand Up @@ -163,7 +164,7 @@ pub contract FlowIDTableStaking {
FlowIDTableStaking.isValidNodeID(id): "The node ID must have only numbers and lowercase hex characters"
FlowIDTableStaking.nodes[id] == nil: "The ID cannot already exist in the record"
role >= UInt8(1) && role <= UInt8(5): "The role must be 1, 2, 3, 4, or 5"
networkingAddress.length > 0 && networkingAddress.length <= 510: "The networkingAddress must be less than 255 bytes (510 hex characters)"
networkingAddress.length > 0 && networkingAddress.length <= 510: "The networkingAddress must be less than 510 characters"
networkingKey.length == 128: "The networkingKey length must be exactly 64 bytes (128 hex characters)"
stakingKey.length == 192: "The stakingKey length must be exactly 96 bytes (192 hex characters)"
!FlowIDTableStaking.getNetworkingAddressClaimed(address: networkingAddress): "The networkingAddress cannot have already been claimed"
Expand Down Expand Up @@ -417,6 +418,22 @@ pub contract FlowIDTableStaking {
self.id = id
}

/// Change the node's networking address to a new one
pub fun updateNetworkingAddress(_ newAddress: String) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot update networking address if the staking auction isn't in progress"
newAddress.length > 0 && newAddress.length <= 510: "The networkingAddress must be less than 510 characters"
!FlowIDTableStaking.getNetworkingAddressClaimed(address: newAddress): "The networkingAddress cannot have already been claimed"
}

// Borrow the node's record from the staking contract
let nodeRecord = FlowIDTableStaking.borrowNodeRecord(self.id)

nodeRecord.networkingAddress = newAddress

emit NetworkingAddressUpdated(nodeID: self.id, newAddress: newAddress)
}

/// Add new tokens to the system to stake during the next epoch
pub fun stakeNewTokens(_ tokens: @FungibleToken.Vault) {
pre {
Expand Down
16 changes: 16 additions & 0 deletions contracts/FlowStakingCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,22 @@ pub contract FlowStakingCollection {
// If they are staking for a delegator, they provide the node ID for the node they are delegating to
// and their delegator ID to specify that it is for their delegator object
/// Updates the stored networking address for the specified node
pub fun updateNetworkingAddress(nodeID: String, newAddress: String) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil): "Specified stake does not exist in this collection"
}

// If the node is stored in the collection, borrow it
if let node = self.borrowNode(nodeID) {
node.updateNetworkingAddress(newAddress)
} else {
// Use the node stored in the locked account
let node = self.tokenHolder!.borrow()!.borrowStaker()
node.updateNetworkingAddress(newAddress)
}
}

/// Function to stake new tokens for an existing Stake or Delegation record in the StakingCollection
pub fun stakeNewTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
Expand Down
12 changes: 12 additions & 0 deletions contracts/LockedTokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ pub contract LockedTokens {
return managerRef.nodeStaker != nil
}

/// Change node networking address
pub fun updateNetworkingAddress(_ newAddress: String) {
let tokenManagerRef = self.tokenManager.borrow()!

assert(
self.nodeObjectExists(tokenManagerRef),
message: "Cannot change networking address if there is no node object!"
)

tokenManagerRef.nodeStaker?.updateNetworkingAddress(newAddress)
}

/// Stakes new locked tokens
pub fun stakeNewTokens(amount: UFix64) {
let tokenManagerRef = self.tokenManager.borrow()!
Expand Down
12 changes: 9 additions & 3 deletions contracts/epochs/FlowEpoch.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,15 @@ pub contract FlowEpoch {
"Invalid startView and endView configuration"
}

// force reset the QC and DKG
FlowEpoch.borrowClusterQCAdmin().forceStopVoting()
FlowEpoch.borrowDKGAdmin().forceEndDKG()
if FlowEpoch.currentEpochPhase == EpochPhase.STAKINGAUCTION {
// Since we are resetting the epoch, we do not need to
// start epoch setup also. We only need to end the staking auction
FlowEpoch.borrowStakingAdmin().endStakingAuction()
} else {
// force reset the QC and DKG
FlowEpoch.borrowClusterQCAdmin().forceStopVoting()
FlowEpoch.borrowDKGAdmin().forceEndDKG()
}

FlowEpoch.calculateAndSetRewards(newPayout)

Expand Down
4 changes: 4 additions & 0 deletions contracts/testContracts/TestFlowIDTableStaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pub contract FlowIDTableStaking {
self.id = id
}

pub fun updateNetworkingAddress(_ newAddress: String) {

}

/// Add new tokens to the system to stake during the next epoch
pub fun stakeNewTokens(_ tokens: @FungibleToken.Vault) {

Expand Down
30 changes: 15 additions & 15 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion lib/go/templates/idtable_staking_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
unstakeAllFilename = "idTableStaking/node/unstake_all.cdc"
withdrawUnstakedTokensFilename = "idTableStaking/node/withdraw_unstaked_tokens.cdc"
withdrawRewardedTokensFilename = "idTableStaking/node/withdraw_rewarded_tokens.cdc"
updateNetworkingAddressFilename = "idTableStaking/node/update_networking_address.cdc"
addPublicNodeCapabilityFilename = "idTableStaking/node/node_add_capability.cdc"

registerManyNodesFilename = "idTableStaking/node/register_many_nodes.cdc"
Expand Down Expand Up @@ -191,7 +192,7 @@ func GenerateScaleRewardsTestScript(env Environment) []byte {
return []byte(replaceAddresses(code, env))
}

// Staker Templates -------------------------------------------------------------
// Node Templates -------------------------------------------------------------

// GenerateRegisterNodeScript creates a script that creates a new
// node struct and stores it in the Node records
Expand Down Expand Up @@ -257,6 +258,14 @@ func GenerateWithdrawRewardedTokensScript(env Environment) []byte {
return []byte(replaceAddresses(code, env))
}

// GenerateUpdateNetworkingAddressScript creates a script changes the networking address
// for an existing node operator
func GenerateUpdateNetworkingAddressScript(env Environment) []byte {
code := assets.MustAssetString(updateNetworkingAddressFilename)

return []byte(replaceAddresses(code, env))
}

func GenerateAddPublicNodeCapabilityScript(env Environment) []byte {
code := assets.MustAssetString(addPublicNodeCapabilityFilename)

Expand Down
Loading

0 comments on commit 6e64b6a

Please sign in to comment.