Skip to content

Commit c574969

Browse files
authored
removing added address field from MachineAccountInfo (#223)
* removing added field * remove address check * remove access node requirement * add machine account address script
1 parent 162007f commit c574969

File tree

8 files changed

+65
-24
lines changed

8 files changed

+65
-24
lines changed

contracts/FlowIDTableStaking.cdc

+1-5
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,7 @@ pub contract FlowIDTableStaking {
11171117
/// Checks if the amount of tokens is greater
11181118
/// than the minimum staking requirement for the specified role
11191119
pub fun isGreaterThanMinimumForRole(numTokens: UFix64, role: UInt8): Bool {
1120-
if role == UInt8(5) {
1121-
return numTokens > 0.0
1122-
} else {
1123-
return numTokens >= self.minimumStakeRequired[role]!
1124-
}
1120+
return numTokens >= self.minimumStakeRequired[role]!
11251121
}
11261122

11271123
/// Indicates if the specified networking address is claimed by a node

contracts/FlowStakingCollection.cdc

+11-9
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,23 @@ pub contract FlowStakingCollection {
5353
pub struct MachineAccountInfo {
5454
pub let nodeID: String
5555
pub let role: UInt8
56-
pub let address: Address
5756
// Capability to the FLOW Vault to allow the owner
5857
// to withdraw or deposit to their machine account if needed
5958
access(contract) let machineAccountVaultProvider: Capability<&FlowToken.Vault>
6059

61-
init(nodeID: String, role: UInt8, machineAccountAddress: Address, machineAccountVaultProvider: Capability<&FlowToken.Vault>) {
60+
init(nodeID: String, role: UInt8, machineAccountVaultProvider: Capability<&FlowToken.Vault>) {
6261
pre {
6362
machineAccountVaultProvider.check(): "Invalid Flow Token Vault Provider"
64-
machineAccountVaultProvider.borrow()!.owner!.address == machineAccountAddress: "Vault provider must be stored in the machine account address provided"
6563
}
6664
self.nodeID = nodeID
6765
self.role = role
68-
self.address = machineAccountAddress
6966
self.machineAccountVaultProvider = machineAccountVaultProvider
7067
}
68+
69+
// Gets the address of the machine account
70+
pub fun getAddress(): Address {
71+
return self.machineAccountVaultProvider.borrow()!.owner!.address
72+
}
7173
}
7274

7375
/// Public interface that users can publish for their staking collection
@@ -424,7 +426,7 @@ pub contract FlowStakingCollection {
424426

425427
// Get the vault capability and create the machineAccountInfo struct
426428
let machineAccountVaultProvider = machineAcct.link<&FlowToken.Vault>(/private/machineAccountPrivateVault, target: /storage/flowTokenVault)!
427-
let machineAccountInfo = MachineAccountInfo(nodeID: nodeInfo.id, role: nodeInfo.role, machineAccountAddress: machineAcct.address, machineAccountVaultProvider: machineAccountVaultProvider)
429+
let machineAccountInfo = MachineAccountInfo(nodeID: nodeInfo.id, role: nodeInfo.role, machineAccountVaultProvider: machineAccountVaultProvider)
428430

429431
// If they are a collector node, create a QC Voter object and store it in the account
430432
if nodeInfo.role == FlowEpoch.NodeRole.Collector.rawValue {
@@ -436,7 +438,7 @@ pub contract FlowStakingCollection {
436438
// set this node's machine account
437439
self.machineAccounts[nodeInfo.id] = machineAccountInfo
438440

439-
emit MachineAccountCreated(nodeID: nodeInfo.id, role: FlowEpoch.NodeRole.Collector.rawValue, address: machineAcct.address)
441+
emit MachineAccountCreated(nodeID: nodeInfo.id, role: FlowEpoch.NodeRole.Collector.rawValue, address: machineAccountVaultProvider.borrow()!.owner!.address)
440442

441443
return machineAcct
442444

@@ -450,7 +452,7 @@ pub contract FlowStakingCollection {
450452
// set this node's machine account
451453
self.machineAccounts[nodeInfo.id] = machineAccountInfo
452454

453-
emit MachineAccountCreated(nodeID: nodeInfo.id, role: FlowEpoch.NodeRole.Consensus.rawValue, address: machineAcct.address)
455+
emit MachineAccountCreated(nodeID: nodeInfo.id, role: FlowEpoch.NodeRole.Consensus.rawValue, address: machineAccountVaultProvider.borrow()!.owner!.address)
454456

455457
return machineAcct
456458
}
@@ -498,7 +500,7 @@ pub contract FlowStakingCollection {
498500
}
499501

500502
// Create the new Machine account info object and store it
501-
let machineAccountInfo = MachineAccountInfo(nodeID: nodeID, role: nodeInfo.role, machineAccountAddress: machineAccount.address, machineAccountVaultProvider: machineAccountVaultProvider)
503+
let machineAccountInfo = MachineAccountInfo(nodeID: nodeID, role: nodeInfo.role, machineAccountVaultProvider: machineAccountVaultProvider)
502504
self.machineAccounts[nodeID] = machineAccountInfo
503505
}
504506

@@ -837,7 +839,7 @@ pub contract FlowStakingCollection {
837839
let unlockedVault = self.unlockedVault!.borrow()!
838840
var availableBalance: UFix64 = 0.0
839841
if FlowStorageFees.storageMegaBytesPerReservedFLOW != (0.0 as UFix64) {
840-
availableBalance = FlowStorageFees.defaultTokenAvailableBalance(machineAccountInfo.address)
842+
availableBalance = FlowStorageFees.defaultTokenAvailableBalance(machineAccountInfo.machineAccountVaultProvider.borrow()!.owner!.address)
841843
} else {
842844
availableBalance = vaultRef.balance
843845
}

lib/go/contracts/internal/assets/assets.go

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/go/templates/internal/assets/assets.go

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/go/templates/staking_collection_templates.go

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
collectionGetUnlockedTokensUsedFilename = "stakingCollection/scripts/get_unlocked_tokens_used.cdc"
4141
collectionDoesAccountHaveStakingCollectionFilename = "stakingCollection/scripts/does_account_have_staking_collection.cdc"
4242
collectionGetMachineAccountsFilename = "stakingCollection/scripts/get_machine_accounts.cdc"
43+
collectionGetMachineAccountAddressFilename = "stakingCollection/scripts/get_machine_account_address.cdc"
4344

4445
// tests
4546
getCollectionTokensFilename = "stakingCollection/test/get_tokens.cdc"
@@ -210,6 +211,12 @@ func GenerateCollectionGetMachineAccountsScript(env Environment) []byte {
210211
return []byte(replaceAddresses(code, env))
211212
}
212213

214+
func GenerateCollectionGetMachineAccountAddressScript(env Environment) []byte {
215+
code := assets.MustAssetString(collectionGetMachineAccountAddressFilename)
216+
217+
return []byte(replaceAddresses(code, env))
218+
}
219+
213220
// Test Templates
214221

215222
func GenerateCollectionGetTokensScript(env Environment) []byte {

lib/go/test/flow_idtable_staking_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ func TestIDTableStaking(t *testing.T) {
634634
result = executeScriptAndCheck(t, b, templates.GenerateReturnCurrentTableScript(env), nil)
635635

636636
idArray := result.(cadence.Array).Values
637-
assert.Len(t, idArray, 0)
637+
assert.Len(t, idArray, 1)
638638

639639
result = executeScriptAndCheck(t, b, templates.GenerateReturnProposedTableScript(env), nil)
640640

@@ -658,7 +658,7 @@ func TestIDTableStaking(t *testing.T) {
658658
result := executeScriptAndCheck(t, b, templates.GenerateReturnCurrentTableScript(env), nil)
659659

660660
idArray := result.(cadence.Array).Values
661-
assert.Len(t, idArray, 0)
661+
assert.Len(t, idArray, 1)
662662

663663
result = executeScriptAndCheck(t, b, templates.GenerateReturnProposedTableScript(env), nil)
664664

lib/go/test/lockedtokens_helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ func verifyStakingCollectionInfo(
499499
assertEqual(t, len(expectedInfo.machineAccounts), len(machineAccountsDictionary))
500500
for _, accountPair := range machineAccountsDictionary {
501501
nodeID := accountPair.Key.(cadence.String)
502-
machineAccountInfo := accountPair.Value.(cadence.Struct).Fields
503-
address := machineAccountInfo[2].(cadence.Address)
502+
result = executeScriptAndCheck(t, b, templates.GenerateCollectionGetMachineAccountAddressScript(env), [][]byte{jsoncdc.MustEncode(cadence.Address(flow.HexToAddress(expectedInfo.accountAddress))), jsoncdc.MustEncode(nodeID)})
503+
address := result.(cadence.Address)
504504
assertEqual(t, cadence.NewAddress(expectedInfo.machineAccounts[nodeID]), address)
505505
}
506506
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import FlowStakingCollection from 0xSTAKINGCOLLECTIONADDRESS
2+
3+
/// Gets the machine account address for a specific node
4+
/// in an account's staking collection
5+
6+
pub fun main(account: Address, nodeID: String): Address {
7+
let machineAccounts = FlowStakingCollection.getMachineAccounts(address: account)
8+
9+
let accountInfo = machineAccounts[nodeID]
10+
?? panic("Could not find machine account info for the specified node ID")
11+
12+
return accountInfo.getAddress()
13+
}

0 commit comments

Comments
 (0)