Fix lot more lightclient imports

This commit is contained in:
Ethan Frey 2017-10-20 20:27:18 +02:00
parent 1f897cabde
commit 624133ad92
7 changed files with 42 additions and 37 deletions

View File

@ -74,7 +74,7 @@ func GetProviders() (trusted certifiers.Provider, source certifiers.Provider) {
}
// GetCertifier constructs a dynamic certifier from the config info
func GetCertifier() (*certifiers.InquiringCertifier, error) {
func GetCertifier() (*certifiers.Inquiring, error) {
// load up the latest store....
trust := GetTrustedProvider()
source := GetSourceProvider()

View File

@ -280,14 +280,14 @@ func initSeed() (err error) {
trust, source := GetProviders()
// load a seed file, or get data from the provider
var seed certifiers.Seed
var seed certifiers.FullCommit
seedFile := viper.GetString(SeedFlag)
if seedFile == "" {
fmt.Println("Loading validator set from tendermint rpc...")
seed, err = certifiers.LatestSeed(source)
seed, err = source.LatestCommit()
} else {
fmt.Printf("Loading validators from file %s\n", seedFile)
seed, err = certifiers.LoadSeed(seedFile)
seed, err = files.LoadFullCommit(seedFile)
}
// can't load the seed? abort!
if err != nil {
@ -305,8 +305,8 @@ func initSeed() (err error) {
if hash != "" {
var hashb []byte
hashb, err = hex.DecodeString(hash)
if err == nil && !bytes.Equal(hashb, seed.Hash()) {
err = errors.Errorf("Seed hash doesn't match expectation: %X", seed.Hash())
if err == nil && !bytes.Equal(hashb, seed.ValidatorsHash()) {
err = errors.Errorf("Seed hash doesn't match expectation: %X", seed.ValidatorsHash())
}
} else {
err = validateHash(seed)
@ -317,14 +317,14 @@ func initSeed() (err error) {
}
// if accepted, store seed as current state
trust.StoreSeed(seed)
trust.StoreCommit(seed)
return nil
}
func validateHash(seed certifiers.Seed) error {
func validateHash(seed certifiers.FullCommit) error {
// ask the user to verify the validator hash
fmt.Println("\nImportant: if this is incorrect, all interaction with the chain will be insecure!")
fmt.Printf(" Given validator hash valid: %X\n", seed.Hash())
fmt.Printf(" Given validator hash valid: %X\n", seed.ValidatorsHash())
fmt.Println("Is this valid (y/n)?")
valid := askForConfirmation()
if !valid {

View File

@ -14,6 +14,8 @@ import (
"github.com/tendermint/light-client/proofs"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands"
)

View File

@ -5,6 +5,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/light-client/certifiers/files"
"github.com/cosmos/cosmos-sdk/client/commands"
)
@ -34,11 +36,11 @@ func exportSeed(cmd *cobra.Command, args []string) error {
trust, _ := commands.GetProviders()
h := viper.GetInt(heightFlag)
hash := viper.GetString(hashFlag)
seed, err := loadSeed(trust, h, hash, "")
fc, err := loadCommit(trust, h, hash, "")
if err != nil {
return err
}
// now get the output file and write it
return seed.WriteJSON(path)
return files.SaveFullCommitJSON(fc, path)
}

View File

@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/light-client/certifiers"
"github.com/tendermint/light-client/certifiers/files"
"github.com/cosmos/cosmos-sdk/client/commands"
)
@ -42,18 +42,18 @@ func importSeed(cmd *cobra.Command, args []string) error {
// parse the input file
path := args[0]
seed, err := certifiers.LoadSeedJSON(path)
fc, err := files.LoadFullCommitJSON(path)
if err != nil {
return err
}
// just do simple checks in --dry-run
if viper.GetBool(dryFlag) {
fmt.Printf("Testing seed %d/%X\n", seed.Height(), seed.Hash())
err = seed.ValidateBasic(cert.ChainID())
fmt.Printf("Testing commit %d/%X\n", fc.Height(), fc.ValidatorsHash())
err = fc.ValidateBasic(cert.ChainID())
} else {
fmt.Printf("Importing seed %d/%X\n", seed.Height(), seed.Hash())
err = cert.Update(seed.Checkpoint, seed.Validators)
fmt.Printf("Importing commit %d/%X\n", fc.Height(), fc.ValidatorsHash())
err = cert.Update(fc)
}
return err
}

View File

@ -9,6 +9,7 @@ import (
"github.com/spf13/viper"
"github.com/tendermint/light-client/certifiers"
"github.com/tendermint/light-client/certifiers/files"
"github.com/cosmos/cosmos-sdk/client/commands"
)
@ -21,11 +22,11 @@ const (
var showCmd = &cobra.Command{
Use: "show",
Short: "Show the details of one selected seed",
Short: "Show the details of one selected commit",
Long: `Shows the most recent downloaded key by default.
If desired, you can select by height, validator hash, or a file.
`,
RunE: commands.RequireInit(showSeed),
RunE: commands.RequireInit(showCommit),
SilenceUsage: true,
}
@ -36,38 +37,38 @@ func init() {
RootCmd.AddCommand(showCmd)
}
func loadSeed(p certifiers.Provider, h int, hash, file string) (seed certifiers.Seed, err error) {
// load the seed from the proper place
func loadCommit(p certifiers.Provider, h int, hash, file string) (fc certifiers.FullCommit, err error) {
// load the commit from the proper place
if h != 0 {
seed, err = p.GetByHeight(h)
fc, err = p.GetByHeight(h)
} else if hash != "" {
var vhash []byte
vhash, err = hex.DecodeString(hash)
if err == nil {
seed, err = p.GetByHash(vhash)
fc, err = p.GetByHash(vhash)
}
} else if file != "" {
seed, err = certifiers.LoadSeedJSON(file)
fc, err = files.LoadFullCommitJSON(file)
} else {
// default is latest seed
seed, err = certifiers.LatestSeed(p)
// default is latest commit
fc, err = p.LatestCommit()
}
return
}
func showSeed(cmd *cobra.Command, args []string) error {
func showCommit(cmd *cobra.Command, args []string) error {
trust, _ := commands.GetProviders()
h := viper.GetInt(heightFlag)
hash := viper.GetString(hashFlag)
file := viper.GetString(fileFlag)
seed, err := loadSeed(trust, h, hash, file)
fc, err := loadCommit(trust, h, hash, file)
if err != nil {
return err
}
// now render it!
data, err := json.MarshalIndent(seed, "", " ")
data, err := json.MarshalIndent(fc, "", " ")
fmt.Println(string(data))
return err
}

View File

@ -13,8 +13,8 @@ import (
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update seed to current height if possible",
RunE: commands.RequireInit(updateSeed),
Short: "Update commit to current height if possible",
RunE: commands.RequireInit(updateCommit),
SilenceUsage: true,
}
@ -23,27 +23,27 @@ func init() {
RootCmd.AddCommand(updateCmd)
}
func updateSeed(cmd *cobra.Command, args []string) error {
func updateCommit(cmd *cobra.Command, args []string) error {
cert, err := commands.GetCertifier()
if err != nil {
return err
}
h := viper.GetInt(heightFlag)
var seed certifiers.Seed
var fc certifiers.FullCommit
if h <= 0 {
// get the lastest from our source
seed, err = certifiers.LatestSeed(cert.SeedSource)
fc, err = cert.Source.LatestCommit()
} else {
seed, err = cert.SeedSource.GetByHeight(h)
fc, err = cert.Source.GetByHeight(h)
}
if err != nil {
return err
}
// let the certifier do it's magic to update....
fmt.Printf("Trying to update to height: %d...\n", seed.Height())
err = cert.Update(seed.Checkpoint, seed.Validators)
fmt.Printf("Trying to update to height: %d...\n", fc.Height())
err = cert.Update(fc)
if err != nil {
return err
}