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

Reorder cxspec/locate.go file. #37

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions cmd/cxchain-cli/cmd_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type genesisFlags struct {
cmd *flag.FlagSet

in string
}

Expand Down Expand Up @@ -58,12 +58,12 @@ func cmdGenesis(args []string) {
Fatal("Failed to decode file")
}

block, err := cSpec.GenerateGenesisBlock()
block, err := cSpec.ObtainGenesisBlock()
if err != nil {
log.WithError(err).
Fatal("Failed to generate genesis block.")
}

hash := block.HashHeader()
fmt.Println(hash.Hex())
}
}
31 changes: 16 additions & 15 deletions cmd/cxchain-cli/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ import (
"os"
"strings"

"github.com/skycoin/skycoin/src/cipher"

"github.com/skycoin/cx/cxgo/cxlexer"
"github.com/skycoin/cx/cxgo/parser"

"github.com/skycoin/cx-chains/src/cx/cxutil"
"github.com/skycoin/skycoin/src/cipher"

"github.com/skycoin/cx-chains/src/cx/cxflags"
"github.com/skycoin/cx-chains/src/cx/cxspec"
"github.com/skycoin/cx-chains/src/cx/cxspec/alpha"
"github.com/skycoin/cx-chains/src/cx/cxutil"
)

const filePerm = 0644

