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

go-kosu: use genesis data on witness start #240

Merged
merged 2 commits into from Aug 28, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

go-kosu: use genesis data on witness start

  • Loading branch information
gchaincl committed Aug 28, 2019
commit 4c380cb9adcc76a0bc5b6d4a0a0cc5b642229677
@@ -2,6 +2,7 @@ package abci

import (
"encoding/json"
"io/ioutil"

abci "github.com/tendermint/tendermint/abci/types"

@@ -23,6 +24,23 @@ func NewGenesisFromRequest(req abci.RequestInitChain) (*Genesis, error) {
return gen, nil
}

// NewGenesisFromFile returns a new Genesis object given a path to the genesis gile
func NewGenesisFromFile(file string) (*Genesis, error) {
gen := &struct {
AppState *Genesis `json:"app_state"`
}{}

data, err := ioutil.ReadFile(file) // nolint:gosec
if err != nil {
return nil, err
}

if err := json.Unmarshal(data, gen); err != nil {
return nil, err
}
return gen.AppState, nil
}

// JSON returns the json representation of the Genesis
func (g *Genesis) JSON() json.RawMessage {
buf, err := json.Marshal(g)
@@ -17,10 +17,6 @@ import (
"go-kosu/witness"
)

const (
nodeAddr = "tcp://0.0.0.0:26657"
)

// Config holds the program execution arguments
type Config struct {
Home string
@@ -42,22 +38,30 @@ func newDB(dir string, debug bool) (db.DB, error) {
return gdb, nil
}

func startWitness(ctx context.Context, ethAddr string, nodeAddr string, key []byte, logger log.Logger) error {
client, err := abci.NewHTTPClient(nodeAddr, key)
func startWitness(ctx context.Context, app *abci.App, ethAddr string, logger log.Logger) error {
client, err := app.NewClient()
if err != nil {
return nil
return err
}

p, err := witness.NewEthereumProvider(ethAddr)
if err != nil {
return err
}

w := witness.New(client, p, witness.DefaultOptions)
gen, err := abci.NewGenesisFromFile(app.Config.GenesisFile())
if err != nil {
return err
}

opts := witness.DefaultOptions
opts.PeriodLimit = int(gen.ConsensusParams.PeriodLimit)
opts.PeriodLength = int(gen.ConsensusParams.PeriodLength)
w := witness.New(client, p, opts)
return w.WithLogger(logger).Start(ctx)
}

func run(cfg *Config, key []byte) error {
func run(cfg *Config) error {
db, err := newDB(cfg.Home, cfg.Debug)
if err != nil {
return err
@@ -78,7 +82,7 @@ func run(cfg *Config, key []byte) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err := startWitness(ctx, cfg.Web3, nodeAddr, key, logger); err != nil {
if err := startWitness(ctx, app, cfg.Web3, logger); err != nil {
return err
}

@@ -89,7 +93,6 @@ func run(cfg *Config, key []byte) error {
func main() {
var (
cfg Config
key []byte
)

cobra.OnInitialize(func() {
@@ -101,13 +104,7 @@ func main() {
Short: "Starts the kosu node",
Long: "Main entrypoint for Kosu validators and full nodes.\nPrior to use, 'kosud init' must be run.",
Run: func(cmd *cobra.Command, args []string) {
var err error
key, err = abci.LoadPrivateKey(cfg.Home)
if err != nil {
stdlog.Fatal(err)
}

if err = run(&cfg, key); err != nil {
if err := run(&cfg); err != nil {
stdlog.Fatal(err)
}
},
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.