Skip to content

Commit

Permalink
Merge pull request #50 from renproject/release/v3.0.5
Browse files Browse the repository at this point in the history
Release/ v3.0.5
  • Loading branch information
jazg committed Feb 13, 2020
2 parents 427bba3 + 44b980f commit e99b315
Show file tree
Hide file tree
Showing 18 changed files with 1,080 additions and 183 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.4
3.0.5
2 changes: 2 additions & 0 deletions artifacts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

echo "Installing Darknode CLI..."

# Install unzip if command not found
if ! [ -x "$(command -v unzip)" ];then
sudo apt-get install unzip
Expand Down
2 changes: 2 additions & 0 deletions artifacts/update.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

echo "Updating Darknode CLI..."

# Check the os type and cpu architecture
ostype="$(uname -s)"
cputype="$(uname -m)"
Expand Down
169 changes: 94 additions & 75 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"math/big"
"os"
"path/filepath"
"strings"
"time"

Expand All @@ -26,53 +25,55 @@ import (
"github.com/urfave/cli"
)

// destroyNode tears down the deployed darknode by its name.
func destroyNode(ctx *cli.Context) error {
force := ctx.Bool("force")
name := ctx.Args().First()
// status represents the registration status of a Darknode.
type status int

// Parse the node config
nodePath := util.NodePath(name)
config, err := darknode.NewConfigFromJSONFile(filepath.Join(nodePath, "config.json"))
if err != nil {
return err
}
const (
nilStatus status = iota // Either not registered or fully deregistered.
pendingRegistration
registered
pendingDeregistration
notRefunded
)

// Connect to Ethereum
client, err := connect(config.Network)
if err != nil {
return err
}
dnr, err := bindings.NewDarknodeRegistry(config.DarknodeRegistryAddress, client.EthClient())
if err != nil {
return err
// err returns the error message of invalid status.
func (s status) err() string {
switch s {
case pendingRegistration:
return "Darknode is currently pending registration."
case registered:
return "Darknode is still registered."
case pendingDeregistration:
return "Darknode is currently pending deregistration."
case notRefunded:
return "Darknode bond has not been withdrawn."
default:
return ""
}
ethAddr := crypto.PubkeyToAddress(config.Keystore.Ecdsa.PublicKey)
}

// Check if the node is registered
if err := checkRegistered(dnr, ethAddr); err != nil {
return err
}
// Check if the node is in pending registration/deregistration stage
if err := checkPendingStage(dnr, ethAddr); err != nil {
return err
}
// Check if the darknode has been refunded
context, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
refunded, err := dnr.IsRefunded(&bind.CallOpts{Context: context}, ethAddr)
if err != nil {
return err
}
if !refunded {
fmt.Println("You haven't refund your darknode. Please refund your darknode from the command center")
return nil
}
// destroyNode tears down the deployed darknode by its name.
func destroyNode(ctx *cli.Context) error {
force := ctx.Bool("force")
name := ctx.Args().First()
path := util.NodePath(name)

// Check if user want to process without extra confirmation
// Check node current registration status.
if !force {
fmt.Println("Do you really want to destroy your darknode? (Yes/No)")
st, err := nodeStatus(name)
if err != nil {
color.Red("Failed to get Darknode registration status: %v", err)
}
switch st {
case pendingRegistration, pendingDeregistration, registered, notRefunded:
color.Red(st.err())
color.Red("Please try again once your Darknode has been fully deregistered and refunded.")
return nil
default:
}

// Last time confirm with user.
fmt.Println("Are you sure you want to destroy your Darknode? (y/N)")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
input := strings.ToLower(strings.TrimSpace(text))
Expand All @@ -81,18 +82,13 @@ func destroyNode(ctx *cli.Context) error {
}
}

color.Green("Back up config...")
backupFolder := filepath.Join(util.Directory, "backup", name)
if err := util.Run("mkdir", "-p", backupFolder); err != nil {
return err
}
backup := fmt.Sprintf("cp %v %v", filepath.Join(nodePath, "config.json"), backupFolder)
if err := util.Run("bash", "-c", backup); err != nil {
color.Green("Backing up config...")
if err := util.BackUpConfig(name); err != nil {
return err
}

color.Green("Destroying your darknode ...")
destroy := fmt.Sprintf("cd %v && terraform destroy --force && cd .. && rm -rf %v", nodePath, name)
color.Green("Destroying your Darknode...")
destroy := fmt.Sprintf("cd %v && terraform destroy --force && cd .. && rm -rf %v", path, name)
return util.Run("bash", "-c", destroy)
}

Expand All @@ -108,7 +104,7 @@ func withdraw(ctx *cli.Context) error {
receiverAddr := common.HexToAddress(withdrawAddress)

// Parse the node config
config, err := darknode.NewConfigFromJSONFile(filepath.Join(util.NodePath(name), "config.json"))
config, err := util.Config(name)
if err != nil {
return err
}
Expand Down Expand Up @@ -180,7 +176,7 @@ func withdraw(ctx *cli.Context) error {
return nil
}

// transfer ETH to
// transfer ETH to the provided address.
func transfer(transactor *bind.TransactOpts, receiver common.Address, amount ethtypes.Amount, client ethclient.Client) (*types.Transaction, error) {
bound := bind.NewBoundContract(receiver, abi.ABI{}, nil, client.EthClient(), nil)
transactor.Value = amount.ToBig()
Expand All @@ -200,6 +196,7 @@ func renAddress(network darknode.Network) string {
}
}

// connect to Ethereum.
func connect(network darknode.Network) (ethclient.Client, error) {
logger := logrus.New()
switch network {
Expand All @@ -212,42 +209,64 @@ func connect(network darknode.Network) (ethclient.Client, error) {
}
}

func checkRegistered(dnr *bindings.DarknodeRegistry, addr common.Address) error {
// nodeStatus returns the registration status of the darknode with given name.
func nodeStatus(name string) (status, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
config, err := util.Config(name)
if err != nil {
return 0, err
}
address := crypto.PubkeyToAddress(config.Keystore.Ecdsa.PublicKey)

registered, err := dnr.IsRegistered(&bind.CallOpts{Context: ctx}, addr)
// Connect to Ethereum
client, err := connect(config.Network)
if err != nil {
return err
return 0, err
}
dnrAddr, err := config.DnrAddr(client.EthClient())
if err != nil {
return 0, err
}
if registered {
color.Red("Your node hasn't been deregistered")
color.Red("Please go to darknode command center to deregister your darknode.")
color.Red("Please try again after you fully deregister your node")
dnr, err := bindings.NewDarknodeRegistry(dnrAddr, client.EthClient())
if err != nil {
return 0, err
}
return nil
}

func checkPendingStage(dnr *bindings.DarknodeRegistry, addr common.Address) error {
reCtx, reCancel := context.WithTimeout(context.Background(), 15*time.Second)
defer reCancel()
pendingRegistration, err := dnr.IsPendingRegistration(&bind.CallOpts{Context: reCtx}, addr)
// Check if node is in pending registration status
pr, err := dnr.IsPendingRegistration(&bind.CallOpts{Context: ctx}, address)
if err != nil {
return err
return 0, err
}
if pendingRegistration {
return fmt.Errorf("your node is currently in pending registration stage, please deregister your node after next epoch shuffle")
if pr {
return pendingRegistration, nil
}

deCtx, deCancel := context.WithTimeout(context.Background(), time.Minute)
defer deCancel()
pendingDeregistration, err := dnr.IsPendingDeregistration(&bind.CallOpts{Context: deCtx}, addr)
// Check if node is registered
r, err := dnr.IsRegistered(&bind.CallOpts{Context: ctx}, address)
if err != nil {
return err
return 0, err
}
if pendingDeregistration {
return fmt.Errorf("your node is currently in pending deregistration stage, please wait for next epoch shuffle and try again")
if r {
return registered, nil
}

return nil
// Check if node in pending deregistration status
pd, err := dnr.IsPendingDeregistration(&bind.CallOpts{Context: ctx}, address)
if err != nil {
return 0, err
}
if pd {
return pendingDeregistration, nil
}

// Check if node has been refunded
refunded, err := dnr.IsRefunded(&bind.CallOpts{Context: ctx}, address)
if err != nil {
return 0, err
}
if !refunded {
return notRefunded, nil
}
return nilStatus, nil
}
18 changes: 5 additions & 13 deletions cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@ var (
}
ScriptFlag = cli.StringFlag{
Name: "script",
Usage: "path of the script file you want to run",
Usage: "A `string` containing commands you want the Darknode to run",
}
NetworkFlag = cli.StringFlag{
Name: "network",
Value: "chaosnet",
Usage: "Darkpool network of your node (default: chaosnet)",
Usage: "Network of your Darknode (default: chaosnet)",
}
AddressFlag = cli.StringFlag{
Name: "address",
Usage: "Ethereum address you want to withdraw the tokens to.",
Usage: "Ethereum address you want to withdraw the tokens to",
}
FileFlag = cli.StringFlag{
Name: "file",
Usage: "path of the script file to run by the darknode",
}
UpdateConfigFlag = cli.BoolFlag{
Name: "config, c",
Usage: "An optional configuration file used to update the configuration",
Usage: "Path of the script file you want the Darknode to run",
}
ForceFlag = cli.BoolFlag{
Name: "force, f",
Expand Down Expand Up @@ -65,10 +61,6 @@ var (
Value: "t3.micro",
Usage: "An optional AWS EC2 instance type (default: t3.micro)",
}
AwsElasticIpFlag = cli.StringFlag{
Name: "aws-elastic-ip",
Usage: "An optional allocation ID for an elastic IP address",
}
AwsProfileFlag = cli.StringFlag{
Name: "aws-profile",
Value: "default",
Expand Down Expand Up @@ -105,7 +97,7 @@ var (
}
GcpCredFlag = cli.StringFlag{
Name: "gcp-credentials",
Usage: "Service Account credential file (JSON) to be used",
Usage: "Path of the Service Account credential file (JSON) to be used",
}
GcpMachineFlag = cli.StringFlag{
Name: "gcp-machine",
Expand Down
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
app.Usage = "A command-line tool for managing Darknodes."
app.Version = binaryVersion

// Fetch latest release and check if our version is bebind.
// Fetch latest release and check if our version is behind.
checkUpdates(app.Version)

// Define sub-commands
Expand Down Expand Up @@ -68,7 +68,7 @@ func main() {
{
Name: "update",
Usage: "Update your Darknodes to the latest software and configuration",
Flags: []cli.Flag{TagsFlag, UpdateConfigFlag},
Flags: []cli.Flag{TagsFlag},
Action: func(c *cli.Context) error {
return updateNode(c)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/provider/aws_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ resource "aws_instance" "darknode" {
inline = [
"set -x",
"until sudo apt update; do sleep 2; done",
"until sudo apt update; do sleep 4; done",
"sudo adduser darknode --gecos \",,,\" --disabled-password",
"sudo rsync --archive --chown=darknode:darknode ~/.ssh /home/darknode",
"sudo DEBIAN_FRONTEND=noninteractive apt-get -y update",
Expand Down
8 changes: 4 additions & 4 deletions cmd/provider/do_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ resource "digitalocean_droplet" "darknode" {
inline = [
"set -x",
"until sudo apt update; do sleep 2; done",
"until sudo apt-get -y update; do sleep 2; done",
"until sudo apt update; do sleep 4; done",
"until sudo apt-get -y update; do sleep 4; done",
"sudo adduser darknode --gecos \",,,\" --disabled-password",
"sudo rsync --archive --chown=darknode:darknode ~/.ssh /home/darknode",
"curl -sSL https://repos.insights.digitalocean.com/install.sh | sudo bash",
"sudo apt-get install ufw",
"until sudo apt-get install ufw; do sleep 4; done",
"sudo ufw limit 22/tcp",
"sudo ufw allow 18514/tcp",
"sudo ufw allow 18515/tcp",
"sudo ufw --force enable",
"until sudo apt-get -y install jq; do sleep 2; done",
"until sudo apt-get -y install jq; do sleep 4; done",
]
connection {
Expand Down
6 changes: 3 additions & 3 deletions cmd/provider/gcp_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,19 @@ resource "google_compute_instance" "darknode" {
inline = [
"set -x",
"until sudo apt update; do sleep 2; done",
"until sudo apt update; do sleep 4; done",
"sudo adduser darknode --gecos \",,,\" --disabled-password",
"sudo rsync --archive --chown=darknode:darknode ~/.ssh /home/darknode",
"sudo DEBIAN_FRONTEND=noninteractive apt-get -y update",
"sudo DEBIAN_FRONTEND=noninteractive apt-get -y upgrade",
"sudo DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade",
"sudo DEBIAN_FRONTEND=noninteractive apt-get -y autoremove",
"sudo apt-get -y install jq",
"sudo apt-get install ufw",
"until sudo apt-get install ufw; do sleep 4; done",
"sudo ufw limit 22/tcp",
"sudo ufw allow 18514/tcp",
"sudo ufw allow 18515/tcp",
"sudo ufw --force enable",
"until sudo apt-get -y install jq; do sleep 4; done",
]
connection {
Expand Down
Loading

0 comments on commit e99b315

Please sign in to comment.