type newFlags struct {
cmd *flag.FlagSet

replace bool
unifyKeys bool
coinName string
coinTicker string
replace bool
unifyKeys bool
coinName string
coinTicker string
trackerAddr string

debugLexer bool
debugProfile int
Expand All @@ -43,10 +43,11 @@ func processNewFlags(args []string) newFlags {
f := newFlags{
cmd: flag.NewFlagSet("cxchain-cli new", flag.ExitOnError),

replace: false,
unifyKeys: false,
coinName: "skycoin",
coinTicker: "SKY",
replace: false,
unifyKeys: false,
coinName: "skycoin",
coinTicker: "SKY",
trackerAddr: cxspec.DefaultTrackerURL,

debugLexer: false,
debugProfile: 0,
Expand All @@ -66,10 +67,10 @@ func processNewFlags(args []string) newFlags {
f.cmd.BoolVar(&f.replace, "r", f.replace, "shorthand for 'replace'")
f.cmd.BoolVar(&f.unifyKeys, "unify", f.unifyKeys, "whether to use the same keys for genesis and chain")
f.cmd.BoolVar(&f.unifyKeys, "u", f.unifyKeys, "shorthand for 'unify'")

f.cmd.StringVar(&f.coinName, "coin", f.coinName, "`NAME` for cx coin")
f.cmd.StringVar(&f.coinName, "c", f.coinName, "shorthand for 'coin'")
f.cmd.StringVar(&f.coinTicker, "ticker", f.coinTicker, "`SYMBOL` for cx coin ticker")
f.cmd.StringVar(&f.coinTicker, "t", f.coinTicker, "shorthand for 'ticker'")
f.cmd.StringVar(&f.trackerAddr, "tracker", f.trackerAddr, "`URL` of cx tracker")

f.cmd.BoolVar(&f.debugLexer, "debug-lexer", f.debugLexer, "enable lexer debugging by printing all scanner tokens")
f.cmd.IntVar(&f.debugProfile, "debug-profile", f.debugProfile, "Enable CPU+MEM profiling and set CPU profiling rate. Visualize .pprof files with 'go get github.com/google/pprof' and 'pprof -http=:8080 file.pprof'")
Expand Down Expand Up @@ -160,7 +161,7 @@ func cmdNew(args []string) {
genAddr := cipher.AddressFromPubKey(genPK)

// Generate and write chain spec file.
cSpec, err := cxspec.New(flags.coinName, flags.coinTicker, chainSK, genAddr, genProgState)
cSpec, err := alpha.New(flags.coinName, flags.coinTicker, chainSK, flags.trackerAddr, genAddr, genProgState)
if err != nil {
log.WithError(err).
Fatal("Failed to generate chain spec.")
Expand Down
8 changes: 7 additions & 1 deletion cmd/cxchain-cli/cmd_peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ func cmdPeers(args []string) {
rootCmd := flag.NewFlagSet("cxchain-cli peers", flag.ExitOnError)
spec := processSpecFlags(context.Background(), rootCmd, args)

webPort, err := spec.ObtainWebInterfacePort()
if err != nil {
log.WithField("spec_era", spec.CXSpecEra()).
Fatal("Failed to obtain WebInterfacePort")
}

// nodeAddr holds the value parsed from the flags 'node' and 'n'
nodeAddr := fmt.Sprintf("http://127.0.0.1:%d", spec.Node.WebInterfacePort)
nodeAddr := fmt.Sprintf("http://127.0.0.1:%d", webPort)
addNodeAddrFlag := func(cmd *flag.FlagSet) {
cmd.StringVar(&nodeAddr, "node", nodeAddr, "HTTP API `ADDRESS` of cxchain node")
cmd.StringVar(&nodeAddr, "n", nodeAddr, "shorthand for 'node'")
Expand Down
10 changes: 6 additions & 4 deletions cmd/cxchain-cli/cmd_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"context"
"flag"
"os"

"github.com/skycoin/skycoin/src/cipher"

"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 {
Expand All @@ -25,8 +27,8 @@ func processPostFlags(args []string) (postFlags, cipher.SecKey) {

specInput: cxspec.DefaultSpecFilepath,
signedOutput: "", // empty for no output
dryRun: false,
tracker: cxspec.DefaultTrackerURL,
dryRun: false,
tracker: cxspec.DefaultTrackerURL,
}

f.cmd.Usage = func() {
Expand Down
24 changes: 20 additions & 4 deletions cmd/cxchain-cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func processRunFlags(args []string) (runFlags, cxspec.ChainSpec, cipher.SecKey)
cmd := flag.NewFlagSet("cxchain-cli run", flag.ExitOnError)
spec := processSpecFlags(context.Background(), cmd, args)

webPort, err := spec.ObtainWebInterfacePort()
if err != nil {
log.WithField("spec_era", spec.CXSpecEra()).
Fatal("Failed to obtain web interface port.")
}

genesisAddr, err := spec.ObtainGenesisAddr()
if err != nil {
log.WithField("spec_era", spec.CXSpecEra()).
Fatal("Failed to obtain genesis address.")
}

f := runFlags{
cmd: cmd,

Expand All @@ -81,7 +93,7 @@ func processRunFlags(args []string) (runFlags, cxspec.ChainSpec, cipher.SecKey)
// TODO @evanlinjin: Find a way to set this later on.
// TODO @evanlinjin: This way, we would not need to call '.Locate' so
// TODO @evanlinjin: early within processSpecFlags()
nodeAddr: fmt.Sprintf("http://127.0.0.1:%d", spec.Node.WebInterfacePort),
nodeAddr: fmt.Sprintf("http://127.0.0.1:%d", webPort),
}

f.cmd.Usage = func() {
Expand All @@ -108,7 +120,7 @@ func processRunFlags(args []string) (runFlags, cxspec.ChainSpec, cipher.SecKey)
// Ensure genesis secret key is provided if 'inject' flag is set.
var genSK cipher.SecKey
if f.inject {
genSK = readRunENVs(cipher.MustDecodeBase58Address(spec.GenesisAddr))
genSK = readRunENVs(genesisAddr)
}

// Log stuff.
Expand Down Expand Up @@ -137,10 +149,14 @@ func cmdRun(args []string) {
c := api.NewClient(flags.nodeAddr)

// Prepare address.
addr := cipher.MustDecodeBase58Address(spec.GenesisAddr)
genesisAddr, err := spec.ObtainGenesisAddr()
if err != nil {
log.WithField("spec_era", spec.CXSpecEra()).
Fatal("Failed to obtain genesis address.")
}

// Parse and run program.
ux, progB, err := PrepareChainProg(cxFilenames, cxRes.CXSources, c, addr, flags.debugLexer, flags.debugProfile)
ux, progB, err := PrepareChainProg(cxFilenames, cxRes.CXSources, c, genesisAddr, flags.debugLexer, flags.debugProfile)
if err != nil {
log.WithError(err).Fatal("Failed to prepare chain CX program.")
}
Expand Down
16 changes: 14 additions & 2 deletions cmd/cxchain-cli/cmd_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,23 @@ func processStateFlags(args []string) (stateFlags, cipher.Address) {
cmd := flag.NewFlagSet("cxchain-cli state", flag.ExitOnError)
spec := processSpecFlags(context.Background(), cmd, args)

webPort, err := spec.ObtainWebInterfacePort()
if err != nil {
log.WithField("spec_era", spec.CXSpecEra()).
Fatal("Failed to obtain web interface port.")
}

genesisAddr, err := spec.ObtainGenesisAddr()
if err != nil {
log.WithField("spec_era", spec.CXSpecEra()).
Fatal("Failed to obtain genesis address.")
}

f := stateFlags{
cmd: cmd,
MemoryFlags: cxflags.DefaultMemoryFlags(),
nodeAddr: fmt.Sprintf("http://127.0.0.1:%d", spec.Node.WebInterfacePort),
appAddr: cipher.MustDecodeBase58Address(spec.GenesisAddr).String(),
nodeAddr: fmt.Sprintf("http://127.0.0.1:%d", webPort),
appAddr: genesisAddr.String(),
}

f.cmd.Usage = func() {
Expand Down
34 changes: 26 additions & 8 deletions cmd/cxchain/cxchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,19 @@ func trackerUpdateLoop(nodeSK cipher.SecKey, nodeTCPAddr string, spec cxspec.Cha
client := cxspec.NewCXTrackerClient(log, nil, specFlags.CXTracker)
nodePK := cipher.MustPubKeyFromSecKey(nodeSK)

block, err := spec.GenerateGenesisBlock()
block, err := spec.ObtainGenesisBlock()
if err != nil {
panic(err) // should not happen
}
hash := block.HashHeader()

chainPK, err := spec.ObtainChainPubKey()
if err != nil {
panic(err) // TODO @evanlinjin: Proper fail and error reporting?
}

// If publisher, ensure spec is registered.
if isPub := nodePK == spec.ProcessedChainPubKey(); isPub {
if isPub := nodePK == chainPK; isPub {
signedSpec, err := cxspec.MakeSignedChainSpec(spec, nodeSK)
if err != nil {
panic(err) // should not happen
Expand Down Expand Up @@ -161,8 +166,10 @@ func trackerUpdateLoop(nodeSK cipher.SecKey, nodeTCPAddr string, spec cxspec.Cha
func main() {
// Register and parse flags for cx chain spec.
spec := locateSpec()
cxspec.PopulateParamsModule(spec)
log.Info(spec.PrintString())
if err := spec.PopulateParamsModule(); err != nil {
log.WithError(err).Fatal("Failed to populate params module with spec.")
}
log.Info(spec.String())

// Register additional CLI flags.
cmd := flag.CommandLine
Expand All @@ -180,9 +187,10 @@ func main() {
ensureConfMode(&conf)

// Node config: Populate node config based on chain spec content.
if err := cxspec.PopulateNodeConfig(specFlags.CXTracker, spec, &conf); err != nil {
log.WithError(err).Fatal("Failed to parse from chain spec file.")
if err := spec.PopulateNodeConfig(&conf); err != nil {
log.WithError(err).Fatal("Failed to populate node config from spec file.")
}
// TODO @evanlinjin: Do we need to do something about the cx tracker URL?

// Node config: Ensure node keys are set.
// - If node secret key is null, randomly generate one.
Expand All @@ -196,10 +204,15 @@ func main() {
}
nodePK = cipher.MustPubKeyFromSecKey(nodeSK)

chainPK, err := spec.ObtainChainPubKey()
if err != nil {
log.WithError(err).Fatal("Failed to obtain chain pk from spec.")
}

// Node config: Enable publisher mode if conditions are met.
// - Skip if 'forceClient' is set.
// - Skip if 'nodePK' is not equal to chain spec's PK.
if !forceClient && nodePK == spec.ProcessedChainPubKey() {
if !forceClient && nodePK == chainPK {
conf.BlockchainSeckeyStr = nodeSK.Hex()
conf.RunBlockPublisher = true
}
Expand Down Expand Up @@ -264,8 +277,13 @@ func main() {
cxdmsg.ServeDmsg(dmsgCtx, dmsgLog, dmsgConf, dmsgAPI)
}()

progState, err := spec.ObtainGenesisProgState()
if err != nil {
log.WithError(err).Fatal("Failed to obtain genesis program state from spec.")
}

// Run main daemon.
if err := coin.Run(spec.RawGenesisProgState(), gwCh); err != nil {
if err := coin.Run(progState, gwCh); err != nil {
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions src/cx/cxdmsg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func (api *API) obtainNodeStats() (NodeStats, error) {
return NodeStats{}, err
}

genBlock, err := api.ChainSpec.GenerateGenesisBlock()
genBlock, err := api.ChainSpec.ObtainGenesisBlock()
if err != nil {
return NodeStats{}, err
}

stats := NodeStats{
CXChainVersion: api.Version,
CXChainSpecEra: api.ChainSpec.SpecEra,
CXChainSpecEra: api.ChainSpec.CXSpecEra(),
GenesisBlockHash: cipher.SHA256(genBlock.HashHeader()),
PrevBlockHash: cipher.SHA256(chainMeta.HeadBlock.Head.PrevHash),
HeadBlockHash: cipher.SHA256(chainMeta.HeadBlock.HashHeader()),
Expand Down
Loading