From d4be4d627b7de023232ddf06bbfa7c50d12622b5 Mon Sep 17 00:00:00 2001 From: Andrew Chiw Date: Wed, 3 Jul 2019 17:42:42 +0200 Subject: [PATCH] refactor: break logic/network part of some chain subcommands out, so that the network can be mocked out --- cmd/chain.go | 61 +++++++++++++++++++++++++++++++++++++++++-------- cmd/contract.go | 7 +++--- cmd/root.go | 20 ++++++++++++---- 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/cmd/chain.go b/cmd/chain.go index 3defa13d..b258dab0 100644 --- a/cmd/chain.go +++ b/cmd/chain.go @@ -19,6 +19,7 @@ import ( "fmt" "github.com/aeternity/aepp-sdk-go/aeternity" + "github.com/aeternity/aepp-sdk-go/swagguard/node/models" "github.com/spf13/cobra" ) @@ -38,7 +39,7 @@ var topCmd = &cobra.Command{ func topFunc(cmd *cobra.Command, args []string) (err error) { aeNode := NewAeNode() - v, err := aeNode.GetTopBlock() + v, err := topDo(aeNode) if err != nil { return err } @@ -46,6 +47,15 @@ func topFunc(cmd *cobra.Command, args []string) (err error) { return nil } +type getTopBlocker interface { + GetTopBlock() (kb *models.KeyBlockOrMicroBlockHeader, err error) +} + +func topDo(conn getTopBlocker) (kb *models.KeyBlockOrMicroBlockHeader, err error) { + kb, err = conn.GetTopBlock() + return +} + var statusCmd = &cobra.Command{ Use: "status", Short: "Get the status and status of the node running the chain", @@ -53,9 +63,13 @@ var statusCmd = &cobra.Command{ RunE: statusFunc, } +type getStatuser interface { + GetStatus() (status *models.Status, err error) +} + func statusFunc(cmd *cobra.Command, args []string) (err error) { aeNode := NewAeNode() - v, err := aeNode.GetStatus() + v, err := statusDo(aeNode) if err != nil { return err } @@ -63,6 +77,11 @@ func statusFunc(cmd *cobra.Command, args []string) (err error) { return nil } +func statusDo(conn getStatuser) (status *models.Status, err error) { + status, err = conn.GetStatus() + return +} + var limit, startFromHeight uint64 var playCmd = &cobra.Command{ Use: "play", @@ -141,15 +160,28 @@ var ttlCmd = &cobra.Command{ func ttlFunc(cmd *cobra.Command, args []string) (err error) { ae := NewAeNode() - height, err := ae.GetHeight() + ans, err := ttlDo(ae) if err != nil { - errFinal := fmt.Errorf("Error getting height from the node: %v", err) - return errFinal + return } - fmt.Println(height + aeternity.Config.Client.TTL) + fmt.Println(ans) return nil } +type getHeighter interface { + GetHeight() (uint64, error) +} + +func ttlDo(conn getHeighter) (ttl uint64, err error) { + height, err := conn.GetHeight() + if err != nil { + errFinal := fmt.Errorf("Error getting height from the node: %v", err) + return 0, errFinal + } + ttl = height + aeternity.Config.Client.TTL + return +} + var networkIDCmd = &cobra.Command{ Use: "networkid", Short: "Get the node's network_id", @@ -160,15 +192,24 @@ var networkIDCmd = &cobra.Command{ func networkIDFunc(cmd *cobra.Command, args []string) (err error) { ae := NewAeNode() - resp, err := ae.GetStatus() + nID, err := networkIDDo(ae) if err != nil { - errFinal := fmt.Errorf("Error getting status information from the node: %v", err) - return errFinal + return err } - fmt.Println(*resp.NetworkID) + fmt.Println(nID) return nil } +func networkIDDo(conn getStatuser) (networkID string, err error) { + resp, err := conn.GetStatus() + if err != nil { + errFinal := fmt.Errorf("Error getting status information from the node: %v", err) + return "", errFinal + } + networkID = *resp.NetworkID + return +} + func init() { RootCmd.AddCommand(chainCmd) chainCmd.AddCommand(topCmd) diff --git a/cmd/contract.go b/cmd/contract.go index 541d1da7..9426f083 100644 --- a/cmd/contract.go +++ b/cmd/contract.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" - "github.com/aeternity/aepp-sdk-go/aeternity" "github.com/spf13/cobra" ) @@ -26,7 +25,7 @@ var compileCmd = &cobra.Command{ } func compileFunc(cmd *cobra.Command, args []string) (err error) { - compiler := aeternity.NewCompiler(compilerURL, debug) + compiler := NewCompiler() s, err := readSource(args[0]) if err != nil { return err @@ -47,7 +46,7 @@ var encodeCalldataCmd = &cobra.Command{ } func encodeCalldataFunc(cmd *cobra.Command, args []string) (err error) { - compiler := aeternity.NewCompiler(compilerURL, debug) + compiler := NewCompiler() s, err := readSource(args[0]) if err != nil { @@ -72,7 +71,7 @@ var decodeCalldataCmd = &cobra.Command{ } func decodeCalldataFunc(cmd *cobra.Command, args []string) (err error) { - compiler := aeternity.NewCompiler(compilerURL, debug) + compiler := NewCompiler() var decodeWithSource = func(path string, callData string) (function string, arguments []interface{}, err error) { source, err := readSource(path) diff --git a/cmd/root.go b/cmd/root.go index 607bae74..9c9b9e0b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,14 +45,24 @@ func Execute(v string) { } } -// NewAeNode is just a helper function that gives you a Node so that -// you don't have to maintain a Node global variable (which needs the -// config vars to be read immediately, with this helper function you can -// defer the reading of the variables until the subcommand's execution) -func NewAeNode() *aeternity.Node { +// NewAeNode is just a helper function that gives you a Node so that you don't +// have to maintain a Node global variable (which needs the config vars to be +// read immediately, with this helper function you can defer the reading of the +// variables until the subcommand's execution) Defined as a var so unittests can +// mock this function out. +var NewAeNode = func() *aeternity.Node { return aeternity.NewNode(aeternity.Config.Node.URL, debug) } +// NewCompiler is just a helper function that gives you a Compiler so that you don't +// have to maintain a Compiler global variable (which needs the config vars to be +// read immediately, with this helper function you can defer the reading of the +// variables until the subcommand's execution) Defined as a var so unittests can +// mock this function out. +var NewCompiler = func() *aeternity.Compiler { + return aeternity.NewCompiler(compilerURL, debug) +} + func init() { // cobra.OnInitialize(initConfig) viper.AutomaticEnv() // read in environment variables that match