Files
tendermint/scripts/json2wal/main.go
M. J. Fromberger cf7537ea5f 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.
2021-09-23 07:52:07 -07:00

70 lines
1.6 KiB
Go

/*
json2wal converts JSON file to binary WAL file.
Usage:
json2wal <path-to-JSON> <path-to-wal>
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/tendermint/tendermint/internal/consensus"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/types"
)
func main() {
if len(os.Args) < 3 {
fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON> <path-to-wal>")
os.Exit(1)
}
f, err := os.Open(os.Args[1])
if err != nil {
panic(fmt.Errorf("failed to open WAL file: %v", err))
}
defer f.Close()
walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(fmt.Errorf("failed to open WAL file: %v", err))
}
defer walFile.Close()
// the length of tendermint/wal/MsgInfo in the wal.json may exceed the defaultBufSize(4096) of bufio
// because of the byte array in BlockPart
// leading to unmarshal error: unexpected end of JSON input
br := bufio.NewReaderSize(f, int(2*types.BlockPartSizeBytes))
dec := consensus.NewWALEncoder(walFile)
for {
msgJSON, _, err := br.ReadLine()
if err == io.EOF {
break
} else if err != nil {
panic(fmt.Errorf("failed to read file: %v", err))
}
// ignore the ENDHEIGHT in json.File
if strings.HasPrefix(string(msgJSON), "ENDHEIGHT") {
continue
}
var msg consensus.TimedWALMessage
err = tmjson.Unmarshal(msgJSON, &msg)
if err != nil {
panic(fmt.Errorf("failed to unmarshal json: %v", err))
}
err = dec.Encode(&msg)
if err != nil {
panic(fmt.Errorf("failed to encode msg: %v", err))
}
}
}