blockchain

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Copyright © 2019 Cyber Republic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

View Source
var (
	Env                string
	Nodes              string
	NodeDockerImageMap = map[string]string{
		"mainchain": "cyberrepublic/elastos-mainchain-node:v0.3.7",
		"did":       "cyberrepublic/elastos-sidechain-did-node:v0.1.2",
		"token":     "cyberrepublic/elastos-sidechain-token-node:v0.1.2",
		"eth":       "cyberrepublic/elastos-sidechain-eth-node:latest",
	}
	NodeDockerDataPathMap = map[string]string{
		"mainchain": "/ela/elastos",
		"did":       "/did/elastos_did",
		"token":     "/token/elastos_token",
		"eth":       "/eth/elastos_eth",
	}
	NodeDockerConfigPathMap = map[string]string{
		"mainchain": "/ela/config.json",
		"did":       "/did/config.json",
		"token":     "/token/config.json",
	}
	NodeDockerPortMainChain = map[string]DockerPort{
		"mainnet": DockerPort{"20334", "20336", "20334", "20336"},
		"testnet": DockerPort{"21334", "21336", "21334", "21336"},
	}
	NodeDockerPortSidechainDID = map[string]DockerPort{
		"mainnet": DockerPort{"20604", "20606", "20604", "20606"},
		"testnet": DockerPort{"21604", "21606", "21604", "21606"},
	}
	NodeDockerPortSidechainToken = map[string]DockerPort{
		"mainnet": DockerPort{"20614", "20616", "20614", "20616"},
		"testnet": DockerPort{"21614", "21616", "21614", "21616"},
	}
	NodeDockerPortSidechainEth = map[string]DockerPort{
		"mainnet": DockerPort{"20634", "20636", "20634", "20636"},
		"testnet": DockerPort{"20634", "20636", "21634", "21636"},
	}
)
View Source
var KillCmd = &cobra.Command{
	Use:   "kill",
	Short: "Kill blockchain nodes",
	Long:  `Kill blockchain nodes`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("blockchain kill called with environment: [%s] and nodes: [%s]\n", Env, Nodes)

		ctx := context.Background()
		cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
		if err != nil {
			log.Fatal(err)
		}

		containers, err := cli.ContainerList(ctx, types.ContainerListOptions{All: true})
		if err != nil {
			log.Fatal(err)
		}

		nodes := strings.Split(strings.Replace(Nodes, " ", "", -1), ",")
		for _, container := range containers {
			for _, containerName := range container.Names {
				if strings.Contains(containerName, "develap") && strings.Contains(containerName, Env) {
					if len(nodes) == 0 {
						fmt.Printf("Stopping container '%v' with ID '%v'...\n", containerName[1:], container.ID[:10])
						if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
							log.Fatal(err)
						}
						fmt.Printf("Removing container '%v' with ID '%v'...\n", containerName[1:], container.ID[:10])
						if err := cli.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
							log.Fatal(err)
						}
					} else {
						for _, node := range nodes {
							if strings.Contains(containerName, node) {
								fmt.Printf("Stopping container '%v' with ID '%v'...\n", containerName[1:], container.ID[:10])
								if err := cli.ContainerStop(ctx, container.ID, nil); err != nil {
									log.Fatal(err)
								}
								fmt.Printf("Removing container '%v' with ID '%v'...\n", containerName[1:], container.ID[:10])
								if err := cli.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
									log.Fatal(err)
								}
							}
						}
					}
					break
				}
			}
		}
	},
}

KillCmd represents the kill command

View Source
var ListCmd = &cobra.Command{
	Use:   "list",
	Short: "List blockchain nodes",
	Long:  `List blockchain nodes`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("blockchain list called with environment: [%s]\n\n", Env)
		ctx := context.Background()
		cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
		if err != nil {
			log.Fatal(err)
		}

		containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
		if err != nil {
			log.Fatal(err)
		}

		for _, container := range containers {
			for _, containerName := range container.Names {
				if strings.Contains(containerName, "develap") && strings.Contains(containerName, Env) {
					i, err := strconv.ParseInt(strconv.FormatInt(container.Created, 10), 10, 64)
					if err != nil {
						log.Fatal(err)
					}
					created := time.Unix(i, 0)
					ports := []uint16{}
					for _, port := range container.Ports {
						if port.IP == "0.0.0.0" {
							ports = append(ports, port.PublicPort)
						}
					}
					fmt.Printf("Name: %v\nID: %v\nImage: %v\nCmd: %v\nCreated: %v\nStatus: %v\nPorts: %v\n\n",
						containerName[1:], container.ID[:10], container.Image, container.Command,
						created, container.Status, ports)
					break
				}
			}

		}
	},
}

ListCmd represents the list command

View Source
var RunCmd = &cobra.Command{
	Use:   "run",
	Short: "Run blockchain nodes",
	Long:  `Run blockchain nodes`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("blockchain run called with environment: [%s] and nodes: [%s]\n", Env, Nodes)

		if !strings.EqualFold(Env, "mainnet") &&
			!strings.EqualFold(Env, "testnet") &&
			!strings.EqualFold(Env, "privatenet") {
			log.Fatalf("%s not recognized as a valid net type\n", Env)
		}

		ctx := context.Background()
		cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
		if err != nil {
			log.Fatal(err)
		}

		nodes := strings.Split(strings.Replace(Nodes, " ", "", -1), ",")
		for _, node := range nodes {
			switch node {
			case "mainchain":
				resp := runDockerContainer(ctx, cli, node)
				fmt.Printf("Container ID: %v\n", resp.ID)
			case "did":
				resp := runDockerContainer(ctx, cli, node)
				fmt.Printf("Container ID: %v\n", resp.ID)
			case "token":
				resp := runDockerContainer(ctx, cli, node)
				fmt.Printf("Container ID: %v\n", resp.ID)
			case "eth":
				if strings.EqualFold(Env, "mainnet") {
					log.Fatalf("%s not recognized as a valid net type for %s\n", Env, node)
				}
				resp := runDockerContainer(ctx, cli, node)
				fmt.Printf("Container ID: %v\n", resp.ID)
			default:
				log.Fatalf("%s not recognized as a valid node type\n", node)
			}
		}
	},
}

RunCmd represents the run command

Functions

This section is empty.

Types

type DockerPort

type DockerPort struct {
	ContainerRESTPort string
	ContainerRPCPort  string
	HostRESTPort      string
	HostRPCPort       string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL