Skip to content

Commit

Permalink
refactor: New pattern in chain, chain_test.go: online/offline tests
Browse files Browse the repository at this point in the history
update comment because newAeNode() and newCompiler() are no longer vars
  • Loading branch information
randomshinichi committed Jul 26, 2019
1 parent 54ac957 commit e8d589b
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 88 deletions.
103 changes: 46 additions & 57 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"

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

Expand All @@ -34,57 +33,58 @@ var topCmd = &cobra.Command{
Use: "top",
Short: "Query the top block of the chain",
Long: ``,
RunE: topFunc,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return topFunc(aeNode, args)
},
}

func topFunc(cmd *cobra.Command, args []string) (err error) {
aeNode := newAeNode()
v, err := topDo(aeNode)
func topFunc(conn aeternity.GetTopBlocker, args []string) error {
v, err := conn.GetTopBlock()
if err != nil {
return err
}
PrintObject("block", v)
return nil
}

func topDo(conn aeternity.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,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return statusFunc(aeNode, args)
},
}

func statusFunc(cmd *cobra.Command, args []string) (err error) {
aeNode := newAeNode()
v, err := statusDo(aeNode)
func statusFunc(conn aeternity.GetStatuser, args []string) (err error) {
v, err := conn.GetStatus()
if err != nil {
return err
}
PrintObject("node", v)
return nil
}

func statusDo(conn aeternity.GetStatuser) (status *models.Status, err error) {
status, err = conn.GetStatus()
return
}

var limit, startFromHeight uint64
var playCmd = &cobra.Command{
Use: "play",
Short: "Query the blocks of the chain one after the other",
Long: ``,
RunE: playFunc,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return playFunc(aeNode, args)
},
}

type playFuncInterface interface {
aeternity.GetHeighter
getGenerationMicroBlockTransactioner
}

func playFunc(cmd *cobra.Command, args []string) (err error) {
aeNode := newAeNode()
blockHeight, err := aeNode.GetHeight()
func playFunc(conn playFuncInterface, args []string) (err error) {
blockHeight, err := conn.GetHeight()
if err != nil {
return err
}
Expand All @@ -108,7 +108,7 @@ func playFunc(cmd *cobra.Command, args []string) (err error) {
}
// run the play
for ; blockHeight > targetHeight; blockHeight-- {
PrintGenerationByHeight(aeNode, blockHeight)
PrintGenerationByHeight(conn, blockHeight)
fmt.Println("")
}

Expand All @@ -120,10 +120,13 @@ var broadcastCmd = &cobra.Command{
Short: "Broadcast a transaction to the network",
Long: ``,
Args: cobra.ExactArgs(1),
RunE: broadcastFunc,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return broadcastFunc(aeNode, args)
},
}

func broadcastFunc(cmd *cobra.Command, args []string) (err error) {
func broadcastFunc(conn aeternity.PostTransactioner, args []string) (err error) {
// Load variables from arguments
txSignedBase64 := args[0]

Expand All @@ -132,8 +135,7 @@ func broadcastFunc(cmd *cobra.Command, args []string) (err error) {
return err
}

aeNode := newAeNode()
err = aeternity.BroadcastTransaction(aeNode, txSignedBase64)
err = aeternity.BroadcastTransaction(conn, txSignedBase64)
if err != nil {
errFinal := fmt.Errorf("Error while broadcasting transaction: %v", err)
return errFinal
Expand All @@ -147,55 +149,42 @@ var ttlCmd = &cobra.Command{
Short: "Get the absolute TTL for a Transaction",
Long: `Get the absolute TTL (node's height + recommended TTL offset) for a Transaction`,
Args: cobra.ExactArgs(0),
RunE: ttlFunc,
}

func ttlFunc(cmd *cobra.Command, args []string) (err error) {
ae := newAeNode()
ans, err := ttlDo(ae)
if err != nil {
return
}
fmt.Println(ans)
return nil
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return ttlFunc(aeNode, args)
},
}

func ttlDo(conn aeternity.GetHeighter) (ttl uint64, err error) {
func ttlFunc(conn aeternity.GetHeighter, args []string) (err error) {
height, err := conn.GetHeight()
if err != nil {
errFinal := fmt.Errorf("Error getting height from the node: %v", err)
return 0, errFinal
return errFinal
}
ttl = height + aeternity.Config.Client.TTL
return
fmt.Println(ttl)
return nil
}

var networkIDCmd = &cobra.Command{
Use: "networkid",
Short: "Get the node's network_id",
Long: ``,
Args: cobra.ExactArgs(0),
RunE: networkIDFunc,
}

func networkIDFunc(cmd *cobra.Command, args []string) (err error) {
ae := newAeNode()
nID, err := networkIDDo(ae)
if err != nil {
return err
}
fmt.Println(nID)
return nil
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return networkIDFunc(aeNode, args)
},
}

func networkIDDo(conn aeternity.GetStatuser) (networkID string, err error) {
func networkIDFunc(conn aeternity.GetStatuser, args []string) (err error) {
resp, err := conn.GetStatus()
if err != nil {
errFinal := fmt.Errorf("Error getting status information from the node: %v", err)
return "", errFinal
return errFinal
}
networkID = *resp.NetworkID
return
fmt.Println(*resp.NetworkID)
return nil
}

func init() {
Expand Down

0 comments on commit e8d589b

Please sign in to comment.