-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from evanlinjin/feature/new-startup-behavior
New startup behavior.
- Loading branch information
Showing
17 changed files
with
898 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/skycoin/cx-chains/src/cx/cxspec" | ||
"github.com/skycoin/cx-chains/src/cx/cxutil" | ||
) | ||
|
||
type genesisFlags struct { | ||
cmd *flag.FlagSet | ||
|
||
in string | ||
} | ||
|
||
func processGenesisFlags(args []string) genesisFlags { | ||
// Specify default flag values. | ||
f := genesisFlags{ | ||
cmd: flag.NewFlagSet("cxchain-cli genesis", flag.ExitOnError), | ||
in: "skycoin.chain_spec.json", | ||
} | ||
|
||
f.cmd.Usage = func() { | ||
usage := cxutil.DefaultUsageFormat("flags") | ||
usage(f.cmd, nil) | ||
} | ||
|
||
f.cmd.StringVar(&f.in, "in", f.in, "`FILENAME` of file to read in") | ||
|
||
if err := f.cmd.Parse(args); err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
return f | ||
} | ||
|
||
func cmdGenesis(args []string) { | ||
flags := processGenesisFlags(args) | ||
|
||
f, err := os.Open(flags.in) | ||
if err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to read in file.") | ||
} | ||
defer func() { | ||
if err := f.Close(); err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to close file.") | ||
} | ||
}() | ||
|
||
var cSpec cxspec.ChainSpec | ||
if err := json.NewDecoder(f).Decode(&cSpec); err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to decode file") | ||
} | ||
|
||
block, err := cSpec.GenerateGenesisBlock() | ||
if err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to generate genesis block.") | ||
} | ||
|
||
hash := block.HashHeader() | ||
fmt.Println(hash.Hex()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/skycoin/cx-chains/src/cx/cxspec" | ||
"github.com/skycoin/cx-chains/src/cx/cxutil" | ||
) | ||
|
||
type keyFlags struct { | ||
cmd *flag.FlagSet | ||
|
||
in string | ||
field string | ||
} | ||
|
||
func processKeyFlags(args []string) keyFlags { | ||
// Specify default flag values. | ||
f := keyFlags{ | ||
cmd: flag.NewFlagSet("cxchain-cli key", flag.ExitOnError), | ||
in: "skycoin.chain_keys.json", // TODO @evanlinjin: Find const for this value. | ||
field: "seckey", | ||
} | ||
|
||
f.cmd.Usage = func() { | ||
usage := cxutil.DefaultUsageFormat("flags") | ||
usage(f.cmd, nil) | ||
} | ||
|
||
f.cmd.StringVar(&f.in, "in", f.in, "`FILENAME` of file to read in") | ||
f.cmd.StringVar(&f.field, "field", f.field, "`NAME` of field to print") | ||
|
||
if err := f.cmd.Parse(args); err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
return f | ||
} | ||
|
||
func cmdKey(args []string) { | ||
flags := processKeyFlags(args) | ||
|
||
f, err := os.Open(flags.in) | ||
if err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to read in file.") | ||
} | ||
defer func() { | ||
if err := f.Close(); err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to close file.") | ||
} | ||
}() | ||
|
||
var kSpec cxspec.KeySpec | ||
if err := json.NewDecoder(f).Decode(&kSpec); err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to decode file.") | ||
} | ||
|
||
var out string | ||
|
||
switch flags.field { | ||
case "spec_era": | ||
out = kSpec.SpecEra | ||
case "key_type": | ||
out = kSpec.KeyType | ||
case "pubkey": | ||
out = kSpec.PubKey | ||
case "seckey": | ||
out = kSpec.SecKey | ||
case "address": | ||
out = kSpec.Address | ||
default: | ||
log.WithField("field", flags.field). | ||
Fatal("Invalid field input.") | ||
} | ||
|
||
fmt.Println(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"github.com/skycoin/cx-chains/src/cx/cxspec" | ||
"github.com/skycoin/cx-chains/src/cx/cxutil" | ||
"github.com/skycoin/skycoin/src/cipher" | ||
"os" | ||
) | ||
|
||
type postFlags struct { | ||
cmd *flag.FlagSet | ||
|
||
specInput string // chain spec input filename | ||
signedOutput string // signed chain spec output filename | ||
dryRun bool // if set, spec file will not be posted to cx-tracker | ||
tracker string // cx tracker URL | ||
} | ||
|
||
func processPostFlags(args []string) (postFlags, cipher.SecKey) { | ||
// Specify default flag values. | ||
f := postFlags{ | ||
cmd: flag.NewFlagSet("cxchain-cli post", flag.ExitOnError), | ||
|
||
specInput: cxspec.DefaultSpecFilepath, | ||
signedOutput: "", // empty for no output | ||
dryRun: false, | ||
tracker: cxspec.DefaultTrackerURL, | ||
} | ||
|
||
f.cmd.Usage = func() { | ||
usage := cxutil.DefaultUsageFormat("flags") | ||
usage(f.cmd, nil) | ||
printChainSKENV(f.cmd) | ||
} | ||
|
||
f.cmd.StringVar(&f.specInput, "spec", f.specInput, "`FILENAME` of chain spec file input") | ||
f.cmd.StringVar(&f.specInput, "s", f.specInput, "shorthand for 'spec'") | ||
|
||
f.cmd.StringVar(&f.signedOutput, "output", f.signedOutput, "`FILENAME` for signed chain spec output (empty for no output)") | ||
f.cmd.StringVar(&f.signedOutput, "o", f.signedOutput, "shorthand for 'output'") | ||
|
||
f.cmd.BoolVar(&f.dryRun, "dry", f.dryRun, "whether to do a dry run (no actual post to cx-tracker)") | ||
|
||
f.cmd.StringVar(&f.tracker, "tracker", f.tracker, "`URL` for cx-tracker") | ||
f.cmd.StringVar(&f.tracker, "t", f.tracker, "shorthand for 'tracker'") | ||
|
||
// Parse flags. | ||
if err := f.cmd.Parse(args); err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
// Parse ENVs. | ||
genSK, err := parseSKEnv(chainSKEnv) | ||
if err != nil { | ||
log.WithError(err). | ||
WithField(chainSKEnv, genSK.Hex()). | ||
Fatal("Failed to read secret key from ENV.") | ||
} | ||
|
||
return f, genSK | ||
} | ||
|
||
func cmdPost(args []string) { | ||
flags, genSK := processPostFlags(args) | ||
|
||
// Obtain chain spec. | ||
spec, err := cxspec.ReadSpecFile(flags.specInput) | ||
if err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to read spec file.") | ||
} | ||
|
||
// Sign chain spec. | ||
signed, err := cxspec.MakeSignedChainSpec(spec, genSK) | ||
if err != nil { | ||
log.WithError(err). | ||
Fatal("Failed to make signed chain spec.") | ||
} | ||
|
||
if flags.signedOutput != "" { | ||
// TODO @evanlinjin: Write signed output. | ||
panic("flag 'output,o' is not implemented yet") | ||
} | ||
|
||
// tracker client. | ||
tC := cxspec.NewCXTrackerClient(log, nil, flags.tracker) | ||
|
||
if !flags.dryRun { | ||
if err := tC.PostSpec(context.Background(), signed); err != nil { | ||
log.WithError(err).Fatal("Failed to post spec to cx-tracker.") | ||
} | ||
} else { | ||
log.WithField("dry_run", flags.dryRun). | ||
Info("This is a dry run.") | ||
} | ||
|
||
log.WithField("spec_file", flags.specInput). | ||
WithField("cx_tracker", flags.tracker). | ||
Info("Chain spec file successfully posted!") | ||
} |
Oops, something went wrong.