Skip to content

Commit b65dc46

Browse files
authored
Development fix runtime benchmarks (#1070)
* test: Add MaxTwinAdmins parameter to DAO pallet mock configuration * ci: Update staticcheck to latest * Remove add/remove twin admin calls from clients * add GetNodeV3BillingOptOutTimestamp to also retrieve the opt-out time
1 parent d0f3818 commit b65dc46

6 files changed

Lines changed: 58 additions & 96 deletions

File tree

.github/workflows/070_lint_and_test_go_bridge.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
- name: staticcheck
3838
uses: dominikh/staticcheck-action@v1.3.0
3939
with:
40-
version: "2022.1.3"
40+
version: "latest"
4141
working-directory: bridge/tfchain_bridge
4242
env:
4343
GO111MODULE: on

clients/tfchain-client-go/node.go

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -826,48 +826,6 @@ func (s *Substrate) OptOutOfV3Billing(identity Identity, nodeID uint32) (hash ty
826826
return callResponse.Hash, nil
827827
}
828828

829-
// AddTwinAdmin adds an account to the list of twin admins allowed to deploy on opted-out nodes.
830-
// Requires council (restricted) origin.
831-
func (s *Substrate) AddTwinAdmin(identity Identity, account AccountID) (hash types.Hash, err error) {
832-
cl, meta, err := s.GetClient()
833-
if err != nil {
834-
return hash, err
835-
}
836-
837-
c, err := types.NewCall(meta, "TfgridModule.add_twin_admin", account)
838-
if err != nil {
839-
return hash, errors.Wrap(err, "failed to create call")
840-
}
841-
842-
callResponse, err := s.Call(cl, meta, identity, c)
843-
if err != nil {
844-
return hash, errors.Wrap(err, "failed to add twin admin")
845-
}
846-
847-
return callResponse.Hash, nil
848-
}
849-
850-
// RemoveTwinAdmin removes an account from the list of twin admins allowed to deploy on opted-out nodes.
851-
// Requires council (restricted) origin.
852-
func (s *Substrate) RemoveTwinAdmin(identity Identity, account AccountID) (hash types.Hash, err error) {
853-
cl, meta, err := s.GetClient()
854-
if err != nil {
855-
return hash, err
856-
}
857-
858-
c, err := types.NewCall(meta, "TfgridModule.remove_twin_admin", account)
859-
if err != nil {
860-
return hash, errors.Wrap(err, "failed to create call")
861-
}
862-
863-
callResponse, err := s.Call(cl, meta, identity, c)
864-
if err != nil {
865-
return hash, errors.Wrap(err, "failed to remove twin admin")
866-
}
867-
868-
return callResponse.Hash, nil
869-
}
870-
871829
// GetAllowedTwinAdmins returns the list of accounts allowed to deploy on opted-out nodes.
872830
// Returns an empty slice if no admins have been configured.
873831
func (s *Substrate) GetAllowedTwinAdmins() ([]AccountID, error) {
@@ -899,28 +857,48 @@ func (s *Substrate) GetAllowedTwinAdmins() ([]AccountID, error) {
899857
}
900858

901859
// IsNodeOptedOutOfV3Billing returns true if the node has opted out of v3 billing.
860+
// Deprecated: use GetNodeV3BillingOptOutTimestamp to also retrieve the opt-out time.
902861
func (s *Substrate) IsNodeOptedOutOfV3Billing(nodeID uint32) (bool, error) {
903-
cl, meta, err := s.GetClient()
862+
ts, err := s.GetNodeV3BillingOptOutTimestamp(nodeID)
904863
if err != nil {
905864
return false, err
906865
}
866+
return ts != nil, nil
867+
}
868+
869+
// GetNodeV3BillingOptOutTimestamp returns the Unix timestamp (seconds) at which the node
870+
// opted out of v3 billing, or nil if the node has not opted out.
871+
func (s *Substrate) GetNodeV3BillingOptOutTimestamp(nodeID uint32) (*uint64, error) {
872+
cl, meta, err := s.GetClient()
873+
if err != nil {
874+
return nil, err
875+
}
907876

908877
bytes, err := Encode(nodeID)
909878
if err != nil {
910-
return false, errors.Wrap(err, "substrate: encoding error building query arguments")
879+
return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
911880
}
912881

913882
key, err := types.CreateStorageKey(meta, "TfgridModule", "NodeV3BillingOptOut", bytes)
914883
if err != nil {
915-
return false, errors.Wrap(err, "failed to create substrate query key")
884+
return nil, errors.Wrap(err, "failed to create substrate query key")
916885
}
917886

918887
raw, err := cl.RPC.State.GetStorageRawLatest(key)
919888
if err != nil {
920-
return false, errors.Wrap(err, "failed to lookup node v3 billing opt-out")
889+
return nil, errors.Wrap(err, "failed to lookup node v3 billing opt-out")
890+
}
891+
892+
if len(*raw) == 0 {
893+
return nil, nil
894+
}
895+
896+
var ts uint64
897+
if err := Decode(*raw, &ts); err != nil {
898+
return nil, errors.Wrap(err, "failed to decode node v3 billing opt-out timestamp")
921899
}
922900

923-
return len(*raw) > 0, nil
901+
return &ts, nil
924902
}
925903

926904
// SetNodeCertificate sets the node certificate type

clients/tfchain-client-go/node_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,6 @@ func TestOptOutOfV3Billing(t *testing.T) {
8989
require.True(t, optedOut)
9090
}
9191

92-
func TestGetAllowedTwinAdmins(t *testing.T) {
93-
cl := startLocalConnection(t)
94-
defer cl.Close()
95-
96-
rootIdentity, err := NewIdentityFromSr25519Phrase(AliceMnemonics)
97-
require.NoError(t, err)
98-
99-
bobAccount, err := FromAddress(BobAddress)
100-
require.NoError(t, err)
101-
102-
// Ensure Bob is in the list
103-
_, err = cl.AddTwinAdmin(rootIdentity, bobAccount)
104-
require.NoError(t, err)
105-
admins, err := cl.GetAllowedTwinAdmins()
106-
require.NoError(t, err)
107-
require.Contains(t, admins, bobAccount)
108-
109-
// Clean up
110-
_, err = cl.RemoveTwinAdmin(rootIdentity, bobAccount)
111-
require.NoError(t, err)
112-
113-
admins, err = cl.GetAllowedTwinAdmins()
114-
require.NoError(t, err)
115-
require.NotContains(t, admins, bobAccount)
116-
}
117-
11892
func TestUptimeReport(t *testing.T) {
11993
cl := startLocalConnection(t)
12094
defer cl.Close()

clients/tfchain-client-js/lib/client.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const {
1818
} = require('./farms')
1919
const {
2020
createNode, updateNode, getNode,
21-
getNodeIDByPubkey, deleteNode, listNodes
21+
getNodeIDByPubkey, deleteNode, listNodes,
22+
optOutOfV3Billing, getAllowedTwinAdmins, isNodeOptedOutOfV3Billing, getNodeV3BillingOptOutTimestamp
2223
} = require('./node')
2324
const { signEntityTwinID, signEntityCreation } = require('./sign')
2425
const { getBalance, transfer } = require('./balance')
@@ -209,6 +210,22 @@ class Client {
209210
return deleteNode(this, id, callback)
210211
}
211212

213+
async optOutOfV3Billing(nodeID, callback) {
214+
return optOutOfV3Billing(this, nodeID, callback)
215+
}
216+
217+
async getAllowedTwinAdmins() {
218+
return getAllowedTwinAdmins(this)
219+
}
220+
221+
async isNodeOptedOutOfV3Billing(nodeID) {
222+
return isNodeOptedOutOfV3Billing(this, nodeID)
223+
}
224+
225+
async getNodeV3BillingOptOutTimestamp(nodeID) {
226+
return getNodeV3BillingOptOutTimestamp(this, nodeID)
227+
}
228+
212229
async setNodePower (nodeId, power, callback) {
213230
return setNodePower(this, nodeId, power, callback)
214231
}

clients/tfchain-client-js/lib/node.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,32 +134,24 @@ async function optOutOfV3Billing (self, nodeID, callback) {
134134
.signAndSend(self.key, { nonce }, callback)
135135
}
136136

137-
// addTwinAdmin adds an account to the list of twin admins (council origin)
138-
async function addTwinAdmin (self, account, callback) {
139-
const nonce = await self.api.rpc.system.accountNextIndex(self.address)
140-
return self.api.tx.tfgridModule
141-
.addTwinAdmin(account)
142-
.signAndSend(self.key, { nonce }, callback)
143-
}
144-
145-
// removeTwinAdmin removes an account from the list of twin admins (council origin)
146-
async function removeTwinAdmin (self, account, callback) {
147-
const nonce = await self.api.rpc.system.accountNextIndex(self.address)
148-
return self.api.tx.tfgridModule
149-
.removeTwinAdmin(account)
150-
.signAndSend(self.key, { nonce }, callback)
151-
}
152-
153137
// getAllowedTwinAdmins returns the list of accounts allowed to deploy on opted-out nodes
154138
async function getAllowedTwinAdmins (self) {
155139
const result = await self.api.query.tfgridModule.allowedTwinAdmins()
156140
return result.toJSON() || []
157141
}
158142

159-
// isNodeOptedOutOfV3Billing returns true if the node has opted out of v3 billing
143+
// isNodeOptedOutOfV3Billing returns true if the node has opted out of v3 billing.
144+
// Deprecated: use getNodeV3BillingOptOutTimestamp to also retrieve the opt-out time.
160145
async function isNodeOptedOutOfV3Billing (self, nodeID) {
146+
return (await getNodeV3BillingOptOutTimestamp(self, nodeID)) !== null
147+
}
148+
149+
// getNodeV3BillingOptOutTimestamp returns the Unix timestamp (seconds) at which the node
150+
// opted out of v3 billing, or null if the node has not opted out.
151+
async function getNodeV3BillingOptOutTimestamp (self, nodeID) {
161152
const result = await self.api.query.tfgridModule.nodeV3BillingOptOut(nodeID)
162-
return !result.isNone
153+
if (result.isNone) return null
154+
return result.unwrap().toNumber()
163155
}
164156

165157
async function validateNode (self, farmID) {
@@ -177,8 +169,7 @@ module.exports = {
177169
deleteNode,
178170
listNodes,
179171
optOutOfV3Billing,
180-
addTwinAdmin,
181-
removeTwinAdmin,
182172
getAllowedTwinAdmins,
183-
isNodeOptedOutOfV3Billing
173+
isNodeOptedOutOfV3Billing,
174+
getNodeV3BillingOptOutTimestamp
184175
}

substrate-node/pallets/pallet-dao/src/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ parameter_types! {
145145
pub const MaxInterfaceIpsLength: u32 = 5;
146146
pub const MaxInterfacesLength: u32 = 10;
147147
pub const MaxFarmPublicIps: u32 = 512;
148+
pub const MaxTwinAdmins: u32 = 10;
148149
pub const TimestampHintDrift: u64 = 60;
149150
}
150151

@@ -172,6 +173,7 @@ impl pallet_tfgrid::Config for TestRuntime {
172173
type FarmName = TestFarmName;
173174
type MaxFarmNameLength = MaxFarmNameLength;
174175
type MaxFarmPublicIps = MaxFarmPublicIps;
176+
type MaxTwinAdmins = MaxTwinAdmins;
175177
type MaxInterfacesLength = MaxInterfacesLength;
176178
type InterfaceName = TestInterfaceName;
177179
type InterfaceMac = TestInterfaceMac;

0 commit comments

Comments
 (0)