github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/cmd/ledgerutil/main.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package main 8 9 import ( 10 "fmt" 11 "os" 12 13 "github.com/hechain20/hechain/internal/ledgerutil" 14 "gopkg.in/alecthomas/kingpin.v2" 15 ) 16 17 const ( 18 compareErrorMessage = "Ledger Compare Error: " 19 outputDirDesc = "Snapshot comparison json results output directory. Default is the current directory." 20 firstDiffsDesc = "Maximum number of differences to record in " + ledgerutil.FirstDiffsByHeight + 21 ". Requesting a report with many differences may result in a large amount " + 22 "of memory usage. Defaults to 10. If set to 0, will not produce " + 23 ledgerutil.FirstDiffsByHeight + "." 24 ) 25 26 var ( 27 app = kingpin.New("ledgerutil", "Ledger Utility Tool") 28 29 compare = app.Command("compare", "Compare channel snapshots from two different peers.") 30 snapshotPath1 = compare.Arg("snapshotPath1", "First ledger snapshot directory.").Required().String() 31 snapshotPath2 = compare.Arg("snapshotPath2", "Second ledger snapshot directory.").Required().String() 32 outputDir = compare.Flag("outputDir", outputDirDesc).Short('o').String() 33 firstDiffs = compare.Flag("firstDiffs", firstDiffsDesc).Short('f').Default("10").Int() 34 35 args = os.Args[1:] 36 ) 37 38 func main() { 39 kingpin.Version("0.0.1") 40 41 command, err := app.Parse(args) 42 if err != nil { 43 kingpin.Fatalf("parsing arguments: %s. Try --help", err) 44 return 45 } 46 47 // Command logic 48 switch command { 49 case compare.FullCommand(): 50 51 // Determine result json file location 52 if *outputDir == "" { 53 *outputDir, err = os.Getwd() 54 if err != nil { 55 fmt.Printf("%s%s\n", compareErrorMessage, err) 56 return 57 } 58 } 59 60 count, outputDirPath, err := ledgerutil.Compare(*snapshotPath1, *snapshotPath2, *outputDir, *firstDiffs) 61 if err != nil { 62 fmt.Printf("%s%s\n", compareErrorMessage, err) 63 return 64 } 65 66 fmt.Print("\nSuccessfully compared snapshots. ") 67 if count == -1 { 68 fmt.Println("Both snapshot public state and private state hashes were the same. No results were generated.") 69 } else { 70 fmt.Printf("Results saved to %s. Total differences found: %d\n", outputDirPath, count) 71 } 72 } 73 }