Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support substrate batch calls #218

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading