From 79b00d91f2e59f7f458b9d84a9c9f9552770fa54 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Tue, 6 Jun 2017 23:57:43 -0400 Subject: [PATCH] ProofCommander integration --- .gitignore | 1 + commands/proofs/get.go | 31 +++++++++++++++++++++---------- commands/proofs/root.go | 8 +++++++- commands/proofs/state.go | 15 ++++++++------- commands/proofs/tx.go | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index bd2a259..778fd92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.swp vendor .keys tmcli diff --git a/commands/proofs/get.go b/commands/proofs/get.go index 3a4c769..1429cfe 100644 --- a/commands/proofs/get.go +++ b/commands/proofs/get.go @@ -13,11 +13,12 @@ import ( "github.com/tendermint/tendermint/rpc/client" ) +// GetCmd creates the get command for a proof func (p ProofCommander) GetCmd() *cobra.Command { cmd := &cobra.Command{ Use: "get", Short: "Get a proof from the tendermint node", - RunE: p.doGet, + RunE: p.getCmd, SilenceUsage: true, } cmd.Flags().Int(heightFlag, 0, "Height to query (skip to use latest block)") @@ -26,18 +27,27 @@ func (p ProofCommander) GetCmd() *cobra.Command { return cmd } -func (p ProofCommander) doGet(cmd *cobra.Command, args []string) error { +func (p ProofCommander) getCmd(cmd *cobra.Command, args []string) error { app := viper.GetString(appFlag) - pres, err := p.Lookup(app) - if err != nil { - return err - } rawkey := viper.GetString(keyFlag) if rawkey == "" { return errors.New("missing required flag: --" + keyFlag) } + height := viper.GetInt(heightFlag) + + return DoGet(p, app, rawkey, height) +} + +// DoGet performs the get command directly from the proof (not from the CLI) +func DoGet(p ProofCommander, app, rawkey string, height int) error { + + pres, err := p.Lookup(app) + if err != nil { + return err + } + // prepare the query in an app-dependent manner key, err := pres.MakeKey(rawkey) if err != nil { @@ -46,13 +56,11 @@ func (p ProofCommander) doGet(cmd *cobra.Command, args []string) error { // instantiate the prover instance and get a proof from the server p.Init() - h := viper.GetInt(heightFlag) - proof, err := p.Get(key, uint64(h)) + proof, err := p.Get(key, uint64(height)) if err != nil { return err } ph := int(proof.BlockHeight()) - // here is the certifier, root of all knowledge cert, err := commands.GetCertifier() if err != nil { @@ -69,7 +77,10 @@ func (p ProofCommander) doGet(cmd *cobra.Command, args []string) error { if err != nil { return err } - check := lc.Checkpoint{commit.Header, commit.Commit} + check := lc.Checkpoint{ + Header: commit.Header, + Commit: commit.Commit, + } err = cert.Certify(check) if err != nil { return err diff --git a/commands/proofs/root.go b/commands/proofs/root.go index 7d16c75..8811e02 100644 --- a/commands/proofs/root.go +++ b/commands/proofs/root.go @@ -36,7 +36,13 @@ func (p *ProofCommander) Init() { p.Prover = p.ProverFunc(p.node) } -func (p ProofCommander) Register(parent *cobra.Command) { +func (p ProofCommander) Register(parent *cobra.Command, cmdReg func(ProofCommander) *cobra.Command) { + // we add each subcommand here, so we can register the + // ProofCommander in one swoop + parent.AddCommand(cmdReg(p)) +} + +func (p ProofCommander) RegisterGet(parent *cobra.Command) { // we add each subcommand here, so we can register the // ProofCommander in one swoop parent.AddCommand(p.GetCmd()) diff --git a/commands/proofs/state.go b/commands/proofs/state.go index 27fbb4a..cfb908a 100644 --- a/commands/proofs/state.go +++ b/commands/proofs/state.go @@ -20,12 +20,13 @@ data to other peers as needed. `, } +var stateProverCommander = ProofCommander{ + ProverFunc: stateProver, + Presenters: StatePresenters, +} + func init() { - stateProver := ProofCommander{ - ProverFunc: stateProver, - Presenters: StatePresenters, - } - stateProver.Register(stateCmd) + stateProverCommander.RegisterGet(stateCmd) RootCmd.AddCommand(stateCmd) } @@ -34,6 +35,6 @@ func stateProver(node client.Client) lc.Prover { } // RegisterProofStateSubcommand registers a subcommand to proof state cmd -func RegisterProofStateSubcommand(cmd *cobra.Command) { - stateCmd.AddCommand(cmd) +func RegisterProofStateSubcommand(cmdReg func(ProofCommander) *cobra.Command) { + stateProverCommander.Register(stateCmd, cmdReg) } diff --git a/commands/proofs/tx.go b/commands/proofs/tx.go index 7f71dcf..f9313ed 100644 --- a/commands/proofs/tx.go +++ b/commands/proofs/tx.go @@ -25,7 +25,7 @@ func init() { ProverFunc: txProver, Presenters: TxPresenters, } - txProver.Register(txCmd) + txProver.RegisterGet(txCmd) RootCmd.AddCommand(txCmd) }