github.com/aeternity/aepp-sdk-go@v1.0.3-0.20190606142815-1c0ffdc21fd9/cmd/chain.go (about) 1 // Copyright © 2018 NAME HERE <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "errors" 19 "fmt" 20 21 "github.com/aeternity/aepp-sdk-go/aeternity" 22 "github.com/spf13/cobra" 23 ) 24 25 // chainCmd represents the chain command 26 var chainCmd = &cobra.Command{ 27 Use: "chain", 28 Short: "Query the state of the chain", 29 Long: ``, 30 } 31 32 var topCmd = &cobra.Command{ 33 Use: "top", 34 Short: "Query the top block of the chain", 35 Long: ``, 36 RunE: topFunc, 37 } 38 39 func topFunc(cmd *cobra.Command, args []string) (err error) { 40 aeCli := NewAeCli() 41 v, err := aeCli.APIGetTopBlock() 42 if err != nil { 43 return err 44 } 45 aeternity.PrintObject("block", v) 46 return nil 47 } 48 49 var statusCmd = &cobra.Command{ 50 Use: "status", 51 Short: "Get the status and status of the node running the chain", 52 Long: ``, 53 RunE: statusFunc, 54 } 55 56 func statusFunc(cmd *cobra.Command, args []string) (err error) { 57 aeCli := NewAeCli() 58 v, err := aeCli.APIGetStatus() 59 if err != nil { 60 return err 61 } 62 aeternity.PrintObject("node", v) 63 return nil 64 } 65 66 var limit, startFromHeight uint64 67 var playCmd = &cobra.Command{ 68 Use: "play", 69 Short: "Query the blocks of the chain one after the other", 70 Long: ``, 71 RunE: playFunc, 72 } 73 74 func playFunc(cmd *cobra.Command, args []string) (err error) { 75 aeCli := NewAeCli() 76 blockHeight, err := aeCli.APIGetHeight() 77 if err != nil { 78 return err 79 } 80 81 // deal with the height parameter 82 if startFromHeight > blockHeight { 83 err := fmt.Errorf("Height (%d) is greater that the top block (%d)", startFromHeight, blockHeight) 84 return err 85 } 86 87 if startFromHeight > 0 { 88 blockHeight = startFromHeight 89 } 90 // deal with the limit parameter 91 targetHeight := uint64(0) 92 if limit > 0 { 93 th := blockHeight - limit 94 if th > targetHeight { 95 targetHeight = th 96 } 97 } 98 // run the play 99 for ; blockHeight > targetHeight; blockHeight-- { 100 aeCli.PrintGenerationByHeight(blockHeight) 101 fmt.Println("") 102 } 103 104 return nil 105 } 106 107 var broadcastCmd = &cobra.Command{ 108 Use: "broadcast SIGNED_TRANSACTION", 109 Short: "Broadcast a transaction to the network", 110 Long: ``, 111 Args: cobra.ExactArgs(1), 112 RunE: broadcastFunc, 113 } 114 115 func broadcastFunc(cmd *cobra.Command, args []string) (err error) { 116 // Load variables from arguments 117 txSignedBase64 := args[0] 118 119 if len(txSignedBase64) == 0 || txSignedBase64[0:3] != "tx_" { 120 err := errors.New("Error, missing or invalid recipient address") 121 return err 122 } 123 124 aeCli := NewAeCli() 125 err = aeCli.BroadcastTransaction(txSignedBase64) 126 if err != nil { 127 errFinal := fmt.Errorf("Error while broadcasting transaction: %v", err) 128 return errFinal 129 } 130 131 return nil 132 } 133 134 var ttlCmd = &cobra.Command{ 135 Use: "ttl", 136 Short: "Get the absolute TTL for a Transaction", 137 Long: `Get the absolute TTL (node's height + recommended TTL offset) for a Transaction`, 138 Args: cobra.ExactArgs(0), 139 RunE: ttlFunc, 140 } 141 142 func ttlFunc(cmd *cobra.Command, args []string) (err error) { 143 ae := NewAeCli() 144 height, err := ae.APIGetHeight() 145 if err != nil { 146 errFinal := fmt.Errorf("Error getting height from the node: %v", err) 147 return errFinal 148 } 149 fmt.Println(height + aeternity.Config.Client.TTL) 150 return nil 151 } 152 153 var networkIDCmd = &cobra.Command{ 154 Use: "networkid", 155 Short: "Get the node's network_id", 156 Long: ``, 157 Args: cobra.ExactArgs(0), 158 RunE: networkIDFunc, 159 } 160 161 func networkIDFunc(cmd *cobra.Command, args []string) (err error) { 162 ae := NewAeCli() 163 resp, err := ae.APIGetStatus() 164 if err != nil { 165 errFinal := fmt.Errorf("Error getting status information from the node: %v", err) 166 return errFinal 167 } 168 fmt.Println(*resp.NetworkID) 169 return nil 170 } 171 172 func init() { 173 RootCmd.AddCommand(chainCmd) 174 chainCmd.AddCommand(topCmd) 175 chainCmd.AddCommand(statusCmd) 176 chainCmd.AddCommand(playCmd) 177 chainCmd.AddCommand(broadcastCmd) 178 chainCmd.AddCommand(ttlCmd) 179 chainCmd.AddCommand(networkIDCmd) 180 181 playCmd.Flags().Uint64Var(&limit, "limit", 0, "Print at max 'limit' generations") 182 playCmd.Flags().Uint64Var(&startFromHeight, "height", 0, "Start playing the chain at 'height'") 183 184 // Here you will define your flags and configuration settings. 185 186 // Cobra supports Persistent Flags which will work for this command 187 // and all subcommands, e.g.: 188 // chainCmd.PersistentFlags().String("foo", "", "A help for foo") 189 190 // Cobra supports local flags which will only run when this command 191 // is called directly, e.g.: 192 // chainCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 193 194 }