github.com/aeternity/aepp-sdk-go/v7@v7.0.1/cmd/inspect.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  	"fmt"
    19  	"os"
    20  	"regexp"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/aeternity/aepp-sdk-go/v7/binary"
    25  	"github.com/aeternity/aepp-sdk-go/v7/naet"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  // inspectCmd represents the inspect command
    31  var inspectCmd = &cobra.Command{
    32  	Use:   "inspect",
    33  	Short: "Inspect an object of the blockchain",
    34  	Long: `Inspect an object of the chain
    35  
    36  Valid object to inspect are block hash, transaction hash, accounts`,
    37  	RunE: func(cmd *cobra.Command, args []string) error {
    38  		node := newAeNode()
    39  		return inspectFunc(node, args)
    40  	},
    41  	Args: cobra.MinimumNArgs(1),
    42  }
    43  
    44  func init() {
    45  	RootCmd.AddCommand(inspectCmd)
    46  
    47  	// Here you will define your flags and configuration settings.
    48  
    49  	// Cobra supports Persistent Flags which will work for this command
    50  	// and all subcommands, e.g.:
    51  	// inspectCmd.PersistentFlags().String("foo", "", "A help for foo")
    52  
    53  	// Cobra supports local flags which will only run when this command
    54  	// is called directly, e.g.:
    55  	// inspectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
    56  
    57  }
    58  
    59  func printResult(title string, v interface{}, err error) {
    60  	if err != nil {
    61  		fmt.Println(err)
    62  		os.Exit(1)
    63  	}
    64  	PrintObject(title, v)
    65  }
    66  
    67  type nodeGetters interface {
    68  	naet.GetGenerationByHeighter
    69  	naet.GetNameEntryByNamer
    70  	naet.GetAccounter
    71  	naet.GetMicroBlockHeaderByHasher
    72  	naet.GetMicroBlockTransactionsByHasher
    73  	naet.GetKeyBlockByHasher
    74  	naet.GetTransactionByHasher
    75  	naet.GetOracleByPubkeyer
    76  }
    77  
    78  func printNameEntry(conn naet.GetNameEntryByNamer, name string) (err error) {
    79  	v, err := conn.GetNameEntryByName(name)
    80  	if err != nil {
    81  		return err
    82  	}
    83  	printResult("aens", v, err)
    84  	return err
    85  }
    86  
    87  func printAccount(conn naet.GetAccounter, accountID string) (err error) {
    88  	v, err := conn.GetAccount(accountID)
    89  	if err != nil {
    90  		return err
    91  	}
    92  	printResult("account", v, err)
    93  	return err
    94  }
    95  
    96  type getMicroBlockHeaderTransactions interface {
    97  	naet.GetMicroBlockHeaderByHasher
    98  	naet.GetMicroBlockTransactionsByHasher
    99  }
   100  
   101  func printMicroBlockAndTransactions(conn getMicroBlockHeaderTransactions, mbHash string) (err error) {
   102  	v, err := conn.GetMicroBlockHeaderByHash(mbHash)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	printResult("block", v, err)
   107  	v1, err := conn.GetMicroBlockTransactionsByHash(mbHash)
   108  	if err != nil {
   109  		return err
   110  	}
   111  	printResult("transaction", v1, err)
   112  	return err
   113  }
   114  
   115  func printKeyBlockByHash(conn naet.GetKeyBlockByHasher, kbHash string) (err error) {
   116  	v, err := conn.GetKeyBlockByHash(kbHash)
   117  	if err != nil {
   118  		return err
   119  	}
   120  	printResult("key-block", v, err)
   121  	dumpV(v)
   122  	return err
   123  }
   124  
   125  func printTransactionByHash(conn naet.GetTransactionByHasher, txHash string) (err error) {
   126  	v, err := conn.GetTransactionByHash(txHash)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	printResult("transaction", v, err)
   131  	return err
   132  }
   133  
   134  func printOracleByPubkey(conn naet.GetOracleByPubkeyer, oracleID string) (err error) {
   135  	v, err := conn.GetOracleByPubkey(oracleID)
   136  	if err != nil {
   137  		return err
   138  	}
   139  	printResult("oracle", v, err)
   140  	return err
   141  }
   142  
   143  func inspectFunc(conn nodeGetters, args []string) (err error) {
   144  	for _, object := range args {
   145  		// height
   146  		if matched, _ := regexp.MatchString(`^\d+$`, object); matched {
   147  			height, _ := strconv.ParseUint(object, 10, 64)
   148  			PrintGenerationByHeight(conn, height)
   149  			continue
   150  		}
   151  		// name
   152  		if strings.HasSuffix(object, ".aet") {
   153  			printNameEntry(conn, object)
   154  			continue
   155  		}
   156  
   157  		switch binary.GetHashPrefix(object) {
   158  		case binary.PrefixAccountPubkey:
   159  			printAccount(conn, object)
   160  		case binary.PrefixMicroBlockHash:
   161  			printMicroBlockAndTransactions(conn, object)
   162  		case binary.PrefixKeyBlockHash:
   163  			printKeyBlockByHash(conn, object)
   164  		case binary.PrefixTransactionHash:
   165  			printTransactionByHash(conn, object)
   166  		case binary.PrefixOraclePubkey:
   167  			printOracleByPubkey(conn, object)
   168  		default:
   169  			return fmt.Errorf("Object %v not yet supported", object)
   170  		}
   171  	}
   172  	return nil
   173  }