You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. /*
  2. json2wal converts JSON file to binary WAL file.
  3. Usage:
  4. json2wal <path-to-JSON> <path-to-wal>
  5. */
  6. package main
  7. import (
  8. "bufio"
  9. "fmt"
  10. "io"
  11. "os"
  12. "strings"
  13. "github.com/tendermint/tendermint/internal/consensus"
  14. tmjson "github.com/tendermint/tendermint/libs/json"
  15. "github.com/tendermint/tendermint/types"
  16. )
  17. func main() {
  18. if len(os.Args) < 3 {
  19. fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON> <path-to-wal>")
  20. os.Exit(1)
  21. }
  22. f, err := os.Open(os.Args[1])
  23. if err != nil {
  24. panic(fmt.Errorf("failed to open WAL file: %v", err))
  25. }
  26. defer f.Close()
  27. walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666)
  28. if err != nil {
  29. panic(fmt.Errorf("failed to open WAL file: %v", err))
  30. }
  31. defer walFile.Close()
  32. // the length of tendermint/wal/MsgInfo in the wal.json may exceed the defaultBufSize(4096) of bufio
  33. // because of the byte array in BlockPart
  34. // leading to unmarshal error: unexpected end of JSON input
  35. br := bufio.NewReaderSize(f, int(2*types.BlockPartSizeBytes))
  36. dec := consensus.NewWALEncoder(walFile)
  37. for {
  38. msgJSON, _, err := br.ReadLine()
  39. if err == io.EOF {
  40. break
  41. } else if err != nil {
  42. panic(fmt.Errorf("failed to read file: %v", err))
  43. }
  44. // ignore the ENDHEIGHT in json.File
  45. if strings.HasPrefix(string(msgJSON), "ENDHEIGHT") {
  46. continue
  47. }
  48. var msg consensus.TimedWALMessage
  49. err = tmjson.Unmarshal(msgJSON, &msg)
  50. if err != nil {
  51. panic(fmt.Errorf("failed to unmarshal json: %v", err))
  52. }
  53. err = dec.Encode(&msg)
  54. if err != nil {
  55. panic(fmt.Errorf("failed to encode msg: %v", err))
  56. }
  57. }
  58. }