Skip to content

Commit

Permalink
Merge pull request #218 from threefoldtech/development_integration_su…
Browse files Browse the repository at this point in the history
…pport_batch_calls

support substrate batch calls
  • Loading branch information
Mahmoud-Emad authored Sep 29, 2024
2 parents de1fcda + c51e12b commit 61de063
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 12 deletions.
65 changes: 65 additions & 0 deletions griddriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,20 @@ func main() {
return generateWgPrivKey()
},
},
{
Name: "generate-wg-public-key",
Usage: "Generates wireguard public key",
Flags: []cli.Flag{
cli.StringFlag{
Name: "key",
Usage: "wireguard private key",
Required: true,
},
},
Action: func(c *cli.Context) error {
return generateWgPublicKey(c)
},
},
{
Name: "rmb",
Usage: "Make RMB call",
Expand Down Expand Up @@ -491,6 +505,57 @@ func main() {
},
Action: rmbDecorator(rmbCall),
},
{
Name: "batch-create-contract",
Description: "makes a batch create contract call to the tfchain",
Flags: []cli.Flag{
cli.StringFlag{
Name: "mnemonics",
Value: "",
Usage: "user mnemonics",
Required: true,
},
cli.StringFlag{
Name: "substrate",
Value: "wss://tfchain.grid.tf/ws",
Usage: "substrate URL",
},
cli.StringFlag{
Name: "contracts-data",
Usage: "json encoding of list of substrate BatchCreateContractData objects",
Required: true,
},
cli.StringFlag{
Name: "contracts-body",
Usage: "deployment body string",
Required: true,
},
},
Action: substrateDecorator(batchAllCreateContract),
},
{
Name: "batch-cancel-contract",
Description: "makes a batch cancel contract call to the tfchain",
Flags: []cli.Flag{
cli.StringFlag{
Name: "mnemonics",
Value: "",
Usage: "user mnemonics",
Required: true,
},
cli.StringFlag{
Name: "substrate",
Value: "wss://tfchain.grid.tf/ws",
Usage: "substrate URL",
},
cli.StringFlag{
Name: "contract-ids",
Usage: "json encoding of list of the contract ids to delete",
Required: true,
},
},
Action: substrateDecorator(batchCancelContract),
},
},
}

Expand Down
30 changes: 21 additions & 9 deletions griddriver/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func deployVM() cli.ActionFunc {
}

func buildNetwork(name, solutionType string, nodes []uint32) workloads.ZNet {
return workloads.ZNet{
Name: name,
Nodes: nodes,
IPRange: gridtypes.NewIPNet(net.IPNet{
IP: net.IPv4(10, 20, 0, 0),
Mask: net.CIDRMask(16, 32),
}),
SolutionType: solutionType,
}
return workloads.ZNet{
Name: name,
Nodes: nodes,
IPRange: gridtypes.NewIPNet(net.IPNet{
IP: net.IPv4(10, 20, 0, 0),
Mask: net.CIDRMask(16, 32),
}),
SolutionType: solutionType,
}
}

func generateWgPrivKey() error {
Expand All @@ -95,3 +95,15 @@ func generateWgPrivKey() error {
return nil

}

func generateWgPublicKey(ctx *cli.Context) error {
keyStr := ctx.String("key")
key, err := wgtypes.ParseKey(keyStr)
if err != nil {
return fmt.Errorf("failed to parse private key: %w", err)
}

fmt.Printf("%s", key.PublicKey().String())
return nil

}
7 changes: 4 additions & 3 deletions griddriver/rmb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"time"

substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
Expand Down Expand Up @@ -33,7 +34,7 @@ func rmbDecorator(action func(c *cli.Context, client *peer.RpcClient) (interface
mnemonics,
subManager,
peer.WithRelay(relay_url),
peer.WithSession("tfgrid-vclient"),
peer.WithSession(fmt.Sprintf("tfgrid-vclient-%d", rand.Int63())),
)
if err != nil {
return fmt.Errorf("failed to create peer client: %w", err)
Expand Down Expand Up @@ -144,7 +145,7 @@ func nodeTakenPorts(c *cli.Context, client *peer.RpcClient) (interface{}, error)
dst := uint32(c.Uint("dst"))
var takenPorts []uint16

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

if err := client.Call(ctx, dst, "zos.network.list_wg_ports", nil, &takenPorts); err != nil {
Expand Down Expand Up @@ -172,6 +173,6 @@ func getNodePublicConfig(c *cli.Context, client *peer.RpcClient) (interface{}, e
if err != nil {
return nil, fmt.Errorf("failed to marshal public configuration: %w", err)
}
fmt.Println(string(json))

return string(json), nil
}
43 changes: 43 additions & 0 deletions griddriver/substrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/hex"
"encoding/json"
"fmt"

"github.com/pkg/errors"
Expand Down Expand Up @@ -149,3 +150,45 @@ func signDeployment(ctx *cli.Context, sub *substrate.Substrate, identity substra
sig := hex.EncodeToString(signatureBytes)
return sig, nil
}

func batchAllCreateContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) {
body := ctx.String("contracts-body")
data := []byte(ctx.String("contracts-data"))

contractData := []substrate.BatchCreateContractData{}
if err := json.Unmarshal(data, &contractData); err != nil {
return nil, fmt.Errorf("failed to decode contract data: %w", err)
}

for id := range contractData {
if contractData[id].Name == "" {
contractData[id].Body = body
}
}

contractIds, err := sub.BatchAllCreateContract(identity, contractData)
if err != nil {
return nil, fmt.Errorf("failed to create contracts: %w", err)
}

ret, err := json.Marshal(contractIds)
if err != nil {
return nil, fmt.Errorf("failed to encode contract ids: %w", err)
}

return string(ret), nil
}

func batchCancelContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) {
data := []byte(ctx.String("contract-ids"))
contractIDs := []uint64{}
if err := json.Unmarshal(data, &contractIDs); err != nil {
return nil, fmt.Errorf("failed to decode contract ids: %w", err)
}

if err := sub.BatchCancelContract(identity, contractIDs); err != nil {
return nil, fmt.Errorf("failed to cancel contracts: %w", err)
}

return nil, nil
}

0 comments on commit 61de063

Please sign in to comment.