Skip to content

Commit

Permalink
update to view functions for stable cadence
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Jul 5, 2023
1 parent de95d0c commit 18e5b36
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 189 deletions.
62 changes: 30 additions & 32 deletions contracts/FlowIDTableStaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ pub contract FlowIDTableStaking {
}

/// borrow a reference to to one of the nodes in the record
access(account) fun borrowNodeRecord(_ nodeID: String): &NodeRecord {
access(account) view fun borrowNodeRecord(_ nodeID: String): &NodeRecord {
pre {
FlowIDTableStaking.nodes[nodeID] != nil:
"Specified node ID does not exist in the record"
Expand Down Expand Up @@ -1623,13 +1623,13 @@ pub contract FlowIDTableStaking {
}

/// Gets the current list of participant (staked in the current epoch) nodes as a dictionary.
pub fun getParticipantNodeList(): {String: Bool}? {
pub view fun getParticipantNodeList(): {String: Bool}? {
return self.account.copy<{String: Bool}>(from: /storage/idTableCurrentList)
}

/// Gets the current list of participant nodes (like getCurrentNodeList) but as a list
/// Kept for backwards compatibility
pub fun getStakedNodeIDs(): [String] {
pub view fun getStakedNodeIDs(): [String] {
let nodeIDs = self.getParticipantNodeList()!
return nodeIDs.keys
}
Expand Down Expand Up @@ -1666,7 +1666,7 @@ pub contract FlowIDTableStaking {

/// Gets a list of node IDs who have pending token movements
/// or who's delegators have pending movements
pub fun getMovesPendingList(): {String: {UInt32: Bool}}? {
pub view fun getMovesPendingList(): {String: {UInt32: Bool}}? {
return self.account.copy<{String: {UInt32: Bool}}>(from: /storage/idTableMovesPendingList)
}

Expand All @@ -1677,7 +1677,7 @@ pub contract FlowIDTableStaking {
/// The candidate node list is a dictionary that maps node roles
/// to a list of node IDs of that role
/// Gets the candidate node list size limits for each role
pub fun getCandidateNodeLimits(): {UInt8: UInt64}? {
pub view fun getCandidateNodeLimits(): {UInt8: UInt64}? {
return self.account.copy<{UInt8: UInt64}>(from: /storage/idTableCandidateNodeLimits)
}

Expand Down Expand Up @@ -1716,19 +1716,19 @@ pub contract FlowIDTableStaking {
}

/// Returns the current candidate node list
pub fun getCandidateNodeList(): {UInt8: {String: Bool}} {
pub view fun getCandidateNodeList(): {UInt8: {String: Bool}} {
return FlowIDTableStaking.account.copy<{UInt8: {String: Bool}}>(from: /storage/idTableCandidateNodes)
?? {1: {}, 2: {}, 3: {}, 4: {}, 5: {}}
}

/// Get slot (count) limits for each node role
pub fun getRoleSlotLimits(): {UInt8: UInt16} {
pub view fun getRoleSlotLimits(): {UInt8: UInt16} {
return FlowIDTableStaking.account.copy<{UInt8: UInt16}>(from: /storage/flowStakingSlotLimits)
?? {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
}

/// Returns a dictionary that indicates how many participant nodes there are for each role
pub fun getCurrentRoleNodeCounts(): {UInt8: UInt16} {
pub view fun getCurrentRoleNodeCounts(): {UInt8: UInt16} {
if let currentCounts = FlowIDTableStaking.account.copy<{UInt8: UInt16}>(from: /storage/flowStakingRoleNodeCounts) {
return currentCounts
} else {
Expand All @@ -1747,7 +1747,7 @@ pub contract FlowIDTableStaking {

/// Checks if the given string has all numbers or lowercase hex characters
/// Used to ensure that there are no duplicate node IDs
pub fun isValidNodeID(_ input: String): Bool {
pub view fun isValidNodeID(_ input: String): Bool {
let byteVersion = input.utf8

for character in byteVersion {
Expand All @@ -1760,7 +1760,7 @@ pub contract FlowIDTableStaking {
}

/// Indicates if the staking auction is currently enabled
pub fun stakingEnabled(): Bool {
pub view fun stakingEnabled(): Bool {
return self.account.copy<Bool>(from: /storage/stakingEnabled) ?? false
}

Expand Down Expand Up @@ -1813,72 +1813,70 @@ pub contract FlowIDTableStaking {
}

/// Gets an array of all the node IDs that have ever registered
pub fun getNodeIDs(): [String] {
pub view fun getNodeIDs(): [String] {
return FlowIDTableStaking.nodes.keys
}

/// Checks if the amount of tokens is greater than the minimum staking requirement
/// for the specified node role
pub fun isGreaterThanMinimumForRole(numTokens: UFix64, role: UInt8): Bool {
let minimumStake = self.minimumStakeRequired[role]
?? panic("Incorrect role provided for minimum stake. Must be 1, 2, 3, 4, or 5")

return numTokens >= minimumStake
/// Checks if the amount of tokens is greater
/// than the minimum staking requirement for the specified role
pub view fun isGreaterThanMinimumForRole(numTokens: UFix64, role: UInt8): Bool {
return numTokens >= self.minimumStakeRequired[role]!
}

/// Indicates if the specified networking address is claimed by a node
pub fun getNetworkingAddressClaimed(address: String): Bool {
pub view fun getNetworkingAddressClaimed(address: String): Bool {
return self.getClaimed(path: /storage/networkingAddressesClaimed, key: address)
}

/// Indicates if the specified networking key is claimed by a node
pub fun getNetworkingKeyClaimed(key: String): Bool {
pub view fun getNetworkingKeyClaimed(key: String): Bool {
return self.getClaimed(path: /storage/networkingKeysClaimed, key: key)
}

/// Indicates if the specified staking key is claimed by a node
pub fun getStakingKeyClaimed(key: String): Bool {
pub view fun getStakingKeyClaimed(key: String): Bool {
return self.getClaimed(path: /storage/stakingKeysClaimed, key: key)
}

/// Gets the claimed status of a particular piece of node metadata
access(account) fun getClaimed(path: StoragePath, key: String): Bool {
access(account) view fun getClaimed(path: StoragePath, key: String): Bool {
let claimedDictionary = self.account.borrow<&{String: Bool}>(from: path)
?? panic("Invalid path for dictionary")
return claimedDictionary[key] ?? false
}

/// Returns the list of approved node IDs that the admin has set
pub fun getApprovedList(): {String: Bool}? {
return self.account.copy<{String: Bool}>(from: /storage/idTableApproveList)
pub view fun getApprovedList(): [String] {
return self.account.copy<[String]>(from: /storage/idTableApproveList)
?? panic("could not get approved list")
}

/// Returns the list of node IDs whose rewards will be reduced in the next payment
pub fun getNonOperationalNodesList(): {String: UFix64} {
pub view fun getNonOperationalNodesList(): {String: UFix64} {
return self.account.copy<{String: UFix64}>(from: /storage/idTableNonOperationalNodesList)
?? panic("could not get non-operational node list")
}

/// Gets the minimum stake requirements for all the node types
pub fun getMinimumStakeRequirements(): {UInt8: UFix64} {
pub view fun getMinimumStakeRequirements(): {UInt8: UFix64} {
return self.minimumStakeRequired
}

/// Gets the minimum stake requirement for delegators
pub fun getDelegatorMinimumStakeRequirement(): UFix64 {
pub view fun getDelegatorMinimumStakeRequirement(): UFix64 {
return self.account.copy<UFix64>(from: /storage/delegatorStakingMinimum)
?? 0.0
}

/// Gets a dictionary that indicates the current number of tokens staked
/// by all the nodes of each type
pub fun getTotalTokensStakedByNodeType(): {UInt8: UFix64} {
pub view fun getTotalTokensStakedByNodeType(): {UInt8: UFix64} {
return self.totalTokensStakedByNodeType
}

/// Gets the total number of FLOW that is currently staked
/// by all of the staked nodes in the current epoch
pub fun getTotalStaked(): UFix64 {
pub view fun getTotalStaked(): UFix64 {
var totalStaked: UFix64 = 0.0
for nodeType in FlowIDTableStaking.totalTokensStakedByNodeType.keys {
// Do not count access nodes
Expand All @@ -1890,18 +1888,18 @@ pub contract FlowIDTableStaking {
}

/// Gets the token payout value for the current epoch
pub fun getEpochTokenPayout(): UFix64 {
pub view fun getEpochTokenPayout(): UFix64 {
return self.epochTokenPayout
}

/// Gets the cut percentage for delegator rewards paid to node operators
pub fun getRewardCutPercentage(): UFix64 {
pub view fun getRewardCutPercentage(): UFix64 {
return self.nodeDelegatingRewardCut
}

/// Gets the ratios of rewards that different node roles recieve
/// NOTE: Currently is not used
pub fun getRewardRatios(): {UInt8: UFix64} {
pub view fun getRewardRatios(): {UInt8: UFix64} {
return self.rewardRatios
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/FlowStakingCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub contract FlowStakingCollection {
pub fun addNodeObject(_ node: @FlowIDTableStaking.NodeStaker, machineAccountInfo: MachineAccountInfo?)
pub fun addDelegatorObject(_ delegator: @FlowIDTableStaking.NodeDelegator)
//pub fun depositToMachineAccount(nodeID: String, from: @FlowToken.Vault)
pub fun doesStakeExist(nodeID: String, delegatorID: UInt32?): Bool
pub view fun doesStakeExist(nodeID: String, delegatorID: UInt32?): Bool
pub fun getNodeIDs(): [String]
pub fun getDelegatorIDs(): [DelegatorIDs]
pub fun getAllNodeInfo(): [FlowIDTableStaking.NodeInfo]
Expand Down Expand Up @@ -266,7 +266,7 @@ pub contract FlowStakingCollection {
}

/// Returns true if a Stake or Delegation record exists in the StakingCollection for a given nodeID and optional delegatorID, otherwise false.
pub fun doesStakeExist(nodeID: String, delegatorID: UInt32?): Bool {
pub view fun doesStakeExist(nodeID: String, delegatorID: UInt32?): Bool {
var tokenHolderNodeID: String? = nil
var tokenHolderDelegatorNodeID: String? = nil
var tokenHolderDelegatorID: UInt32? = nil
Expand Down Expand Up @@ -576,7 +576,7 @@ pub contract FlowStakingCollection {
}

/// Borrows a reference to a node in the collection
access(self) fun borrowNode(_ nodeID: String): &FlowIDTableStaking.NodeStaker? {
access(self) view fun borrowNode(_ nodeID: String): &FlowIDTableStaking.NodeStaker? {
if self.nodeStakers[nodeID] != nil {
return &self.nodeStakers[nodeID] as &FlowIDTableStaking.NodeStaker?
} else {
Expand All @@ -585,7 +585,7 @@ pub contract FlowStakingCollection {
}

/// Borrows a reference to a delegator in the collection
access(self) fun borrowDelegator(nodeID: String, delegatorID: UInt32): &FlowIDTableStaking.NodeDelegator? {
access(self) view fun borrowDelegator(nodeID: String, delegatorID: UInt32): &FlowIDTableStaking.NodeDelegator? {
if self.nodeDelegators[nodeID] != nil {
let delegatorRef = (&self.nodeDelegators[nodeID] as &FlowIDTableStaking.NodeDelegator?)!
if delegatorRef.id == delegatorID { return delegatorRef } else { return nil }
Expand Down
23 changes: 8 additions & 15 deletions contracts/LockedTokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,8 @@ pub contract LockedTokens {
}

pub fun borrowNode(): &FlowIDTableStaking.NodeStaker? {
let nodeOpt <- self.nodeStaker <- nil
if let node <- nodeOpt {
let nodeRef = &node as &FlowIDTableStaking.NodeStaker
self.nodeStaker <-! node
return nodeRef
} else {
self.nodeStaker <-! nodeOpt
return nil
}
let nodeRef: &FlowIDTableStaking.NodeStaker? = &self.nodeStaker as &FlowIDTableStaking.NodeStaker?
return nodeRef
}

pub fun removeNode(): @FlowIDTableStaking.NodeStaker? {
Expand All @@ -264,9 +257,9 @@ pub contract LockedTokens {
pub fun getLockedAccountAddress(): Address
pub fun getLockedAccountBalance(): UFix64
pub fun getUnlockLimit(): UFix64
pub fun getNodeID(): String?
pub fun getDelegatorID(): UInt32?
pub fun getDelegatorNodeID(): String?
pub view fun getNodeID(): String?
pub view fun getDelegatorID(): UInt32?
pub view fun getDelegatorNodeID(): String?
}

/// Stored in Holder unlocked account
Expand Down Expand Up @@ -370,7 +363,7 @@ pub contract LockedTokens {
return self.nodeStakerProxy!
}

pub fun getNodeID(): String? {
pub view fun getNodeID(): String? {
let tokenManager = self.tokenManager.borrow()!

return tokenManager.nodeStaker?.id
Expand All @@ -386,13 +379,13 @@ pub contract LockedTokens {
return self.nodeDelegatorProxy!
}

pub fun getDelegatorID(): UInt32? {
pub view fun getDelegatorID(): UInt32? {
let tokenManager = self.tokenManager.borrow()!

return tokenManager.nodeDelegator?.id
}

pub fun getDelegatorNodeID(): String? {
pub view fun getDelegatorNodeID(): String? {
let tokenManager = self.tokenManager.borrow()!

return tokenManager.nodeDelegator?.nodeID
Expand Down
18 changes: 9 additions & 9 deletions contracts/epochs/FlowClusterQC.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub contract FlowClusterQC {

/// Returns the minimum sum of vote weight required in order to be able to generate a
/// valid quorum certificate for this cluster.
pub fun voteThreshold(): UInt64 {
pub view fun voteThreshold(): UInt64 {
if self.totalWeight == 0 as UInt64 {
return 0 as UInt64
}
Expand All @@ -127,7 +127,7 @@ pub contract FlowClusterQC {
/// Then this cluster's QC generation is considered complete and this method returns
/// the vote message that reached quorum
/// If no vote is found to reach quorum, then `nil` is returned
pub fun isComplete(): String? {
pub view fun isComplete(): String? {
for message in self.uniqueVoteMessageTotalWeights.keys {
if self.uniqueVoteMessageTotalWeights[message]! >= self.voteThreshold() {
return message
Expand Down Expand Up @@ -166,7 +166,7 @@ pub contract FlowClusterQC {
}

/// Gets a vote that was generated for a node ID
access(contract) fun getGeneratedVote(nodeId: String): Vote? {
access(contract) view fun getGeneratedVote(nodeId: String): Vote? {
return self.generatedVotes[nodeId]
}

Expand All @@ -176,7 +176,7 @@ pub contract FlowClusterQC {
}

/// Gets the total weight commited for a unique vote
access(contract) fun getUniqueVoteMessageTotalWeight(vote: String): UInt64? {
access(contract) view fun getUniqueVoteMessageTotalWeight(vote: String): UInt64? {
return self.uniqueVoteMessageTotalWeights[vote]
}

Expand Down Expand Up @@ -395,19 +395,19 @@ pub contract FlowClusterQC {
}

/// Returns a boolean telling if the voter is registered for the current voting phase
pub fun voterIsRegistered(_ nodeID: String): Bool {
pub view fun voterIsRegistered(_ nodeID: String): Bool {
return FlowClusterQC.nodeCluster[nodeID] != nil
}

/// Returns a boolean telling if the node has claimed their `Voter` resource object
/// The object can only be claimed once, but if the node destroys their `Voter` object,
/// It could be claimed again
pub fun voterIsClaimed(_ nodeID: String): Bool {
pub view fun voterIsClaimed(_ nodeID: String): Bool {
return FlowClusterQC.voterClaimed[nodeID] != nil
}

/// Returns whether this voter has successfully submitted a vote for this epoch.
pub fun nodeHasVoted(_ nodeID: String): Bool {
pub view fun nodeHasVoted(_ nodeID: String): Bool {

// Get the cluster that this node belongs to
if let clusterIndex = FlowClusterQC.nodeCluster[nodeID] {
Expand All @@ -424,12 +424,12 @@ pub contract FlowClusterQC {
}

/// Gets all of the collector clusters for the current epoch
pub fun getClusters(): [Cluster] {
pub view fun getClusters(): [Cluster] {
return self.clusters
}

/// Returns true if we have collected enough votes for all clusters.
pub fun votingCompleted(): Bool {
pub view fun votingCompleted(): Bool {
for cluster in FlowClusterQC.clusters {
if cluster.isComplete() == nil {
return false
Expand Down
Loading

0 comments on commit 18e5b36

Please sign in to comment.