Skip to content

Commit

Permalink
refactor: break logic/network part of some chain subcommands out, so …
Browse files Browse the repository at this point in the history
…that the network can be mocked out
  • Loading branch information
randomshinichi committed Jul 4, 2019
1 parent 62a7590 commit d4be4d6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
61 changes: 51 additions & 10 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -38,31 +39,49 @@ 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
}
aeternity.PrintObject("block", v)
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",
Long: ``,
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
}
aeternity.PrintObject("node", v)
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",
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"os"

"github.com/aeternity/aepp-sdk-go/aeternity"
"github.com/spf13/cobra"
)

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
20 changes: 15 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4be4d6

Please sign in to comment.