From f363a9406d0af5635a016129a89da556eed94a13 Mon Sep 17 00:00:00 2001 From: mariobassem Date: Mon, 23 Sep 2024 11:33:24 +0300 Subject: [PATCH 1/4] support substrate batch calls --- griddriver/main.go | 46 +++++++++++++++++++++++++++++++++++++++++ griddriver/substrate.go | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/griddriver/main.go b/griddriver/main.go index 4823047b0..606e40daa 100644 --- a/griddriver/main.go +++ b/griddriver/main.go @@ -491,6 +491,52 @@ 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, + }, + }, + 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), + }, }, } diff --git a/griddriver/substrate.go b/griddriver/substrate.go index d39fb5aa7..b17c8d854 100644 --- a/griddriver/substrate.go +++ b/griddriver/substrate.go @@ -2,6 +2,7 @@ package main import ( "encoding/hex" + "encoding/json" "fmt" "github.com/pkg/errors" @@ -149,3 +150,38 @@ 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) { + 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) + } + + 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 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 +} From f1442cffaac74babe15527d0a1fdc5f21d2d37cf Mon Sep 17 00:00:00 2001 From: Mahmoud Emad Date: Thu, 26 Sep 2024 16:52:56 +0300 Subject: [PATCH 2/4] feat: Support 'contracts-body' flag on the 'batch-create-contract' command --- griddriver/main.go | 5 +++++ griddriver/substrate.go | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/griddriver/main.go b/griddriver/main.go index 606e40daa..b1af3d843 100644 --- a/griddriver/main.go +++ b/griddriver/main.go @@ -511,6 +511,11 @@ func main() { 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), }, diff --git a/griddriver/substrate.go b/griddriver/substrate.go index b17c8d854..62ab48b4d 100644 --- a/griddriver/substrate.go +++ b/griddriver/substrate.go @@ -152,6 +152,7 @@ func signDeployment(ctx *cli.Context, sub *substrate.Substrate, identity substra } 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{} @@ -159,6 +160,12 @@ func batchAllCreateContract(ctx *cli.Context, sub *substrate.Substrate, identity 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) @@ -169,7 +176,7 @@ func batchAllCreateContract(ctx *cli.Context, sub *substrate.Substrate, identity return nil, fmt.Errorf("failed to encode contract ids: %w", err) } - return ret, nil + return string(ret), nil } func batchCancelContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) { From f901712f23cab190beb78b64a8c04ebff02c540e Mon Sep 17 00:00:00 2001 From: mariobassem Date: Thu, 26 Sep 2024 20:06:41 +0300 Subject: [PATCH 3/4] add command for generating wg public key from private key --- griddriver/main.go | 14 ++++++++++++++ griddriver/network.go | 30 +++++++++++++++++++++--------- griddriver/rmb.go | 4 ++-- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/griddriver/main.go b/griddriver/main.go index b1af3d843..ddd0ffc3f 100644 --- a/griddriver/main.go +++ b/griddriver/main.go @@ -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", diff --git a/griddriver/network.go b/griddriver/network.go index 4db35a79b..50edbc98b 100644 --- a/griddriver/network.go +++ b/griddriver/network.go @@ -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 { @@ -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 + +} diff --git a/griddriver/rmb.go b/griddriver/rmb.go index 69aa5f386..9e81381b3 100644 --- a/griddriver/rmb.go +++ b/griddriver/rmb.go @@ -144,7 +144,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 { @@ -172,6 +172,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 } From c51e12bd5c6bd349c71ae6ff0d825bf9d2f22e77 Mon Sep 17 00:00:00 2001 From: mariobassem Date: Sun, 29 Sep 2024 16:58:56 +0300 Subject: [PATCH 4/4] use random session name for rmb peer --- griddriver/rmb.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/griddriver/rmb.go b/griddriver/rmb.go index 9e81381b3..93becd4ae 100644 --- a/griddriver/rmb.go +++ b/griddriver/rmb.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "time" substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" @@ -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)