github.com/vipernet-xyz/tendermint-core@v0.32.0/scripts/json2wal/main.go (about)

     1  /*
     2  	json2wal converts JSON file to binary WAL file.
     3  
     4  	Usage:
     5  			json2wal <path-to-JSON>  <path-to-wal>
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"bufio"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  	"strings"
    16  
    17  	amino "github.com/tendermint/go-amino"
    18  
    19  	cs "github.com/tendermint/tendermint/consensus"
    20  	"github.com/tendermint/tendermint/types"
    21  )
    22  
    23  var cdc = amino.NewCodec()
    24  
    25  func init() {
    26  	cs.RegisterMessages(cdc)
    27  	cs.RegisterWALMessages(cdc)
    28  	types.RegisterBlockAmino(cdc)
    29  }
    30  
    31  func main() {
    32  	if len(os.Args) < 3 {
    33  		fmt.Fprintln(os.Stderr, "missing arguments: Usage:json2wal <path-to-JSON>  <path-to-wal>")
    34  		os.Exit(1)
    35  	}
    36  
    37  	f, err := os.Open(os.Args[1])
    38  	if err != nil {
    39  		panic(fmt.Errorf("failed to open WAL file: %v", err))
    40  	}
    41  	defer f.Close()
    42  
    43  	walFile, err := os.OpenFile(os.Args[2], os.O_EXCL|os.O_WRONLY|os.O_CREATE, 0666)
    44  	if err != nil {
    45  		panic(fmt.Errorf("failed to open WAL file: %v", err))
    46  	}
    47  	defer walFile.Close()
    48  
    49  	// the length of tendermint/wal/MsgInfo in the wal.json may exceed the defaultBufSize(4096) of bufio
    50  	// because of the byte array in BlockPart
    51  	// leading to unmarshal error: unexpected end of JSON input
    52  	br := bufio.NewReaderSize(f, 2*types.BlockPartSizeBytes)
    53  	dec := cs.NewWALEncoder(walFile)
    54  
    55  	for {
    56  		msgJSON, _, err := br.ReadLine()
    57  		if err == io.EOF {
    58  			break
    59  		} else if err != nil {
    60  			panic(fmt.Errorf("failed to read file: %v", err))
    61  		}
    62  		// ignore the ENDHEIGHT in json.File
    63  		if strings.HasPrefix(string(msgJSON), "ENDHEIGHT") {
    64  			continue
    65  		}
    66  
    67  		var msg cs.TimedWALMessage
    68  		err = cdc.UnmarshalJSON(msgJSON, &msg)
    69  		if err != nil {
    70  			panic(fmt.Errorf("failed to unmarshal json: %v", err))
    71  		}
    72  
    73  		err = dec.Encode(&msg)
    74  		if err != nil {
    75  			panic(fmt.Errorf("failed to encode msg: %v", err))
    76  		}
    77  	}
    78  }