Skip to content

Commit

Permalink
refactor load network from grid to get all node deployments at once
Browse files Browse the repository at this point in the history
  • Loading branch information
Eslam-Nawara committed Dec 17, 2024
1 parent 2244ce7 commit 92d1aa0
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions grid-client/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,17 @@ func (st *State) LoadNetworkFromGrid(ctx context.Context, name string) (znet wor
return znet, errors.Wrapf(err, "could not get node client: %d", nodeID)
}

for _, contractID := range st.CurrentNodeDeployments[nodeID] {
dl, err := nodeClient.DeploymentGet(ctx, contractID)
if err != nil {
return znet, errors.Wrapf(err, "could not get network deployment %d from node %d", contractID, nodeID)
}
dls, err := nodeClient.DeploymentList(ctx)
if err != nil {
return znet, errors.Wrapf(err, "could not get all deployments from node %d", nodeID)
}

for _, dl := range dls {

if len(strings.TrimSpace(dl.Metadata)) == 0 {
contract, err := sub.GetContract(contractID)
dl.Metadata, err = getContractMetadata(sub, dl.ContractID, nodeID)
if err != nil {
return znet, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID)
}
dl.Metadata = contract.ContractType.NodeContract.DeploymentData
if len(strings.TrimSpace(dl.Metadata)) == 0 {
return znet, errors.Wrapf(err, "contract %d doesn't have metadata", contractID)
return znet, err
}
}

Expand Down Expand Up @@ -410,25 +407,22 @@ func (st *State) LoadNetworkLightFromGrid(ctx context.Context, name string) (zne

sub := st.Substrate
for nodeID := range st.CurrentNodeDeployments {

nodeClient, err := st.NcPool.GetNodeClient(sub, nodeID)
if err != nil {
return znet, errors.Wrapf(err, "could not get node client: %d", nodeID)
}

for _, contractID := range st.CurrentNodeDeployments[nodeID] {
dl, err := nodeClient.DeploymentGet(ctx, contractID)
if err != nil {
return znet, errors.Wrapf(err, "could not get network deployment %d from node %d", contractID, nodeID)
}
dls, err := nodeClient.DeploymentList(ctx)
if err != nil {
return znet, errors.Wrapf(err, "could not get all deployments from node %d", nodeID)
}

for _, dl := range dls {
if len(strings.TrimSpace(dl.Metadata)) == 0 {
contract, err := sub.GetContract(contractID)
dl.Metadata, err = getContractMetadata(sub, dl.ContractID, nodeID)
if err != nil {
return znet, errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID)
}
dl.Metadata = contract.ContractType.NodeContract.DeploymentData
if len(strings.TrimSpace(dl.Metadata)) == 0 {
return znet, errors.Wrapf(err, "contract %d doesn't have metadata", contractID)
return znet, err
}
}

Expand Down Expand Up @@ -571,3 +565,17 @@ func (st *State) AssignNodesIPRange(k *workloads.K8sCluster) (err error) {

return nil
}

func getContractMetadata(sub subi.SubstrateExt, contractID uint64, nodeID uint32) (string, error) {
contract, err := sub.GetContract(contractID)
if err != nil {
return "", errors.Wrapf(err, "could not get contract %d from node %d", contractID, nodeID)
}

metadata := contract.ContractType.NodeContract.DeploymentData
if len(strings.TrimSpace(metadata)) == 0 {
return "", errors.Wrapf(err, "contract %d doesn't have metadata", contractID)
}

return metadata, nil
}

0 comments on commit 92d1aa0

Please sign in to comment.